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
)
38 GLenum texture_target
;
39 unsigned int view_flags
;
41 enum wined3d_gl_extension extension
;
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
);
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
)
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
));
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
));
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
));
121 format
= wined3d_get_format(adapter
, WINED3DFMT_R32_UINT
, resource
->bind_flags
);
122 element_size
= buffer
->structure_byte_stride
;
126 element_size
= format
->byte_count
;
132 buffer_size
= buffer
->resource
.size
/ element_size
;
133 if (!wined3d_bound_range(desc
->u
.buffer
.start_idx
, desc
->u
.buffer
.count
, buffer_size
))
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
));
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
);
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
))
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
;
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");
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
);
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");
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
))
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");
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
);
268 view
->target
= GL_TEXTURE_BUFFER
;
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
));
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
;
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
;
319 struct wined3d_texture
*texture
;
321 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
323 wined3d_buffer_invalidate_location(buffer_from_resource(resource
), location
);
327 texture
= texture_from_resource(resource
);
328 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
330 wined3d_texture_invalidate_location(texture
, desc
->u
.texture
.level_idx
, location
);
334 sub_resource_idx
= desc
->u
.texture
.layer_idx
* texture
->level_count
+ desc
->u
.texture
.level_idx
;
335 for (i
= 0; i
< desc
->u
.texture
.layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
336 wined3d_texture_invalidate_location(texture
, sub_resource_idx
, location
);
339 static void wined3d_view_load_location(struct wined3d_resource
*resource
,
340 const struct wined3d_view_desc
*desc
, struct wined3d_context
*context
, DWORD location
)
342 unsigned int i
, sub_resource_idx
;
343 struct wined3d_texture
*texture
;
345 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
347 wined3d_buffer_load_location(buffer_from_resource(resource
), context
, location
);
351 texture
= texture_from_resource(resource
);
352 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
354 wined3d_texture_load_location(texture
, desc
->u
.texture
.level_idx
, context
, location
);
358 sub_resource_idx
= desc
->u
.texture
.layer_idx
* texture
->level_count
+ desc
->u
.texture
.level_idx
;
359 for (i
= 0; i
< desc
->u
.texture
.layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
360 wined3d_texture_load_location(texture
, sub_resource_idx
, context
, location
);
363 ULONG CDECL
wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view
*view
)
365 unsigned int refcount
= InterlockedIncrement(&view
->refcount
);
367 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
372 void wined3d_rendertarget_view_cleanup(struct wined3d_rendertarget_view
*view
)
374 /* Call wined3d_object_destroyed() before releasing the resource,
375 * since releasing the resource may end up destroying the parent. */
376 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
377 wined3d_resource_decref(view
->resource
);
380 ULONG CDECL
wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view
*view
)
382 unsigned int refcount
= InterlockedDecrement(&view
->refcount
);
384 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
388 wined3d_mutex_lock();
389 view
->resource
->device
->adapter
->adapter_ops
->adapter_destroy_rendertarget_view(view
);
390 wined3d_mutex_unlock();
396 void * CDECL
wined3d_rendertarget_view_get_parent(const struct wined3d_rendertarget_view
*view
)
398 TRACE("view %p.\n", view
);
403 void * CDECL
wined3d_rendertarget_view_get_sub_resource_parent(const struct wined3d_rendertarget_view
*view
)
405 struct wined3d_texture
*texture
;
407 TRACE("view %p.\n", view
);
409 if (view
->resource
->type
== WINED3D_RTYPE_BUFFER
)
410 return wined3d_buffer_get_parent(buffer_from_resource(view
->resource
));
412 texture
= texture_from_resource(view
->resource
);
414 return texture
->sub_resources
[view
->sub_resource_idx
].parent
;
417 void CDECL
wined3d_rendertarget_view_set_parent(struct wined3d_rendertarget_view
*view
,
418 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
420 TRACE("view %p, parent %p, parent_ops %p.\n", view
, parent
, parent_ops
);
422 view
->parent
= parent
;
423 view
->parent_ops
= parent_ops
;
426 struct wined3d_resource
* CDECL
wined3d_rendertarget_view_get_resource(const struct wined3d_rendertarget_view
*view
)
428 TRACE("view %p.\n", view
);
430 return view
->resource
;
433 void wined3d_rendertarget_view_get_drawable_size(const struct wined3d_rendertarget_view
*view
,
434 const struct wined3d_context
*context
, unsigned int *width
, unsigned int *height
)
436 const struct wined3d_texture
*texture
;
438 if (view
->resource
->type
!= WINED3D_RTYPE_TEXTURE_2D
)
440 *width
= view
->width
;
441 *height
= view
->height
;
445 texture
= texture_from_resource(view
->resource
);
446 if (texture
->swapchain
)
448 /* The drawable size of an onscreen drawable is the surface size.
449 * (Actually: The window size, but the surface is created in window
451 *width
= texture
->resource
.width
;
452 *height
= texture
->resource
.height
;
454 else if (wined3d_settings
.offscreen_rendering_mode
== ORM_BACKBUFFER
)
456 const struct wined3d_swapchain_desc
*desc
= &context
->swapchain
->state
.desc
;
458 /* The drawable size of a backbuffer / aux buffer offscreen target is
459 * the size of the current context's drawable, which is the size of
460 * the back buffer of the swapchain the active context belongs to. */
461 *width
= desc
->backbuffer_width
;
462 *height
= desc
->backbuffer_height
;
466 unsigned int level_idx
= view
->sub_resource_idx
% texture
->level_count
;
468 /* The drawable size of an FBO target is the OpenGL texture size,
469 * which is the power of two size. */
470 *width
= wined3d_texture_get_level_pow2_width(texture
, level_idx
);
471 *height
= wined3d_texture_get_level_pow2_height(texture
, level_idx
);
475 void wined3d_rendertarget_view_prepare_location(struct wined3d_rendertarget_view
*view
,
476 struct wined3d_context
*context
, uint32_t location
)
478 struct wined3d_resource
*resource
= view
->resource
;
479 unsigned int i
, sub_resource_idx
, layer_count
;
480 struct wined3d_texture
*texture
;
482 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
484 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
488 texture
= texture_from_resource(resource
);
489 sub_resource_idx
= view
->sub_resource_idx
;
490 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
491 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
492 wined3d_texture_prepare_location(texture
, sub_resource_idx
, context
, location
);
495 void wined3d_rendertarget_view_load_location(struct wined3d_rendertarget_view
*view
,
496 struct wined3d_context
*context
, uint32_t location
)
498 wined3d_view_load_location(view
->resource
, &view
->desc
, context
, location
);
501 void wined3d_rendertarget_view_validate_location(struct wined3d_rendertarget_view
*view
, uint32_t location
)
503 struct wined3d_resource
*resource
= view
->resource
;
504 unsigned int i
, sub_resource_idx
, layer_count
;
505 struct wined3d_texture
*texture
;
507 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
509 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
513 texture
= texture_from_resource(resource
);
514 sub_resource_idx
= view
->sub_resource_idx
;
515 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
516 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
517 wined3d_texture_validate_location(texture
, sub_resource_idx
, location
);
520 void wined3d_rendertarget_view_invalidate_location(struct wined3d_rendertarget_view
*view
, uint32_t location
)
522 wined3d_view_invalidate_location(view
->resource
, &view
->desc
, location
);
525 /* Note: This may return 0 if the selected layers do not have a location in common. */
526 DWORD
wined3d_rendertarget_view_get_locations(const struct wined3d_rendertarget_view
*view
)
528 struct wined3d_resource
*resource
= view
->resource
;
529 unsigned int i
, sub_resource_idx
, layer_count
;
530 const struct wined3d_texture
*texture
;
533 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
534 return buffer_from_resource(resource
)->locations
;
536 texture
= texture_from_resource(resource
);
537 sub_resource_idx
= view
->sub_resource_idx
;
538 layer_count
= resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
? view
->layer_count
: 1;
539 for (i
= 0; i
< layer_count
; ++i
, sub_resource_idx
+= texture
->level_count
)
540 ret
&= texture
->sub_resources
[sub_resource_idx
].locations
;
543 WARN("View %p (texture %p) layers do not have a location in common.\n", view
, texture
);
548 void wined3d_rendertarget_view_get_box(struct wined3d_rendertarget_view
*view
,
549 struct wined3d_box
*box
)
551 if (view
->resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
)
553 wined3d_box_set(box
, 0, 0, view
->width
, view
->height
, 0, 1);
557 struct wined3d_texture
*texture
= texture_from_resource(view
->resource
);
558 wined3d_texture_get_level_box(texture
, view
->sub_resource_idx
, box
);
562 static void wined3d_render_target_view_gl_cs_init(void *object
)
564 struct wined3d_rendertarget_view_gl
*view_gl
= object
;
565 struct wined3d_resource
*resource
= view_gl
->v
.resource
;
566 const struct wined3d_view_desc
*desc
= &view_gl
->v
.desc
;
568 TRACE("view_gl %p.\n", view_gl
);
570 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
572 FIXME("Not implemented for resources %s.\n", debug_d3dresourcetype(resource
->type
));
576 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
577 unsigned int depth_or_layer_count
;
579 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
580 depth_or_layer_count
= wined3d_texture_get_level_depth(&texture_gl
->t
, desc
->u
.texture
.level_idx
);
582 depth_or_layer_count
= texture_gl
->t
.layer_count
;
584 if (resource
->format
->id
!= view_gl
->v
.format
->id
585 || (view_gl
->v
.layer_count
!= 1 && view_gl
->v
.layer_count
!= depth_or_layer_count
))
587 GLenum resource_class
, view_class
;
589 resource_class
= wined3d_format_gl(resource
->format
)->view_class
;
590 view_class
= wined3d_format_gl(view_gl
->v
.format
)->view_class
;
591 if (resource_class
!= view_class
)
593 FIXME("Render target view not supported, resource format %s, view format %s.\n",
594 debug_d3dformat(resource
->format
->id
), debug_d3dformat(view_gl
->v
.format
->id
));
597 if (texture_gl
->t
.swapchain
&& texture_gl
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
599 FIXME("Swapchain views not supported.\n");
603 create_texture_view(&view_gl
->gl_view
, texture_gl
->target
, desc
, texture_gl
, view_gl
->v
.format
);
608 static HRESULT
wined3d_rendertarget_view_init(struct wined3d_rendertarget_view
*view
,
609 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
610 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
612 BOOL allow_srgb_toggle
= FALSE
;
615 view
->parent
= parent
;
616 view
->parent_ops
= parent_ops
;
618 if (resource
->type
!= WINED3D_RTYPE_BUFFER
)
620 struct wined3d_texture
*texture
= texture_from_resource(resource
);
622 if (texture
->swapchain
)
623 allow_srgb_toggle
= TRUE
;
625 if (!(view
->format
= validate_resource_view(desc
, resource
, TRUE
, allow_srgb_toggle
)))
627 view
->format_attrs
= view
->format
->attrs
;
628 view
->format_caps
= view
->format
->caps
[resource
->gl_type
];
631 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
633 view
->sub_resource_idx
= 0;
634 view
->layer_count
= 1;
635 view
->width
= desc
->u
.buffer
.count
;
640 struct wined3d_texture
*texture
= texture_from_resource(resource
);
642 view
->sub_resource_idx
= desc
->u
.texture
.level_idx
;
643 if (resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
)
644 view
->sub_resource_idx
+= desc
->u
.texture
.layer_idx
* texture
->level_count
;
645 view
->layer_count
= desc
->u
.texture
.layer_count
;
646 view
->width
= wined3d_texture_get_level_width(texture
, desc
->u
.texture
.level_idx
);
647 view
->height
= wined3d_texture_get_level_height(texture
, desc
->u
.texture
.level_idx
);
650 wined3d_resource_incref(view
->resource
= resource
);
655 HRESULT
wined3d_rendertarget_view_no3d_init(struct wined3d_rendertarget_view
*view_no3d
,
656 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
657 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
659 TRACE("view_no3d %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
660 view_no3d
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
662 return wined3d_rendertarget_view_init(view_no3d
, desc
, resource
, parent
, parent_ops
);
665 HRESULT
wined3d_rendertarget_view_gl_init(struct wined3d_rendertarget_view_gl
*view_gl
,
666 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
667 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
671 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
672 view_gl
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
674 if (FAILED(hr
= wined3d_rendertarget_view_init(&view_gl
->v
, desc
, resource
, parent
, parent_ops
)))
677 wined3d_cs_init_object(resource
->device
->cs
, wined3d_render_target_view_gl_cs_init
, view_gl
);
682 VkImageViewType
vk_image_view_type_from_wined3d(enum wined3d_resource_type type
, uint32_t flags
)
686 case WINED3D_RTYPE_TEXTURE_1D
:
687 if (flags
& WINED3D_VIEW_TEXTURE_ARRAY
)
688 return VK_IMAGE_VIEW_TYPE_1D_ARRAY
;
690 return VK_IMAGE_VIEW_TYPE_1D
;
692 case WINED3D_RTYPE_TEXTURE_2D
:
693 if (flags
& WINED3D_VIEW_TEXTURE_CUBE
)
695 if (flags
& WINED3D_VIEW_TEXTURE_ARRAY
)
696 return VK_IMAGE_VIEW_TYPE_CUBE_ARRAY
;
698 return VK_IMAGE_VIEW_TYPE_CUBE
;
700 if (flags
& WINED3D_VIEW_TEXTURE_ARRAY
)
701 return VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
703 return VK_IMAGE_VIEW_TYPE_2D
;
705 case WINED3D_RTYPE_TEXTURE_3D
:
706 return VK_IMAGE_VIEW_TYPE_3D
;
709 ERR("Unhandled resource type %s.\n", debug_d3dresourcetype(type
));
714 static VkBufferView
wined3d_view_vk_create_vk_buffer_view(struct wined3d_context_vk
*context_vk
,
715 const struct wined3d_view_desc
*desc
, struct wined3d_buffer_vk
*buffer_vk
,
716 const struct wined3d_format_vk
*view_format_vk
)
718 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
719 VkBufferViewCreateInfo create_info
;
720 struct wined3d_device_vk
*device_vk
;
721 VkBufferView vk_buffer_view
;
722 unsigned int offset
, size
;
723 struct wined3d_bo_vk
*bo
;
726 get_buffer_view_range(&buffer_vk
->b
, desc
, &view_format_vk
->f
, &offset
, &size
);
727 wined3d_buffer_prepare_location(&buffer_vk
->b
, &context_vk
->c
, WINED3D_LOCATION_BUFFER
);
728 bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
730 create_info
.sType
= VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
;
731 create_info
.pNext
= NULL
;
732 create_info
.flags
= 0;
733 create_info
.buffer
= bo
->vk_buffer
;
734 create_info
.format
= view_format_vk
->vk_format
;
735 create_info
.offset
= bo
->b
.buffer_offset
+ offset
;
736 create_info
.range
= size
;
738 device_vk
= wined3d_device_vk(buffer_vk
->b
.resource
.device
);
739 if ((vr
= VK_CALL(vkCreateBufferView(device_vk
->vk_device
, &create_info
, NULL
, &vk_buffer_view
))) < 0)
741 ERR("Failed to create buffer view, vr %s.\n", wined3d_debug_vkresult(vr
));
742 return VK_NULL_HANDLE
;
745 return vk_buffer_view
;
748 static VkImageView
wined3d_view_vk_create_vk_image_view(struct wined3d_context_vk
*context_vk
,
749 const struct wined3d_view_desc
*desc
, struct wined3d_texture_vk
*texture_vk
,
750 const struct wined3d_format_vk
*view_format_vk
, struct color_fixup_desc fixup
, bool rtv
)
752 const struct wined3d_resource
*resource
= &texture_vk
->t
.resource
;
753 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
754 const struct wined3d_format_vk
*format_vk
;
755 struct wined3d_device_vk
*device_vk
;
756 VkImageViewCreateInfo create_info
;
757 VkImageView vk_image_view
;
760 device_vk
= wined3d_device_vk(resource
->device
);
762 if (!wined3d_texture_vk_prepare_texture(texture_vk
, context_vk
))
764 ERR("Failed to prepare texture.\n");
765 return VK_NULL_HANDLE
;
768 /* Depth formats are a little complicated. For example, the typeless
769 * format corresponding to depth/stencil view format WINED3DFMT_D32_FLOAT
770 * is WINED3DFMT_R32_TYPELESS, and the corresponding shader resource view
771 * format would be WINED3DFMT_R32_FLOAT. Vulkan depth/stencil formats are
772 * only compatible with themselves, so it's not possible to create e.g. a
773 * VK_FORMAT_R32_SFLOAT view on a VK_FORMAT_D32_SFLOAT image. In order to
774 * make it work, we create Vulkan images for WINED3DFMT_R32_TYPELESS
775 * resources with either a depth format (VK_FORMAT_D32_SFLOAT) or a colour
776 * format, depending on whether the bind flags include
777 * WINED3D_BIND_DEPTH_STENCIL or not. In order to then create a Vulkan
778 * view on the image, we then replace the view format here with the
779 * underlying resource format. However, that means it's still not possible
780 * to create e.g. a WINED3DFMT_R32_UINT view on a WINED3DFMT_R32_TYPELESS
781 * depth/stencil resource. */
782 if (resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
)
783 format_vk
= wined3d_format_vk(resource
->format
);
785 format_vk
= view_format_vk
;
787 create_info
.sType
= VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO
;
788 create_info
.pNext
= NULL
;
789 create_info
.flags
= 0;
790 create_info
.image
= texture_vk
->image
.vk_image
;
791 create_info
.viewType
= vk_image_view_type_from_wined3d(resource
->type
, desc
->flags
);
792 if (rtv
&& create_info
.viewType
== VK_IMAGE_VIEW_TYPE_3D
)
794 if (desc
->u
.texture
.layer_count
> 1)
795 create_info
.viewType
= VK_IMAGE_VIEW_TYPE_2D_ARRAY
;
797 create_info
.viewType
= VK_IMAGE_VIEW_TYPE_2D
;
799 create_info
.format
= format_vk
->vk_format
;
800 if (is_stencil_view_format(&view_format_vk
->f
))
802 create_info
.components
.r
= VK_COMPONENT_SWIZZLE_ZERO
;
803 create_info
.components
.g
= VK_COMPONENT_SWIZZLE_R
;
804 create_info
.components
.b
= VK_COMPONENT_SWIZZLE_ZERO
;
805 create_info
.components
.a
= VK_COMPONENT_SWIZZLE_ZERO
;
807 else if (is_identity_fixup(fixup
) || !can_use_texture_swizzle(context_vk
->c
.d3d_info
, &format_vk
->f
))
809 create_info
.components
.r
= VK_COMPONENT_SWIZZLE_IDENTITY
;
810 create_info
.components
.g
= VK_COMPONENT_SWIZZLE_IDENTITY
;
811 create_info
.components
.b
= VK_COMPONENT_SWIZZLE_IDENTITY
;
812 create_info
.components
.a
= VK_COMPONENT_SWIZZLE_IDENTITY
;
816 wined3d_vk_swizzle_from_color_fixup(&create_info
.components
, fixup
);
818 if ((resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
)
819 && (view_format_vk
->f
.red_size
|| view_format_vk
->f
.green_size
))
821 create_info
.subresourceRange
.aspectMask
= 0;
822 if (view_format_vk
->f
.red_size
)
823 create_info
.subresourceRange
.aspectMask
|= VK_IMAGE_ASPECT_DEPTH_BIT
;
824 if (view_format_vk
->f
.green_size
)
825 create_info
.subresourceRange
.aspectMask
|= VK_IMAGE_ASPECT_STENCIL_BIT
;
829 create_info
.subresourceRange
.aspectMask
= vk_aspect_mask_from_format(&format_vk
->f
);
831 create_info
.subresourceRange
.baseMipLevel
= desc
->u
.texture
.level_idx
;
832 create_info
.subresourceRange
.levelCount
= desc
->u
.texture
.level_count
;
833 if (create_info
.viewType
== VK_IMAGE_VIEW_TYPE_3D
)
835 if (desc
->u
.texture
.layer_idx
|| (desc
->u
.texture
.layer_count
!= texture_vk
->t
.resource
.depth
836 && desc
->u
.texture
.layer_count
!= ~0u))
837 WARN("Partial 3D texture views are not supported.\n");
838 create_info
.subresourceRange
.baseArrayLayer
= 0;
839 create_info
.subresourceRange
.layerCount
= 1;
843 create_info
.subresourceRange
.baseArrayLayer
= desc
->u
.texture
.layer_idx
;
844 create_info
.subresourceRange
.layerCount
= desc
->u
.texture
.layer_count
;
846 if ((vr
= VK_CALL(vkCreateImageView(device_vk
->vk_device
, &create_info
, NULL
, &vk_image_view
))) < 0)
848 ERR("Failed to create Vulkan image view, vr %s.\n", wined3d_debug_vkresult(vr
));
849 return VK_NULL_HANDLE
;
852 return vk_image_view
;
855 static void wined3d_render_target_view_vk_cs_init(void *object
)
857 struct wined3d_rendertarget_view_vk
*view_vk
= object
;
858 struct wined3d_view_desc
*desc
= &view_vk
->v
.desc
;
859 const struct wined3d_format_vk
*format_vk
;
860 struct wined3d_texture_vk
*texture_vk
;
861 struct wined3d_resource
*resource
;
862 struct wined3d_context
*context
;
863 uint32_t default_flags
= 0;
865 TRACE("view_vk %p.\n", view_vk
);
867 resource
= view_vk
->v
.resource
;
868 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
870 FIXME("Buffer views not implemented.\n");
874 texture_vk
= wined3d_texture_vk(texture_from_resource(resource
));
875 format_vk
= wined3d_format_vk(view_vk
->v
.format
);
877 if (texture_vk
->t
.layer_count
> 1)
878 default_flags
|= WINED3D_VIEW_TEXTURE_ARRAY
;
880 if (resource
->format
->id
== format_vk
->f
.id
&& desc
->flags
== default_flags
881 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_vk
->t
.level_count
882 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_vk
->t
.layer_count
883 && !is_stencil_view_format(&format_vk
->f
) && resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
884 && is_identity_fixup(format_vk
->f
.color_fixup
))
886 TRACE("Creating identity render target view.\n");
890 if (texture_vk
->t
.swapchain
&& texture_vk
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
892 FIXME("Swapchain views not supported.\n");
896 context
= context_acquire(resource
->device
, NULL
, 0);
897 view_vk
->vk_image_view
= wined3d_view_vk_create_vk_image_view(wined3d_context_vk(context
),
898 desc
, texture_vk
, format_vk
, COLOR_FIXUP_IDENTITY
, true);
899 context_release(context
);
901 if (!view_vk
->vk_image_view
)
904 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(view_vk
->vk_image_view
));
907 HRESULT
wined3d_rendertarget_view_vk_init(struct wined3d_rendertarget_view_vk
*view_vk
,
908 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
909 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
913 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
914 view_vk
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
916 if (FAILED(hr
= wined3d_rendertarget_view_init(&view_vk
->v
, desc
, resource
, parent
, parent_ops
)))
919 wined3d_cs_init_object(resource
->device
->cs
, wined3d_render_target_view_vk_cs_init
, view_vk
);
924 HRESULT CDECL
wined3d_rendertarget_view_create(const struct wined3d_view_desc
*desc
,
925 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
926 struct wined3d_rendertarget_view
**view
)
928 const struct wined3d_adapter_ops
*adapter_ops
;
930 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
931 wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
, view
);
933 adapter_ops
= resource
->device
->adapter
->adapter_ops
;
934 return adapter_ops
->adapter_create_rendertarget_view(desc
, resource
, parent
, parent_ops
, view
);
937 HRESULT CDECL
wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture
*texture
,
938 unsigned int sub_resource_idx
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
939 struct wined3d_rendertarget_view
**view
)
941 struct wined3d_view_desc desc
;
943 TRACE("texture %p, sub_resource_idx %u, parent %p, parent_ops %p, view %p.\n",
944 texture
, sub_resource_idx
, parent
, parent_ops
, view
);
946 desc
.format_id
= texture
->resource
.format
->id
;
948 desc
.u
.texture
.level_idx
= sub_resource_idx
% texture
->level_count
;
949 desc
.u
.texture
.level_count
= 1;
950 desc
.u
.texture
.layer_idx
= sub_resource_idx
/ texture
->level_count
;
951 desc
.u
.texture
.layer_count
= 1;
953 return wined3d_rendertarget_view_create(&desc
, &texture
->resource
, parent
, parent_ops
, view
);
956 ULONG CDECL
wined3d_shader_resource_view_incref(struct wined3d_shader_resource_view
*view
)
958 unsigned int refcount
= InterlockedIncrement(&view
->refcount
);
960 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
965 void wined3d_shader_resource_view_cleanup(struct wined3d_shader_resource_view
*view
)
967 /* Call wined3d_object_destroyed() before releasing the resource,
968 * since releasing the resource may end up destroying the parent. */
969 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
970 wined3d_resource_decref(view
->resource
);
973 ULONG CDECL
wined3d_shader_resource_view_decref(struct wined3d_shader_resource_view
*view
)
975 unsigned int refcount
= InterlockedDecrement(&view
->refcount
);
977 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
981 wined3d_mutex_lock();
982 view
->resource
->device
->adapter
->adapter_ops
->adapter_destroy_shader_resource_view(view
);
983 wined3d_mutex_unlock();
989 void * CDECL
wined3d_shader_resource_view_get_parent(const struct wined3d_shader_resource_view
*view
)
991 TRACE("view %p.\n", view
);
996 void wined3d_shader_resource_view_gl_update(struct wined3d_shader_resource_view_gl
*srv_gl
,
997 struct wined3d_context_gl
*context_gl
)
999 create_buffer_view(&srv_gl
->gl_view
, &context_gl
->c
, &srv_gl
->v
.desc
,
1000 buffer_from_resource(srv_gl
->v
.resource
), srv_gl
->v
.format
);
1001 srv_gl
->bo_user
.valid
= true;
1004 static void wined3d_shader_resource_view_gl_cs_init(void *object
)
1006 struct wined3d_shader_resource_view_gl
*view_gl
= object
;
1007 struct wined3d_resource
*resource
= view_gl
->v
.resource
;
1008 const struct wined3d_format
*view_format
;
1009 const struct wined3d_gl_info
*gl_info
;
1010 const struct wined3d_view_desc
*desc
;
1013 TRACE("view_gl %p.\n", view_gl
);
1015 view_format
= view_gl
->v
.format
;
1016 gl_info
= &resource
->device
->adapter
->gl_info
;
1017 desc
= &view_gl
->v
.desc
;
1019 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1021 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1022 struct wined3d_context
*context
;
1024 context
= context_acquire(resource
->device
, NULL
, 0);
1025 create_buffer_view(&view_gl
->gl_view
, context
, desc
, buffer
, view_format
);
1026 view_gl
->bo_user
.valid
= true;
1027 list_add_head(&buffer
->buffer_object
->users
, &view_gl
->bo_user
.entry
);
1028 context_release(context
);
1032 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
1033 GLenum resource_class
, view_class
;
1035 resource_class
= wined3d_format_gl(resource
->format
)->view_class
;
1036 view_class
= wined3d_format_gl(view_format
)->view_class
;
1037 view_target
= get_texture_view_target(gl_info
, desc
, texture_gl
);
1039 if (resource
->format
->id
== view_format
->id
&& texture_gl
->target
== view_target
1040 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_gl
->t
.level_count
1041 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_gl
->t
.layer_count
1042 && !is_stencil_view_format(view_format
))
1044 TRACE("Creating identity shader resource view.\n");
1046 else if (texture_gl
->t
.swapchain
&& texture_gl
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
1048 FIXME("Swapchain shader resource views not supported.\n");
1050 else if (resource
->format
->typeless_id
== view_format
->typeless_id
1051 && resource_class
== view_class
)
1053 create_texture_view(&view_gl
->gl_view
, view_target
, desc
, texture_gl
, view_format
);
1055 else if (wined3d_format_is_depth_view(resource
->format
->id
, view_format
->id
))
1057 create_texture_view(&view_gl
->gl_view
, view_target
, desc
, texture_gl
, resource
->format
);
1061 FIXME("Shader resource view not supported, resource format %s, view format %s.\n",
1062 debug_d3dformat(resource
->format
->id
), debug_d3dformat(view_format
->id
));
1067 static HRESULT
wined3d_shader_resource_view_init(struct wined3d_shader_resource_view
*view
,
1068 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1069 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1072 view
->parent
= parent
;
1073 view
->parent_ops
= parent_ops
;
1075 if (!(resource
->bind_flags
& WINED3D_BIND_SHADER_RESOURCE
))
1076 return E_INVALIDARG
;
1077 if (!(view
->format
= validate_resource_view(desc
, resource
, FALSE
, FALSE
)))
1078 return E_INVALIDARG
;
1081 wined3d_resource_incref(view
->resource
= resource
);
1086 HRESULT
wined3d_shader_resource_view_gl_init(struct wined3d_shader_resource_view_gl
*view_gl
,
1087 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1088 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1092 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1093 view_gl
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
1095 if (FAILED(hr
= wined3d_shader_resource_view_init(&view_gl
->v
, desc
, resource
, parent
, parent_ops
)))
1098 list_init(&view_gl
->bo_user
.entry
);
1099 wined3d_cs_init_object(resource
->device
->cs
, wined3d_shader_resource_view_gl_cs_init
, view_gl
);
1104 void wined3d_shader_resource_view_vk_update_buffer(struct wined3d_shader_resource_view_vk
*srv_vk
,
1105 struct wined3d_context_vk
*context_vk
)
1107 const struct wined3d_format_vk
*view_format_vk
= wined3d_format_vk(srv_vk
->v
.format
);
1108 const struct wined3d_view_desc
*desc
= &srv_vk
->v
.desc
;
1109 struct wined3d_resource
*resource
= srv_vk
->v
.resource
;
1110 struct wined3d_view_vk
*view_vk
= &srv_vk
->view_vk
;
1111 struct wined3d_buffer_vk
*buffer_vk
;
1112 VkBufferView vk_buffer_view
;
1114 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
1115 wined3d_context_vk_destroy_vk_buffer_view(context_vk
, view_vk
->u
.vk_buffer_view
, view_vk
->command_buffer_id
);
1116 if ((vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, desc
, buffer_vk
, view_format_vk
)))
1118 view_vk
->u
.vk_buffer_view
= vk_buffer_view
;
1119 view_vk
->bo_user
.valid
= true;
1123 void wined3d_shader_resource_view_vk_update_layout(struct wined3d_shader_resource_view_vk
*srv_vk
,
1124 VkImageLayout layout
)
1126 srv_vk
->view_vk
.u
.vk_image_info
.imageLayout
= layout
;
1129 static void wined3d_shader_resource_view_vk_cs_init(void *object
)
1131 struct wined3d_shader_resource_view_vk
*srv_vk
= object
;
1132 struct wined3d_view_desc
*desc
= &srv_vk
->v
.desc
;
1133 struct wined3d_texture_vk
*texture_vk
;
1134 const struct wined3d_format
*format
;
1135 struct wined3d_buffer_vk
*buffer_vk
;
1136 struct wined3d_resource
*resource
;
1137 struct wined3d_context
*context
;
1138 VkBufferView vk_buffer_view
;
1139 uint32_t default_flags
= 0;
1140 VkImageView vk_image_view
;
1141 struct wined3d_bo_vk
*bo
;
1143 TRACE("srv_vk %p.\n", srv_vk
);
1145 resource
= srv_vk
->v
.resource
;
1146 format
= srv_vk
->v
.format
;
1148 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1150 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
1152 context
= context_acquire(resource
->device
, NULL
, 0);
1153 vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(wined3d_context_vk(context
),
1154 desc
, buffer_vk
, wined3d_format_vk(format
));
1155 context_release(context
);
1157 if (!vk_buffer_view
)
1159 bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
1161 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(vk_buffer_view
));
1163 srv_vk
->view_vk
.u
.vk_buffer_view
= vk_buffer_view
;
1164 srv_vk
->view_vk
.bo_user
.valid
= true;
1165 list_add_head(&bo
->b
.users
, &srv_vk
->view_vk
.bo_user
.entry
);
1170 texture_vk
= wined3d_texture_vk(texture_from_resource(resource
));
1172 if (texture_vk
->t
.layer_count
> 1)
1173 default_flags
|= WINED3D_VIEW_TEXTURE_ARRAY
;
1175 if (resource
->format
->id
== format
->id
&& desc
->flags
== default_flags
1176 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_vk
->t
.level_count
1177 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_vk
->t
.layer_count
1178 && !(resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
))
1180 TRACE("Creating identity shader resource view.\n");
1184 if (texture_vk
->t
.swapchain
&& texture_vk
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
1185 FIXME("Swapchain shader resource views not supported.\n");
1187 context
= context_acquire(resource
->device
, NULL
, 0);
1188 vk_image_view
= wined3d_view_vk_create_vk_image_view(wined3d_context_vk(context
),
1189 desc
, texture_vk
, wined3d_format_vk(format
), format
->color_fixup
, false);
1190 context_release(context
);
1195 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(vk_image_view
));
1197 srv_vk
->view_vk
.u
.vk_image_info
.imageView
= vk_image_view
;
1198 srv_vk
->view_vk
.u
.vk_image_info
.sampler
= VK_NULL_HANDLE
;
1199 if (texture_vk
->layout
== VK_IMAGE_LAYOUT_GENERAL
)
1200 srv_vk
->view_vk
.u
.vk_image_info
.imageLayout
= VK_IMAGE_LAYOUT_GENERAL
;
1202 srv_vk
->view_vk
.u
.vk_image_info
.imageLayout
= VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
;
1205 HRESULT
wined3d_shader_resource_view_vk_init(struct wined3d_shader_resource_view_vk
*view_vk
,
1206 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1207 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1211 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1212 view_vk
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
1214 if (FAILED(hr
= wined3d_shader_resource_view_init(&view_vk
->v
, desc
, resource
, parent
, parent_ops
)))
1217 list_init(&view_vk
->view_vk
.bo_user
.entry
);
1218 wined3d_cs_init_object(resource
->device
->cs
, wined3d_shader_resource_view_vk_cs_init
, view_vk
);
1223 HRESULT CDECL
wined3d_shader_resource_view_create(const struct wined3d_view_desc
*desc
,
1224 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
1225 struct wined3d_shader_resource_view
**view
)
1227 const struct wined3d_adapter_ops
*adapter_ops
;
1229 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
1230 wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
, view
);
1232 adapter_ops
= resource
->device
->adapter
->adapter_ops
;
1233 return adapter_ops
->adapter_create_shader_resource_view(desc
, resource
, parent
, parent_ops
, view
);
1236 void wined3d_shader_resource_view_gl_bind(struct wined3d_shader_resource_view_gl
*view_gl
,
1237 unsigned int unit
, struct wined3d_sampler_gl
*sampler_gl
, struct wined3d_context_gl
*context_gl
)
1239 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1240 struct wined3d_texture_gl
*texture_gl
;
1242 wined3d_context_gl_active_texture(context_gl
, gl_info
, unit
);
1244 if (view_gl
->gl_view
.name
)
1246 wined3d_context_gl_bind_texture(context_gl
, view_gl
->gl_view
.target
, view_gl
->gl_view
.name
);
1247 wined3d_sampler_gl_bind(sampler_gl
, unit
, NULL
, context_gl
);
1251 if (view_gl
->v
.resource
->type
== WINED3D_RTYPE_BUFFER
)
1253 FIXME("Buffer shader resources not supported.\n");
1257 texture_gl
= wined3d_texture_gl(wined3d_texture_from_resource(view_gl
->v
.resource
));
1258 wined3d_texture_gl_bind(texture_gl
, context_gl
, FALSE
);
1259 wined3d_sampler_gl_bind(sampler_gl
, unit
, texture_gl
, context_gl
);
1262 /* Context activation is done by the caller. */
1263 static void shader_resource_view_gl_bind_and_dirtify(struct wined3d_shader_resource_view_gl
*view_gl
,
1264 struct wined3d_context_gl
*context_gl
)
1266 if (context_gl
->active_texture
< ARRAY_SIZE(context_gl
->rev_tex_unit_map
))
1268 unsigned int active_sampler
= context_gl
->rev_tex_unit_map
[context_gl
->active_texture
];
1269 if (active_sampler
!= WINED3D_UNMAPPED_STAGE
)
1270 context_invalidate_state(&context_gl
->c
, STATE_SAMPLER(active_sampler
));
1272 /* FIXME: Ideally we'd only do this when touching a binding that's used by
1274 context_invalidate_compute_state(&context_gl
->c
, STATE_COMPUTE_SHADER_RESOURCE_BINDING
);
1275 context_invalidate_state(&context_gl
->c
, STATE_GRAPHICS_SHADER_RESOURCE_BINDING
);
1277 wined3d_context_gl_bind_texture(context_gl
, view_gl
->gl_view
.target
, view_gl
->gl_view
.name
);
1280 void wined3d_shader_resource_view_gl_generate_mipmap(struct wined3d_shader_resource_view_gl
*view_gl
,
1281 struct wined3d_context_gl
*context_gl
)
1283 unsigned int i
, j
, layer_count
, level_count
, base_level
, base_layer
, sub_resource_idx
;
1284 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1285 struct wined3d_texture_gl
*texture_gl
;
1286 struct gl_texture
*gl_tex
;
1290 TRACE("view_gl %p.\n", view_gl
);
1292 layer_count
= view_gl
->v
.desc
.u
.texture
.layer_count
;
1293 level_count
= view_gl
->v
.desc
.u
.texture
.level_count
;
1294 base_level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
1295 base_layer
= view_gl
->v
.desc
.u
.texture
.layer_idx
;
1297 texture_gl
= wined3d_texture_gl(texture_from_resource(view_gl
->v
.resource
));
1298 srgb
= !!(texture_gl
->t
.flags
& WINED3D_TEXTURE_IS_SRGB
);
1299 location
= srgb
? WINED3D_LOCATION_TEXTURE_SRGB
: WINED3D_LOCATION_TEXTURE_RGB
;
1300 for (i
= 0; i
< layer_count
; ++i
)
1302 sub_resource_idx
= (base_layer
+ i
) * texture_gl
->t
.level_count
+ base_level
;
1303 if (!wined3d_texture_load_location(&texture_gl
->t
, sub_resource_idx
, &context_gl
->c
, location
))
1304 ERR("Failed to load source layer %u.\n", base_layer
+ i
);
1307 if (view_gl
->gl_view
.name
)
1309 shader_resource_view_gl_bind_and_dirtify(view_gl
, context_gl
);
1313 wined3d_texture_gl_bind_and_dirtify(texture_gl
, context_gl
, srgb
);
1314 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
, GL_TEXTURE_BASE_LEVEL
, base_level
);
1315 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
, GL_TEXTURE_MAX_LEVEL
, base_level
+ level_count
- 1);
1318 if (gl_info
->supported
[ARB_SAMPLER_OBJECTS
])
1319 GL_EXTCALL(glBindSampler(context_gl
->active_texture
, 0));
1320 gl_tex
= wined3d_texture_gl_get_gl_texture(texture_gl
, srgb
);
1321 if (context_gl
->c
.d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
1322 && gl_info
->supported
[EXT_TEXTURE_SRGB_DECODE
])
1324 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
,
1325 GL_TEXTURE_SRGB_DECODE_EXT
, GL_SKIP_DECODE_EXT
);
1326 gl_tex
->sampler_desc
.srgb_decode
= FALSE
;
1329 gl_info
->fbo_ops
.glGenerateMipmap(texture_gl
->target
);
1330 checkGLcall("glGenerateMipMap()");
1332 for (i
= 0; i
< layer_count
; ++i
)
1334 for (j
= 1; j
< level_count
; ++j
)
1336 sub_resource_idx
= (base_layer
+ i
) * texture_gl
->t
.level_count
+ base_level
+ j
;
1337 wined3d_texture_validate_location(&texture_gl
->t
, sub_resource_idx
, location
);
1338 wined3d_texture_invalidate_location(&texture_gl
->t
, sub_resource_idx
, ~location
);
1342 if (!view_gl
->gl_view
.name
)
1344 gl_tex
->base_level
= base_level
;
1345 gl_info
->gl_ops
.gl
.p_glTexParameteri(texture_gl
->target
,
1346 GL_TEXTURE_MAX_LEVEL
, texture_gl
->t
.level_count
- 1);
1350 void wined3d_shader_resource_view_vk_generate_mipmap(struct wined3d_shader_resource_view_vk
*srv_vk
,
1351 struct wined3d_context_vk
*context_vk
)
1353 unsigned int i
, j
, layer_count
, level_count
, base_level
, base_layer
, sub_resource_idx
;
1354 const struct wined3d_vk_info
*vk_info
= context_vk
->vk_info
;
1355 VkImageSubresourceRange vk_src_range
, vk_dst_range
;
1356 struct wined3d_texture_vk
*texture_vk
;
1357 VkCommandBuffer vk_command_buffer
;
1360 TRACE("srv_vk %p.\n", srv_vk
);
1362 layer_count
= srv_vk
->v
.desc
.u
.texture
.layer_count
;
1363 level_count
= srv_vk
->v
.desc
.u
.texture
.level_count
;
1364 base_level
= srv_vk
->v
.desc
.u
.texture
.level_idx
;
1365 base_layer
= srv_vk
->v
.desc
.u
.texture
.layer_idx
;
1367 texture_vk
= wined3d_texture_vk(texture_from_resource(srv_vk
->v
.resource
));
1368 for (i
= 0; i
< layer_count
; ++i
)
1370 sub_resource_idx
= (base_layer
+ i
) * texture_vk
->t
.level_count
+ base_level
;
1371 if (!wined3d_texture_load_location(&texture_vk
->t
, sub_resource_idx
,
1372 &context_vk
->c
, WINED3D_LOCATION_TEXTURE_RGB
))
1373 ERR("Failed to load source layer %u.\n", base_layer
+ i
);
1376 if (context_vk
->c
.d3d_info
->wined3d_creation_flags
& WINED3D_SRGB_READ_WRITE_CONTROL
)
1377 FIXME("Unhandled sRGB read/write control.\n");
1379 if (wined3d_format_vk(srv_vk
->v
.format
)->vk_format
!= wined3d_format_vk(texture_vk
->t
.resource
.format
)->vk_format
)
1380 FIXME("Ignoring view format %s.\n", debug_d3dformat(srv_vk
->v
.format
->id
));
1382 if (wined3d_resource_get_sample_count(&texture_vk
->t
.resource
) > 1)
1383 FIXME("Unhandled multi-sampled resource.\n");
1385 if (!(vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
)))
1387 ERR("Failed to get command buffer.\n");
1391 vk_src_range
.aspectMask
= vk_aspect_mask_from_format(texture_vk
->t
.resource
.format
);
1392 vk_src_range
.baseMipLevel
= base_level
;
1393 vk_src_range
.levelCount
= 1;
1394 vk_src_range
.baseArrayLayer
= base_layer
;
1395 vk_src_range
.layerCount
= layer_count
;
1397 vk_dst_range
= vk_src_range
;
1398 ++vk_dst_range
.baseMipLevel
;
1400 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1401 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1402 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1403 VK_ACCESS_TRANSFER_READ_BIT
,
1404 texture_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1405 texture_vk
->image
.vk_image
, &vk_src_range
);
1406 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1407 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1408 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1409 VK_ACCESS_TRANSFER_WRITE_BIT
,
1410 texture_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1411 texture_vk
->image
.vk_image
, &vk_dst_range
);
1413 region
.srcSubresource
.aspectMask
= vk_src_range
.aspectMask
;
1414 region
.srcSubresource
.mipLevel
= vk_src_range
.baseMipLevel
;
1415 region
.srcSubresource
.baseArrayLayer
= vk_src_range
.baseArrayLayer
;
1416 region
.srcSubresource
.layerCount
= vk_src_range
.layerCount
;
1417 region
.srcOffsets
[0].x
= 0;
1418 region
.srcOffsets
[0].y
= 0;
1419 region
.srcOffsets
[0].z
= 0;
1421 region
.dstSubresource
.aspectMask
= vk_dst_range
.aspectMask
;
1422 region
.dstSubresource
.mipLevel
= vk_dst_range
.baseMipLevel
;
1423 region
.dstSubresource
.baseArrayLayer
= vk_dst_range
.baseArrayLayer
;
1424 region
.dstSubresource
.layerCount
= vk_dst_range
.layerCount
;
1425 region
.dstOffsets
[0].x
= 0;
1426 region
.dstOffsets
[0].y
= 0;
1427 region
.dstOffsets
[0].z
= 0;
1429 for (i
= 1; i
< level_count
; ++i
)
1431 region
.srcOffsets
[1].x
= wined3d_texture_get_level_width(&texture_vk
->t
, vk_src_range
.baseMipLevel
);
1432 region
.srcOffsets
[1].y
= wined3d_texture_get_level_height(&texture_vk
->t
, vk_src_range
.baseMipLevel
);
1433 region
.srcOffsets
[1].z
= wined3d_texture_get_level_depth(&texture_vk
->t
, vk_src_range
.baseMipLevel
);
1435 region
.dstOffsets
[1].x
= wined3d_texture_get_level_width(&texture_vk
->t
, vk_dst_range
.baseMipLevel
);
1436 region
.dstOffsets
[1].y
= wined3d_texture_get_level_height(&texture_vk
->t
, vk_dst_range
.baseMipLevel
);
1437 region
.dstOffsets
[1].z
= wined3d_texture_get_level_depth(&texture_vk
->t
, vk_dst_range
.baseMipLevel
);
1439 VK_CALL(vkCmdBlitImage(vk_command_buffer
, texture_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1440 texture_vk
->image
.vk_image
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, 1, ®ion
, VK_FILTER_LINEAR
));
1442 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1443 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1444 VK_ACCESS_TRANSFER_READ_BIT
,
1445 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1446 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
, texture_vk
->layout
,
1447 texture_vk
->image
.vk_image
, &vk_src_range
);
1449 if (i
== level_count
- 1)
1451 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1452 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
,
1453 VK_ACCESS_TRANSFER_WRITE_BIT
,
1454 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1455 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, texture_vk
->layout
,
1456 texture_vk
->image
.vk_image
, &vk_dst_range
);
1460 region
.srcSubresource
.mipLevel
= ++vk_src_range
.baseMipLevel
;
1461 region
.dstSubresource
.mipLevel
= ++vk_dst_range
.baseMipLevel
;
1463 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1464 VK_PIPELINE_STAGE_TRANSFER_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1465 VK_ACCESS_TRANSFER_WRITE_BIT
, VK_ACCESS_TRANSFER_READ_BIT
,
1466 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL
,
1467 texture_vk
->image
.vk_image
, &vk_src_range
);
1468 wined3d_context_vk_image_barrier(context_vk
, vk_command_buffer
,
1469 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT
, VK_PIPELINE_STAGE_TRANSFER_BIT
,
1470 vk_access_mask_from_bind_flags(texture_vk
->t
.resource
.bind_flags
),
1471 VK_ACCESS_TRANSFER_WRITE_BIT
,
1472 texture_vk
->layout
, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
,
1473 texture_vk
->image
.vk_image
, &vk_dst_range
);
1477 for (i
= 0; i
< layer_count
; ++i
)
1479 for (j
= 1; j
< level_count
; ++j
)
1481 sub_resource_idx
= (base_layer
+ i
) * texture_vk
->t
.level_count
+ base_level
+ j
;
1482 wined3d_texture_validate_location(&texture_vk
->t
, sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
1483 wined3d_texture_invalidate_location(&texture_vk
->t
, sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1487 wined3d_context_vk_reference_texture(context_vk
, texture_vk
);
1490 ULONG CDECL
wined3d_unordered_access_view_incref(struct wined3d_unordered_access_view
*view
)
1492 unsigned int refcount
= InterlockedIncrement(&view
->refcount
);
1494 TRACE("%p increasing refcount to %u.\n", view
, refcount
);
1499 void wined3d_unordered_access_view_cleanup(struct wined3d_unordered_access_view
*view
)
1501 /* Call wined3d_object_destroyed() before releasing the resource,
1502 * since releasing the resource may end up destroying the parent. */
1503 view
->parent_ops
->wined3d_object_destroyed(view
->parent
);
1504 wined3d_resource_decref(view
->resource
);
1507 ULONG CDECL
wined3d_unordered_access_view_decref(struct wined3d_unordered_access_view
*view
)
1509 unsigned int refcount
= InterlockedDecrement(&view
->refcount
);
1511 TRACE("%p decreasing refcount to %u.\n", view
, refcount
);
1515 wined3d_mutex_lock();
1516 view
->resource
->device
->adapter
->adapter_ops
->adapter_destroy_unordered_access_view(view
);
1517 wined3d_mutex_unlock();
1523 void * CDECL
wined3d_unordered_access_view_get_parent(const struct wined3d_unordered_access_view
*view
)
1525 TRACE("view %p.\n", view
);
1527 return view
->parent
;
1530 void wined3d_unordered_access_view_invalidate_location(struct wined3d_unordered_access_view
*view
,
1533 wined3d_view_invalidate_location(view
->resource
, &view
->desc
, location
);
1536 void wined3d_unordered_access_view_gl_clear(struct wined3d_unordered_access_view_gl
*view_gl
,
1537 const struct wined3d_uvec4
*clear_value
, struct wined3d_context_gl
*context_gl
, bool fp
)
1539 const struct wined3d_gl_info
*gl_info
= context_gl
->gl_info
;
1540 const struct wined3d_format_gl
*format_gl
;
1541 struct wined3d_resource
*resource
;
1542 struct wined3d_buffer
*buffer
;
1543 struct wined3d_bo_gl
*bo_gl
;
1544 unsigned int offset
, size
;
1546 resource
= view_gl
->v
.resource
;
1547 if (resource
->type
!= WINED3D_RTYPE_BUFFER
)
1549 unsigned int layer_count
, level_count
, base_level
, base_layer
;
1550 unsigned int sub_resource_idx
, width
, height
, depth
, i
, j
;
1551 struct wined3d_texture_gl
*texture_gl
;
1552 const void *data
= clear_value
;
1553 GLenum gl_format
, gl_type
;
1556 if (!gl_info
->supported
[ARB_CLEAR_TEXTURE
])
1558 FIXME("OpenGL implementation does not support ARB_clear_texture.\n");
1562 format_gl
= wined3d_format_gl(resource
->format
);
1563 texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
1564 level_count
= view_gl
->v
.desc
.u
.texture
.level_count
;
1565 base_level
= view_gl
->v
.desc
.u
.texture
.level_idx
;
1567 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
1574 layer_count
= view_gl
->v
.desc
.u
.texture
.layer_count
;
1575 base_layer
= view_gl
->v
.desc
.u
.texture
.layer_idx
;
1578 if (format_gl
->f
.byte_count
<= 4 && !fp
)
1580 gl_format
= format_gl
->format
;
1581 gl_type
= format_gl
->type
;
1582 packed
= wined3d_format_pack(&format_gl
->f
, clear_value
);
1585 else if (resource
->format_attrs
& WINED3D_FORMAT_ATTR_INTEGER
)
1587 gl_format
= GL_RGBA_INTEGER
;
1588 gl_type
= GL_UNSIGNED_INT
;
1592 gl_format
= GL_RGBA
;
1596 for (i
= 0; i
< layer_count
; ++i
)
1598 for (j
= 0; j
< level_count
; ++j
)
1600 sub_resource_idx
= (base_layer
+ i
) * texture_gl
->t
.level_count
+ base_level
+ j
;
1601 wined3d_texture_prepare_location(&texture_gl
->t
, sub_resource_idx
,
1602 &context_gl
->c
, WINED3D_LOCATION_TEXTURE_RGB
);
1604 width
= wined3d_texture_get_level_width(&texture_gl
->t
, base_level
+ j
);
1605 height
= wined3d_texture_get_level_height(&texture_gl
->t
, base_level
+ j
);
1606 depth
= wined3d_texture_get_level_depth(&texture_gl
->t
, base_level
+ j
);
1608 switch (texture_gl
->target
)
1610 case GL_TEXTURE_1D_ARRAY
:
1611 GL_EXTCALL(glClearTexSubImage(texture_gl
->texture_rgb
.name
, base_level
+ j
,
1612 0, base_layer
+ i
, 0, width
, 1, 1, gl_format
, gl_type
, data
));
1615 case GL_TEXTURE_2D_ARRAY
:
1616 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY
:
1617 case GL_TEXTURE_CUBE_MAP
:
1618 case GL_TEXTURE_CUBE_MAP_ARRAY
:
1619 GL_EXTCALL(glClearTexSubImage(texture_gl
->texture_rgb
.name
, base_level
+ j
,
1620 0, 0, base_layer
+ i
, width
, height
, 1, gl_format
, gl_type
, data
));
1624 GL_EXTCALL(glClearTexSubImage(texture_gl
->texture_rgb
.name
, base_level
+ j
,
1625 0, 0, 0, width
, height
, depth
, gl_format
, gl_type
, data
));
1629 wined3d_texture_validate_location(&texture_gl
->t
, sub_resource_idx
, WINED3D_LOCATION_TEXTURE_RGB
);
1630 wined3d_texture_invalidate_location(&texture_gl
->t
, sub_resource_idx
, ~WINED3D_LOCATION_TEXTURE_RGB
);
1637 if (!gl_info
->supported
[ARB_CLEAR_BUFFER_OBJECT
])
1639 FIXME("OpenGL implementation does not support ARB_clear_buffer_object.\n");
1643 format_gl
= wined3d_format_gl(view_gl
->v
.format
);
1644 if (format_gl
->f
.id
!= WINED3DFMT_R32_UINT
&& format_gl
->f
.id
!= WINED3DFMT_R32_SINT
1645 && format_gl
->f
.id
!= WINED3DFMT_R32G32B32A32_UINT
1646 && format_gl
->f
.id
!= WINED3DFMT_R32G32B32A32_SINT
)
1648 FIXME("Not implemented for format %s.\n", debug_d3dformat(format_gl
->f
.id
));
1654 FIXME("Floating-point buffer clears not implemented.\n");
1658 buffer
= buffer_from_resource(resource
);
1659 wined3d_buffer_load_location(buffer
, &context_gl
->c
, WINED3D_LOCATION_BUFFER
);
1660 wined3d_unordered_access_view_invalidate_location(&view_gl
->v
, ~WINED3D_LOCATION_BUFFER
);
1662 bo_gl
= wined3d_bo_gl(buffer
->buffer_object
);
1663 get_buffer_view_range(buffer
, &view_gl
->v
.desc
, &format_gl
->f
, &offset
, &size
);
1664 wined3d_context_gl_bind_bo(context_gl
, bo_gl
->binding
, bo_gl
->id
);
1665 GL_EXTCALL(glClearBufferSubData(bo_gl
->binding
, format_gl
->internal
,
1666 bo_gl
->b
.buffer_offset
+ offset
, size
, format_gl
->format
, format_gl
->type
, clear_value
));
1667 wined3d_context_gl_reference_bo(context_gl
, bo_gl
);
1668 checkGLcall("clear unordered access view");
1671 void wined3d_unordered_access_view_set_counter(struct wined3d_unordered_access_view
*view
,
1674 struct wined3d_bo_address dst
, src
;
1675 struct wined3d_context
*context
;
1676 struct wined3d_range range
;
1678 if (!view
->counter_bo
)
1681 context
= context_acquire(view
->resource
->device
, NULL
, 0);
1683 src
.buffer_object
= 0;
1684 src
.addr
= (void *)&value
;
1686 dst
.buffer_object
= view
->counter_bo
;
1690 range
.size
= sizeof(value
);
1691 wined3d_context_copy_bo_address(context
, &dst
, &src
, 1, &range
);
1693 context_release(context
);
1696 void wined3d_unordered_access_view_copy_counter(struct wined3d_unordered_access_view
*view
,
1697 struct wined3d_buffer
*buffer
, unsigned int offset
, struct wined3d_context
*context
)
1699 struct wined3d_const_bo_address src
;
1701 if (!view
->counter_bo
)
1704 src
.buffer_object
= view
->counter_bo
;
1707 wined3d_buffer_copy_bo_address(buffer
, context
, offset
, &src
, sizeof(uint32_t));
1710 void wined3d_unordered_access_view_gl_update(struct wined3d_unordered_access_view_gl
*uav_gl
,
1711 struct wined3d_context_gl
*context_gl
)
1713 create_buffer_view(&uav_gl
->gl_view
, &context_gl
->c
, &uav_gl
->v
.desc
,
1714 buffer_from_resource(uav_gl
->v
.resource
), uav_gl
->v
.format
);
1715 uav_gl
->bo_user
.valid
= true;
1718 static void wined3d_unordered_access_view_gl_cs_init(void *object
)
1720 struct wined3d_unordered_access_view_gl
*view_gl
= object
;
1721 struct wined3d_resource
*resource
= view_gl
->v
.resource
;
1722 struct wined3d_view_desc
*desc
= &view_gl
->v
.desc
;
1723 const struct wined3d_gl_info
*gl_info
;
1725 TRACE("view_gl %p.\n", view_gl
);
1727 gl_info
= &resource
->device
->adapter
->gl_info
;
1729 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
1731 struct wined3d_device_gl
*device_gl
= wined3d_device_gl(resource
->device
);
1732 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
1733 struct wined3d_context_gl
*context_gl
;
1735 context_gl
= wined3d_context_gl(context_acquire(&device_gl
->d
, NULL
, 0));
1736 create_buffer_view(&view_gl
->gl_view
, &context_gl
->c
, desc
, buffer
, view_gl
->v
.format
);
1737 view_gl
->bo_user
.valid
= true;
1738 list_add_head(&buffer
->buffer_object
->users
, &view_gl
->bo_user
.entry
);
1739 if (desc
->flags
& (WINED3D_VIEW_BUFFER_COUNTER
| WINED3D_VIEW_BUFFER_APPEND
))
1741 struct wined3d_bo_gl
*bo
= &view_gl
->counter_bo
;
1743 view_gl
->v
.counter_bo
= &bo
->b
;
1744 wined3d_device_gl_create_bo(device_gl
, context_gl
, sizeof(uint32_t), GL_ATOMIC_COUNTER_BUFFER
,
1745 GL_STATIC_DRAW
, true, GL_MAP_READ_BIT
| GL_MAP_WRITE_BIT
| GL_CLIENT_STORAGE_BIT
, bo
);
1746 wined3d_unordered_access_view_set_counter(&view_gl
->v
, 0);
1748 context_release(&context_gl
->c
);
1752 struct wined3d_texture_gl
*texture_gl
= wined3d_texture_gl(texture_from_resource(resource
));
1753 unsigned int depth_or_layer_count
;
1755 if (resource
->type
== WINED3D_RTYPE_TEXTURE_3D
)
1756 depth_or_layer_count
= wined3d_texture_get_level_depth(&texture_gl
->t
, desc
->u
.texture
.level_idx
);
1758 depth_or_layer_count
= texture_gl
->t
.layer_count
;
1760 if (desc
->u
.texture
.layer_idx
|| desc
->u
.texture
.layer_count
!= depth_or_layer_count
)
1762 create_texture_view(&view_gl
->gl_view
, get_texture_view_target(gl_info
, desc
, texture_gl
),
1763 desc
, texture_gl
, view_gl
->v
.format
);
1768 static HRESULT
wined3d_unordered_access_view_init(struct wined3d_unordered_access_view
*view
,
1769 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1770 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1773 view
->parent
= parent
;
1774 view
->parent_ops
= parent_ops
;
1776 if (!(resource
->bind_flags
& WINED3D_BIND_UNORDERED_ACCESS
))
1777 return E_INVALIDARG
;
1778 if (!(view
->format
= validate_resource_view(desc
, resource
, TRUE
, FALSE
)))
1779 return E_INVALIDARG
;
1782 wined3d_resource_incref(view
->resource
= resource
);
1787 HRESULT
wined3d_unordered_access_view_gl_init(struct wined3d_unordered_access_view_gl
*view_gl
,
1788 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
1789 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
1793 TRACE("view_gl %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
1794 view_gl
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
1796 if (FAILED(hr
= wined3d_unordered_access_view_init(&view_gl
->v
, desc
, resource
, parent
, parent_ops
)))
1799 list_init(&view_gl
->bo_user
.entry
);
1800 wined3d_cs_init_object(resource
->device
->cs
, wined3d_unordered_access_view_gl_cs_init
, view_gl
);
1805 struct wined3d_uav_clear_constants_vk
1807 VkClearColorValue color
;
1812 static VkPipeline
create_uav_pipeline(struct wined3d_context_vk
*context_vk
,
1813 struct wined3d_pipeline_layout_vk
*layout
, const unsigned int *byte_code
, size_t byte_code_size
,
1814 enum wined3d_shader_resource_type resource_type
)
1816 VkComputePipelineCreateInfo pipeline_info
;
1817 struct wined3d_shader_desc shader_desc
;
1818 const struct wined3d_vk_info
*vk_info
;
1819 struct wined3d_context
*context
;
1820 VkShaderModule shader_module
;
1825 vk_info
= context_vk
->vk_info
;
1826 context
= &context_vk
->c
;
1828 shader_desc
.byte_code
= (const DWORD
*)byte_code
;
1829 shader_desc
.byte_code_size
= byte_code_size
;
1831 shader_module
= (VkShaderModule
)context
->device
->adapter
->shader_backend
->shader_compile(context
, &shader_desc
,
1832 WINED3D_SHADER_TYPE_COMPUTE
);
1833 if (shader_module
== VK_NULL_HANDLE
)
1835 ERR("Failed to create shader.\n");
1836 return VK_NULL_HANDLE
;
1839 pipeline_info
.sType
= VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO
;
1840 pipeline_info
.pNext
= NULL
;
1841 pipeline_info
.flags
= 0;
1842 pipeline_info
.stage
.sType
= VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO
;
1843 pipeline_info
.stage
.pNext
= NULL
;
1844 pipeline_info
.stage
.flags
= 0;
1845 pipeline_info
.stage
.stage
= VK_SHADER_STAGE_COMPUTE_BIT
;
1846 pipeline_info
.stage
.pName
= "main";
1847 pipeline_info
.stage
.pSpecializationInfo
= NULL
;
1848 pipeline_info
.stage
.module
= shader_module
;
1849 pipeline_info
.layout
= layout
->vk_pipeline_layout
;
1850 pipeline_info
.basePipelineHandle
= VK_NULL_HANDLE
;
1851 pipeline_info
.basePipelineIndex
= -1;
1853 vk_device
= wined3d_device_vk(context
->device
)->vk_device
;
1855 if ((vr
= VK_CALL(vkCreateComputePipelines(vk_device
, VK_NULL_HANDLE
, 1, &pipeline_info
, NULL
, &result
))) < 0)
1857 ERR("Failed to create Vulkan compute pipeline, vr %s.\n", wined3d_debug_vkresult(vr
));
1858 return VK_NULL_HANDLE
;
1861 VK_CALL(vkDestroyShaderModule(vk_device
, shader_module
, NULL
));
1865 void wined3d_device_vk_uav_clear_state_init(struct wined3d_device_vk
*device_vk
)
1867 struct wined3d_uav_clear_state_vk
*state
= &device_vk
->uav_clear_state
;
1868 struct wined3d_context_vk
*context_vk
= &device_vk
->context_vk
;
1869 VkDescriptorSetLayoutBinding vk_set_bindings
[2];
1871 vk_set_bindings
[0].binding
= 0;
1872 vk_set_bindings
[0].descriptorCount
= 1;
1873 vk_set_bindings
[0].stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
;
1874 vk_set_bindings
[0].pImmutableSamplers
= NULL
;
1875 vk_set_bindings
[0].descriptorType
= VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
1877 vk_set_bindings
[1].binding
= 1;
1878 vk_set_bindings
[1].descriptorCount
= 1;
1879 vk_set_bindings
[1].stageFlags
= VK_SHADER_STAGE_COMPUTE_BIT
;
1880 vk_set_bindings
[1].pImmutableSamplers
= NULL
;
1882 vk_set_bindings
[1].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
;
1883 state
->image_layout
= wined3d_context_vk_get_pipeline_layout(context_vk
, vk_set_bindings
, 2);
1885 vk_set_bindings
[1].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
;
1886 state
->buffer_layout
= wined3d_context_vk_get_pipeline_layout(context_vk
, vk_set_bindings
, 2);
1888 #define SHADER_DESC(name) name, sizeof(name)
1889 state
->float_pipelines
.buffer
= create_uav_pipeline(context_vk
, state
->buffer_layout
,
1890 SHADER_DESC(cs_uav_clear_buffer_float_code
), WINED3D_SHADER_RESOURCE_BUFFER
);
1891 state
->uint_pipelines
.buffer
= create_uav_pipeline(context_vk
, state
->buffer_layout
,
1892 SHADER_DESC(cs_uav_clear_buffer_uint_code
), WINED3D_SHADER_RESOURCE_BUFFER
);
1893 state
->float_pipelines
.image_1d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1894 SHADER_DESC(cs_uav_clear_1d_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1D
);
1895 state
->uint_pipelines
.image_1d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1896 SHADER_DESC(cs_uav_clear_1d_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1D
);
1897 state
->float_pipelines
.image_1d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1898 SHADER_DESC(cs_uav_clear_1d_array_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
);
1899 state
->uint_pipelines
.image_1d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1900 SHADER_DESC(cs_uav_clear_1d_array_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
);
1901 state
->float_pipelines
.image_2d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1902 SHADER_DESC(cs_uav_clear_2d_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2D
);
1903 state
->uint_pipelines
.image_2d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1904 SHADER_DESC(cs_uav_clear_2d_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2D
);
1905 state
->float_pipelines
.image_2d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1906 SHADER_DESC(cs_uav_clear_2d_array_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
);
1907 state
->uint_pipelines
.image_2d_array
= create_uav_pipeline(context_vk
, state
->image_layout
,
1908 SHADER_DESC(cs_uav_clear_2d_array_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY
);
1909 state
->float_pipelines
.image_3d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1910 SHADER_DESC(cs_uav_clear_3d_float_code
), WINED3D_SHADER_RESOURCE_TEXTURE_3D
);
1911 state
->uint_pipelines
.image_3d
= create_uav_pipeline(context_vk
, state
->image_layout
,
1912 SHADER_DESC(cs_uav_clear_3d_uint_code
), WINED3D_SHADER_RESOURCE_TEXTURE_3D
);
1915 state
->buffer_group_size
.x
= 128;
1916 state
->buffer_group_size
.y
= 1;
1917 state
->buffer_group_size
.z
= 1;
1918 state
->image_1d_group_size
.x
= 64;
1919 state
->image_1d_group_size
.y
= 1;
1920 state
->image_1d_group_size
.z
= 1;
1921 state
->image_1d_array_group_size
.x
= 64;
1922 state
->image_1d_array_group_size
.y
= 1;
1923 state
->image_1d_array_group_size
.z
= 1;
1924 state
->image_2d_group_size
.x
= 8;
1925 state
->image_2d_group_size
.y
= 8;
1926 state
->image_2d_group_size
.z
= 1;
1927 state
->image_2d_array_group_size
.x
= 8;
1928 state
->image_2d_array_group_size
.y
= 8;
1929 state
->image_2d_array_group_size
.z
= 1;
1930 state
->image_3d_group_size
.x
= 8;
1931 state
->image_3d_group_size
.y
= 8;
1932 state
->image_3d_group_size
.z
= 1;
1935 void wined3d_device_vk_uav_clear_state_cleanup(struct wined3d_device_vk
*device_vk
)
1937 struct wined3d_uav_clear_state_vk
*state
= &device_vk
->uav_clear_state
;
1938 const struct wined3d_vk_info
*vk_info
= &device_vk
->vk_info
;
1940 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.buffer
, NULL
));
1941 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_1d
, NULL
));
1942 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_1d_array
, NULL
));
1943 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_2d
, NULL
));
1944 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_2d_array
, NULL
));
1945 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->float_pipelines
.image_3d
, NULL
));
1947 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.buffer
, NULL
));
1948 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_1d
, NULL
));
1949 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_1d_array
, NULL
));
1950 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_2d
, NULL
));
1951 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_2d_array
, NULL
));
1952 VK_CALL(vkDestroyPipeline(device_vk
->vk_device
, state
->uint_pipelines
.image_3d
, NULL
));
1955 void wined3d_unordered_access_view_vk_clear(struct wined3d_unordered_access_view_vk
*view_vk
,
1956 const struct wined3d_uvec4
*clear_value
, struct wined3d_context_vk
*context_vk
, bool fp
)
1958 struct wined3d_bo_address cb_source_address
, cb_destination_address
;
1959 const struct wined3d_format
*view_format
= view_vk
->v
.format
;
1960 struct wined3d_view_desc
*view_desc
= &view_vk
->v
.desc
;
1961 struct wined3d_uav_clear_constants_vk constants
= {0};
1962 struct wined3d_device
*device
= context_vk
->c
.device
;
1963 struct wined3d_shader_thread_group_size group_count
;
1964 enum wined3d_format_id format_id
= view_format
->id
;
1965 struct wined3d_uav_clear_pipelines_vk
*pipelines
;
1966 struct wined3d_texture_vk
*texture_vk
= NULL
;
1967 struct wined3d_pipeline_layout_vk
*layout
;
1968 struct wined3d_uav_clear_state_vk
*state
;
1969 const struct wined3d_vk_info
*vk_info
;
1970 VkDescriptorImageInfo vk_image_info
;
1971 struct wined3d_device_vk
*device_vk
;
1972 VkDescriptorBufferInfo buffer_info
;
1973 struct wined3d_resource
*resource
;
1974 VkCommandBuffer vk_command_buffer
;
1975 struct wined3d_bo_vk constants_bo
;
1976 VkWriteDescriptorSet vk_writes
[2];
1977 VkBufferView vk_buffer_view
;
1978 struct wined3d_range range
;
1979 VkMemoryBarrier vk_barrier
;
1980 VkPipeline vk_pipeline
;
1985 device_vk
= wined3d_device_vk(device
);
1986 state
= &device_vk
->uav_clear_state
;
1987 pipelines
= fp
? &state
->float_pipelines
: &state
->uint_pipelines
;
1989 resource
= view_vk
->v
.resource
;
1990 is_array
= view_desc
->flags
& WINED3D_VIEW_TEXTURE_ARRAY
;
1992 switch (resource
->type
)
1994 case WINED3D_RTYPE_BUFFER
:
1995 vk_pipeline
= pipelines
->buffer
;
1996 group_count
= state
->buffer_group_size
;
1998 case WINED3D_RTYPE_TEXTURE_1D
:
2001 vk_pipeline
= pipelines
->image_1d_array
;
2002 group_count
= state
->image_1d_array_group_size
;
2006 vk_pipeline
= pipelines
->image_1d
;
2007 group_count
= state
->image_1d_group_size
;
2010 case WINED3D_RTYPE_TEXTURE_2D
:
2013 vk_pipeline
= pipelines
->image_2d_array
;
2014 group_count
= state
->image_2d_array_group_size
;
2018 vk_pipeline
= pipelines
->image_2d
;
2019 group_count
= state
->image_2d_group_size
;
2022 case WINED3D_RTYPE_TEXTURE_3D
:
2023 vk_pipeline
= pipelines
->image_3d
;
2024 group_count
= state
->image_3d_group_size
;
2028 ERR("Unhandled resource type %s.\n", debug_d3dresourcetype(resource
->type
));
2032 if (vk_pipeline
== VK_NULL_HANDLE
)
2034 ERR("Pipeline was not correctly initialized.\n");
2038 vk_writes
[0].sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
;
2039 vk_writes
[0].pNext
= NULL
;
2040 vk_writes
[0].dstBinding
= 1;
2041 vk_writes
[0].dstArrayElement
= 0;
2042 vk_writes
[0].descriptorCount
= 1;
2043 vk_writes
[0].pImageInfo
= NULL
;
2044 vk_writes
[0].pTexelBufferView
= &vk_buffer_view
;
2045 vk_writes
[0].pImageInfo
= &vk_image_info
;
2047 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2049 struct wined3d_buffer
*buffer
= buffer_from_resource(resource
);
2051 uav_location
= WINED3D_LOCATION_BUFFER
;
2052 layout
= state
->buffer_layout
;
2053 vk_writes
[0].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
;
2055 if (buffer
->structure_byte_stride
)
2056 constants
.extent
.width
= view_desc
->u
.buffer
.count
* buffer
->structure_byte_stride
/ 4;
2058 constants
.extent
.width
= view_desc
->u
.buffer
.count
;
2059 constants
.extent
.height
= 1;
2063 texture_vk
= wined3d_texture_vk(wined3d_texture_from_resource(resource
));
2065 uav_location
= WINED3D_LOCATION_TEXTURE_RGB
;
2066 layout
= state
->image_layout
;
2067 vk_writes
[0].descriptorType
= VK_DESCRIPTOR_TYPE_STORAGE_IMAGE
;
2069 level
= view_desc
->u
.texture
.level_idx
;
2070 constants
.extent
.width
= wined3d_texture_get_level_width(&texture_vk
->t
, level
);
2071 constants
.extent
.height
= wined3d_texture_get_level_height(&texture_vk
->t
, level
);
2072 group_count
.z
= (view_desc
->u
.texture
.layer_count
+ group_count
.z
- 1) / group_count
.z
;
2075 group_count
.x
= (constants
.extent
.width
+ group_count
.x
- 1) / group_count
.x
;
2076 group_count
.y
= (constants
.extent
.height
+ group_count
.y
- 1) / group_count
.y
;
2078 constants
.color
.uint32
[0] = clear_value
->x
;
2079 constants
.color
.uint32
[1] = clear_value
->y
;
2080 constants
.color
.uint32
[2] = clear_value
->z
;
2081 constants
.color
.uint32
[3] = clear_value
->w
;
2085 /* Make sure values are truncated, not saturated to some maximum value. */
2086 constants
.color
.uint32
[0] &= wined3d_mask_from_size(view_format
->red_size
);
2087 constants
.color
.uint32
[1] &= wined3d_mask_from_size(view_format
->green_size
);
2088 constants
.color
.uint32
[2] &= wined3d_mask_from_size(view_format
->blue_size
);
2089 constants
.color
.uint32
[3] &= wined3d_mask_from_size(view_format
->alpha_size
);
2091 if (view_format
->id
== WINED3DFMT_R11G11B10_FLOAT
)
2093 constants
.color
.uint32
[0] |= constants
.color
.uint32
[1] << 11;
2094 constants
.color
.uint32
[0] |= constants
.color
.uint32
[2] << 22;
2095 format_id
= WINED3DFMT_R32_UINT
;
2099 format_id
= wined3d_get_typed_format_id(context_vk
->c
.device
->adapter
, view_format
,
2100 WINED3D_CHANNEL_TYPE_UINT
);
2104 if (format_id
== WINED3DFMT_UNKNOWN
)
2106 ERR("Unsupported format %s.\n", debug_d3dformat(view_format
->id
));
2110 wined3d_view_load_location(resource
, view_desc
, &context_vk
->c
, uav_location
);
2111 wined3d_unordered_access_view_invalidate_location(&view_vk
->v
, ~uav_location
);
2113 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2115 if (format_id
== view_format
->id
)
2116 vk_buffer_view
= view_vk
->view_vk
.u
.vk_buffer_view
;
2119 vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, view_desc
,
2120 wined3d_buffer_vk(buffer_from_resource(resource
)), wined3d_format_vk(
2121 wined3d_get_format(context_vk
->c
.device
->adapter
, format_id
, WINED3D_BIND_UNORDERED_ACCESS
)));
2122 if (vk_buffer_view
== VK_NULL_HANDLE
)
2128 if (format_id
== view_format
->id
)
2130 vk_image_info
= view_vk
->view_vk
.u
.vk_image_info
;
2131 if (!vk_image_info
.imageView
)
2133 const VkDescriptorImageInfo
*default_info
;
2134 if (!(default_info
= wined3d_texture_vk_get_default_image_info(texture_vk
, context_vk
)))
2136 vk_image_info
= *default_info
;
2141 vk_image_info
.sampler
= VK_NULL_HANDLE
;
2142 vk_image_info
.imageLayout
= texture_vk
->layout
;
2143 vk_image_info
.imageView
= wined3d_view_vk_create_vk_image_view(context_vk
, view_desc
, texture_vk
,
2144 wined3d_format_vk(wined3d_get_format(context_vk
->c
.device
->adapter
, format_id
,
2145 WINED3D_BIND_UNORDERED_ACCESS
)),
2146 COLOR_FIXUP_IDENTITY
, false);
2148 if (vk_image_info
.imageView
== VK_NULL_HANDLE
)
2153 vk_writes
[1].sType
= VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET
;
2154 vk_writes
[1].pNext
= NULL
;
2155 vk_writes
[1].dstBinding
= 0;
2156 vk_writes
[1].dstArrayElement
= 0;
2157 vk_writes
[1].descriptorCount
= 1;
2158 vk_writes
[1].pImageInfo
= NULL
;
2159 vk_writes
[1].pTexelBufferView
= NULL
;
2160 vk_writes
[1].descriptorType
= VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER
;
2161 vk_writes
[1].pBufferInfo
= &buffer_info
;
2163 if (!wined3d_context_vk_create_bo(context_vk
, sizeof(constants
), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
,
2164 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
, &constants_bo
))
2166 ERR("Failed to create constants BO.\n");
2170 cb_source_address
.buffer_object
= NULL
;
2171 cb_source_address
.addr
= (BYTE
*)&constants
;
2172 cb_destination_address
.buffer_object
= &constants_bo
.b
;
2173 cb_destination_address
.addr
= 0;
2176 range
.size
= sizeof(constants
);
2177 adapter_vk_copy_bo_address(&context_vk
->c
, &cb_destination_address
, &cb_source_address
, 1, &range
);
2179 buffer_info
.buffer
= constants_bo
.vk_buffer
;
2180 buffer_info
.range
= constants_bo
.size
;
2181 buffer_info
.offset
= constants_bo
.b
.buffer_offset
;
2183 vk_info
= context_vk
->vk_info
;
2185 if (!(vk_writes
[0].dstSet
= wined3d_context_vk_create_vk_descriptor_set(context_vk
, layout
->vk_set_layout
)))
2187 ERR("Failed to create descriptor set.\n");
2188 wined3d_context_vk_destroy_bo(context_vk
, &constants_bo
);
2192 vk_writes
[1].dstSet
= vk_writes
[0].dstSet
;
2194 VK_CALL(vkUpdateDescriptorSets(device_vk
->vk_device
, 2, vk_writes
, 0, NULL
));
2196 vk_command_buffer
= wined3d_context_vk_get_command_buffer(context_vk
);
2197 wined3d_context_vk_end_current_render_pass(context_vk
);
2199 wined3d_context_vk_reference_unordered_access_view(context_vk
, view_vk
);
2200 wined3d_context_vk_reference_bo(context_vk
, &constants_bo
);
2201 wined3d_context_vk_destroy_bo(context_vk
, &constants_bo
);
2203 vk_barrier
.sType
= VK_STRUCTURE_TYPE_MEMORY_BARRIER
;
2204 vk_barrier
.pNext
= NULL
;
2205 vk_barrier
.srcAccessMask
= vk_access_mask_from_bind_flags(resource
->bind_flags
);
2206 vk_barrier
.dstAccessMask
= VK_ACCESS_SHADER_WRITE_BIT
;
2208 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer
,
2209 vk_pipeline_stage_mask_from_bind_flags(resource
->bind_flags
),
2210 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
,
2211 0, 1, &vk_barrier
, 0, NULL
, 0, NULL
));
2212 VK_CALL(vkCmdBindPipeline(vk_command_buffer
, VK_PIPELINE_BIND_POINT_COMPUTE
, vk_pipeline
));
2213 VK_CALL(vkCmdBindDescriptorSets(vk_command_buffer
, VK_PIPELINE_BIND_POINT_COMPUTE
,
2214 layout
->vk_pipeline_layout
, 0, 1, &vk_writes
[0].dstSet
, 0, NULL
));
2215 VK_CALL(vkCmdDispatch(vk_command_buffer
, group_count
.x
, group_count
.y
, group_count
.z
));
2217 vk_barrier
.srcAccessMask
= VK_ACCESS_SHADER_WRITE_BIT
;
2218 vk_barrier
.dstAccessMask
= vk_access_mask_from_bind_flags(resource
->bind_flags
);
2220 VK_CALL(vkCmdPipelineBarrier(vk_command_buffer
,
2221 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT
,
2222 vk_pipeline_stage_mask_from_bind_flags(resource
->bind_flags
),
2223 0, 1, &vk_barrier
, 0, NULL
, 0, NULL
));
2225 context_invalidate_compute_state(&context_vk
->c
, STATE_COMPUTE_SHADER
);
2228 if (format_id
!= view_format
->id
)
2230 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2231 wined3d_context_vk_destroy_vk_buffer_view(context_vk
, vk_buffer_view
, view_vk
->view_vk
.command_buffer_id
);
2233 wined3d_context_vk_destroy_vk_image_view(context_vk
, vk_image_info
.imageView
,
2234 view_vk
->view_vk
.command_buffer_id
);
2238 void wined3d_unordered_access_view_vk_update(struct wined3d_unordered_access_view_vk
*uav_vk
,
2239 struct wined3d_context_vk
*context_vk
)
2241 const struct wined3d_format_vk
*view_format_vk
= wined3d_format_vk(uav_vk
->v
.format
);
2242 const struct wined3d_view_desc
*desc
= &uav_vk
->v
.desc
;
2243 struct wined3d_resource
*resource
= uav_vk
->v
.resource
;
2244 struct wined3d_view_vk
*view_vk
= &uav_vk
->view_vk
;
2245 struct wined3d_buffer_vk
*buffer_vk
;
2246 VkBufferView vk_buffer_view
;
2248 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
2249 wined3d_context_vk_destroy_vk_buffer_view(context_vk
, view_vk
->u
.vk_buffer_view
, view_vk
->command_buffer_id
);
2250 if ((vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, desc
, buffer_vk
, view_format_vk
)))
2252 view_vk
->u
.vk_buffer_view
= vk_buffer_view
;
2253 view_vk
->bo_user
.valid
= true;
2257 static void wined3d_unordered_access_view_vk_cs_init(void *object
)
2259 struct wined3d_unordered_access_view_vk
*uav_vk
= object
;
2260 struct wined3d_view_vk
*view_vk
= &uav_vk
->view_vk
;
2261 struct wined3d_view_desc
*desc
= &uav_vk
->v
.desc
;
2262 const struct wined3d_format_vk
*format_vk
;
2263 const struct wined3d_vk_info
*vk_info
;
2264 struct wined3d_texture_vk
*texture_vk
;
2265 struct wined3d_context_vk
*context_vk
;
2266 struct wined3d_device_vk
*device_vk
;
2267 struct wined3d_buffer_vk
*buffer_vk
;
2268 VkBufferViewCreateInfo create_info
;
2269 struct wined3d_resource
*resource
;
2270 VkBufferView vk_buffer_view
;
2271 uint32_t default_flags
= 0;
2272 VkImageView vk_image_view
;
2275 TRACE("uav_vk %p.\n", uav_vk
);
2277 resource
= uav_vk
->v
.resource
;
2278 device_vk
= wined3d_device_vk(resource
->device
);
2279 format_vk
= wined3d_format_vk(uav_vk
->v
.format
);
2281 if (resource
->type
== WINED3D_RTYPE_BUFFER
)
2283 buffer_vk
= wined3d_buffer_vk(buffer_from_resource(resource
));
2285 context_vk
= wined3d_context_vk(context_acquire(&device_vk
->d
, NULL
, 0));
2286 vk_info
= context_vk
->vk_info
;
2288 if ((vk_buffer_view
= wined3d_view_vk_create_vk_buffer_view(context_vk
, desc
, buffer_vk
, format_vk
)))
2290 struct wined3d_bo_vk
*bo
= wined3d_bo_vk(buffer_vk
->b
.buffer_object
);
2292 TRACE("Created buffer view 0x%s.\n", wine_dbgstr_longlong(vk_buffer_view
));
2294 uav_vk
->view_vk
.u
.vk_buffer_view
= vk_buffer_view
;
2295 uav_vk
->view_vk
.bo_user
.valid
= true;
2296 list_add_head(&bo
->b
.users
, &view_vk
->bo_user
.entry
);
2299 if (desc
->flags
& (WINED3D_VIEW_BUFFER_COUNTER
| WINED3D_VIEW_BUFFER_APPEND
))
2301 if (!wined3d_context_vk_create_bo(context_vk
, sizeof(uint32_t), VK_BUFFER_USAGE_TRANSFER_SRC_BIT
2302 | VK_BUFFER_USAGE_TRANSFER_DST_BIT
| VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
,
2303 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
, &uav_vk
->counter_bo
))
2305 ERR("Failed to create counter bo.\n");
2306 context_release(&context_vk
->c
);
2311 wined3d_context_vk_end_current_render_pass(context_vk
);
2312 VK_CALL(vkCmdFillBuffer(wined3d_context_vk_get_command_buffer(context_vk
),
2313 uav_vk
->counter_bo
.vk_buffer
, uav_vk
->counter_bo
.b
.buffer_offset
, sizeof(uint32_t), 0));
2314 wined3d_context_vk_reference_bo(context_vk
, &uav_vk
->counter_bo
);
2316 create_info
.sType
= VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO
;
2317 create_info
.pNext
= NULL
;
2318 create_info
.flags
= 0;
2319 create_info
.buffer
= uav_vk
->counter_bo
.vk_buffer
;
2320 create_info
.format
= VK_FORMAT_R32_UINT
;
2321 create_info
.offset
= uav_vk
->counter_bo
.b
.buffer_offset
;
2322 create_info
.range
= sizeof(uint32_t);
2323 if ((vr
= VK_CALL(vkCreateBufferView(device_vk
->vk_device
,
2324 &create_info
, NULL
, &uav_vk
->vk_counter_view
))) < 0)
2326 ERR("Failed to create counter buffer view, vr %s.\n", wined3d_debug_vkresult(vr
));
2330 TRACE("Created counter buffer view 0x%s.\n", wine_dbgstr_longlong(uav_vk
->vk_counter_view
));
2332 uav_vk
->v
.counter_bo
= &uav_vk
->counter_bo
.b
;
2336 context_release(&context_vk
->c
);
2341 texture_vk
= wined3d_texture_vk(texture_from_resource(resource
));
2343 if (texture_vk
->t
.layer_count
> 1)
2344 default_flags
|= WINED3D_VIEW_TEXTURE_ARRAY
;
2346 if (resource
->format
->id
== format_vk
->f
.id
&& desc
->flags
== default_flags
2347 && !desc
->u
.texture
.level_idx
&& desc
->u
.texture
.level_count
== texture_vk
->t
.level_count
2348 && !desc
->u
.texture
.layer_idx
&& desc
->u
.texture
.layer_count
== texture_vk
->t
.layer_count
2349 && !(resource
->bind_flags
& WINED3D_BIND_DEPTH_STENCIL
) && resource
->type
!= WINED3D_RTYPE_TEXTURE_3D
)
2351 TRACE("Creating identity unordered access view.\n");
2355 if (texture_vk
->t
.swapchain
&& texture_vk
->t
.swapchain
->state
.desc
.backbuffer_count
> 1)
2356 FIXME("Swapchain unordered access views not supported.\n");
2358 context_vk
= wined3d_context_vk(context_acquire(&device_vk
->d
, NULL
, 0));
2359 vk_image_view
= wined3d_view_vk_create_vk_image_view(context_vk
, desc
,
2360 texture_vk
, format_vk
, format_vk
->f
.color_fixup
, false);
2361 context_release(&context_vk
->c
);
2366 TRACE("Created image view 0x%s.\n", wine_dbgstr_longlong(vk_image_view
));
2368 view_vk
->u
.vk_image_info
.imageView
= vk_image_view
;
2369 view_vk
->u
.vk_image_info
.sampler
= VK_NULL_HANDLE
;
2370 view_vk
->u
.vk_image_info
.imageLayout
= texture_vk
->layout
;
2373 HRESULT
wined3d_unordered_access_view_vk_init(struct wined3d_unordered_access_view_vk
*view_vk
,
2374 const struct wined3d_view_desc
*desc
, struct wined3d_resource
*resource
,
2375 void *parent
, const struct wined3d_parent_ops
*parent_ops
)
2379 TRACE("view_vk %p, desc %s, resource %p, parent %p, parent_ops %p.\n",
2380 view_vk
, wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
);
2382 if (FAILED(hr
= wined3d_unordered_access_view_init(&view_vk
->v
, desc
, resource
, parent
, parent_ops
)))
2385 list_init(&view_vk
->view_vk
.bo_user
.entry
);
2386 wined3d_cs_init_object(resource
->device
->cs
, wined3d_unordered_access_view_vk_cs_init
, view_vk
);
2391 HRESULT CDECL
wined3d_unordered_access_view_create(const struct wined3d_view_desc
*desc
,
2392 struct wined3d_resource
*resource
, void *parent
, const struct wined3d_parent_ops
*parent_ops
,
2393 struct wined3d_unordered_access_view
**view
)
2395 const struct wined3d_adapter_ops
*adapter_ops
;
2397 TRACE("desc %s, resource %p, parent %p, parent_ops %p, view %p.\n",
2398 wined3d_debug_view_desc(desc
, resource
), resource
, parent
, parent_ops
, view
);
2400 adapter_ops
= resource
->device
->adapter
->adapter_ops
;
2401 return adapter_ops
->adapter_create_unordered_access_view(desc
, resource
, parent
, parent_ops
, view
);