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(struct radv_meta_saved_state
*state
,
35 struct radv_cmd_buffer
*cmd_buffer
, uint32_t flags
)
37 assert(flags
& (RADV_META_SAVE_GRAPHICS_PIPELINE
|
38 RADV_META_SAVE_COMPUTE_PIPELINE
));
42 if (state
->flags
& RADV_META_SAVE_GRAPHICS_PIPELINE
) {
43 assert(!(state
->flags
& RADV_META_SAVE_COMPUTE_PIPELINE
));
45 state
->old_pipeline
= cmd_buffer
->state
.pipeline
;
47 /* Save all viewports. */
48 state
->viewport
.count
= cmd_buffer
->state
.dynamic
.viewport
.count
;
49 typed_memcpy(state
->viewport
.viewports
,
50 cmd_buffer
->state
.dynamic
.viewport
.viewports
,
53 /* Save all scissors. */
54 state
->scissor
.count
= cmd_buffer
->state
.dynamic
.scissor
.count
;
55 typed_memcpy(state
->scissor
.scissors
,
56 cmd_buffer
->state
.dynamic
.scissor
.scissors
,
59 /* The most common meta operations all want to have the
60 * viewport reset and any scissors disabled. The rest of the
61 * dynamic state should have no effect.
63 cmd_buffer
->state
.dynamic
.viewport
.count
= 0;
64 cmd_buffer
->state
.dynamic
.scissor
.count
= 0;
65 cmd_buffer
->state
.dirty
|= 1 << VK_DYNAMIC_STATE_VIEWPORT
|
66 1 << VK_DYNAMIC_STATE_SCISSOR
;
69 if (state
->flags
& RADV_META_SAVE_COMPUTE_PIPELINE
) {
70 assert(!(state
->flags
& RADV_META_SAVE_GRAPHICS_PIPELINE
));
72 state
->old_pipeline
= cmd_buffer
->state
.compute_pipeline
;
75 if (state
->flags
& RADV_META_SAVE_DESCRIPTORS
) {
76 if (cmd_buffer
->state
.valid_descriptors
& (1 << 0))
77 state
->old_descriptor_set0
= cmd_buffer
->descriptors
[0];
79 state
->old_descriptor_set0
= NULL
;
82 if (state
->flags
& RADV_META_SAVE_CONSTANTS
) {
83 memcpy(state
->push_constants
, cmd_buffer
->push_constants
,
84 MAX_PUSH_CONSTANTS_SIZE
);
87 if (state
->flags
& RADV_META_SAVE_PASS
) {
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
;
97 radv_meta_restore(const struct radv_meta_saved_state
*state
,
98 struct radv_cmd_buffer
*cmd_buffer
)
100 if (state
->flags
& RADV_META_SAVE_GRAPHICS_PIPELINE
) {
101 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
),
102 VK_PIPELINE_BIND_POINT_GRAPHICS
,
103 radv_pipeline_to_handle(state
->old_pipeline
));
105 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_PIPELINE
;
107 /* Restore all viewports. */
108 cmd_buffer
->state
.dynamic
.viewport
.count
= state
->viewport
.count
;
109 typed_memcpy(cmd_buffer
->state
.dynamic
.viewport
.viewports
,
110 state
->viewport
.viewports
,
113 /* Restore all scissors. */
114 cmd_buffer
->state
.dynamic
.scissor
.count
= state
->scissor
.count
;
115 typed_memcpy(cmd_buffer
->state
.dynamic
.scissor
.scissors
,
116 state
->scissor
.scissors
,
119 cmd_buffer
->state
.dirty
|= 1 << VK_DYNAMIC_STATE_VIEWPORT
|
120 1 << VK_DYNAMIC_STATE_SCISSOR
;
123 if (state
->flags
& RADV_META_SAVE_COMPUTE_PIPELINE
) {
124 radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer
),
125 VK_PIPELINE_BIND_POINT_COMPUTE
,
126 radv_pipeline_to_handle(state
->old_pipeline
));
129 if (state
->flags
& RADV_META_SAVE_DESCRIPTORS
) {
130 radv_set_descriptor_set(cmd_buffer
, state
->old_descriptor_set0
, 0);
133 if (state
->flags
& RADV_META_SAVE_CONSTANTS
) {
134 memcpy(cmd_buffer
->push_constants
, state
->push_constants
,
135 MAX_PUSH_CONSTANTS_SIZE
);
136 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_COMPUTE_BIT
;
138 if (state
->flags
& RADV_META_SAVE_GRAPHICS_PIPELINE
) {
139 cmd_buffer
->push_constant_stages
|= VK_SHADER_STAGE_ALL_GRAPHICS
;
143 if (state
->flags
& RADV_META_SAVE_PASS
) {
144 cmd_buffer
->state
.pass
= state
->pass
;
145 cmd_buffer
->state
.subpass
= state
->subpass
;
146 cmd_buffer
->state
.framebuffer
= state
->framebuffer
;
147 cmd_buffer
->state
.attachments
= state
->attachments
;
148 cmd_buffer
->state
.render_area
= state
->render_area
;
150 cmd_buffer
->state
.dirty
|= RADV_CMD_DIRTY_FRAMEBUFFER
;
155 radv_meta_get_view_type(const struct radv_image
*image
)
157 switch (image
->type
) {
158 case VK_IMAGE_TYPE_1D
: return VK_IMAGE_VIEW_TYPE_1D
;
159 case VK_IMAGE_TYPE_2D
: return VK_IMAGE_VIEW_TYPE_2D
;
160 case VK_IMAGE_TYPE_3D
: return VK_IMAGE_VIEW_TYPE_3D
;
162 unreachable("bad VkImageViewType");
167 * When creating a destination VkImageView, this function provides the needed
168 * VkImageViewCreateInfo::subresourceRange::baseArrayLayer.
171 radv_meta_get_iview_layer(const struct radv_image
*dest_image
,
172 const VkImageSubresourceLayers
*dest_subresource
,
173 const VkOffset3D
*dest_offset
)
175 switch (dest_image
->type
) {
176 case VK_IMAGE_TYPE_1D
:
177 case VK_IMAGE_TYPE_2D
:
178 return dest_subresource
->baseArrayLayer
;
179 case VK_IMAGE_TYPE_3D
:
180 /* HACK: Vulkan does not allow attaching a 3D image to a framebuffer,
181 * but meta does it anyway. When doing so, we translate the
182 * destination's z offset into an array offset.
184 return dest_offset
->z
;
186 assert(!"bad VkImageType");
192 meta_alloc(void* _device
, size_t size
, size_t alignment
,
193 VkSystemAllocationScope allocationScope
)
195 struct radv_device
*device
= _device
;
196 return device
->alloc
.pfnAllocation(device
->alloc
.pUserData
, size
, alignment
,
197 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
201 meta_realloc(void* _device
, void *original
, size_t size
, size_t alignment
,
202 VkSystemAllocationScope allocationScope
)
204 struct radv_device
*device
= _device
;
205 return device
->alloc
.pfnReallocation(device
->alloc
.pUserData
, original
,
207 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
211 meta_free(void* _device
, void *data
)
213 struct radv_device
*device
= _device
;
214 return device
->alloc
.pfnFree(device
->alloc
.pUserData
, data
);
218 radv_builtin_cache_path(char *path
)
220 char *xdg_cache_home
= getenv("XDG_CACHE_HOME");
221 const char *suffix
= "/radv_builtin_shaders";
222 const char *suffix2
= "/.cache/radv_builtin_shaders";
223 struct passwd pwd
, *result
;
224 char path2
[PATH_MAX
+ 1]; /* PATH_MAX is not a real max,but suffices here. */
226 if (xdg_cache_home
) {
228 if (strlen(xdg_cache_home
) + strlen(suffix
) > PATH_MAX
)
231 strcpy(path
, xdg_cache_home
);
232 strcat(path
, suffix
);
236 getpwuid_r(getuid(), &pwd
, path2
, PATH_MAX
- strlen(suffix2
), &result
);
240 strcpy(path
, pwd
.pw_dir
);
241 strcat(path
, "/.cache");
244 strcat(path
, suffix
);
249 radv_load_meta_pipeline(struct radv_device
*device
)
251 char path
[PATH_MAX
+ 1];
255 if (!radv_builtin_cache_path(path
))
258 int fd
= open(path
, O_RDONLY
);
263 data
= malloc(st
.st_size
);
266 if(read(fd
, data
, st
.st_size
) == -1)
269 radv_pipeline_cache_load(&device
->meta_state
.cache
, data
, st
.st_size
);
276 radv_store_meta_pipeline(struct radv_device
*device
)
278 char path
[PATH_MAX
+ 1], path2
[PATH_MAX
+ 7];
282 if (!device
->meta_state
.cache
.modified
)
285 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
286 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
290 if (!radv_builtin_cache_path(path
))
294 strcat(path2
, "XXXXXX");
295 int fd
= mkstemp(path2
);//open(path, O_WRONLY | O_CREAT, 0600);
302 if (radv_GetPipelineCacheData(radv_device_to_handle(device
),
303 radv_pipeline_cache_to_handle(&device
->meta_state
.cache
),
306 if(write(fd
, data
, size
) == -1)
317 radv_device_init_meta(struct radv_device
*device
)
321 device
->meta_state
.alloc
= (VkAllocationCallbacks
) {
323 .pfnAllocation
= meta_alloc
,
324 .pfnReallocation
= meta_realloc
,
325 .pfnFree
= meta_free
,
328 device
->meta_state
.cache
.alloc
= device
->meta_state
.alloc
;
329 radv_pipeline_cache_init(&device
->meta_state
.cache
, device
);
330 radv_load_meta_pipeline(device
);
332 result
= radv_device_init_meta_clear_state(device
);
333 if (result
!= VK_SUCCESS
)
336 result
= radv_device_init_meta_resolve_state(device
);
337 if (result
!= VK_SUCCESS
)
340 result
= radv_device_init_meta_blit_state(device
);
341 if (result
!= VK_SUCCESS
)
344 result
= radv_device_init_meta_blit2d_state(device
);
345 if (result
!= VK_SUCCESS
)
348 result
= radv_device_init_meta_bufimage_state(device
);
349 if (result
!= VK_SUCCESS
)
352 result
= radv_device_init_meta_depth_decomp_state(device
);
353 if (result
!= VK_SUCCESS
)
354 goto fail_depth_decomp
;
356 result
= radv_device_init_meta_buffer_state(device
);
357 if (result
!= VK_SUCCESS
)
360 result
= radv_device_init_meta_query_state(device
);
361 if (result
!= VK_SUCCESS
)
364 result
= radv_device_init_meta_fast_clear_flush_state(device
);
365 if (result
!= VK_SUCCESS
)
366 goto fail_fast_clear
;
368 result
= radv_device_init_meta_resolve_compute_state(device
);
369 if (result
!= VK_SUCCESS
)
370 goto fail_resolve_compute
;
372 result
= radv_device_init_meta_resolve_fragment_state(device
);
373 if (result
!= VK_SUCCESS
)
374 goto fail_resolve_fragment
;
377 fail_resolve_fragment
:
378 radv_device_finish_meta_resolve_compute_state(device
);
379 fail_resolve_compute
:
380 radv_device_finish_meta_fast_clear_flush_state(device
);
382 radv_device_finish_meta_buffer_state(device
);
384 radv_device_finish_meta_query_state(device
);
386 radv_device_finish_meta_depth_decomp_state(device
);
388 radv_device_finish_meta_bufimage_state(device
);
390 radv_device_finish_meta_blit2d_state(device
);
392 radv_device_finish_meta_blit_state(device
);
394 radv_device_finish_meta_resolve_state(device
);
396 radv_device_finish_meta_clear_state(device
);
398 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
403 radv_device_finish_meta(struct radv_device
*device
)
405 radv_device_finish_meta_clear_state(device
);
406 radv_device_finish_meta_resolve_state(device
);
407 radv_device_finish_meta_blit_state(device
);
408 radv_device_finish_meta_blit2d_state(device
);
409 radv_device_finish_meta_bufimage_state(device
);
410 radv_device_finish_meta_depth_decomp_state(device
);
411 radv_device_finish_meta_query_state(device
);
412 radv_device_finish_meta_buffer_state(device
);
413 radv_device_finish_meta_fast_clear_flush_state(device
);
414 radv_device_finish_meta_resolve_compute_state(device
);
415 radv_device_finish_meta_resolve_fragment_state(device
);
417 radv_store_meta_pipeline(device
);
418 radv_pipeline_cache_finish(&device
->meta_state
.cache
);
421 nir_ssa_def
*radv_meta_gen_rect_vertices_comp2(nir_builder
*vs_b
, nir_ssa_def
*comp2
)
424 nir_intrinsic_instr
*vertex_id
= nir_intrinsic_instr_create(vs_b
->shader
, nir_intrinsic_load_vertex_id_zero_base
);
425 nir_ssa_dest_init(&vertex_id
->instr
, &vertex_id
->dest
, 1, 32, "vertexid");
426 nir_builder_instr_insert(vs_b
, &vertex_id
->instr
);
428 /* vertex 0 - -1.0, -1.0 */
429 /* vertex 1 - -1.0, 1.0 */
430 /* vertex 2 - 1.0, -1.0 */
431 /* so channel 0 is vertex_id != 2 ? -1.0 : 1.0
432 channel 1 is vertex id != 1 ? -1.0 : 1.0 */
434 nir_ssa_def
*c0cmp
= nir_ine(vs_b
, &vertex_id
->dest
.ssa
,
435 nir_imm_int(vs_b
, 2));
436 nir_ssa_def
*c1cmp
= nir_ine(vs_b
, &vertex_id
->dest
.ssa
,
437 nir_imm_int(vs_b
, 1));
439 nir_ssa_def
*comp
[4];
440 comp
[0] = nir_bcsel(vs_b
, c0cmp
,
441 nir_imm_float(vs_b
, -1.0),
442 nir_imm_float(vs_b
, 1.0));
444 comp
[1] = nir_bcsel(vs_b
, c1cmp
,
445 nir_imm_float(vs_b
, -1.0),
446 nir_imm_float(vs_b
, 1.0));
448 comp
[3] = nir_imm_float(vs_b
, 1.0);
449 nir_ssa_def
*outvec
= nir_vec(vs_b
, comp
, 4);
454 nir_ssa_def
*radv_meta_gen_rect_vertices(nir_builder
*vs_b
)
456 return radv_meta_gen_rect_vertices_comp2(vs_b
, nir_imm_float(vs_b
, 0.0));
459 /* vertex shader that generates vertices */
461 radv_meta_build_nir_vs_generate_vertices(void)
463 const struct glsl_type
*vec4
= glsl_vec4_type();
466 nir_variable
*v_position
;
468 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_VERTEX
, NULL
);
469 b
.shader
->info
.name
= ralloc_strdup(b
.shader
, "meta_vs_gen_verts");
471 nir_ssa_def
*outvec
= radv_meta_gen_rect_vertices(&b
);
473 v_position
= nir_variable_create(b
.shader
, nir_var_shader_out
, vec4
,
475 v_position
->data
.location
= VARYING_SLOT_POS
;
477 nir_store_var(&b
, v_position
, outvec
, 0xf);
483 radv_meta_build_nir_fs_noop(void)
487 nir_builder_init_simple_shader(&b
, NULL
, MESA_SHADER_FRAGMENT
, NULL
);
488 b
.shader
->info
.name
= ralloc_asprintf(b
.shader
,
494 void radv_meta_build_resolve_shader_core(nir_builder
*b
,
497 nir_variable
*input_img
,
499 nir_ssa_def
*img_coord
)
501 /* do a txf_ms on each sample */
503 nir_if
*outer_if
= NULL
;
505 nir_tex_instr
*tex
= nir_tex_instr_create(b
->shader
, 2);
506 tex
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
507 tex
->op
= nir_texop_txf_ms
;
508 tex
->src
[0].src_type
= nir_tex_src_coord
;
509 tex
->src
[0].src
= nir_src_for_ssa(img_coord
);
510 tex
->src
[1].src_type
= nir_tex_src_ms_index
;
511 tex
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, 0));
512 tex
->dest_type
= nir_type_float
;
513 tex
->is_array
= false;
514 tex
->coord_components
= 2;
515 tex
->texture
= nir_deref_var_create(tex
, input_img
);
518 nir_ssa_dest_init(&tex
->instr
, &tex
->dest
, 4, 32, "tex");
519 nir_builder_instr_insert(b
, &tex
->instr
);
521 tmp
= &tex
->dest
.ssa
;
523 if (!is_integer
&& samples
> 1) {
524 nir_tex_instr
*tex_all_same
= nir_tex_instr_create(b
->shader
, 1);
525 tex_all_same
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
526 tex_all_same
->op
= nir_texop_samples_identical
;
527 tex_all_same
->src
[0].src_type
= nir_tex_src_coord
;
528 tex_all_same
->src
[0].src
= nir_src_for_ssa(img_coord
);
529 tex_all_same
->dest_type
= nir_type_float
;
530 tex_all_same
->is_array
= false;
531 tex_all_same
->coord_components
= 2;
532 tex_all_same
->texture
= nir_deref_var_create(tex_all_same
, input_img
);
533 tex_all_same
->sampler
= NULL
;
535 nir_ssa_dest_init(&tex_all_same
->instr
, &tex_all_same
->dest
, 1, 32, "tex");
536 nir_builder_instr_insert(b
, &tex_all_same
->instr
);
538 nir_ssa_def
*all_same
= nir_ine(b
, &tex_all_same
->dest
.ssa
, nir_imm_int(b
, 0));
539 nir_if
*if_stmt
= nir_if_create(b
->shader
);
540 if_stmt
->condition
= nir_src_for_ssa(all_same
);
541 nir_cf_node_insert(b
->cursor
, &if_stmt
->cf_node
);
543 b
->cursor
= nir_after_cf_list(&if_stmt
->then_list
);
544 for (int i
= 1; i
< samples
; i
++) {
545 nir_tex_instr
*tex_add
= nir_tex_instr_create(b
->shader
, 2);
546 tex_add
->sampler_dim
= GLSL_SAMPLER_DIM_MS
;
547 tex_add
->op
= nir_texop_txf_ms
;
548 tex_add
->src
[0].src_type
= nir_tex_src_coord
;
549 tex_add
->src
[0].src
= nir_src_for_ssa(img_coord
);
550 tex_add
->src
[1].src_type
= nir_tex_src_ms_index
;
551 tex_add
->src
[1].src
= nir_src_for_ssa(nir_imm_int(b
, i
));
552 tex_add
->dest_type
= nir_type_float
;
553 tex_add
->is_array
= false;
554 tex_add
->coord_components
= 2;
555 tex_add
->texture
= nir_deref_var_create(tex_add
, input_img
);
556 tex_add
->sampler
= NULL
;
558 nir_ssa_dest_init(&tex_add
->instr
, &tex_add
->dest
, 4, 32, "tex");
559 nir_builder_instr_insert(b
, &tex_add
->instr
);
561 tmp
= nir_fadd(b
, tmp
, &tex_add
->dest
.ssa
);
564 tmp
= nir_fdiv(b
, tmp
, nir_imm_float(b
, samples
));
565 nir_store_var(b
, color
, tmp
, 0xf);
566 b
->cursor
= nir_after_cf_list(&if_stmt
->else_list
);
569 nir_store_var(b
, color
, &tex
->dest
.ssa
, 0xf);
572 b
->cursor
= nir_after_cf_node(&outer_if
->cf_node
);