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_novertex(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
;
40 state
->dynamic_mask
= dynamic_mask
;
41 radv_dynamic_state_copy(&state
->dynamic
, &cmd_buffer
->state
.dynamic
,
44 memcpy(state
->push_constants
, cmd_buffer
->push_constants
, MAX_PUSH_CONSTANTS_SIZE
);
45 state
->vertex_saved
= false;
49 radv_meta_save(struct radv_meta_saved_state
*state
,
50 const struct radv_cmd_buffer
*cmd_buffer
,
51 uint32_t dynamic_mask
)
53 radv_meta_save_novertex(state
, cmd_buffer
, dynamic_mask
);
54 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
55 memcpy(state
->old_vertex_bindings
, cmd_buffer
->state
.vertex_bindings
,
56 sizeof(state
->old_vertex_bindings
));
57 state
->vertex_saved
= true;
61 radv_meta_restore(const struct radv_meta_saved_state
*state
,
62 struct radv_cmd_buffer
*cmd_buffer
)
64 cmd_buffer
->state
.pipeline
= state
->old_pipeline
;
65 if (state
->vertex_saved
) {
66 radv_bind_descriptor_set(cmd_buffer
, state
->old_descriptor_set0
, 0);
67 memcpy(cmd_buffer
->state
.vertex_bindings
, state
->old_vertex_bindings
,
68 sizeof(state
->old_vertex_bindings
));
69 cmd_buffer
->state
.vb_dirty
|= (1 << RADV_META_VERTEX_BINDING_COUNT
) - 1;
72 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_PIPELINE
;
74 radv_dynamic_state_copy(&cmd_buffer
->state
.dynamic
, &state
->dynamic
,
76 cmd_buffer
->state
.dirty
|= state
->dynamic_mask
;
78 memcpy(cmd_buffer
->push_constants
, state
->push_constants
, MAX_PUSH_CONSTANTS_SIZE
);
79 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_ALL_GRAPHICS
| VK_SHADER_STAGE_COMPUTE_BIT
;
83 radv_meta_save_pass(struct radv_meta_saved_pass_state
*state
,
84 const struct radv_cmd_buffer
*cmd_buffer
)
86 state
->pass
= cmd_buffer
->state
.pass
;
87 state
->subpass
= cmd_buffer
->state
.subpass
;
88 state
->framebuffer
= cmd_buffer
->state
.framebuffer
;
89 state
->attachments
= cmd_buffer
->state
.attachments
;
90 state
->render_area
= cmd_buffer
->state
.render_area
;
94 radv_meta_restore_pass(const struct radv_meta_saved_pass_state
*state
,
95 struct radv_cmd_buffer
*cmd_buffer
)
97 cmd_buffer
->state
.pass
= state
->pass
;
98 cmd_buffer
->state
.subpass
= state
->subpass
;
99 cmd_buffer
->state
.framebuffer
= state
->framebuffer
;
100 cmd_buffer
->state
.attachments
= state
->attachments
;
101 cmd_buffer
->state
.render_area
= state
->render_area
;
103 radv_emit_framebuffer_state(cmd_buffer
);
107 radv_meta_save_compute(struct radv_meta_saved_compute_state
*state
,
108 const struct radv_cmd_buffer
*cmd_buffer
,
109 unsigned push_constant_size
)
111 state
->old_pipeline
= cmd_buffer
->state
.compute_pipeline
;
112 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
114 if (push_constant_size
)
115 memcpy(state
->push_constants
, cmd_buffer
->push_constants
, push_constant_size
);
119 radv_meta_restore_compute(const struct radv_meta_saved_compute_state
*state
,
120 struct radv_cmd_buffer
*cmd_buffer
,
121 unsigned push_constant_size
)
123 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
), VK_PIPELINE_BIND_POINT_COMPUTE
,
124 radv_pipeline_to_handle(state
->old_pipeline
));
125 radv_bind_descriptor_set(cmd_buffer
, state
->old_descriptor_set0
, 0);
127 if (push_constant_size
) {
128 memcpy(cmd_buffer
->push_constants
, state
->push_constants
, push_constant_size
);
129 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_COMPUTE_BIT
;
134 radv_meta_get_view_type(const struct radv_image
*image
)
136 switch (image
->type
) {
137 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
138 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
139 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
141 unreachable("bad VkImageViewType");
146 * When creating a destination VkImageView, this function provides the needed
147 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
150 radv_meta_get_iview_layer(const struct radv_image
*dest_image
,
151 const VkImageSubresourceLayers
*dest_subresource
,
152 const VkOffset3D
*dest_offset
)
154 switch (dest_image
->type
) {
155 case VK_IMAGE_TYPE_1D
:
156 case VK_IMAGE_TYPE_2D
:
157 return dest_subresource
->baseArrayLayer
;
158 case VK_IMAGE_TYPE_3D
:
159 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
160 * but meta does it anyway. When doing so, we translate the
161 * destination's z offset into an array offset.
163 return dest_offset
->z
;
165 assert(!"bad VkImageType");
171 meta_alloc(void* _device
, size_t size
, size_t alignment
,
172 VkSystemAllocationScope allocationScope
)
174 struct radv_device
*device
= _device
;
175 return device
->alloc
.pfnAllocation(device
->alloc
.pUserData
, size
, alignment
,
176 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
180 meta_realloc(void* _device
, void *original
, size_t size
, size_t alignment
,
181 VkSystemAllocationScope allocationScope
)
183 struct radv_device
*device
= _device
;
184 return device
->alloc
.pfnReallocation(device
->alloc
.pUserData
, original
,
186 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
190 meta_free(void* _device
, void *data
)
192 struct radv_device
*device
= _device
;
193 return device
->alloc
.pfnFree(device
->alloc
.pUserData
, data
);
197 radv_builtin_cache_path(char *path
)
199 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
200 const char *suffix
= "/radv_builtin_shaders";
201 const char *suffix2
= "/.cache/radv_builtin_shaders";
202 struct passwd pwd
, *result
;
203 char path2
[PATH_MAX
+ 1]; /* PATH_MAX is not a real max,but suffices here. */
205 if (xdg_cache_home
) {
207 if (strlen(xdg_cache_home
) + strlen(suffix
) > PATH_MAX
)
210 strcpy(path
, xdg_cache_home
);
211 strcat(path
, suffix
);
215 getpwuid_r(getuid(), &pwd
, path2
, PATH_MAX
- strlen(suffix2
), &result
);
219 strcpy(path
, pwd
.pw_dir
);
220 strcat(path
, "/.cache");
223 strcat(path
, suffix
);
228 radv_load_meta_pipeline(struct radv_device
*device
)
230 char path
[PATH_MAX
+ 1];
234 if (!radv_builtin_cache_path(path
))
237 int fd
= open(path
, O_RDONLY
);
242 data
= malloc(st
.st_size
);
245 if(read(fd
, data
, st
.st_size
) == -1)
248 radv_pipeline_cache_load(&device
->meta_state
.cache
, data
, st
.st_size
);
255 radv_store_meta_pipeline(struct radv_device
*device
)
257 char path
[PATH_MAX
+ 1], path2
[PATH_MAX
+ 7];
261 if (!device
->meta_state
.cache
.modified
)
264 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
265 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
269 if (!radv_builtin_cache_path(path
))
273 strcat(path2
, "XXXXXX");
274 int fd
= mkstemp(path2
);//open(path, O_WRONLY | O_CREAT, 0600);
281 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
282 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
285 if(write(fd
, data
, size
) == -1)
296 radv_device_init_meta(struct radv_device
*device
)
300 device
->meta_state
.alloc
= (VkAllocationCallbacks
) {
302 .pfnAllocation
= meta_alloc
,
303 .pfnReallocation
= meta_realloc
,
304 .pfnFree
= meta_free
,
307 device
->meta_state
.cache
.alloc
= device
->meta_state
.alloc
;
308 radv_pipeline_cache_init(&device
->meta_state
.cache
, device
);
309 radv_load_meta_pipeline(device
);
311 result
= radv_device_init_meta_clear_state(device
);
312 if (result
!= VK_SUCCESS
)
315 result
= radv_device_init_meta_resolve_state(device
);
316 if (result
!= VK_SUCCESS
)
319 result
= radv_device_init_meta_blit_state(device
);
320 if (result
!= VK_SUCCESS
)
323 result
= radv_device_init_meta_blit2d_state(device
);
324 if (result
!= VK_SUCCESS
)
327 result
= radv_device_init_meta_bufimage_state(device
);
328 if (result
!= VK_SUCCESS
)
331 result
= radv_device_init_meta_depth_decomp_state(device
);
332 if (result
!= VK_SUCCESS
)
333 goto fail_depth_decomp
;
335 result
= radv_device_init_meta_buffer_state(device
);
336 if (result
!= VK_SUCCESS
)
339 result
= radv_device_init_meta_query_state(device
);
340 if (result
!= VK_SUCCESS
)
343 result
= radv_device_init_meta_fast_clear_flush_state(device
);
344 if (result
!= VK_SUCCESS
)
345 goto fail_fast_clear
;
347 result
= radv_device_init_meta_resolve_compute_state(device
);
348 if (result
!= VK_SUCCESS
)
349 goto fail_resolve_compute
;
352 fail_resolve_compute
:
353 radv_device_finish_meta_fast_clear_flush_state(device
);
355 radv_device_finish_meta_buffer_state(device
);
357 radv_device_finish_meta_query_state(device
);
359 radv_device_finish_meta_depth_decomp_state(device
);
361 radv_device_finish_meta_bufimage_state(device
);
363 radv_device_finish_meta_blit2d_state(device
);
365 radv_device_finish_meta_blit_state(device
);
367 radv_device_finish_meta_resolve_state(device
);
369 radv_device_finish_meta_clear_state(device
);
371 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
376 radv_device_finish_meta(struct radv_device
*device
)
378 radv_device_finish_meta_clear_state(device
);
379 radv_device_finish_meta_resolve_state(device
);
380 radv_device_finish_meta_blit_state(device
);
381 radv_device_finish_meta_blit2d_state(device
);
382 radv_device_finish_meta_bufimage_state(device
);
383 radv_device_finish_meta_depth_decomp_state(device
);
384 radv_device_finish_meta_query_state(device
);
385 radv_device_finish_meta_buffer_state(device
);
386 radv_device_finish_meta_fast_clear_flush_state(device
);
387 radv_device_finish_meta_resolve_compute_state(device
);
389 radv_store_meta_pipeline(device
);
390 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
394 * The most common meta operations all want to have the viewport
395 * reset and any scissors disabled. The rest of the dynamic state
396 * should have no effect.
399 radv_meta_save_graphics_reset_vport_scissor(struct radv_meta_saved_state
*saved_state
,
400 struct radv_cmd_buffer
*cmd_buffer
)
402 uint32_t dirty_state
= (1 << VK_DYNAMIC_STATE_VIEWPORT
) | (1 << VK_DYNAMIC_STATE_SCISSOR
);
403 radv_meta_save(saved_state
, cmd_buffer
, dirty_state
);
404 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
405 cmd_buffer
->state
.dynamic
.scissor
.count
= 0;
406 cmd_buffer
->state
.dirty
|= dirty_state
;
410 radv_meta_save_graphics_reset_vport_scissor_novertex(struct radv_meta_saved_state
*saved_state
,
411 struct radv_cmd_buffer
*cmd_buffer
)
413 uint32_t dirty_state
= (1 << VK_DYNAMIC_STATE_VIEWPORT
) | (1 << VK_DYNAMIC_STATE_SCISSOR
);
414 radv_meta_save_novertex(saved_state
, cmd_buffer
, dirty_state
);
415 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
416 cmd_buffer
->state
.dynamic
.scissor
.count
= 0;
417 cmd_buffer
->state
.dirty
|= dirty_state
;