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
,
40 if (state
->flags
& RADV_META_SAVE_GRAPHICS_PIPELINE
) {
41 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
43 /* Save all viewports. */
44 state
->viewport
.count
= cmd_buffer
->state
.dynamic
.viewport
.count
;
45 typed_memcpy(state
->viewport
.viewports
,
46 cmd_buffer
->state
.dynamic
.viewport
.viewports
,
49 /* Save all scissors. */
50 state
->scissor
.count
= cmd_buffer
->state
.dynamic
.scissor
.count
;
51 typed_memcpy(state
->scissor
.scissors
,
52 cmd_buffer
->state
.dynamic
.scissor
.scissors
,
56 if (state
->flags
& RADV_META_SAVE_DESCRIPTORS
) {
57 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
60 if (state
->flags
& RADV_META_SAVE_CONSTANTS
) {
61 memcpy(state
->push_constants
, cmd_buffer
->push_constants
,
62 MAX_PUSH_CONSTANTS_SIZE
);
65 if (state
->flags
& RADV_META_SAVE_PASS
) {
66 state
->pass
= cmd_buffer
->state
.pass
;
67 state
->subpass
= cmd_buffer
->state
.subpass
;
68 state
->framebuffer
= cmd_buffer
->state
.framebuffer
;
69 state
->attachments
= cmd_buffer
->state
.attachments
;
70 state
->render_area
= cmd_buffer
->state
.render_area
;
75 radv_meta_restore(const struct radv_meta_saved_state
*state
,
76 struct radv_cmd_buffer
*cmd_buffer
)
78 if (state
->flags
& RADV_META_SAVE_GRAPHICS_PIPELINE
) {
79 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
),
80 VK_PIPELINE_BIND_POINT_GRAPHICS
,
81 radv_pipeline_to_handle(state
->old_pipeline
));
83 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_PIPELINE
;
85 /* Restore all viewports. */
86 cmd_buffer
->state
.dynamic
.viewport
.count
= state
->viewport
.count
;
87 typed_memcpy(cmd_buffer
->state
.dynamic
.viewport
.viewports
,
88 state
->viewport
.viewports
,
91 /* Restore all scissors. */
92 cmd_buffer
->state
.dynamic
.scissor
.count
= state
->scissor
.count
;
93 typed_memcpy(cmd_buffer
->state
.dynamic
.scissor
.scissors
,
94 state
->scissor
.scissors
,
97 cmd_buffer
->state
.dirty
|= 1 << VK_DYNAMIC_STATE_VIEWPORT
|
98 1 << VK_DYNAMIC_STATE_SCISSOR
;
101 if (state
->flags
& RADV_META_SAVE_DESCRIPTORS
) {
102 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
103 cmd_buffer
->state
.descriptors_dirty
|= (1 << 0);
106 if (state
->flags
& RADV_META_SAVE_CONSTANTS
) {
107 memcpy(cmd_buffer
->push_constants
, state
->push_constants
,
108 MAX_PUSH_CONSTANTS_SIZE
);
109 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_ALL_GRAPHICS
|
110 VK_SHADER_STAGE_COMPUTE_BIT
;
113 if (state
->flags
& RADV_META_SAVE_PASS
) {
114 cmd_buffer
->state
.pass
= state
->pass
;
115 cmd_buffer
->state
.subpass
= state
->subpass
;
116 cmd_buffer
->state
.framebuffer
= state
->framebuffer
;
117 cmd_buffer
->state
.attachments
= state
->attachments
;
118 cmd_buffer
->state
.render_area
= state
->render_area
;
120 radv_emit_framebuffer_state(cmd_buffer
);
125 radv_meta_save_compute(struct radv_meta_saved_compute_state
*state
,
126 const struct radv_cmd_buffer
*cmd_buffer
,
127 unsigned push_constant_size
)
129 state
->old_pipeline
= cmd_buffer
->state
.compute_pipeline
;
130 state
->old_descriptor_set0
= cmd_buffer
->state
.descriptors
[0];
131 state
->push_constant_size
= push_constant_size
;
133 if (state
->push_constant_size
) {
134 memcpy(state
->push_constants
, cmd_buffer
->push_constants
,
135 state
->push_constant_size
);
140 radv_meta_restore_compute(const struct radv_meta_saved_compute_state
*state
,
141 struct radv_cmd_buffer
*cmd_buffer
)
143 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
), VK_PIPELINE_BIND_POINT_COMPUTE
,
144 radv_pipeline_to_handle(state
->old_pipeline
));
146 cmd_buffer
->state
.descriptors
[0] = state
->old_descriptor_set0
;
148 if (state
->push_constant_size
) {
149 memcpy(cmd_buffer
->push_constants
, state
->push_constants
,
150 state
->push_constant_size
);
151 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_COMPUTE_BIT
;
156 radv_meta_get_view_type(const struct radv_image
*image
)
158 switch (image
->type
) {
159 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
160 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
161 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
163 unreachable("bad VkImageViewType");
168 * When creating a destination VkImageView, this function provides the needed
169 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
172 radv_meta_get_iview_layer(const struct radv_image
*dest_image
,
173 const VkImageSubresourceLayers
*dest_subresource
,
174 const VkOffset3D
*dest_offset
)
176 switch (dest_image
->type
) {
177 case VK_IMAGE_TYPE_1D
:
178 case VK_IMAGE_TYPE_2D
:
179 return dest_subresource
->baseArrayLayer
;
180 case VK_IMAGE_TYPE_3D
:
181 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
182 * but meta does it anyway. When doing so, we translate the
183 * destination's z offset into an array offset.
185 return dest_offset
->z
;
187 assert(!"bad VkImageType");
193 meta_alloc(void* _device
, size_t size
, size_t alignment
,
194 VkSystemAllocationScope allocationScope
)
196 struct radv_device
*device
= _device
;
197 return device
->alloc
.pfnAllocation(device
->alloc
.pUserData
, size
, alignment
,
198 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
202 meta_realloc(void* _device
, void *original
, size_t size
, size_t alignment
,
203 VkSystemAllocationScope allocationScope
)
205 struct radv_device
*device
= _device
;
206 return device
->alloc
.pfnReallocation(device
->alloc
.pUserData
, original
,
208 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
212 meta_free(void* _device
, void *data
)
214 struct radv_device
*device
= _device
;
215 return device
->alloc
.pfnFree(device
->alloc
.pUserData
, data
);
219 radv_builtin_cache_path(char *path
)
221 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
222 const char *suffix
= "/radv_builtin_shaders";
223 const char *suffix2
= "/.cache/radv_builtin_shaders";
224 struct passwd pwd
, *result
;
225 char path2
[PATH_MAX
+ 1]; /* PATH_MAX is not a real max,but suffices here. */
227 if (xdg_cache_home
) {
229 if (strlen(xdg_cache_home
) + strlen(suffix
) > PATH_MAX
)
232 strcpy(path
, xdg_cache_home
);
233 strcat(path
, suffix
);
237 getpwuid_r(getuid(), &pwd
, path2
, PATH_MAX
- strlen(suffix2
), &result
);
241 strcpy(path
, pwd
.pw_dir
);
242 strcat(path
, "/.cache");
245 strcat(path
, suffix
);
250 radv_load_meta_pipeline(struct radv_device
*device
)
252 char path
[PATH_MAX
+ 1];
256 if (!radv_builtin_cache_path(path
))
259 int fd
= open(path
, O_RDONLY
);
264 data
= malloc(st
.st_size
);
267 if(read(fd
, data
, st
.st_size
) == -1)
270 radv_pipeline_cache_load(&device
->meta_state
.cache
, data
, st
.st_size
);
277 radv_store_meta_pipeline(struct radv_device
*device
)
279 char path
[PATH_MAX
+ 1], path2
[PATH_MAX
+ 7];
283 if (!device
->meta_state
.cache
.modified
)
286 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
287 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
291 if (!radv_builtin_cache_path(path
))
295 strcat(path2
, "XXXXXX");
296 int fd
= mkstemp(path2
);//open(path, O_WRONLY | O_CREAT, 0600);
303 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
304 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
307 if(write(fd
, data
, size
) == -1)
318 radv_device_init_meta(struct radv_device
*device
)
322 device
->meta_state
.alloc
= (VkAllocationCallbacks
) {
324 .pfnAllocation
= meta_alloc
,
325 .pfnReallocation
= meta_realloc
,
326 .pfnFree
= meta_free
,
329 device
->meta_state
.cache
.alloc
= device
->meta_state
.alloc
;
330 radv_pipeline_cache_init(&device
->meta_state
.cache
, device
);
331 radv_load_meta_pipeline(device
);
333 result
= radv_device_init_meta_clear_state(device
);
334 if (result
!= VK_SUCCESS
)
337 result
= radv_device_init_meta_resolve_state(device
);
338 if (result
!= VK_SUCCESS
)
341 result
= radv_device_init_meta_blit_state(device
);
342 if (result
!= VK_SUCCESS
)
345 result
= radv_device_init_meta_blit2d_state(device
);
346 if (result
!= VK_SUCCESS
)
349 result
= radv_device_init_meta_bufimage_state(device
);
350 if (result
!= VK_SUCCESS
)
353 result
= radv_device_init_meta_depth_decomp_state(device
);
354 if (result
!= VK_SUCCESS
)
355 goto fail_depth_decomp
;
357 result
= radv_device_init_meta_buffer_state(device
);
358 if (result
!= VK_SUCCESS
)
361 result
= radv_device_init_meta_query_state(device
);
362 if (result
!= VK_SUCCESS
)
365 result
= radv_device_init_meta_fast_clear_flush_state(device
);
366 if (result
!= VK_SUCCESS
)
367 goto fail_fast_clear
;
369 result
= radv_device_init_meta_resolve_compute_state(device
);
370 if (result
!= VK_SUCCESS
)
371 goto fail_resolve_compute
;
373 result
= radv_device_init_meta_resolve_fragment_state(device
);
374 if (result
!= VK_SUCCESS
)
375 goto fail_resolve_fragment
;
378 fail_resolve_fragment
:
379 radv_device_finish_meta_resolve_compute_state(device
);
380 fail_resolve_compute
:
381 radv_device_finish_meta_fast_clear_flush_state(device
);
383 radv_device_finish_meta_buffer_state(device
);
385 radv_device_finish_meta_query_state(device
);
387 radv_device_finish_meta_depth_decomp_state(device
);
389 radv_device_finish_meta_bufimage_state(device
);
391 radv_device_finish_meta_blit2d_state(device
);
393 radv_device_finish_meta_blit_state(device
);
395 radv_device_finish_meta_resolve_state(device
);
397 radv_device_finish_meta_clear_state(device
);
399 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
404 radv_device_finish_meta(struct radv_device
*device
)
406 radv_device_finish_meta_clear_state(device
);
407 radv_device_finish_meta_resolve_state(device
);
408 radv_device_finish_meta_blit_state(device
);
409 radv_device_finish_meta_blit2d_state(device
);
410 radv_device_finish_meta_bufimage_state(device
);
411 radv_device_finish_meta_depth_decomp_state(device
);
412 radv_device_finish_meta_query_state(device
);
413 radv_device_finish_meta_buffer_state(device
);
414 radv_device_finish_meta_fast_clear_flush_state(device
);
415 radv_device_finish_meta_resolve_compute_state(device
);
416 radv_device_finish_meta_resolve_fragment_state(device
);
418 radv_store_meta_pipeline(device
);
419 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
423 * The most common meta operations all want to have the viewport
424 * reset and any scissors disabled. The rest of the dynamic state
425 * should have no effect.
428 radv_meta_save_graphics_reset_vport_scissor_novertex(struct radv_meta_saved_state
*saved_state
,
429 struct radv_cmd_buffer
*cmd_buffer
,
432 radv_meta_save_novertex(saved_state
, cmd_buffer
, flags
);
433 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
434 cmd_buffer
->state
.dynamic
.scissor
.count
= 0;
435 cmd_buffer
->state
.dirty
|= 1 << VK_DYNAMIC_STATE_VIEWPORT
|
436 1 << VK_DYNAMIC_STATE_SCISSOR
;
439 nir_ssa_def
*radv_meta_gen_rect_vertices_comp2(nir_builder
*vs_b
, nir_ssa_def
*comp2
)
442 nir_intrinsic_instr
*vertex_id
= nir_intrinsic_instr_create(vs_b
->shader
, nir_intrinsic_load_vertex_id_zero_base
);
443 nir_ssa_dest_init(&vertex_id
->instr
, &vertex_id
->dest
, 1, 32, "vertexid");
444 nir_builder_instr_insert(vs_b
, &vertex_id
->instr
);
446 /* vertex 0 - -1.0, -1.0 */
447 /* vertex 1 - -1.0, 1.0 */
448 /* vertex 2 - 1.0, -1.0 */
449 /* so channel 0 is vertex_id != 2 ? -1.0 : 1.0
450 channel 1 is vertex id != 1 ? -1.0 : 1.0 */
452 nir_ssa_def
*c0cmp
= nir_ine(vs_b
, &vertex_id
->dest
.ssa
,
453 nir_imm_int(vs_b
, 2));
454 nir_ssa_def
*c1cmp
= nir_ine(vs_b
, &vertex_id
->dest
.ssa
,
455 nir_imm_int(vs_b
, 1));
457 nir_ssa_def
*comp
[4];
458 comp
[0] = nir_bcsel(vs_b
, c0cmp
,
459 nir_imm_float(vs_b
, -1.0),
460 nir_imm_float(vs_b
, 1.0));
462 comp
[1] = nir_bcsel(vs_b
, c1cmp
,
463 nir_imm_float(vs_b
, -1.0),
464 nir_imm_float(vs_b
, 1.0));
466 comp
[3] = nir_imm_float(vs_b
, 1.0);
467 nir_ssa_def
*outvec
= nir_vec(vs_b
, comp
, 4);
472 nir_ssa_def
*radv_meta_gen_rect_vertices(nir_builder
*vs_b
)
474 return radv_meta_gen_rect_vertices_comp2(vs_b
, nir_imm_float(vs_b
, 0.0));
477 /* vertex shader that generates vertices */
479 radv_meta_build_nir_vs_generate_vertices(void)
481 const struct glsl_type
*vec4
= glsl_vec4_type();
484 nir_variable
*v_position
;
486 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
487 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_vs_gen_verts");
489 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices(&b
);
491 v_position
= nir_variable_create(b
.shader
, nir_var_shader_out
, vec4
,
493 v_position
->data
.location
= VARYING_SLOT_POS
;
495 nir_store_var(&b
, v_position
, outvec
, 0xf);
501 radv_meta_build_nir_fs_noop(void)
505 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
506 b
.shader
->info
.name
= ralloc_asprintf(b
.shader
,
512 void radv_meta_build_resolve_shader_core(nir_builder
*b
,
515 nir_variable
*input_img
,
517 nir_ssa_def
*img_coord
)
519 /* do a txf_ms on each sample */
521 nir_if
*outer_if
= NULL
;
523 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, 2);
524 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
525 tex
->op
= nir_texop_txf_ms
;
526 tex
->src
[0].src_type
= nir_tex_src_coord
;
527 tex
->src
[0].src
= nir_src_for_ssa(img_coord
);
528 tex
->src
[1].src_type
= nir_tex_src_ms_index
;
529 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
530 tex
->dest_type
= nir_type_float
;
531 tex
->is_array
= false;
532 tex
->coord_components
= 2;
533 tex
->texture
= nir_deref_var_create(tex
, input_img
);
536 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, "tex");
537 nir_builder_instr_insert(b
, &tex
->instr
);
539 tmp
= &tex
->dest
.ssa
;
541 if (!is_integer
&& samples
> 1) {
542 nir_tex_instr
*tex_all_same
= nir_tex_instr_create(b
->shader
, 1);
543 tex_all_same
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
544 tex_all_same
->op
= nir_texop_samples_identical
;
545 tex_all_same
->src
[0].src_type
= nir_tex_src_coord
;
546 tex_all_same
->src
[0].src
= nir_src_for_ssa(img_coord
);
547 tex_all_same
->dest_type
= nir_type_float
;
548 tex_all_same
->is_array
= false;
549 tex_all_same
->coord_components
= 2;
550 tex_all_same
->texture
= nir_deref_var_create(tex_all_same
, input_img
);
551 tex_all_same
->sampler
= NULL
;
553 nir_ssa_dest_init(&tex_all_same
->instr
, &tex_all_same
->dest
, 1, 32, "tex");
554 nir_builder_instr_insert(b
, &tex_all_same
->instr
);
556 nir_ssa_def
*all_same
= nir_ine(b
, &tex_all_same
->dest
.ssa
, nir_imm_int(b
, 0));
557 nir_if
*if_stmt
= nir_if_create(b
->shader
);
558 if_stmt
->condition
= nir_src_for_ssa(all_same
);
559 nir_cf_node_insert(b
->cursor
, &if_stmt
->cf_node
);
561 b
->cursor
= nir_after_cf_list(&if_stmt
->then_list
);
562 for (int i
= 1; i
< samples
; i
++) {
563 nir_tex_instr
*tex_add
= nir_tex_instr_create(b
->shader
, 2);
564 tex_add
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
565 tex_add
->op
= nir_texop_txf_ms
;
566 tex_add
->src
[0].src_type
= nir_tex_src_coord
;
567 tex_add
->src
[0].src
= nir_src_for_ssa(img_coord
);
568 tex_add
->src
[1].src_type
= nir_tex_src_ms_index
;
569 tex_add
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, i
));
570 tex_add
->dest_type
= nir_type_float
;
571 tex_add
->is_array
= false;
572 tex_add
->coord_components
= 2;
573 tex_add
->texture
= nir_deref_var_create(tex_add
, input_img
);
574 tex_add
->sampler
= NULL
;
576 nir_ssa_dest_init(&tex_add
->instr
, &tex_add
->dest
, 4, 32, "tex");
577 nir_builder_instr_insert(b
, &tex_add
->instr
);
579 tmp
= nir_fadd(b
, tmp
, &tex_add
->dest
.ssa
);
582 tmp
= nir_fdiv(b
, tmp
, nir_imm_float(b
, samples
));
583 nir_store_var(b
, color
, tmp
, 0xf);
584 b
->cursor
= nir_after_cf_list(&if_stmt
->else_list
);
587 nir_store_var(b
, color
, &tex
->dest
.ssa
, 0xf);
590 b
->cursor
= nir_after_cf_node(&outer_if
->cf_node
);