radv: Fix 3d blits.
[mesa.git] / src / amd / vulkan / radv_meta_blit.c
index f68496d32026221b9492a67da17584bbd774f9e9..71cea3b0a316827e529712c74dd64b395e5b6439 100644 (file)
@@ -31,6 +31,13 @@ struct blit_region {
        VkExtent3D dest_extent;
 };
 
+static VkResult
+build_pipeline(struct radv_device *device,
+               VkImageAspectFlagBits aspect,
+               enum glsl_sampler_dim tex_dim,
+               unsigned fs_key,
+               VkPipeline *pipeline);
+
 static nir_shader *
 build_nir_vertex_shader(void)
 {
@@ -121,7 +128,7 @@ build_nir_copy_fragment_shader(enum glsl_sampler_dim tex_dim)
        unsigned swz[] = { 0, (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 1), 2 };
        nir_ssa_def *const tex_pos =
                nir_swizzle(&b, nir_load_var(&b, tex_pos_in), swz,
-                           (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3), false);
+                           (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3));
 
        const struct glsl_type *sampler_type =
                glsl_sampler_type(tex_dim, false, tex_dim != GLSL_SAMPLER_DIM_3D,
@@ -179,7 +186,7 @@ build_nir_copy_fragment_shader_depth(enum glsl_sampler_dim tex_dim)
        unsigned swz[] = { 0, (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 1), 2 };
        nir_ssa_def *const tex_pos =
                nir_swizzle(&b, nir_load_var(&b, tex_pos_in), swz,
-                           (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3), false);
+                           (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3));
 
        const struct glsl_type *sampler_type =
                glsl_sampler_type(tex_dim, false, tex_dim != GLSL_SAMPLER_DIM_3D,
@@ -237,7 +244,7 @@ build_nir_copy_fragment_shader_stencil(enum glsl_sampler_dim tex_dim)
        unsigned swz[] = { 0, (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 1), 2 };
        nir_ssa_def *const tex_pos =
                nir_swizzle(&b, nir_load_var(&b, tex_pos_in), swz,
-                           (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3), false);
+                           (tex_dim == GLSL_SAMPLER_DIM_1D ? 2 : 3));
 
        const struct glsl_type *sampler_type =
                glsl_sampler_type(tex_dim, false, tex_dim != GLSL_SAMPLER_DIM_3D,
@@ -273,20 +280,34 @@ build_nir_copy_fragment_shader_stencil(enum glsl_sampler_dim tex_dim)
        return b.shader;
 }
 
+static enum glsl_sampler_dim
+translate_sampler_dim(VkImageType type) {
+       switch(type) {
+       case VK_IMAGE_TYPE_1D:
+               return GLSL_SAMPLER_DIM_1D;
+       case VK_IMAGE_TYPE_2D:
+               return GLSL_SAMPLER_DIM_2D;
+       case VK_IMAGE_TYPE_3D:
+               return GLSL_SAMPLER_DIM_3D;
+       default:
+               unreachable("Unhandled image type");
+       }
+}
+
 static void
 meta_emit_blit(struct radv_cmd_buffer *cmd_buffer,
                struct radv_image *src_image,
                struct radv_image_view *src_iview,
               VkImageLayout src_image_layout,
-               VkOffset3D src_offset_0,
-               VkOffset3D src_offset_1,
+               float src_offset_0[3],
+               float src_offset_1[3],
                struct radv_image *dest_image,
                struct radv_image_view *dest_iview,
               VkImageLayout dest_image_layout,
                VkOffset2D dest_offset_0,
                VkOffset2D dest_offset_1,
                VkRect2D dest_box,
-               VkFilter blit_filter)
+               VkSampler sampler)
 {
        struct radv_device *device = cmd_buffer->device;
        uint32_t src_width = radv_minify(src_iview->image->info.width, src_iview->base_mip);
@@ -298,11 +319,11 @@ meta_emit_blit(struct radv_cmd_buffer *cmd_buffer,
        assert(src_image->info.samples == dest_image->info.samples);
 
        float vertex_push_constants[5] = {
-               (float)src_offset_0.x / (float)src_width,
-               (float)src_offset_0.y / (float)src_height,
-               (float)src_offset_1.x / (float)src_width,
-               (float)src_offset_1.y / (float)src_height,
-               (float)src_offset_0.z / (float)src_depth,
+               src_offset_0[0] / (float)src_width,
+               src_offset_0[1] / (float)src_height,
+               src_offset_1[0] / (float)src_width,
+               src_offset_1[1] / (float)src_height,
+               src_offset_0[2] / (float)src_depth,
        };
 
        radv_CmdPushConstants(radv_cmd_buffer_to_handle(cmd_buffer),
@@ -310,17 +331,6 @@ meta_emit_blit(struct radv_cmd_buffer *cmd_buffer,
                              VK_SHADER_STAGE_VERTEX_BIT, 0, 20,
                              vertex_push_constants);
 
-       VkSampler sampler;
-       radv_CreateSampler(radv_device_to_handle(device),
-                                &(VkSamplerCreateInfo) {
-                                        .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
-                                                .magFilter = blit_filter,
-                                                .minFilter = blit_filter,
-                                                .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
-                                                .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
-                                                .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
-                                                }, &cmd_buffer->pool->alloc, &sampler);
-
        VkFramebuffer fb;
        radv_CreateFramebuffer(radv_device_to_handle(device),
                               &(VkFramebufferCreateInfo) {
@@ -333,103 +343,115 @@ meta_emit_blit(struct radv_cmd_buffer *cmd_buffer,
                                       .height = dst_height,
                                       .layers = 1,
                                }, &cmd_buffer->pool->alloc, &fb);
-       VkPipeline pipeline;
+       VkPipeline* pipeline = NULL;
+       unsigned fs_key = 0;
        switch (src_iview->aspect_mask) {
        case VK_IMAGE_ASPECT_COLOR_BIT: {
-               unsigned fs_key = radv_format_meta_fs_key(dest_image->vk_format);
                unsigned dst_layout = radv_meta_dst_layout_from_layout(dest_image_layout);
-
-               radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer),
-                                             &(VkRenderPassBeginInfo) {
-                                                     .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
-                                                             .renderPass = device->meta_state.blit.render_pass[fs_key][dst_layout],
-                                                             .framebuffer = fb,
-                                                             .renderArea = {
-                                                             .offset = { dest_box.offset.x, dest_box.offset.y },
-                                                             .extent = { dest_box.extent.width, dest_box.extent.height },
-                                                     },
-                                                             .clearValueCount = 0,
-                                                                      .pClearValues = NULL,
-                                                      }, VK_SUBPASS_CONTENTS_INLINE);
+               fs_key = radv_format_meta_fs_key(dest_image->vk_format);
+
+               radv_cmd_buffer_begin_render_pass(cmd_buffer,
+                                                 &(VkRenderPassBeginInfo) {
+                                                       .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
+                                                               .renderPass = device->meta_state.blit.render_pass[fs_key][dst_layout],
+                                                               .framebuffer = fb,
+                                                               .renderArea = {
+                                                                       .offset = { dest_box.offset.x, dest_box.offset.y },
+                                                                       .extent = { dest_box.extent.width, dest_box.extent.height },
+                                                               },
+                                                       .clearValueCount = 0,
+                                                       .pClearValues = NULL,
+                                               });
                switch (src_image->type) {
                case VK_IMAGE_TYPE_1D:
-                       pipeline = device->meta_state.blit.pipeline_1d_src[fs_key];
+                       pipeline = &device->meta_state.blit.pipeline_1d_src[fs_key];
                        break;
                case VK_IMAGE_TYPE_2D:
-                       pipeline = device->meta_state.blit.pipeline_2d_src[fs_key];
+                       pipeline = &device->meta_state.blit.pipeline_2d_src[fs_key];
                        break;
                case VK_IMAGE_TYPE_3D:
-                       pipeline = device->meta_state.blit.pipeline_3d_src[fs_key];
+                       pipeline = &device->meta_state.blit.pipeline_3d_src[fs_key];
                        break;
                default:
-                       unreachable(!"bad VkImageType");
+                       unreachable("bad VkImageType");
                }
                break;
        }
        case VK_IMAGE_ASPECT_DEPTH_BIT: {
                enum radv_blit_ds_layout ds_layout = radv_meta_blit_ds_to_type(dest_image_layout);
-               radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer),
-                                             &(VkRenderPassBeginInfo) {
-                                                     .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
-                                                             .renderPass = device->meta_state.blit.depth_only_rp[ds_layout],
-                                                             .framebuffer = fb,
-                                                             .renderArea = {
-                                                             .offset = { dest_box.offset.x, dest_box.offset.y },
-                                                             .extent = { dest_box.extent.width, dest_box.extent.height },
-                                                     },
-                                                             .clearValueCount = 0,
-                                                                      .pClearValues = NULL,
-                                                      }, VK_SUBPASS_CONTENTS_INLINE);
+               radv_cmd_buffer_begin_render_pass(cmd_buffer,
+                                                 &(VkRenderPassBeginInfo) {
+                                                       .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
+                                                       .renderPass = device->meta_state.blit.depth_only_rp[ds_layout],
+                                                       .framebuffer = fb,
+                                                       .renderArea = {
+                                                               .offset = { dest_box.offset.x, dest_box.offset.y },
+                                                               .extent = { dest_box.extent.width, dest_box.extent.height },
+                                                       },
+                                                       .clearValueCount = 0,
+                                                       .pClearValues = NULL,
+                                                 });
                switch (src_image->type) {
                case VK_IMAGE_TYPE_1D:
-                       pipeline = device->meta_state.blit.depth_only_1d_pipeline;
+                       pipeline = &device->meta_state.blit.depth_only_1d_pipeline;
                        break;
                case VK_IMAGE_TYPE_2D:
-                       pipeline = device->meta_state.blit.depth_only_2d_pipeline;
+                       pipeline = &device->meta_state.blit.depth_only_2d_pipeline;
                        break;
                case VK_IMAGE_TYPE_3D:
-                       pipeline = device->meta_state.blit.depth_only_3d_pipeline;
+                       pipeline = &device->meta_state.blit.depth_only_3d_pipeline;
                        break;
                default:
-                       unreachable(!"bad VkImageType");
+                       unreachable("bad VkImageType");
                }
                break;
        }
        case VK_IMAGE_ASPECT_STENCIL_BIT: {
                enum radv_blit_ds_layout ds_layout = radv_meta_blit_ds_to_type(dest_image_layout);
-               radv_CmdBeginRenderPass(radv_cmd_buffer_to_handle(cmd_buffer),
-                                             &(VkRenderPassBeginInfo) {
-                                                     .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
-                                                             .renderPass = device->meta_state.blit.stencil_only_rp[ds_layout],
-                                                             .framebuffer = fb,
-                                                             .renderArea = {
-                                                             .offset = { dest_box.offset.x, dest_box.offset.y },
-                                                             .extent = { dest_box.extent.width, dest_box.extent.height },
-                                                             },
-                                                             .clearValueCount = 0,
-                                                                      .pClearValues = NULL,
-                                                      }, VK_SUBPASS_CONTENTS_INLINE);
+               radv_cmd_buffer_begin_render_pass(cmd_buffer,
+                                                 &(VkRenderPassBeginInfo) {
+                                                       .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
+                                                       .renderPass = device->meta_state.blit.stencil_only_rp[ds_layout],
+                                                       .framebuffer = fb,
+                                                       .renderArea = {
+                                                               .offset = { dest_box.offset.x, dest_box.offset.y },
+                                                               .extent = { dest_box.extent.width, dest_box.extent.height },
+                                                       },
+                                                       .clearValueCount = 0,
+                                                       .pClearValues = NULL,
+                                                 });
                switch (src_image->type) {
                case VK_IMAGE_TYPE_1D:
-                       pipeline = device->meta_state.blit.stencil_only_1d_pipeline;
+                       pipeline = &device->meta_state.blit.stencil_only_1d_pipeline;
                        break;
                case VK_IMAGE_TYPE_2D:
-                       pipeline = device->meta_state.blit.stencil_only_2d_pipeline;
+                       pipeline = &device->meta_state.blit.stencil_only_2d_pipeline;
                        break;
                case VK_IMAGE_TYPE_3D:
-                       pipeline = device->meta_state.blit.stencil_only_3d_pipeline;
+                       pipeline = &device->meta_state.blit.stencil_only_3d_pipeline;
                        break;
                default:
-                       unreachable(!"bad VkImageType");
+                       unreachable("bad VkImageType");
                }
                break;
        }
        default:
-               unreachable(!"bad VkImageType");
+               unreachable("bad VkImageType");
+       }
+
+       radv_cmd_buffer_set_subpass(cmd_buffer,
+                                   &cmd_buffer->state.pass->subpasses[0]);
+
+       if (!*pipeline) {
+               VkResult ret = build_pipeline(device, src_iview->aspect_mask, translate_sampler_dim(src_image->type), fs_key, pipeline);
+               if (ret != VK_SUCCESS) {
+                       cmd_buffer->record_result = ret;
+                       goto fail_pipeline;
+               }
        }
 
        radv_CmdBindPipeline(radv_cmd_buffer_to_handle(cmd_buffer),
-                            VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
+                            VK_PIPELINE_BIND_POINT_GRAPHICS, *pipeline);
 
        radv_meta_push_descriptor_set(cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
                                      device->meta_state.blit.pipeline_layout,
@@ -471,7 +493,8 @@ meta_emit_blit(struct radv_cmd_buffer *cmd_buffer,
 
        radv_CmdDraw(radv_cmd_buffer_to_handle(cmd_buffer), 3, 1, 0, 0);
 
-       radv_CmdEndRenderPass(radv_cmd_buffer_to_handle(cmd_buffer));
+fail_pipeline:
+       radv_cmd_buffer_end_render_pass(cmd_buffer);
 
        /* At the point where we emit the draw call, all data from the
         * descriptor sets, etc. has been used.  We are free to delete it.
@@ -479,8 +502,6 @@ meta_emit_blit(struct radv_cmd_buffer *cmd_buffer,
        /* TODO: above comment is not valid for at least descriptor sets/pools,
         * as we may not free them till after execution finishes. Check others. */
 
-       radv_DestroySampler(radv_device_to_handle(device), sampler,
-                           &cmd_buffer->pool->alloc);
        radv_DestroyFramebuffer(radv_device_to_handle(device), fb,
                                &cmd_buffer->pool->alloc);
 }
@@ -519,8 +540,10 @@ void radv_CmdBlitImage(
        RADV_FROM_HANDLE(radv_cmd_buffer, cmd_buffer, commandBuffer);
        RADV_FROM_HANDLE(radv_image, src_image, srcImage);
        RADV_FROM_HANDLE(radv_image, dest_image, destImage);
+       struct radv_device *device = cmd_buffer->device;
        struct radv_meta_saved_state saved_state;
        bool old_predicating;
+       VkSampler sampler;
 
        /* From the Vulkan 1.0 spec:
         *
@@ -530,6 +553,16 @@ void radv_CmdBlitImage(
        assert(src_image->info.samples == 1);
        assert(dest_image->info.samples == 1);
 
+       radv_CreateSampler(radv_device_to_handle(device),
+                          &(VkSamplerCreateInfo) {
+                               .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
+                               .magFilter = filter,
+                               .minFilter = filter,
+                               .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
+                               .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
+                               .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
+                          }, &cmd_buffer->pool->alloc, &sampler);
+
        radv_meta_save(&saved_state, cmd_buffer,
                       RADV_META_SAVE_GRAPHICS_PIPELINE |
                       RADV_META_SAVE_CONSTANTS |
@@ -566,12 +599,19 @@ void radv_CmdBlitImage(
                }
 
                bool flip_z = flip_coords(&src_start, &src_end, &dst_start, &dst_end);
-               float src_z_step = (float)(src_end + 1 - src_start) /
-                       (float)(dst_end + 1 - dst_start);
+               float src_z_step = (float)(src_end - src_start) /
+                       (float)(dst_end - dst_start);
+
+               /* There is no interpolation to the pixel center during
+                * rendering, so add the 0.5 offset ourselves here. */
+               float depth_center_offset = 0;
+               if (src_image->type == VK_IMAGE_TYPE_3D)
+                       depth_center_offset = 0.5 / (dst_end - dst_start) * (src_end - src_start);
 
                if (flip_z) {
                        src_start = src_end;
                        src_z_step *= -1;
+                       depth_center_offset *= -1;
                }
 
                unsigned src_x0 = pRegions[r].srcOffsets[0].x;
@@ -587,8 +627,8 @@ void radv_CmdBlitImage(
                VkRect2D dest_box;
                dest_box.offset.x = MIN2(dst_x0, dst_x1);
                dest_box.offset.y = MIN2(dst_y0, dst_y1);
-               dest_box.extent.width = abs(dst_x1 - dst_x0);
-               dest_box.extent.height = abs(dst_y1 - dst_y0);
+               dest_box.extent.width = dst_x1 - dst_x0;
+               dest_box.extent.height = dst_y1 - dst_y0;
 
                const unsigned num_layers = dst_end - dst_start;
                for (unsigned i = 0; i < num_layers; i++) {
@@ -602,15 +642,16 @@ void radv_CmdBlitImage(
                                .x = dst_x1,
                                .y = dst_y1,
                        };
-                       VkOffset3D src_offset_0 = {
-                               .x = src_x0,
-                               .y = src_y0,
-                               .z = src_start + i * src_z_step,
+
+                       float src_offset_0[3] = {
+                               src_x0,
+                               src_y0,
+                               src_start + i * src_z_step + depth_center_offset,
                        };
-                       VkOffset3D src_offset_1 = {
-                               .x = src_x1,
-                               .y = src_y1,
-                               .z = src_start + i * src_z_step,
+                       float src_offset_1[3] = {
+                               src_x1,
+                               src_y1,
+                               src_start + i * src_z_step + depth_center_offset,
                        };
                        const uint32_t dest_array_slice = dst_start + i;
 
@@ -630,7 +671,7 @@ void radv_CmdBlitImage(
                                                             .baseArrayLayer = dest_array_slice,
                                                             .layerCount = 1
                                                     },
-                                            });
+                                            }, NULL);
                        radv_image_view_init(&src_iview, cmd_buffer->device,
                                             &(VkImageViewCreateInfo) {
                                                .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
@@ -644,14 +685,14 @@ void radv_CmdBlitImage(
                                                        .baseArrayLayer = src_array_slice,
                                                        .layerCount = 1
                                                },
-                                       });
+                                       }, NULL);
                        meta_emit_blit(cmd_buffer,
                                       src_image, &src_iview, srcImageLayout,
                                       src_offset_0, src_offset_1,
                                       dest_image, &dest_iview, destImageLayout,
                                       dest_offset_0, dest_offset_1,
                                       dest_box,
-                                      filter);
+                                      sampler);
                }
        }
 
@@ -659,6 +700,9 @@ void radv_CmdBlitImage(
        cmd_buffer->state.predicating = old_predicating;
 
        radv_meta_restore(&saved_state, cmd_buffer);
+
+       radv_DestroySampler(radv_device_to_handle(device), sampler,
+                           &cmd_buffer->pool->alloc);
 }
 
 void
@@ -715,15 +759,184 @@ radv_device_finish_meta_blit_state(struct radv_device *device)
 }
 
 static VkResult
-radv_device_init_meta_blit_color(struct radv_device *device,
-                                struct radv_shader_module *vs)
+build_pipeline(struct radv_device *device,
+               VkImageAspectFlagBits aspect,
+               enum glsl_sampler_dim tex_dim,
+               unsigned fs_key,
+               VkPipeline *pipeline)
 {
-       struct radv_shader_module fs_1d = {0}, fs_2d = {0}, fs_3d = {0};
-       VkResult result;
+       VkResult result = VK_SUCCESS;
+
+       mtx_lock(&device->meta_state.mtx);
+
+       if (*pipeline) {
+               mtx_unlock(&device->meta_state.mtx);
+               return VK_SUCCESS;
+       }
+
+       struct radv_shader_module fs = {0};
+       struct radv_shader_module vs = {.nir = build_nir_vertex_shader()};
+       VkRenderPass rp;
+
+       switch(aspect) {
+       case VK_IMAGE_ASPECT_COLOR_BIT:
+               fs.nir = build_nir_copy_fragment_shader(tex_dim);
+               rp = device->meta_state.blit.render_pass[fs_key][0];
+               break;
+       case VK_IMAGE_ASPECT_DEPTH_BIT:
+               fs.nir = build_nir_copy_fragment_shader_depth(tex_dim);
+               rp = device->meta_state.blit.depth_only_rp[0];
+               break;
+       case VK_IMAGE_ASPECT_STENCIL_BIT:
+               fs.nir = build_nir_copy_fragment_shader_stencil(tex_dim);
+               rp = device->meta_state.blit.stencil_only_rp[0];
+               break;
+       default:
+               unreachable("Unhandled aspect");
+       }
+       VkPipelineVertexInputStateCreateInfo vi_create_info = {
+               .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
+               .vertexBindingDescriptionCount = 0,
+               .vertexAttributeDescriptionCount = 0,
+       };
+
+       VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
+               {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
+                       .stage = VK_SHADER_STAGE_VERTEX_BIT,
+                       .module = radv_shader_module_to_handle(&vs),
+                       .pName = "main",
+                       .pSpecializationInfo = NULL
+               }, {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
+                       .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
+                       .module = radv_shader_module_to_handle(&fs),
+                       .pName = "main",
+                       .pSpecializationInfo = NULL
+               },
+       };
+
+       VkGraphicsPipelineCreateInfo vk_pipeline_info = {
+               .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
+               .stageCount = ARRAY_SIZE(pipeline_shader_stages),
+               .pStages = pipeline_shader_stages,
+               .pVertexInputState = &vi_create_info,
+               .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
+                       .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
+                       .primitiveRestartEnable = false,
+               },
+               .pViewportState = &(VkPipelineViewportStateCreateInfo) {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
+                       .viewportCount = 1,
+                       .scissorCount = 1,
+               },
+               .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
+                       .rasterizerDiscardEnable = false,
+                       .polygonMode = VK_POLYGON_MODE_FILL,
+                       .cullMode = VK_CULL_MODE_NONE,
+                       .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
+               },
+               .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
+                       .rasterizationSamples = 1,
+                       .sampleShadingEnable = false,
+                       .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
+               },
+               .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
+                       .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
+                       .dynamicStateCount = 4,
+                       .pDynamicStates = (VkDynamicState[]) {
+                               VK_DYNAMIC_STATE_VIEWPORT,
+                               VK_DYNAMIC_STATE_SCISSOR,
+                               VK_DYNAMIC_STATE_LINE_WIDTH,
+                               VK_DYNAMIC_STATE_BLEND_CONSTANTS,
+                       },
+               },
+               .flags = 0,
+               .layout = device->meta_state.blit.pipeline_layout,
+               .renderPass = rp,
+               .subpass = 0,
+       };
+
+       VkPipelineColorBlendStateCreateInfo color_blend_info = {
+               .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
+               .attachmentCount = 1,
+               .pAttachments = (VkPipelineColorBlendAttachmentState []) {
+                       {
+                               .colorWriteMask = VK_COLOR_COMPONENT_A_BIT |
+                                                 VK_COLOR_COMPONENT_R_BIT |
+                                                 VK_COLOR_COMPONENT_G_BIT |
+                                                 VK_COLOR_COMPONENT_B_BIT },
+                       }
+               };
 
-       fs_1d.nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_1D);
-       fs_2d.nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_2D);
-       fs_3d.nir = build_nir_copy_fragment_shader(GLSL_SAMPLER_DIM_3D);
+       VkPipelineDepthStencilStateCreateInfo depth_info = {
+               .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
+               .depthTestEnable = true,
+               .depthWriteEnable = true,
+               .depthCompareOp = VK_COMPARE_OP_ALWAYS,
+       };
+
+       VkPipelineDepthStencilStateCreateInfo stencil_info = {
+               .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
+               .depthTestEnable = false,
+               .depthWriteEnable = false,
+               .stencilTestEnable = true,
+               .front = {
+                       .failOp = VK_STENCIL_OP_REPLACE,
+                       .passOp = VK_STENCIL_OP_REPLACE,
+                       .depthFailOp = VK_STENCIL_OP_REPLACE,
+                       .compareOp = VK_COMPARE_OP_ALWAYS,
+                       .compareMask = 0xff,
+                       .writeMask = 0xff,
+                       .reference = 0
+               },
+               .back = {
+                       .failOp = VK_STENCIL_OP_REPLACE,
+                       .passOp = VK_STENCIL_OP_REPLACE,
+                       .depthFailOp = VK_STENCIL_OP_REPLACE,
+                       .compareOp = VK_COMPARE_OP_ALWAYS,
+                       .compareMask = 0xff,
+                       .writeMask = 0xff,
+                       .reference = 0
+               },
+               .depthCompareOp = VK_COMPARE_OP_ALWAYS,
+       };
+
+       switch(aspect) {
+       case VK_IMAGE_ASPECT_COLOR_BIT:
+               vk_pipeline_info.pColorBlendState = &color_blend_info;
+               break;
+       case VK_IMAGE_ASPECT_DEPTH_BIT:
+               vk_pipeline_info.pDepthStencilState = &depth_info;
+               break;
+       case VK_IMAGE_ASPECT_STENCIL_BIT:
+               vk_pipeline_info.pDepthStencilState = &stencil_info;
+               break;
+       default:
+               unreachable("Unhandled aspect");
+       }
+
+       const struct radv_graphics_pipeline_create_info radv_pipeline_info = {
+               .use_rectlist = true
+       };
+
+       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
+                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
+                                              &vk_pipeline_info, &radv_pipeline_info,
+                                              &device->meta_state.alloc, pipeline);
+       ralloc_free(vs.nir);
+       ralloc_free(fs.nir);
+       mtx_unlock(&device->meta_state.mtx);
+       return result;
+}
+
+static VkResult
+radv_device_init_meta_blit_color(struct radv_device *device, bool on_demand)
+{
+       VkResult result;
 
        for (unsigned i = 0; i < NUM_META_FS_KEYS; ++i) {
                unsigned key = radv_format_meta_fs_key(radv_fs_key_format_exemplars[i]);
@@ -754,117 +967,47 @@ radv_device_init_meta_blit_color(struct radv_device *device,
                                                                        .attachment = VK_ATTACHMENT_UNUSED,
                                                                        .layout = VK_IMAGE_LAYOUT_GENERAL,
                                                                },
-                                                               .preserveAttachmentCount = 1,
-                                                               .pPreserveAttachments = (uint32_t[]) { 0 },
+                                                               .preserveAttachmentCount = 0,
+                                                               .pPreserveAttachments = NULL,
+                                                       },
+                                                       .dependencyCount = 2,
+                                                       .pDependencies = (VkSubpassDependency[]) {
+                                                               {
+                                                                       .srcSubpass = VK_SUBPASS_EXTERNAL,
+                                                                       .dstSubpass = 0,
+                                                                       .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                                                                       .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
+                                                                       .srcAccessMask = 0,
+                                                                       .dstAccessMask = 0,
+                                                                       .dependencyFlags = 0
+                                                               },
+                                                               {
+                                                                       .srcSubpass = 0,
+                                                                       .dstSubpass = VK_SUBPASS_EXTERNAL,
+                                                                       .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                                                                       .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
+                                                                       .srcAccessMask = 0,
+                                                                       .dstAccessMask = 0,
+                                                                       .dependencyFlags = 0
+                                                               }
                                                        },
-                                                       .dependencyCount = 0,
                                                }, &device->meta_state.alloc, &device->meta_state.blit.render_pass[key][j]);
                        if (result != VK_SUCCESS)
                                goto fail;
                }
 
-               VkPipelineVertexInputStateCreateInfo vi_create_info = {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
-                       .vertexBindingDescriptionCount = 0,
-                       .vertexAttributeDescriptionCount = 0,
-               };
-
-               VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
-                       {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-                               .stage = VK_SHADER_STAGE_VERTEX_BIT,
-                               .module = radv_shader_module_to_handle(vs),
-                               .pName = "main",
-                               .pSpecializationInfo = NULL
-                       }, {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-                               .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
-                               .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
-                               .pName = "main",
-                               .pSpecializationInfo = NULL
-                       },
-               };
-
-               const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
-                       .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
-                       .stageCount = ARRAY_SIZE(pipeline_shader_stages),
-                       .pStages = pipeline_shader_stages,
-                       .pVertexInputState = &vi_create_info,
-                       .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
-                               .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
-                               .primitiveRestartEnable = false,
-                       },
-                       .pViewportState = &(VkPipelineViewportStateCreateInfo) {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
-                               .viewportCount = 1,
-                               .scissorCount = 1,
-                       },
-                       .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
-                               .rasterizerDiscardEnable = false,
-                               .polygonMode = VK_POLYGON_MODE_FILL,
-                               .cullMode = VK_CULL_MODE_NONE,
-                               .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
-                       },
-                       .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
-                               .rasterizationSamples = 1,
-                               .sampleShadingEnable = false,
-                               .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
-                       },
-                       .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
-                               .attachmentCount = 1,
-                               .pAttachments = (VkPipelineColorBlendAttachmentState []) {
-                                       { .colorWriteMask =
-                                       VK_COLOR_COMPONENT_A_BIT |
-                                       VK_COLOR_COMPONENT_R_BIT |
-                                       VK_COLOR_COMPONENT_G_BIT |
-                                       VK_COLOR_COMPONENT_B_BIT },
-                               }
-                       },
-                       .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
-                               .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
-                               .dynamicStateCount = 4,
-                               .pDynamicStates = (VkDynamicState[]) {
-                                       VK_DYNAMIC_STATE_VIEWPORT,
-                                       VK_DYNAMIC_STATE_SCISSOR,
-                                       VK_DYNAMIC_STATE_LINE_WIDTH,
-                                       VK_DYNAMIC_STATE_BLEND_CONSTANTS,
-                               },
-                       },
-                       .flags = 0,
-                       .layout = device->meta_state.blit.pipeline_layout,
-                       .renderPass = device->meta_state.blit.render_pass[key][0],
-                       .subpass = 0,
-               };
-
-               const struct radv_graphics_pipeline_create_info radv_pipeline_info = {
-                       .use_rectlist = true
-               };
+               if (on_demand)
+                       continue;
 
-               pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_1d);
-               result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                               radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                               &vk_pipeline_info, &radv_pipeline_info,
-                                               &device->meta_state.alloc, &device->meta_state.blit.pipeline_1d_src[key]);
+               result = build_pipeline(device, VK_IMAGE_ASPECT_COLOR_BIT, GLSL_SAMPLER_DIM_1D, key, &device->meta_state.blit.pipeline_1d_src[key]);
                if (result != VK_SUCCESS)
                        goto fail;
 
-               pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_2d);
-               result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                               radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                               &vk_pipeline_info, &radv_pipeline_info,
-                                               &device->meta_state.alloc, &device->meta_state.blit.pipeline_2d_src[key]);
+               result = build_pipeline(device, VK_IMAGE_ASPECT_COLOR_BIT, GLSL_SAMPLER_DIM_2D, key, &device->meta_state.blit.pipeline_2d_src[key]);
                if (result != VK_SUCCESS)
                        goto fail;
 
-               pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_3d);
-               result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                               radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                               &vk_pipeline_info, &radv_pipeline_info,
-                                               &device->meta_state.alloc, &device->meta_state.blit.pipeline_3d_src[key]);
+               result = build_pipeline(device, VK_IMAGE_ASPECT_COLOR_BIT, GLSL_SAMPLER_DIM_3D, key, &device->meta_state.blit.pipeline_3d_src[key]);
                if (result != VK_SUCCESS)
                        goto fail;
 
@@ -872,23 +1015,14 @@ radv_device_init_meta_blit_color(struct radv_device *device,
 
        result = VK_SUCCESS;
 fail:
-       ralloc_free(fs_1d.nir);
-       ralloc_free(fs_2d.nir);
-       ralloc_free(fs_3d.nir);
        return result;
 }
 
 static VkResult
-radv_device_init_meta_blit_depth(struct radv_device *device,
-                                struct radv_shader_module *vs)
+radv_device_init_meta_blit_depth(struct radv_device *device, bool on_demand)
 {
-       struct radv_shader_module fs_1d = {0}, fs_2d = {0}, fs_3d = {0};
        VkResult result;
 
-       fs_1d.nir = build_nir_copy_fragment_shader_depth(GLSL_SAMPLER_DIM_1D);
-       fs_2d.nir = build_nir_copy_fragment_shader_depth(GLSL_SAMPLER_DIM_2D);
-       fs_3d.nir = build_nir_copy_fragment_shader_depth(GLSL_SAMPLER_DIM_3D);
-
        for (enum radv_blit_ds_layout ds_layout = RADV_BLIT_DS_LAYOUT_TILE_ENABLE; ds_layout < RADV_BLIT_DS_LAYOUT_COUNT; ds_layout++) {
                VkImageLayout layout = radv_meta_blit_ds_to_layout(ds_layout);
                result = radv_CreateRenderPass(radv_device_to_handle(device),
@@ -913,143 +1047,59 @@ radv_device_init_meta_blit_depth(struct radv_device *device,
                                                                       .attachment = 0,
                                                                       .layout = layout,
                                                                },
-                                                              .preserveAttachmentCount = 1,
-                                                              .pPreserveAttachments = (uint32_t[]) { 0 },
+                                                              .preserveAttachmentCount = 0,
+                                                              .pPreserveAttachments = NULL,
+                                                       },
+                                                       .dependencyCount = 2,
+                                                       .pDependencies = (VkSubpassDependency[]) {
+                                                               {
+                                                                       .srcSubpass = VK_SUBPASS_EXTERNAL,
+                                                                       .dstSubpass = 0,
+                                                                       .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                                                                       .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
+                                                                       .srcAccessMask = 0,
+                                                                       .dstAccessMask = 0,
+                                                                       .dependencyFlags = 0
+                                                               },
+                                                               {
+                                                                       .srcSubpass = 0,
+                                                                       .dstSubpass = VK_SUBPASS_EXTERNAL,
+                                                                       .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                                                                       .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
+                                                                       .srcAccessMask = 0,
+                                                                       .dstAccessMask = 0,
+                                                                       .dependencyFlags = 0
+                                                               }
                                                        },
-                                                       .dependencyCount = 0,
                                                }, &device->meta_state.alloc, &device->meta_state.blit.depth_only_rp[ds_layout]);
                if (result != VK_SUCCESS)
                        goto fail;
        }
 
-       VkPipelineVertexInputStateCreateInfo vi_create_info = {
-               .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
-               .vertexBindingDescriptionCount = 0,
-               .vertexAttributeDescriptionCount = 0,
-       };
+       if (on_demand)
+               return VK_SUCCESS;
 
-       VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
-               {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-                       .stage = VK_SHADER_STAGE_VERTEX_BIT,
-                       .module = radv_shader_module_to_handle(vs),
-                       .pName = "main",
-                       .pSpecializationInfo = NULL
-               }, {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-                       .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
-                       .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
-                       .pName = "main",
-                       .pSpecializationInfo = NULL
-               },
-       };
-
-       const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
-               .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
-               .stageCount = ARRAY_SIZE(pipeline_shader_stages),
-               .pStages = pipeline_shader_stages,
-               .pVertexInputState = &vi_create_info,
-               .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
-                       .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
-                       .primitiveRestartEnable = false,
-               },
-               .pViewportState = &(VkPipelineViewportStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
-                       .viewportCount = 1,
-                       .scissorCount = 1,
-               },
-               .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
-                       .rasterizerDiscardEnable = false,
-                       .polygonMode = VK_POLYGON_MODE_FILL,
-                       .cullMode = VK_CULL_MODE_NONE,
-                       .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
-               },
-               .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
-                       .rasterizationSamples = 1,
-                       .sampleShadingEnable = false,
-                       .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
-               },
-               .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
-                       .attachmentCount = 0,
-                       .pAttachments = NULL,
-               },
-               .pDepthStencilState = &(VkPipelineDepthStencilStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
-                       .depthTestEnable = true,
-                       .depthWriteEnable = true,
-                       .depthCompareOp = VK_COMPARE_OP_ALWAYS,
-               },
-               .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
-                       .dynamicStateCount = 9,
-                       .pDynamicStates = (VkDynamicState[]) {
-                               VK_DYNAMIC_STATE_VIEWPORT,
-                               VK_DYNAMIC_STATE_SCISSOR,
-                               VK_DYNAMIC_STATE_LINE_WIDTH,
-                               VK_DYNAMIC_STATE_DEPTH_BIAS,
-                               VK_DYNAMIC_STATE_BLEND_CONSTANTS,
-                               VK_DYNAMIC_STATE_DEPTH_BOUNDS,
-                               VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK,
-                               VK_DYNAMIC_STATE_STENCIL_WRITE_MASK,
-                               VK_DYNAMIC_STATE_STENCIL_REFERENCE,
-                       },
-               },
-               .flags = 0,
-               .layout = device->meta_state.blit.pipeline_layout,
-               .renderPass = device->meta_state.blit.depth_only_rp[0],
-               .subpass = 0,
-       };
-
-       const struct radv_graphics_pipeline_create_info radv_pipeline_info = {
-               .use_rectlist = true
-       };
-
-       pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_1d);
-       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                              &vk_pipeline_info, &radv_pipeline_info,
-                                              &device->meta_state.alloc, &device->meta_state.blit.depth_only_1d_pipeline);
+       result = build_pipeline(device, VK_IMAGE_ASPECT_DEPTH_BIT, GLSL_SAMPLER_DIM_1D, 0, &device->meta_state.blit.depth_only_1d_pipeline);
        if (result != VK_SUCCESS)
                goto fail;
 
-       pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_2d);
-       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                              &vk_pipeline_info, &radv_pipeline_info,
-                                              &device->meta_state.alloc, &device->meta_state.blit.depth_only_2d_pipeline);
+       result = build_pipeline(device, VK_IMAGE_ASPECT_DEPTH_BIT, GLSL_SAMPLER_DIM_2D, 0, &device->meta_state.blit.depth_only_2d_pipeline);
        if (result != VK_SUCCESS)
                goto fail;
 
-       pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_3d);
-       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                              &vk_pipeline_info, &radv_pipeline_info,
-                                              &device->meta_state.alloc, &device->meta_state.blit.depth_only_3d_pipeline);
+       result = build_pipeline(device, VK_IMAGE_ASPECT_DEPTH_BIT, GLSL_SAMPLER_DIM_3D, 0, &device->meta_state.blit.depth_only_3d_pipeline);
        if (result != VK_SUCCESS)
                goto fail;
 
 fail:
-       ralloc_free(fs_1d.nir);
-       ralloc_free(fs_2d.nir);
-       ralloc_free(fs_3d.nir);
        return result;
 }
 
 static VkResult
-radv_device_init_meta_blit_stencil(struct radv_device *device,
-                                  struct radv_shader_module *vs)
+radv_device_init_meta_blit_stencil(struct radv_device *device, bool on_demand)
 {
-       struct radv_shader_module fs_1d = {0}, fs_2d = {0}, fs_3d = {0};
        VkResult result;
 
-       fs_1d.nir = build_nir_copy_fragment_shader_stencil(GLSL_SAMPLER_DIM_1D);
-       fs_2d.nir = build_nir_copy_fragment_shader_stencil(GLSL_SAMPLER_DIM_2D);
-       fs_3d.nir = build_nir_copy_fragment_shader_stencil(GLSL_SAMPLER_DIM_3D);
-
        for (enum radv_blit_ds_layout ds_layout = RADV_BLIT_DS_LAYOUT_TILE_ENABLE; ds_layout < RADV_BLIT_DS_LAYOUT_COUNT; ds_layout++) {
                VkImageLayout layout = radv_meta_blit_ds_to_layout(ds_layout);
                result = radv_CreateRenderPass(radv_device_to_handle(device),
@@ -1074,154 +1124,60 @@ radv_device_init_meta_blit_stencil(struct radv_device *device,
                                                                       .attachment = 0,
                                                                       .layout = layout,
                                                               },
-                                                              .preserveAttachmentCount = 1,
-                                                              .pPreserveAttachments = (uint32_t[]) { 0 },
+                                                              .preserveAttachmentCount = 0,
+                                                              .pPreserveAttachments = NULL,
                                                       },
-                                                      .dependencyCount = 0,
+                                                      .dependencyCount = 2,
+                                                      .pDependencies = (VkSubpassDependency[]) {
+                                                               {
+                                                                       .srcSubpass = VK_SUBPASS_EXTERNAL,
+                                                                       .dstSubpass = 0,
+                                                                       .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                                                                       .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
+                                                                       .srcAccessMask = 0,
+                                                                       .dstAccessMask = 0,
+                                                                       .dependencyFlags = 0
+                                                               },
+                                                               {
+                                                                       .srcSubpass = 0,
+                                                                       .dstSubpass = VK_SUBPASS_EXTERNAL,
+                                                                       .srcStageMask = VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
+                                                                       .dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT,
+                                                                       .srcAccessMask = 0,
+                                                                       .dstAccessMask = 0,
+                                                                       .dependencyFlags = 0
+                                                               }
+                                                       },
+
                                         }, &device->meta_state.alloc, &device->meta_state.blit.stencil_only_rp[ds_layout]);
        }
        if (result != VK_SUCCESS)
                goto fail;
 
-       VkPipelineVertexInputStateCreateInfo vi_create_info = {
-               .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
-               .vertexBindingDescriptionCount = 0,
-               .vertexAttributeDescriptionCount = 0,
-       };
-
-       VkPipelineShaderStageCreateInfo pipeline_shader_stages[] = {
-               {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-                       .stage = VK_SHADER_STAGE_VERTEX_BIT,
-                       .module = radv_shader_module_to_handle(vs),
-                       .pName = "main",
-                       .pSpecializationInfo = NULL
-               }, {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-                       .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
-                       .module = VK_NULL_HANDLE, /* TEMPLATE VALUE! FILL ME IN! */
-                       .pName = "main",
-                       .pSpecializationInfo = NULL
-               },
-       };
-
-       const VkGraphicsPipelineCreateInfo vk_pipeline_info = {
-               .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
-               .stageCount = ARRAY_SIZE(pipeline_shader_stages),
-               .pStages = pipeline_shader_stages,
-               .pVertexInputState = &vi_create_info,
-               .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
-                       .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
-                       .primitiveRestartEnable = false,
-               },
-               .pViewportState = &(VkPipelineViewportStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
-                       .viewportCount = 1,
-                       .scissorCount = 1,
-               },
-               .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
-                       .rasterizerDiscardEnable = false,
-                       .polygonMode = VK_POLYGON_MODE_FILL,
-                       .cullMode = VK_CULL_MODE_NONE,
-                       .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE
-               },
-               .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
-                       .rasterizationSamples = 1,
-                       .sampleShadingEnable = false,
-                       .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
-               },
-               .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
-                       .attachmentCount = 0,
-                       .pAttachments = NULL,
-               },
-               .pDepthStencilState = &(VkPipelineDepthStencilStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
-                       .depthTestEnable = false,
-                       .depthWriteEnable = false,
-                       .stencilTestEnable = true,
-                       .front = {
-                               .failOp = VK_STENCIL_OP_REPLACE,
-                               .passOp = VK_STENCIL_OP_REPLACE,
-                               .depthFailOp = VK_STENCIL_OP_REPLACE,
-                               .compareOp = VK_COMPARE_OP_ALWAYS,
-                               .compareMask = 0xff,
-                               .writeMask = 0xff,
-                               .reference = 0
-                       },
-                       .back = {
-                               .failOp = VK_STENCIL_OP_REPLACE,
-                               .passOp = VK_STENCIL_OP_REPLACE,
-                               .depthFailOp = VK_STENCIL_OP_REPLACE,
-                               .compareOp = VK_COMPARE_OP_ALWAYS,
-                               .compareMask = 0xff,
-                               .writeMask = 0xff,
-                               .reference = 0
-                       },
-                       .depthCompareOp = VK_COMPARE_OP_ALWAYS,
-               },
-               .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
-                       .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
-                       .dynamicStateCount = 6,
-                       .pDynamicStates = (VkDynamicState[]) {
-                               VK_DYNAMIC_STATE_VIEWPORT,
-                               VK_DYNAMIC_STATE_SCISSOR,
-                               VK_DYNAMIC_STATE_LINE_WIDTH,
-                               VK_DYNAMIC_STATE_DEPTH_BIAS,
-                               VK_DYNAMIC_STATE_BLEND_CONSTANTS,
-                               VK_DYNAMIC_STATE_DEPTH_BOUNDS,
-                       },
-               },
-               .flags = 0,
-               .layout = device->meta_state.blit.pipeline_layout,
-               .renderPass = device->meta_state.blit.stencil_only_rp[0],
-               .subpass = 0,
-       };
+       if (on_demand)
+               return VK_SUCCESS;
 
-       const struct radv_graphics_pipeline_create_info radv_pipeline_info = {
-               .use_rectlist = true
-       };
-
-       pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_1d);
-       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                              &vk_pipeline_info, &radv_pipeline_info,
-                                              &device->meta_state.alloc, &device->meta_state.blit.stencil_only_1d_pipeline);
+       result = build_pipeline(device, VK_IMAGE_ASPECT_STENCIL_BIT, GLSL_SAMPLER_DIM_1D, 0, &device->meta_state.blit.stencil_only_1d_pipeline);
        if (result != VK_SUCCESS)
                goto fail;
 
-       pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_2d);
-       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                              &vk_pipeline_info, &radv_pipeline_info,
-                                              &device->meta_state.alloc, &device->meta_state.blit.stencil_only_2d_pipeline);
+       result = build_pipeline(device, VK_IMAGE_ASPECT_STENCIL_BIT, GLSL_SAMPLER_DIM_2D, 0, &device->meta_state.blit.stencil_only_2d_pipeline);
        if (result != VK_SUCCESS)
                goto fail;
 
-       pipeline_shader_stages[1].module = radv_shader_module_to_handle(&fs_3d);
-       result = radv_graphics_pipeline_create(radv_device_to_handle(device),
-                                              radv_pipeline_cache_to_handle(&device->meta_state.cache),
-                                              &vk_pipeline_info, &radv_pipeline_info,
-                                              &device->meta_state.alloc, &device->meta_state.blit.stencil_only_3d_pipeline);
+       result = build_pipeline(device, VK_IMAGE_ASPECT_STENCIL_BIT, GLSL_SAMPLER_DIM_3D, 0, &device->meta_state.blit.stencil_only_3d_pipeline);
        if (result != VK_SUCCESS)
                goto fail;
 
 
 fail:
-       ralloc_free(fs_1d.nir);
-       ralloc_free(fs_2d.nir);
-       ralloc_free(fs_3d.nir);
        return result;
 }
 
 VkResult
-radv_device_init_meta_blit_state(struct radv_device *device)
+radv_device_init_meta_blit_state(struct radv_device *device, bool on_demand)
 {
        VkResult result;
-       struct radv_shader_module vs = {0};
 
        VkDescriptorSetLayoutCreateInfo ds_layout_info = {
                .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
@@ -1258,20 +1214,17 @@ radv_device_init_meta_blit_state(struct radv_device *device)
        if (result != VK_SUCCESS)
                goto fail;
 
-       vs.nir = build_nir_vertex_shader();
-
-       result = radv_device_init_meta_blit_color(device, &vs);
+       result = radv_device_init_meta_blit_color(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail;
 
-       result = radv_device_init_meta_blit_depth(device, &vs);
+       result = radv_device_init_meta_blit_depth(device, on_demand);
        if (result != VK_SUCCESS)
                goto fail;
 
-       result = radv_device_init_meta_blit_stencil(device, &vs);
+       result = radv_device_init_meta_blit_stencil(device, on_demand);
 
 fail:
-       ralloc_free(vs.nir);
        if (result != VK_SUCCESS)
                radv_device_finish_meta_blit_state(device);
        return result;