radv/meta: do not restore a NULL descriptor
[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(struct radv_meta_saved_state *state,
35 struct radv_cmd_buffer *cmd_buffer, uint32_t flags)
36 {
37 VkPipelineBindPoint bind_point =
38 flags & RADV_META_SAVE_GRAPHICS_PIPELINE ?
39 VK_PIPELINE_BIND_POINT_GRAPHICS :
40 VK_PIPELINE_BIND_POINT_COMPUTE;
41 struct radv_descriptor_state *descriptors_state =
42 radv_get_descriptors_state(cmd_buffer, bind_point);
43
44 assert(flags & (RADV_META_SAVE_GRAPHICS_PIPELINE |
45 RADV_META_SAVE_COMPUTE_PIPELINE));
46
47 state->flags = flags;
48
49 if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
50 assert(!(state->flags & RADV_META_SAVE_COMPUTE_PIPELINE));
51
52 state->old_pipeline = cmd_buffer->state.pipeline;
53
54 /* Save all viewports. */
55 state->viewport.count = cmd_buffer->state.dynamic.viewport.count;
56 typed_memcpy(state->viewport.viewports,
57 cmd_buffer->state.dynamic.viewport.viewports,
58 MAX_VIEWPORTS);
59
60 /* Save all scissors. */
61 state->scissor.count = cmd_buffer->state.dynamic.scissor.count;
62 typed_memcpy(state->scissor.scissors,
63 cmd_buffer->state.dynamic.scissor.scissors,
64 MAX_SCISSORS);
65
66 /* The most common meta operations all want to have the
67 * viewport reset and any scissors disabled. The rest of the
68 * dynamic state should have no effect.
69 */
70 cmd_buffer->state.dynamic.viewport.count = 0;
71 cmd_buffer->state.dynamic.scissor.count = 0;
72 cmd_buffer->state.dirty |= 1 << VK_DYNAMIC_STATE_VIEWPORT |
73 1 << VK_DYNAMIC_STATE_SCISSOR;
74 }
75
76 if (state->flags & RADV_META_SAVE_COMPUTE_PIPELINE) {
77 assert(!(state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE));
78
79 state->old_pipeline = cmd_buffer->state.compute_pipeline;
80 }
81
82 if (state->flags & RADV_META_SAVE_DESCRIPTORS) {
83 state->old_descriptor_set0 = descriptors_state->sets[0];
84 if (!state->old_descriptor_set0)
85 state->flags &= ~RADV_META_SAVE_DESCRIPTORS;
86 }
87
88 if (state->flags & RADV_META_SAVE_CONSTANTS) {
89 memcpy(state->push_constants, cmd_buffer->push_constants,
90 MAX_PUSH_CONSTANTS_SIZE);
91 }
92
93 if (state->flags & RADV_META_SAVE_PASS) {
94 state->pass = cmd_buffer->state.pass;
95 state->subpass = cmd_buffer->state.subpass;
96 state->framebuffer = cmd_buffer->state.framebuffer;
97 state->attachments = cmd_buffer->state.attachments;
98 state->render_area = cmd_buffer->state.render_area;
99 }
100 }
101
102 void
103 radv_meta_restore(const struct radv_meta_saved_state *state,
104 struct radv_cmd_buffer *cmd_buffer)
105 {
106 VkPipelineBindPoint bind_point =
107 state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE ?
108 VK_PIPELINE_BIND_POINT_GRAPHICS :
109 VK_PIPELINE_BIND_POINT_COMPUTE;
110
111 if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
112 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer),
113 VK_PIPELINE_BIND_POINT_GRAPHICS,
114 radv_pipeline_to_handle(state->old_pipeline));
115
116 cmd_buffer->state.dirty |= RADV_CMD_DIRTY_PIPELINE;
117
118 /* Restore all viewports. */
119 cmd_buffer->state.dynamic.viewport.count = state->viewport.count;
120 typed_memcpy(cmd_buffer->state.dynamic.viewport.viewports,
121 state->viewport.viewports,
122 MAX_VIEWPORTS);
123
124 /* Restore all scissors. */
125 cmd_buffer->state.dynamic.scissor.count = state->scissor.count;
126 typed_memcpy(cmd_buffer->state.dynamic.scissor.scissors,
127 state->scissor.scissors,
128 MAX_SCISSORS);
129
130 cmd_buffer->state.dirty |= 1 << VK_DYNAMIC_STATE_VIEWPORT |
131 1 << VK_DYNAMIC_STATE_SCISSOR;
132 }
133
134 if (state->flags & RADV_META_SAVE_COMPUTE_PIPELINE) {
135 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer),
136 VK_PIPELINE_BIND_POINT_COMPUTE,
137 radv_pipeline_to_handle(state->old_pipeline));
138 }
139
140 if (state->flags & RADV_META_SAVE_DESCRIPTORS) {
141 radv_set_descriptor_set(cmd_buffer, bind_point,
142 state->old_descriptor_set0, 0);
143 }
144
145 if (state->flags & RADV_META_SAVE_CONSTANTS) {
146 memcpy(cmd_buffer->push_constants, state->push_constants,
147 MAX_PUSH_CONSTANTS_SIZE);
148 cmd_buffer->push_constant_stages |= VK_SHADER_STAGE_COMPUTE_BIT;
149
150 if (state->flags & RADV_META_SAVE_GRAPHICS_PIPELINE) {
151 cmd_buffer->push_constant_stages |= VK_SHADER_STAGE_ALL_GRAPHICS;
152 }
153 }
154
155 if (state->flags & RADV_META_SAVE_PASS) {
156 cmd_buffer->state.pass = state->pass;
157 cmd_buffer->state.subpass = state->subpass;
158 cmd_buffer->state.framebuffer = state->framebuffer;
159 cmd_buffer->state.attachments = state->attachments;
160 cmd_buffer->state.render_area = state->render_area;
161 if (state->subpass)
162 cmd_buffer->state.dirty |= RADV_CMD_DIRTY_FRAMEBUFFER;
163 }
164 }
165
166 VkImageViewType
167 radv_meta_get_view_type(const struct radv_image *image)
168 {
169 switch (image->type) {
170 case VK_IMAGE_TYPE_1D: return VK_IMAGE_VIEW_TYPE_1D;
171 case VK_IMAGE_TYPE_2D: return VK_IMAGE_VIEW_TYPE_2D;
172 case VK_IMAGE_TYPE_3D: return VK_IMAGE_VIEW_TYPE_3D;
173 default:
174 unreachable("bad VkImageViewType");
175 }
176 }
177
178 /**
179 * When creating a destination VkImageView, this function provides the needed
180 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
181 */
182 uint32_t
183 radv_meta_get_iview_layer(const struct radv_image *dest_image,
184 const VkImageSubresourceLayers *dest_subresource,
185 const VkOffset3D *dest_offset)
186 {
187 switch (dest_image->type) {
188 case VK_IMAGE_TYPE_1D:
189 case VK_IMAGE_TYPE_2D:
190 return dest_subresource->baseArrayLayer;
191 case VK_IMAGE_TYPE_3D:
192 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
193 * but meta does it anyway. When doing so, we translate the
194 * destination's z offset into an array offset.
195 */
196 return dest_offset->z;
197 default:
198 assert(!"bad VkImageType");
199 return 0;
200 }
201 }
202
203 static void *
204 meta_alloc(void* _device, size_t size, size_t alignment,
205 VkSystemAllocationScope allocationScope)
206 {
207 struct radv_device *device = _device;
208 return device->alloc.pfnAllocation(device->alloc.pUserData, size, alignment,
209 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
210 }
211
212 static void *
213 meta_realloc(void* _device, void *original, size_t size, size_t alignment,
214 VkSystemAllocationScope allocationScope)
215 {
216 struct radv_device *device = _device;
217 return device->alloc.pfnReallocation(device->alloc.pUserData, original,
218 size, alignment,
219 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
220 }
221
222 static void
223 meta_free(void* _device, void *data)
224 {
225 struct radv_device *device = _device;
226 return device->alloc.pfnFree(device->alloc.pUserData, data);
227 }
228
229 static bool
230 radv_builtin_cache_path(char *path)
231 {
232 char *xdg_cache_home = getenv("XDG_CACHE_HOME");
233 const char *suffix = "/radv_builtin_shaders";
234 const char *suffix2 = "/.cache/radv_builtin_shaders";
235 struct passwd pwd, *result;
236 char path2[PATH_MAX + 1]; /* PATH_MAX is not a real max,but suffices here. */
237
238 if (xdg_cache_home) {
239
240 if (strlen(xdg_cache_home) + strlen(suffix) > PATH_MAX)
241 return false;
242
243 strcpy(path, xdg_cache_home);
244 strcat(path, suffix);
245 return true;
246 }
247
248 getpwuid_r(getuid(), &pwd, path2, PATH_MAX - strlen(suffix2), &result);
249 if (!result)
250 return false;
251
252 strcpy(path, pwd.pw_dir);
253 strcat(path, "/.cache");
254 mkdir(path, 0755);
255
256 strcat(path, suffix);
257 return true;
258 }
259
260 static void
261 radv_load_meta_pipeline(struct radv_device *device)
262 {
263 char path[PATH_MAX + 1];
264 struct stat st;
265 void *data = NULL;
266
267 if (!radv_builtin_cache_path(path))
268 return;
269
270 int fd = open(path, O_RDONLY);
271 if (fd < 0)
272 return;
273 if (fstat(fd, &st))
274 goto fail;
275 data = malloc(st.st_size);
276 if (!data)
277 goto fail;
278 if(read(fd, data, st.st_size) == -1)
279 goto fail;
280
281 radv_pipeline_cache_load(&device->meta_state.cache, data, st.st_size);
282 fail:
283 free(data);
284 close(fd);
285 }
286
287 static void
288 radv_store_meta_pipeline(struct radv_device *device)
289 {
290 char path[PATH_MAX + 1], path2[PATH_MAX + 7];
291 size_t size;
292 void *data = NULL;
293
294 if (!device->meta_state.cache.modified)
295 return;
296
297 if (radv_GetPipelineCacheData(radv_device_to_handle(device),
298 radv_pipeline_cache_to_handle(&device->meta_state.cache),
299 &size, NULL))
300 return;
301
302 if (!radv_builtin_cache_path(path))
303 return;
304
305 strcpy(path2, path);
306 strcat(path2, "XXXXXX");
307 int fd = mkstemp(path2);//open(path, O_WRONLY | O_CREAT, 0600);
308 if (fd < 0)
309 return;
310 data = malloc(size);
311 if (!data)
312 goto fail;
313
314 if (radv_GetPipelineCacheData(radv_device_to_handle(device),
315 radv_pipeline_cache_to_handle(&device->meta_state.cache),
316 &size, data))
317 goto fail;
318 if(write(fd, data, size) == -1)
319 goto fail;
320
321 rename(path2, path);
322 fail:
323 free(data);
324 close(fd);
325 unlink(path2);
326 }
327
328 VkResult
329 radv_device_init_meta(struct radv_device *device)
330 {
331 VkResult result;
332
333 device->meta_state.alloc = (VkAllocationCallbacks) {
334 .pUserData = device,
335 .pfnAllocation = meta_alloc,
336 .pfnReallocation = meta_realloc,
337 .pfnFree = meta_free,
338 };
339
340 device->meta_state.cache.alloc = device->meta_state.alloc;
341 radv_pipeline_cache_init(&device->meta_state.cache, device);
342 radv_load_meta_pipeline(device);
343
344 result = radv_device_init_meta_clear_state(device);
345 if (result != VK_SUCCESS)
346 goto fail_clear;
347
348 result = radv_device_init_meta_resolve_state(device);
349 if (result != VK_SUCCESS)
350 goto fail_resolve;
351
352 result = radv_device_init_meta_blit_state(device);
353 if (result != VK_SUCCESS)
354 goto fail_blit;
355
356 result = radv_device_init_meta_blit2d_state(device);
357 if (result != VK_SUCCESS)
358 goto fail_blit2d;
359
360 result = radv_device_init_meta_bufimage_state(device);
361 if (result != VK_SUCCESS)
362 goto fail_bufimage;
363
364 result = radv_device_init_meta_depth_decomp_state(device);
365 if (result != VK_SUCCESS)
366 goto fail_depth_decomp;
367
368 result = radv_device_init_meta_buffer_state(device);
369 if (result != VK_SUCCESS)
370 goto fail_buffer;
371
372 result = radv_device_init_meta_query_state(device);
373 if (result != VK_SUCCESS)
374 goto fail_query;
375
376 result = radv_device_init_meta_fast_clear_flush_state(device);
377 if (result != VK_SUCCESS)
378 goto fail_fast_clear;
379
380 result = radv_device_init_meta_resolve_compute_state(device);
381 if (result != VK_SUCCESS)
382 goto fail_resolve_compute;
383
384 result = radv_device_init_meta_resolve_fragment_state(device);
385 if (result != VK_SUCCESS)
386 goto fail_resolve_fragment;
387 return VK_SUCCESS;
388
389 fail_resolve_fragment:
390 radv_device_finish_meta_resolve_compute_state(device);
391 fail_resolve_compute:
392 radv_device_finish_meta_fast_clear_flush_state(device);
393 fail_fast_clear:
394 radv_device_finish_meta_query_state(device);
395 fail_query:
396 radv_device_finish_meta_buffer_state(device);
397 fail_buffer:
398 radv_device_finish_meta_depth_decomp_state(device);
399 fail_depth_decomp:
400 radv_device_finish_meta_bufimage_state(device);
401 fail_bufimage:
402 radv_device_finish_meta_blit2d_state(device);
403 fail_blit2d:
404 radv_device_finish_meta_blit_state(device);
405 fail_blit:
406 radv_device_finish_meta_resolve_state(device);
407 fail_resolve:
408 radv_device_finish_meta_clear_state(device);
409 fail_clear:
410 radv_pipeline_cache_finish(&device->meta_state.cache);
411 return result;
412 }
413
414 void
415 radv_device_finish_meta(struct radv_device *device)
416 {
417 radv_device_finish_meta_clear_state(device);
418 radv_device_finish_meta_resolve_state(device);
419 radv_device_finish_meta_blit_state(device);
420 radv_device_finish_meta_blit2d_state(device);
421 radv_device_finish_meta_bufimage_state(device);
422 radv_device_finish_meta_depth_decomp_state(device);
423 radv_device_finish_meta_query_state(device);
424 radv_device_finish_meta_buffer_state(device);
425 radv_device_finish_meta_fast_clear_flush_state(device);
426 radv_device_finish_meta_resolve_compute_state(device);
427 radv_device_finish_meta_resolve_fragment_state(device);
428
429 radv_store_meta_pipeline(device);
430 radv_pipeline_cache_finish(&device->meta_state.cache);
431 }
432
433 nir_ssa_def *radv_meta_gen_rect_vertices_comp2(nir_builder *vs_b, nir_ssa_def *comp2)
434 {
435
436 nir_intrinsic_instr *vertex_id = nir_intrinsic_instr_create(vs_b->shader, nir_intrinsic_load_vertex_id_zero_base);
437 nir_ssa_dest_init(&vertex_id->instr, &vertex_id->dest, 1, 32, "vertexid");
438 nir_builder_instr_insert(vs_b, &vertex_id->instr);
439
440 /* vertex 0 - -1.0, -1.0 */
441 /* vertex 1 - -1.0, 1.0 */
442 /* vertex 2 - 1.0, -1.0 */
443 /* so channel 0 is vertex_id != 2 ? -1.0 : 1.0
444 channel 1 is vertex id != 1 ? -1.0 : 1.0 */
445
446 nir_ssa_def *c0cmp = nir_ine(vs_b, &vertex_id->dest.ssa,
447 nir_imm_int(vs_b, 2));
448 nir_ssa_def *c1cmp = nir_ine(vs_b, &vertex_id->dest.ssa,
449 nir_imm_int(vs_b, 1));
450
451 nir_ssa_def *comp[4];
452 comp[0] = nir_bcsel(vs_b, c0cmp,
453 nir_imm_float(vs_b, -1.0),
454 nir_imm_float(vs_b, 1.0));
455
456 comp[1] = nir_bcsel(vs_b, c1cmp,
457 nir_imm_float(vs_b, -1.0),
458 nir_imm_float(vs_b, 1.0));
459 comp[2] = comp2;
460 comp[3] = nir_imm_float(vs_b, 1.0);
461 nir_ssa_def *outvec = nir_vec(vs_b, comp, 4);
462
463 return outvec;
464 }
465
466 nir_ssa_def *radv_meta_gen_rect_vertices(nir_builder *vs_b)
467 {
468 return radv_meta_gen_rect_vertices_comp2(vs_b, nir_imm_float(vs_b, 0.0));
469 }
470
471 /* vertex shader that generates vertices */
472 nir_shader *
473 radv_meta_build_nir_vs_generate_vertices(void)
474 {
475 const struct glsl_type *vec4 = glsl_vec4_type();
476
477 nir_builder b;
478 nir_variable *v_position;
479
480 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_VERTEX, NULL);
481 b.shader->info.name = ralloc_strdup(b.shader, "meta_vs_gen_verts");
482
483 nir_ssa_def *outvec = radv_meta_gen_rect_vertices(&b);
484
485 v_position = nir_variable_create(b.shader, nir_var_shader_out, vec4,
486 "gl_Position");
487 v_position->data.location = VARYING_SLOT_POS;
488
489 nir_store_var(&b, v_position, outvec, 0xf);
490
491 return b.shader;
492 }
493
494 nir_shader *
495 radv_meta_build_nir_fs_noop(void)
496 {
497 nir_builder b;
498
499 nir_builder_init_simple_shader(&b, NULL, MESA_SHADER_FRAGMENT, NULL);
500 b.shader->info.name = ralloc_asprintf(b.shader,
501 "meta_noop_fs");
502
503 return b.shader;
504 }
505
506 void radv_meta_build_resolve_shader_core(nir_builder *b,
507 bool is_integer,
508 int samples,
509 nir_variable *input_img,
510 nir_variable *color,
511 nir_ssa_def *img_coord)
512 {
513 /* do a txf_ms on each sample */
514 nir_ssa_def *tmp;
515 nir_if *outer_if = NULL;
516
517 nir_ssa_def *input_img_deref = &nir_build_deref_var(b, input_img)->dest.ssa;
518
519 nir_tex_instr *tex = nir_tex_instr_create(b->shader, 3);
520 tex->sampler_dim = GLSL_SAMPLER_DIM_MS;
521 tex->op = nir_texop_txf_ms;
522 tex->src[0].src_type = nir_tex_src_coord;
523 tex->src[0].src = nir_src_for_ssa(img_coord);
524 tex->src[1].src_type = nir_tex_src_ms_index;
525 tex->src[1].src = nir_src_for_ssa(nir_imm_int(b, 0));
526 tex->src[2].src_type = nir_tex_src_texture_deref;
527 tex->src[2].src = nir_src_for_ssa(input_img_deref);
528 tex->dest_type = nir_type_float;
529 tex->is_array = false;
530 tex->coord_components = 2;
531
532 nir_ssa_dest_init(&tex->instr, &tex->dest, 4, 32, "tex");
533 nir_builder_instr_insert(b, &tex->instr);
534
535 tmp = &tex->dest.ssa;
536
537 if (!is_integer && samples > 1) {
538 nir_tex_instr *tex_all_same = nir_tex_instr_create(b->shader, 2);
539 tex_all_same->sampler_dim = GLSL_SAMPLER_DIM_MS;
540 tex_all_same->op = nir_texop_samples_identical;
541 tex_all_same->src[0].src_type = nir_tex_src_coord;
542 tex_all_same->src[0].src = nir_src_for_ssa(img_coord);
543 tex_all_same->src[1].src_type = nir_tex_src_texture_deref;
544 tex_all_same->src[1].src = nir_src_for_ssa(input_img_deref);
545 tex_all_same->dest_type = nir_type_float;
546 tex_all_same->is_array = false;
547 tex_all_same->coord_components = 2;
548
549 nir_ssa_dest_init(&tex_all_same->instr, &tex_all_same->dest, 1, 32, "tex");
550 nir_builder_instr_insert(b, &tex_all_same->instr);
551
552 nir_ssa_def *all_same = nir_ieq(b, &tex_all_same->dest.ssa, nir_imm_int(b, 0));
553 nir_if *if_stmt = nir_if_create(b->shader);
554 if_stmt->condition = nir_src_for_ssa(all_same);
555 nir_cf_node_insert(b->cursor, &if_stmt->cf_node);
556
557 b->cursor = nir_after_cf_list(&if_stmt->then_list);
558 for (int i = 1; i < samples; i++) {
559 nir_tex_instr *tex_add = nir_tex_instr_create(b->shader, 3);
560 tex_add->sampler_dim = GLSL_SAMPLER_DIM_MS;
561 tex_add->op = nir_texop_txf_ms;
562 tex_add->src[0].src_type = nir_tex_src_coord;
563 tex_add->src[0].src = nir_src_for_ssa(img_coord);
564 tex_add->src[1].src_type = nir_tex_src_ms_index;
565 tex_add->src[1].src = nir_src_for_ssa(nir_imm_int(b, i));
566 tex_add->src[2].src_type = nir_tex_src_texture_deref;
567 tex_add->src[2].src = nir_src_for_ssa(input_img_deref);
568 tex_add->dest_type = nir_type_float;
569 tex_add->is_array = false;
570 tex_add->coord_components = 2;
571
572 nir_ssa_dest_init(&tex_add->instr, &tex_add->dest, 4, 32, "tex");
573 nir_builder_instr_insert(b, &tex_add->instr);
574
575 tmp = nir_fadd(b, tmp, &tex_add->dest.ssa);
576 }
577
578 tmp = nir_fdiv(b, tmp, nir_imm_float(b, samples));
579 nir_store_var(b, color, tmp, 0xf);
580 b->cursor = nir_after_cf_list(&if_stmt->else_list);
581 outer_if = if_stmt;
582 }
583 nir_store_var(b, color, &tex->dest.ssa, 0xf);
584
585 if (outer_if)
586 b->cursor = nir_after_cf_node(&outer_if->cf_node);
587 }