2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "radv_meta.h"
34 radv_meta_save(struct radv_meta_saved_state
*state
,
35 const struct radv_cmd_buffer
*cmd_buffer
,
36 uint32_t dynamic_mask
)
38 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
39 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
40 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
41 sizeof(state
->old_vertex_bindings
));
43 state
->dynamic_mask
= dynamic_mask
;
44 radv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
47 memcpy(state
->push_constants
, cmd_buffer
->push_constants
, MAX_PUSH_CONSTANTS_SIZE
);
51 radv_meta_restore(const struct radv_meta_saved_state
*state
,
52 struct radv_cmd_buffer
*cmd_buffer
)
54 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
55 radv_bind_descriptor_set(cmd_buffer
, state
->old_descriptor_set0
, 0);
56 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
57 sizeof(state
->old_vertex_bindings
));
59 cmd_buffer
->state
.vb_dirty
|= (1 << RADV_META_VERTEX_BINDING_COUNT
) - 1;
60 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_PIPELINE
;
62 radv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
64 cmd_buffer
->state
.dirty
|= state
->dynamic_mask
;
66 memcpy(cmd_buffer
->push_constants
, state
->push_constants
, MAX_PUSH_CONSTANTS_SIZE
);
67 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_ALL_GRAPHICS
| VK_SHADER_STAGE_COMPUTE_BIT
;
71 radv_meta_save_pass(struct radv_meta_saved_pass_state
*state
,
72 const struct radv_cmd_buffer
*cmd_buffer
)
74 state
->pass
= cmd_buffer
->state
.pass
;
75 state
->subpass
= cmd_buffer
->state
.subpass
;
76 state
->framebuffer
= cmd_buffer
->state
.framebuffer
;
77 state
->attachments
= cmd_buffer
->state
.attachments
;
78 state
->render_area
= cmd_buffer
->state
.render_area
;
82 radv_meta_restore_pass(const struct radv_meta_saved_pass_state
*state
,
83 struct radv_cmd_buffer
*cmd_buffer
)
85 cmd_buffer
->state
.pass
= state
->pass
;
86 cmd_buffer
->state
.subpass
= state
->subpass
;
87 cmd_buffer
->state
.framebuffer
= state
->framebuffer
;
88 cmd_buffer
->state
.attachments
= state
->attachments
;
89 cmd_buffer
->state
.render_area
= state
->render_area
;
91 radv_emit_framebuffer_state(cmd_buffer
);
95 radv_meta_save_compute(struct radv_meta_saved_compute_state
*state
,
96 const struct radv_cmd_buffer
*cmd_buffer
,
97 unsigned push_constant_size
)
99 state
->old_pipeline
= cmd_buffer
->state
.compute_pipeline
;
100 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
102 if (push_constant_size
)
103 memcpy(state
->push_constants
, cmd_buffer
->push_constants
, push_constant_size
);
107 radv_meta_restore_compute(const struct radv_meta_saved_compute_state
*state
,
108 struct radv_cmd_buffer
*cmd_buffer
,
109 unsigned push_constant_size
)
111 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
), VK_PIPELINE_BIND_POINT_COMPUTE
,
112 radv_pipeline_to_handle(state
->old_pipeline
));
113 radv_bind_descriptor_set(cmd_buffer
, state
->old_descriptor_set0
, 0);
115 if (push_constant_size
) {
116 memcpy(cmd_buffer
->push_constants
, state
->push_constants
, push_constant_size
);
117 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_COMPUTE_BIT
;
122 radv_meta_get_view_type(const struct radv_image
*image
)
124 switch (image
->type
) {
125 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
126 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
127 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
129 unreachable("bad VkImageViewType");
134 * When creating a destination VkImageView, this function provides the needed
135 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
138 radv_meta_get_iview_layer(const struct radv_image
*dest_image
,
139 const VkImageSubresourceLayers
*dest_subresource
,
140 const VkOffset3D
*dest_offset
)
142 switch (dest_image
->type
) {
143 case VK_IMAGE_TYPE_1D
:
144 case VK_IMAGE_TYPE_2D
:
145 return dest_subresource
->baseArrayLayer
;
146 case VK_IMAGE_TYPE_3D
:
147 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
148 * but meta does it anyway. When doing so, we translate the
149 * destination's z offset into an array offset.
151 return dest_offset
->z
;
153 assert(!"bad VkImageType");
159 meta_alloc(void* _device
, size_t size
, size_t alignment
,
160 VkSystemAllocationScope allocationScope
)
162 struct radv_device
*device
= _device
;
163 return device
->alloc
.pfnAllocation(device
->alloc
.pUserData
, size
, alignment
,
164 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
168 meta_realloc(void* _device
, void *original
, size_t size
, size_t alignment
,
169 VkSystemAllocationScope allocationScope
)
171 struct radv_device
*device
= _device
;
172 return device
->alloc
.pfnReallocation(device
->alloc
.pUserData
, original
,
174 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
178 meta_free(void* _device
, void *data
)
180 struct radv_device
*device
= _device
;
181 return device
->alloc
.pfnFree(device
->alloc
.pUserData
, data
);
185 radv_builtin_cache_path(char *path
)
187 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
188 const char *suffix
= "/radv_builtin_shaders";
189 const char *suffix2
= "/.cache/radv_builtin_shaders";
190 struct passwd pwd
, *result
;
191 char path2
[PATH_MAX
+ 1]; /* PATH_MAX is not a real max,but suffices here. */
193 if (xdg_cache_home
) {
195 if (strlen(xdg_cache_home
) + strlen(suffix
) > PATH_MAX
)
198 strcpy(path
, xdg_cache_home
);
199 strcat(path
, suffix
);
203 getpwuid_r(getuid(), &pwd
, path2
, PATH_MAX
- strlen(suffix2
), &result
);
207 strcpy(path
, pwd
.pw_dir
);
208 strcat(path
, "/.cache");
211 strcat(path
, suffix
);
216 radv_load_meta_pipeline(struct radv_device
*device
)
218 char path
[PATH_MAX
+ 1];
222 if (!radv_builtin_cache_path(path
))
225 int fd
= open(path
, O_RDONLY
);
230 data
= malloc(st
.st_size
);
233 if(read(fd
, data
, st
.st_size
) == -1)
236 radv_pipeline_cache_load(&device
->meta_state
.cache
, data
, st
.st_size
);
243 radv_store_meta_pipeline(struct radv_device
*device
)
245 char path
[PATH_MAX
+ 1], path2
[PATH_MAX
+ 7];
249 if (!device
->meta_state
.cache
.modified
)
252 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
253 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
257 if (!radv_builtin_cache_path(path
))
261 strcat(path2
, "XXXXXX");
262 int fd
= mkstemp(path2
);//open(path, O_WRONLY | O_CREAT, 0600);
269 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
270 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
273 if(write(fd
, data
, size
) == -1)
284 radv_device_init_meta(struct radv_device
*device
)
288 device
->meta_state
.alloc
= (VkAllocationCallbacks
) {
290 .pfnAllocation
= meta_alloc
,
291 .pfnReallocation
= meta_realloc
,
292 .pfnFree
= meta_free
,
295 device
->meta_state
.cache
.alloc
= device
->meta_state
.alloc
;
296 radv_pipeline_cache_init(&device
->meta_state
.cache
, device
);
297 radv_load_meta_pipeline(device
);
299 result
= radv_device_init_meta_clear_state(device
);
300 if (result
!= VK_SUCCESS
)
303 result
= radv_device_init_meta_resolve_state(device
);
304 if (result
!= VK_SUCCESS
)
307 result
= radv_device_init_meta_blit_state(device
);
308 if (result
!= VK_SUCCESS
)
311 result
= radv_device_init_meta_blit2d_state(device
);
312 if (result
!= VK_SUCCESS
)
315 result
= radv_device_init_meta_bufimage_state(device
);
316 if (result
!= VK_SUCCESS
)
319 result
= radv_device_init_meta_depth_decomp_state(device
);
320 if (result
!= VK_SUCCESS
)
321 goto fail_depth_decomp
;
323 result
= radv_device_init_meta_buffer_state(device
);
324 if (result
!= VK_SUCCESS
)
327 result
= radv_device_init_meta_fast_clear_flush_state(device
);
328 if (result
!= VK_SUCCESS
)
329 goto fail_fast_clear
;
331 result
= radv_device_init_meta_resolve_compute_state(device
);
332 if (result
!= VK_SUCCESS
)
333 goto fail_resolve_compute
;
336 fail_resolve_compute
:
337 radv_device_finish_meta_fast_clear_flush_state(device
);
339 radv_device_finish_meta_buffer_state(device
);
341 radv_device_finish_meta_depth_decomp_state(device
);
343 radv_device_finish_meta_bufimage_state(device
);
345 radv_device_finish_meta_blit2d_state(device
);
347 radv_device_finish_meta_blit_state(device
);
349 radv_device_finish_meta_resolve_state(device
);
351 radv_device_finish_meta_clear_state(device
);
353 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
358 radv_device_finish_meta(struct radv_device
*device
)
360 radv_device_finish_meta_clear_state(device
);
361 radv_device_finish_meta_resolve_state(device
);
362 radv_device_finish_meta_blit_state(device
);
363 radv_device_finish_meta_blit2d_state(device
);
364 radv_device_finish_meta_bufimage_state(device
);
365 radv_device_finish_meta_depth_decomp_state(device
);
366 radv_device_finish_meta_buffer_state(device
);
367 radv_device_finish_meta_fast_clear_flush_state(device
);
368 radv_device_finish_meta_resolve_compute_state(device
);
370 radv_store_meta_pipeline(device
);
371 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
375 * The most common meta operations all want to have the viewport
376 * reset and any scissors disabled. The rest of the dynamic state
377 * should have no effect.
380 radv_meta_save_graphics_reset_vport_scissor(struct radv_meta_saved_state
*saved_state
,
381 struct radv_cmd_buffer
*cmd_buffer
)
383 uint32_t dirty_state
= (1 << VK_DYNAMIC_STATE_VIEWPORT
) | (1 << VK_DYNAMIC_STATE_SCISSOR
);
384 radv_meta_save(saved_state
, cmd_buffer
, dirty_state
);
385 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
386 cmd_buffer
->state
.dynamic
.scissor
.count
= 0;
387 cmd_buffer
->state
.dirty
|= dirty_state
;