radv/meta: add support for save/restore meta without vertex data.
[mesa.git] / src / amd / vulkan / radv_meta.c
1 /*
2 * Copyright © 2016 Red Hat
3 * based on intel anv code:
4 * Copyright © 2015 Intel Corporation
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * IN THE SOFTWARE.
24 */
25
26 #include "radv_meta.h"
27
28 #include <fcntl.h>
29 #include <limits.h>
30 #include <pwd.h>
31 #include <sys/stat.h>
32
33 void
34 radv_meta_save_novertex(struct radv_meta_saved_state *state,
35 const struct radv_cmd_buffer *cmd_buffer,
36 uint32_t dynamic_mask)
37 {
38 state->old_pipeline = cmd_buffer->state.pipeline;
39
40 state->dynamic_mask = dynamic_mask;
41 radv_dynamic_state_copy(&state->dynamic, &cmd_buffer->state.dynamic,
42 dynamic_mask);
43
44 memcpy(state->push_constants, cmd_buffer->push_constants, MAX_PUSH_CONSTANTS_SIZE);
45 state->vertex_saved = false;
46 }
47
48 void
49 radv_meta_save(struct radv_meta_saved_state *state,
50 const struct radv_cmd_buffer *cmd_buffer,
51 uint32_t dynamic_mask)
52 {
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;
58 }
59
60 void
61 radv_meta_restore(const struct radv_meta_saved_state *state,
62 struct radv_cmd_buffer *cmd_buffer)
63 {
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;
70 }
71
72 cmd_buffer->state.dirty |= RADV_CMD_DIRTY_PIPELINE;
73
74 radv_dynamic_state_copy(&cmd_buffer->state.dynamic, &state->dynamic,
75 state->dynamic_mask);
76 cmd_buffer->state.dirty |= state->dynamic_mask;
77
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;
80 }
81
82 void
83 radv_meta_save_pass(struct radv_meta_saved_pass_state *state,
84 const struct radv_cmd_buffer *cmd_buffer)
85 {
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;
91 }
92
93 void
94 radv_meta_restore_pass(const struct radv_meta_saved_pass_state *state,
95 struct radv_cmd_buffer *cmd_buffer)
96 {
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;
102 if (state->subpass)
103 radv_emit_framebuffer_state(cmd_buffer);
104 }
105
106 void
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)
110 {
111 state->old_pipeline = cmd_buffer->state.compute_pipeline;
112 state->old_descriptor_set0 = cmd_buffer->state.descriptors[0];
113
114 if (push_constant_size)
115 memcpy(state->push_constants, cmd_buffer->push_constants, push_constant_size);
116 }
117
118 void
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)
122 {
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);
126
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;
130 }
131 }
132
133 VkImageViewType
134 radv_meta_get_view_type(const struct radv_image *image)
135 {
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;
140 default:
141 unreachable("bad VkImageViewType");
142 }
143 }
144
145 /**
146 * When creating a destination VkImageView, this function provides the needed
147 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
148 */
149 uint32_t
150 radv_meta_get_iview_layer(const struct radv_image *dest_image,
151 const VkImageSubresourceLayers *dest_subresource,
152 const VkOffset3D *dest_offset)
153 {
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.
162 */
163 return dest_offset->z;
164 default:
165 assert(!"bad VkImageType");
166 return 0;
167 }
168 }
169
170 static void *
171 meta_alloc(void* _device, size_t size, size_t alignment,
172 VkSystemAllocationScope allocationScope)
173 {
174 struct radv_device *device = _device;
175 return device->alloc.pfnAllocation(device->alloc.pUserData, size, alignment,
176 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
177 }
178
179 static void *
180 meta_realloc(void* _device, void *original, size_t size, size_t alignment,
181 VkSystemAllocationScope allocationScope)
182 {
183 struct radv_device *device = _device;
184 return device->alloc.pfnReallocation(device->alloc.pUserData, original,
185 size, alignment,
186 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
187 }
188
189 static void
190 meta_free(void* _device, void *data)
191 {
192 struct radv_device *device = _device;
193 return device->alloc.pfnFree(device->alloc.pUserData, data);
194 }
195
196 static bool
197 radv_builtin_cache_path(char *path)
198 {
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. */
204
205 if (xdg_cache_home) {
206
207 if (strlen(xdg_cache_home) + strlen(suffix) > PATH_MAX)
208 return false;
209
210 strcpy(path, xdg_cache_home);
211 strcat(path, suffix);
212 return true;
213 }
214
215 getpwuid_r(getuid(), &pwd, path2, PATH_MAX - strlen(suffix2), &result);
216 if (!result)
217 return false;
218
219 strcpy(path, pwd.pw_dir);
220 strcat(path, "/.cache");
221 mkdir(path, 0755);
222
223 strcat(path, suffix);
224 return true;
225 }
226
227 static void
228 radv_load_meta_pipeline(struct radv_device *device)
229 {
230 char path[PATH_MAX + 1];
231 struct stat st;
232 void *data = NULL;
233
234 if (!radv_builtin_cache_path(path))
235 return;
236
237 int fd = open(path, O_RDONLY);
238 if (fd < 0)
239 return;
240 if (fstat(fd, &st))
241 goto fail;
242 data = malloc(st.st_size);
243 if (!data)
244 goto fail;
245 if(read(fd, data, st.st_size) == -1)
246 goto fail;
247
248 radv_pipeline_cache_load(&device->meta_state.cache, data, st.st_size);
249 fail:
250 free(data);
251 close(fd);
252 }
253
254 static void
255 radv_store_meta_pipeline(struct radv_device *device)
256 {
257 char path[PATH_MAX + 1], path2[PATH_MAX + 7];
258 size_t size;
259 void *data = NULL;
260
261 if (!device->meta_state.cache.modified)
262 return;
263
264 if (radv_GetPipelineCacheData(radv_device_to_handle(device),
265 radv_pipeline_cache_to_handle(&device->meta_state.cache),
266 &size, NULL))
267 return;
268
269 if (!radv_builtin_cache_path(path))
270 return;
271
272 strcpy(path2, path);
273 strcat(path2, "XXXXXX");
274 int fd = mkstemp(path2);//open(path, O_WRONLY | O_CREAT, 0600);
275 if (fd < 0)
276 return;
277 data = malloc(size);
278 if (!data)
279 goto fail;
280
281 if (radv_GetPipelineCacheData(radv_device_to_handle(device),
282 radv_pipeline_cache_to_handle(&device->meta_state.cache),
283 &size, data))
284 goto fail;
285 if(write(fd, data, size) == -1)
286 goto fail;
287
288 rename(path2, path);
289 fail:
290 free(data);
291 close(fd);
292 unlink(path2);
293 }
294
295 VkResult
296 radv_device_init_meta(struct radv_device *device)
297 {
298 VkResult result;
299
300 device->meta_state.alloc = (VkAllocationCallbacks) {
301 .pUserData = device,
302 .pfnAllocation = meta_alloc,
303 .pfnReallocation = meta_realloc,
304 .pfnFree = meta_free,
305 };
306
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);
310
311 result = radv_device_init_meta_clear_state(device);
312 if (result != VK_SUCCESS)
313 goto fail_clear;
314
315 result = radv_device_init_meta_resolve_state(device);
316 if (result != VK_SUCCESS)
317 goto fail_resolve;
318
319 result = radv_device_init_meta_blit_state(device);
320 if (result != VK_SUCCESS)
321 goto fail_blit;
322
323 result = radv_device_init_meta_blit2d_state(device);
324 if (result != VK_SUCCESS)
325 goto fail_blit2d;
326
327 result = radv_device_init_meta_bufimage_state(device);
328 if (result != VK_SUCCESS)
329 goto fail_bufimage;
330
331 result = radv_device_init_meta_depth_decomp_state(device);
332 if (result != VK_SUCCESS)
333 goto fail_depth_decomp;
334
335 result = radv_device_init_meta_buffer_state(device);
336 if (result != VK_SUCCESS)
337 goto fail_buffer;
338
339 result = radv_device_init_meta_query_state(device);
340 if (result != VK_SUCCESS)
341 goto fail_query;
342
343 result = radv_device_init_meta_fast_clear_flush_state(device);
344 if (result != VK_SUCCESS)
345 goto fail_fast_clear;
346
347 result = radv_device_init_meta_resolve_compute_state(device);
348 if (result != VK_SUCCESS)
349 goto fail_resolve_compute;
350 return VK_SUCCESS;
351
352 fail_resolve_compute:
353 radv_device_finish_meta_fast_clear_flush_state(device);
354 fail_fast_clear:
355 radv_device_finish_meta_buffer_state(device);
356 fail_query:
357 radv_device_finish_meta_query_state(device);
358 fail_buffer:
359 radv_device_finish_meta_depth_decomp_state(device);
360 fail_depth_decomp:
361 radv_device_finish_meta_bufimage_state(device);
362 fail_bufimage:
363 radv_device_finish_meta_blit2d_state(device);
364 fail_blit2d:
365 radv_device_finish_meta_blit_state(device);
366 fail_blit:
367 radv_device_finish_meta_resolve_state(device);
368 fail_resolve:
369 radv_device_finish_meta_clear_state(device);
370 fail_clear:
371 radv_pipeline_cache_finish(&device->meta_state.cache);
372 return result;
373 }
374
375 void
376 radv_device_finish_meta(struct radv_device *device)
377 {
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);
388
389 radv_store_meta_pipeline(device);
390 radv_pipeline_cache_finish(&device->meta_state.cache);
391 }
392
393 /*
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.
397 */
398 void
399 radv_meta_save_graphics_reset_vport_scissor(struct radv_meta_saved_state *saved_state,
400 struct radv_cmd_buffer *cmd_buffer)
401 {
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;
407 }
408
409 void
410 radv_meta_save_graphics_reset_vport_scissor_novertex(struct radv_meta_saved_state *saved_state,
411 struct radv_cmd_buffer *cmd_buffer)
412 {
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;
418 }