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