anv/pipeline: Handle output lowering in anv_pipeline instead of spirv_to_nir
[mesa.git] / src / vulkan / anv_meta_clear.c
index 8f217105b4ff9aeacdc771530d972159c8af40b7..1a4300c07e329440b9359a75f830d0fa1171c721 100644 (file)
 
 #include "anv_meta.h"
 #include "anv_meta_clear.h"
-#include "anv_nir_builder.h"
 #include "anv_private.h"
+#include "glsl/nir/nir_builder.h"
 
-struct clear_instance_data {
+/** Vertex attributes for color clears.  */
+struct color_clear_vattrs {
    struct anv_vue_header vue_header;
+   float position[2]; /**< 3DPRIM_RECTLIST */
    VkClearColorValue color;
 };
 
-static void
-meta_emit_clear(struct anv_cmd_buffer *cmd_buffer,
-                int num_instances,
-                struct clear_instance_data *instance_data,
-                VkClearDepthStencilValue ds_clear_value)
-{
-   struct anv_device *device = cmd_buffer->device;
-   struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
-   struct anv_state state;
-   uint32_t size;
-
-   const float vertex_data[] = {
-      /* Rect-list coordinates */
-            0.0,        0.0, ds_clear_value.depth,
-      fb->width,        0.0, ds_clear_value.depth,
-      fb->width, fb->height, ds_clear_value.depth,
-
-      /* Align to 16 bytes */
-      0.0, 0.0, 0.0,
-   };
-
-   size = sizeof(vertex_data) + num_instances * sizeof(*instance_data);
-   state = anv_cmd_buffer_alloc_dynamic_state(cmd_buffer, size, 16);
-
-   /* Copy in the vertex and instance data */
-   memcpy(state.map, vertex_data, sizeof(vertex_data));
-   memcpy(state.map + sizeof(vertex_data), instance_data,
-          num_instances * sizeof(*instance_data));
-
-   struct anv_buffer vertex_buffer = {
-      .device = cmd_buffer->device,
-      .size = size,
-      .bo = &device->dynamic_state_block_pool.bo,
-      .offset = state.offset
-   };
-
-   anv_CmdBindVertexBuffers(anv_cmd_buffer_to_handle(cmd_buffer), 0, 2,
-      (VkBuffer[]) {
-         anv_buffer_to_handle(&vertex_buffer),
-         anv_buffer_to_handle(&vertex_buffer)
-      },
-      (VkDeviceSize[]) {
-         0,
-         sizeof(vertex_data)
-      });
-
-   if (cmd_buffer->state.pipeline != anv_pipeline_from_handle(device->meta_state.clear.pipeline))
-      anv_CmdBindPipeline(anv_cmd_buffer_to_handle(cmd_buffer),
-                          VK_PIPELINE_BIND_POINT_GRAPHICS,
-                          device->meta_state.clear.pipeline);
-
-   ANV_CALL(CmdDraw)(anv_cmd_buffer_to_handle(cmd_buffer),
-                     3, num_instances, 0, 0);
-}
+/** Vertex attributes for depthstencil clears.  */
+struct depthstencil_clear_vattrs {
+   struct anv_vue_header vue_header;
+   float position[2]; /*<< 3DPRIM_RECTLIST */
+};
 
-void
-anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
-                                 struct anv_render_pass *pass,
-                                 const VkClearValue *clear_values)
+static void
+meta_clear_begin(struct anv_meta_saved_state *saved_state,
+                 struct anv_cmd_buffer *cmd_buffer)
 {
-   struct anv_meta_saved_state saved_state;
-
-   if (pass->has_stencil_clear_attachment)
-      anv_finishme("stencil clear");
-
-   /* FINISHME: Rethink how we count clear attachments in light of
-    * 0.138.2 -> 0.170.2 diff.
-    */
-   if (pass->num_color_clear_attachments == 0 &&
-       !pass->has_depth_clear_attachment)
-      return;
-
-   struct clear_instance_data instance_data[pass->num_color_clear_attachments];
-   uint32_t color_attachments[pass->num_color_clear_attachments];
-   uint32_t ds_attachment = VK_ATTACHMENT_UNUSED;
-   VkClearDepthStencilValue ds_clear_value = {0};
-
-   int layer = 0;
-   for (uint32_t i = 0; i < pass->attachment_count; i++) {
-      const struct anv_render_pass_attachment *att = &pass->attachments[i];
-
-      if (anv_format_is_color(att->format)) {
-         if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
-            instance_data[layer] = (struct clear_instance_data) {
-               .vue_header = {
-                  .RTAIndex = i,
-                  .ViewportIndex = 0,
-                  .PointWidth = 0.0
-               },
-               .color = clear_values[i].color,
-            };
-            color_attachments[layer] = i;
-            layer++;
-         }
-      } else {
-         if (att->format->depth_format &&
-             att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
-            assert(ds_attachment == VK_ATTACHMENT_UNUSED);
-            ds_attachment = i;
-            ds_clear_value = clear_values[ds_attachment].depthStencil;
-         }
+   anv_meta_save(saved_state, cmd_buffer,
+                 (1 << VK_DYNAMIC_STATE_VIEWPORT) |
+                 (1 << VK_DYNAMIC_STATE_SCISSOR) |
+                 (1 << VK_DYNAMIC_STATE_STENCIL_REFERENCE));
 
-         if (att->format->has_stencil &&
-             att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
-            anv_finishme("stencil clear");
-         }
-      }
-   }
-
-   anv_meta_save(&saved_state, cmd_buffer,
-                 (1 << VK_DYNAMIC_STATE_VIEWPORT));
    cmd_buffer->state.dynamic.viewport.count = 0;
-
-   struct anv_subpass subpass = {
-      .input_count = 0,
-      .color_count = pass->num_color_clear_attachments,
-      .color_attachments = color_attachments,
-      .depth_stencil_attachment = ds_attachment,
-   };
-
-   anv_cmd_buffer_begin_subpass(cmd_buffer, &subpass);
-
-   meta_emit_clear(cmd_buffer, pass->num_color_clear_attachments,
-                   instance_data, ds_clear_value);
-
-   anv_meta_restore(&saved_state, cmd_buffer);
+   cmd_buffer->state.dynamic.scissor.count = 0;
 }
 
-static nir_shader *
-build_nir_vertex_shader(bool attr_flat)
+static void
+meta_clear_end(struct anv_meta_saved_state *saved_state,
+               struct anv_cmd_buffer *cmd_buffer)
 {
-   nir_builder b;
-
-   const struct glsl_type *vertex_type = glsl_vec4_type();
-
-   nir_builder_init_simple_shader(&b, MESA_SHADER_VERTEX);
-
-   nir_variable *pos_in = nir_variable_create(b.shader, nir_var_shader_in,
-                                              vertex_type, "a_pos");
-   pos_in->data.location = VERT_ATTRIB_GENERIC0;
-   nir_variable *pos_out = nir_variable_create(b.shader, nir_var_shader_out,
-                                               vertex_type, "gl_Position");
-   pos_in->data.location = VARYING_SLOT_POS;
-   nir_copy_var(&b, pos_out, pos_in);
-
-   /* Add one more pass-through attribute.  For clear shaders, this is used
-    * to store the color and for blit shaders it's the texture coordinate.
-    */
-   const struct glsl_type *attr_type = glsl_vec4_type();
-   nir_variable *attr_in = nir_variable_create(b.shader, nir_var_shader_in,
-                                               attr_type, "a_attr");
-   attr_in->data.location = VERT_ATTRIB_GENERIC1;
-   nir_variable *attr_out = nir_variable_create(b.shader, nir_var_shader_out,
-                                                attr_type, "v_attr");
-   attr_out->data.location = VARYING_SLOT_VAR0;
-   attr_out->data.interpolation = attr_flat ? INTERP_QUALIFIER_FLAT :
-                                              INTERP_QUALIFIER_SMOOTH;
-   nir_copy_var(&b, attr_out, attr_in);
-
-   return b.shader;
+   anv_meta_restore(saved_state, cmd_buffer);
 }
 
-static nir_shader *
-build_nir_clear_fragment_shader(void)
+static void
+build_color_shaders(struct nir_shader **out_vs,
+                    struct nir_shader **out_fs)
 {
-   nir_builder b;
-
-   const struct glsl_type *color_type = glsl_vec4_type();
+   nir_builder vs_b;
+   nir_builder fs_b;
 
-   nir_builder_init_simple_shader(&b, MESA_SHADER_FRAGMENT);
+   nir_builder_init_simple_shader(&vs_b, NULL, MESA_SHADER_VERTEX, NULL);
+   nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
 
-   nir_variable *color_in = nir_variable_create(b.shader, nir_var_shader_in,
-                                                color_type, "v_attr");
-   color_in->data.location = VARYING_SLOT_VAR0;
-   color_in->data.interpolation = INTERP_QUALIFIER_FLAT;
-   nir_variable *color_out = nir_variable_create(b.shader, nir_var_shader_out,
-                                                 color_type, "f_color");
-   color_out->data.location = FRAG_RESULT_DATA0;
-   nir_copy_var(&b, color_out, color_in);
+   const struct glsl_type *position_type = glsl_vec4_type();
+   const struct glsl_type *color_type = glsl_vec4_type();
 
-   return b.shader;
+   nir_variable *vs_in_pos =
+      nir_variable_create(vs_b.shader, nir_var_shader_in, position_type,
+                          "a_position");
+   vs_in_pos->data.location = VERT_ATTRIB_GENERIC0;
+
+   nir_variable *vs_out_pos =
+      nir_variable_create(vs_b.shader, nir_var_shader_out, position_type,
+                          "gl_Position");
+   vs_out_pos->data.location = VARYING_SLOT_POS;
+
+   nir_variable *vs_in_color =
+      nir_variable_create(vs_b.shader, nir_var_shader_in, color_type,
+                          "a_color");
+   vs_in_color->data.location = VERT_ATTRIB_GENERIC1;
+
+   nir_variable *vs_out_color =
+      nir_variable_create(vs_b.shader, nir_var_shader_out, color_type,
+                          "v_color");
+   vs_out_color->data.location = VARYING_SLOT_VAR0;
+   vs_out_color->data.interpolation = INTERP_QUALIFIER_FLAT;
+
+   nir_variable *fs_in_color =
+      nir_variable_create(fs_b.shader, nir_var_shader_in, color_type,
+                          "v_color");
+   fs_in_color->data.location = vs_out_color->data.location;
+   fs_in_color->data.interpolation = vs_out_color->data.interpolation;
+
+   nir_variable *fs_out_color =
+      nir_variable_create(fs_b.shader, nir_var_shader_out, color_type,
+                          "f_color");
+   fs_out_color->data.location = FRAG_RESULT_DATA0;
+
+   nir_copy_var(&vs_b, vs_out_pos, vs_in_pos);
+   nir_copy_var(&vs_b, vs_out_color, vs_in_color);
+   nir_copy_var(&fs_b, fs_out_color, fs_in_color);
+
+   *out_vs = vs_b.shader;
+   *out_fs = fs_b.shader;
 }
 
-void
-anv_device_init_meta_clear_state(struct anv_device *device)
+static VkResult
+create_pipeline(struct anv_device *device,
+                struct nir_shader *vs_nir,
+                struct nir_shader *fs_nir,
+                const VkPipelineVertexInputStateCreateInfo *vi_state,
+                const VkPipelineDepthStencilStateCreateInfo *ds_state,
+                const VkPipelineColorBlendStateCreateInfo *cb_state,
+                const VkAllocationCallbacks *alloc,
+                struct anv_pipeline **pipeline)
 {
-   struct anv_shader_module vsm = {
-      .nir = build_nir_vertex_shader(true),
-   };
-
-   struct anv_shader_module fsm = {
-      .nir = build_nir_clear_fragment_shader(),
-   };
+   VkDevice device_h = anv_device_to_handle(device);
+   VkResult result;
 
-   VkShader vs;
-   anv_CreateShader(anv_device_to_handle(device),
-      &(VkShaderCreateInfo) {
-         .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
-         .module = anv_shader_module_to_handle(&vsm),
-         .pName = "main",
-      }, &vs);
-
-   VkShader fs;
-   anv_CreateShader(anv_device_to_handle(device),
-      &(VkShaderCreateInfo) {
-         .sType = VK_STRUCTURE_TYPE_SHADER_CREATE_INFO,
-         .module = anv_shader_module_to_handle(&fsm),
-         .pName = "main",
-      }, &fs);
-
-   /* We use instanced rendering to clear multiple render targets. We have two
-    * vertex buffers: the first vertex buffer holds per-vertex data and
-    * provides the vertices for the clear rectangle. The second one holds
-    * per-instance data, which consists of the VUE header (which selects the
-    * layer) and the color (Vulkan supports per-RT clear colors).
-    */
-   VkPipelineVertexInputStateCreateInfo vi_create_info = {
-      .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
-      .bindingCount = 2,
-      .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
-         {
-            .binding = 0,
-            .strideInBytes = 12,
-            .stepRate = VK_VERTEX_INPUT_STEP_RATE_VERTEX
-         },
-         {
-            .binding = 1,
-            .strideInBytes = 32,
-            .stepRate = VK_VERTEX_INPUT_STEP_RATE_INSTANCE
-         },
-      },
-      .attributeCount = 3,
-      .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
-         {
-            /* VUE Header */
-            .location = 0,
-            .binding = 1,
-            .format = VK_FORMAT_R32G32B32A32_UINT,
-            .offsetInBytes = 0
-         },
-         {
-            /* Position */
-            .location = 1,
-            .binding = 0,
-            .format = VK_FORMAT_R32G32B32_SFLOAT,
-            .offsetInBytes = 0
-         },
-         {
-            /* Color */
-            .location = 2,
-            .binding = 1,
-            .format = VK_FORMAT_R32G32B32A32_SFLOAT,
-            .offsetInBytes = 16
-         }
-      }
-   };
+   struct anv_shader_module vs_m = { .nir = vs_nir };
+   struct anv_shader_module fs_m = { .nir = fs_nir };
 
-   anv_graphics_pipeline_create(anv_device_to_handle(device),
+   VkPipeline pipeline_h;
+   result = anv_graphics_pipeline_create(device_h,
+      VK_NULL_HANDLE,
       &(VkGraphicsPipelineCreateInfo) {
          .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
-
          .stageCount = 2,
          .pStages = (VkPipelineShaderStageCreateInfo[]) {
             {
                .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-               .stage = VK_SHADER_STAGE_VERTEX,
-               .shader = vs,
-               .pSpecializationInfo = NULL
-            }, {
+               .stage = VK_SHADER_STAGE_VERTEX_BIT,
+               .module = anv_shader_module_to_handle(&vs_m),
+               .pName = "main",
+            },
+            {
                .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
-               .stage = VK_SHADER_STAGE_FRAGMENT,
-               .shader = fs,
-               .pSpecializationInfo = NULL,
-            }
+               .stage = VK_SHADER_STAGE_FRAGMENT_BIT,
+               .module = anv_shader_module_to_handle(&fs_m),
+               .pName = "main",
+            },
          },
-         .pVertexInputState = &vi_create_info,
+         .pVertexInputState = vi_state,
          .pInputAssemblyState = &(VkPipelineInputAssemblyStateCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
             .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
@@ -315,47 +157,34 @@ anv_device_init_meta_clear_state(struct anv_device *device)
          .pViewportState = &(VkPipelineViewportStateCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
             .viewportCount = 1,
+            .pViewports = NULL, /* dynamic */
             .scissorCount = 1,
+            .pScissors = NULL, /* dynamic */
          },
-         .pRasterState = &(VkPipelineRasterStateCreateInfo) {
-            .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTER_STATE_CREATE_INFO,
-            .depthClipEnable = true,
+         .pRasterizationState = &(VkPipelineRasterizationStateCreateInfo) {
+            .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
             .rasterizerDiscardEnable = false,
-            .fillMode = VK_FILL_MODE_SOLID,
+            .polygonMode = VK_POLYGON_MODE_FILL,
             .cullMode = VK_CULL_MODE_NONE,
-            .frontFace = VK_FRONT_FACE_CCW
+            .frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE,
+            .depthBiasEnable = false,
          },
          .pMultisampleState = &(VkPipelineMultisampleStateCreateInfo) {
             .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
-            .rasterSamples = 1,
+            .rasterizationSamples = 1, /* FINISHME: Multisampling */
             .sampleShadingEnable = false,
             .pSampleMask = (VkSampleMask[]) { UINT32_MAX },
+            .alphaToCoverageEnable = false,
+            .alphaToOneEnable = false,
          },
-         .pDepthStencilState = &(VkPipelineDepthStencilStateCreateInfo) {
-            .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
-            .depthTestEnable = true,
-            .depthWriteEnable = true,
-            .depthCompareOp = VK_COMPARE_OP_ALWAYS,
-            .depthBoundsTestEnable = false,
-            .stencilTestEnable = true,
-            .front = (VkStencilOpState) {
-               .stencilPassOp = VK_STENCIL_OP_REPLACE,
-               .stencilCompareOp = VK_COMPARE_OP_ALWAYS,
-            },
-            .back = (VkStencilOpState) {
-               .stencilPassOp = VK_STENCIL_OP_REPLACE,
-               .stencilCompareOp = VK_COMPARE_OP_ALWAYS,
-            },
-         },
-         .pColorBlendState = &(VkPipelineColorBlendStateCreateInfo) {
-            .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
-            .attachmentCount = 1,
-            .pAttachments = (VkPipelineColorBlendAttachmentState []) {
-               { .channelWriteMask = VK_CHANNEL_A_BIT |
-                    VK_CHANNEL_R_BIT | VK_CHANNEL_G_BIT | VK_CHANNEL_B_BIT },
-            }
-         },
+         .pDepthStencilState = ds_state,
+         .pColorBlendState = cb_state,
          .pDynamicState = &(VkPipelineDynamicStateCreateInfo) {
+            /* The meta clear pipeline declares all state as dynamic.
+             * As a consequence, vkCmdBindPipeline writes no dynamic state
+             * to the cmd buffer. Therefore, at the end of the meta clear,
+             * we need only restore dynamic state was vkCmdSet.
+             */
             .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
             .dynamicStateCount = 9,
             .pDynamicStates = (VkDynamicState[]) {
@@ -380,52 +209,534 @@ anv_device_init_meta_clear_state(struct anv_device *device)
          .disable_vs = true,
          .use_rectlist = true
       },
-      &device->meta_state.clear.pipeline);
+      alloc,
+      &pipeline_h);
+
+   ralloc_free(vs_nir);
+   ralloc_free(fs_nir);
+
+   *pipeline = anv_pipeline_from_handle(pipeline_h);
+
+   return result;
+}
+
+static VkResult
+init_color_pipeline(struct anv_device *device)
+{
+   struct nir_shader *vs_nir;
+   struct nir_shader *fs_nir;
+   build_color_shaders(&vs_nir, &fs_nir);
+
+   const VkPipelineVertexInputStateCreateInfo vi_state = {
+      .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
+      .vertexBindingDescriptionCount = 1,
+      .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
+         {
+            .binding = 0,
+            .stride = sizeof(struct color_clear_vattrs),
+            .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
+         },
+      },
+      .vertexAttributeDescriptionCount = 3,
+      .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
+         {
+            /* VUE Header */
+            .location = 0,
+            .binding = 0,
+            .format = VK_FORMAT_R32G32B32A32_UINT,
+            .offset = offsetof(struct color_clear_vattrs, vue_header),
+         },
+         {
+            /* Position */
+            .location = 1,
+            .binding = 0,
+            .format = VK_FORMAT_R32G32_SFLOAT,
+            .offset = offsetof(struct color_clear_vattrs, position),
+         },
+         {
+            /* Color */
+            .location = 2,
+            .binding = 0,
+            .format = VK_FORMAT_R32G32B32A32_SFLOAT,
+            .offset = offsetof(struct color_clear_vattrs, color),
+         },
+      },
+   };
+
+   const VkPipelineDepthStencilStateCreateInfo ds_state = {
+      .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
+      .depthTestEnable = false,
+      .depthWriteEnable = false,
+      .depthBoundsTestEnable = false,
+      .stencilTestEnable = false,
+   };
+
+   const VkPipelineColorBlendStateCreateInfo cb_state = {
+      .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
+      .logicOpEnable = false,
+      .attachmentCount = 1,
+      .pAttachments = (VkPipelineColorBlendAttachmentState []) {
+         {
+            .blendEnable = false,
+            .colorWriteMask = VK_COLOR_COMPONENT_A_BIT |
+                              VK_COLOR_COMPONENT_R_BIT |
+                              VK_COLOR_COMPONENT_G_BIT |
+                              VK_COLOR_COMPONENT_B_BIT,
+         },
+      },
+   };
+
+   return
+      create_pipeline(device, vs_nir, fs_nir, &vi_state, &ds_state,
+                      &cb_state, NULL,
+                      &device->meta_state.clear.color_pipeline);
+}
+
+static void
+emit_load_color_clear(struct anv_cmd_buffer *cmd_buffer,
+                      uint32_t attachment,
+                      VkClearColorValue clear_value)
+{
+   struct anv_device *device = cmd_buffer->device;
+   VkCommandBuffer cmd_buffer_h = anv_cmd_buffer_to_handle(cmd_buffer);
+   const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
+   VkPipeline pipeline_h =
+      anv_pipeline_to_handle(device->meta_state.clear.color_pipeline);
+
+   const struct color_clear_vattrs vertex_data[3] = {
+      {
+         .vue_header = { 0 },
+         .position = { 0.0, 0.0 },
+         .color = clear_value,
+      },
+      {
+         .vue_header = { 0 },
+         .position = { fb->width, 0.0 },
+         .color = clear_value,
+      },
+      {
+         .vue_header = { 0 },
+         .position = { fb->width, fb->height },
+         .color = clear_value,
+      },
+   };
+
+   struct anv_state state =
+      anv_cmd_buffer_emit_dynamic(cmd_buffer, vertex_data, sizeof(vertex_data), 16);
+
+   struct anv_buffer vertex_buffer = {
+      .device = device,
+      .size = sizeof(vertex_data),
+      .bo = &device->dynamic_state_block_pool.bo,
+      .offset = state.offset,
+   };
+
+   anv_cmd_buffer_begin_subpass(cmd_buffer,
+      &(struct anv_subpass) {
+         .color_count = 1,
+         .color_attachments = (uint32_t[]) { attachment },
+         .depth_stencil_attachment = VK_ATTACHMENT_UNUSED,
+      });
+
+   ANV_CALL(CmdSetViewport)(cmd_buffer_h, 1,
+      (VkViewport[]) {
+         {
+            .x = 0,
+            .y = 0,
+            .width = fb->width,
+            .height = fb->height,
+            .minDepth = 0.0,
+            .maxDepth = 1.0,
+         },
+      });
+
+   ANV_CALL(CmdSetScissor)(cmd_buffer_h, 1,
+      (VkRect2D[]) {
+         {
+            .offset = { 0, 0 },
+            .extent = { fb->width, fb->height },
+         }
+      });
+
+   ANV_CALL(CmdBindVertexBuffers)(cmd_buffer_h, 0, 1,
+      (VkBuffer[]) { anv_buffer_to_handle(&vertex_buffer) },
+      (VkDeviceSize[]) { 0 });
+
+   if (cmd_buffer->state.pipeline != device->meta_state.clear.color_pipeline) {
+      ANV_CALL(CmdBindPipeline)(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
+                                pipeline_h);
+   }
+
+   ANV_CALL(CmdDraw)(cmd_buffer_h, 3, 1, 0, 0);
+}
+
+
+static void
+build_depthstencil_shaders(struct nir_shader **out_vs,
+                           struct nir_shader **out_fs)
+{
+   nir_builder vs_b;
+   nir_builder fs_b;
+
+   nir_builder_init_simple_shader(&vs_b, NULL, MESA_SHADER_VERTEX, NULL);
+   nir_builder_init_simple_shader(&fs_b, NULL, MESA_SHADER_FRAGMENT, NULL);
+
+   const struct glsl_type *position_type = glsl_vec4_type();
+
+   nir_variable *vs_in_pos =
+      nir_variable_create(vs_b.shader, nir_var_shader_in, position_type,
+                          "a_position");
+   vs_in_pos->data.location = VERT_ATTRIB_GENERIC0;
+
+   nir_variable *vs_out_pos =
+      nir_variable_create(vs_b.shader, nir_var_shader_out, position_type,
+                          "gl_Position");
+   vs_out_pos->data.location = VARYING_SLOT_POS;
+
+   nir_copy_var(&vs_b, vs_out_pos, vs_in_pos);
+
+   *out_vs = vs_b.shader;
+   *out_fs = fs_b.shader;
+}
+
+static VkResult
+create_depthstencil_pipeline(struct anv_device *device,
+                             VkImageAspectFlags aspects,
+                             struct anv_pipeline **pipeline)
+{
+   struct nir_shader *vs_nir;
+   struct nir_shader *fs_nir;
+
+   build_depthstencil_shaders(&vs_nir, &fs_nir);
+
+   const VkPipelineVertexInputStateCreateInfo vi_state = {
+      .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
+      .vertexBindingDescriptionCount = 1,
+      .pVertexBindingDescriptions = (VkVertexInputBindingDescription[]) {
+         {
+            .binding = 0,
+            .stride = sizeof(struct depthstencil_clear_vattrs),
+            .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
+         },
+      },
+      .vertexAttributeDescriptionCount = 2,
+      .pVertexAttributeDescriptions = (VkVertexInputAttributeDescription[]) {
+         {
+            /* VUE Header */
+            .location = 0,
+            .binding = 0,
+            .format = VK_FORMAT_R32G32B32A32_UINT,
+            .offset = offsetof(struct depthstencil_clear_vattrs, vue_header),
+         },
+         {
+            /* Position */
+            .location = 1,
+            .binding = 0,
+            .format = VK_FORMAT_R32G32_SFLOAT,
+            .offset = offsetof(struct depthstencil_clear_vattrs, position),
+         },
+      },
+   };
+
+   const VkPipelineDepthStencilStateCreateInfo ds_state = {
+      .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
+      .depthTestEnable = (aspects & VK_IMAGE_ASPECT_DEPTH_BIT),
+      .depthCompareOp = VK_COMPARE_OP_ALWAYS,
+      .depthWriteEnable = (aspects & VK_IMAGE_ASPECT_DEPTH_BIT),
+      .depthBoundsTestEnable = false,
+      .stencilTestEnable = (aspects & VK_IMAGE_ASPECT_STENCIL_BIT),
+      .front = {
+         .passOp = VK_STENCIL_OP_REPLACE,
+         .compareOp = VK_COMPARE_OP_ALWAYS,
+         .writeMask = UINT32_MAX,
+         .reference = 0, /* dynamic */
+      },
+      .back = { 0 /* dont care */ },
+   };
+
+   const VkPipelineColorBlendStateCreateInfo cb_state = {
+      .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO,
+      .logicOpEnable = false,
+      .attachmentCount = 0,
+      .pAttachments = NULL,
+   };
+
+   return create_pipeline(device, vs_nir, fs_nir, &vi_state, &ds_state,
+                          &cb_state, NULL, pipeline);
+}
+
+static void
+emit_load_depthstencil_clear(struct anv_cmd_buffer *cmd_buffer,
+                             uint32_t attachment,
+                             VkImageAspectFlags aspects,
+                             VkClearDepthStencilValue clear_value)
+{
+   struct anv_device *device = cmd_buffer->device;
+   VkCommandBuffer cmd_buffer_h = anv_cmd_buffer_to_handle(cmd_buffer);
+   const struct anv_framebuffer *fb = cmd_buffer->state.framebuffer;
+
+   const struct depthstencil_clear_vattrs vertex_data[3] = {
+      {
+         .vue_header = { 0 },
+         .position = { 0.0, 0.0 },
+      },
+      {
+         .vue_header = { 0 },
+         .position = { fb->width, 0.0 },
+      },
+      {
+         .vue_header = { 0 },
+         .position = { fb->width, fb->height },
+      },
+   };
+
+   struct anv_state state =
+      anv_cmd_buffer_emit_dynamic(cmd_buffer, vertex_data, sizeof(vertex_data), 16);
+
+   struct anv_buffer vertex_buffer = {
+      .device = device,
+      .size = sizeof(vertex_data),
+      .bo = &device->dynamic_state_block_pool.bo,
+      .offset = state.offset,
+   };
+
+   anv_cmd_buffer_begin_subpass(cmd_buffer,
+      &(struct anv_subpass) {
+         .color_count = 0,
+         .depth_stencil_attachment = attachment,
+      });
+
+   ANV_CALL(CmdSetViewport)(cmd_buffer_h, 1,
+      (VkViewport[]) {
+         {
+            .x = 0,
+            .y = 0,
+            .width = fb->width,
+            .height = fb->height,
+
+            /* Ignored when clearing only stencil. */
+            .minDepth = clear_value.depth,
+            .maxDepth = clear_value.depth,
+         },
+      });
+
+   ANV_CALL(CmdSetScissor)(cmd_buffer_h, 1,
+      (VkRect2D[]) {
+         {
+            .offset = { 0, 0 },
+            .extent = { fb->width, fb->height },
+         }
+      });
+
+   if (aspects & VK_IMAGE_ASPECT_STENCIL_BIT) {
+      ANV_CALL(CmdSetStencilReference)(cmd_buffer_h, VK_STENCIL_FACE_FRONT_BIT,
+                                       clear_value.stencil);
+   }
+
+   ANV_CALL(CmdBindVertexBuffers)(cmd_buffer_h, 0, 1,
+      (VkBuffer[]) { anv_buffer_to_handle(&vertex_buffer) },
+      (VkDeviceSize[]) { 0 });
+
+   struct anv_pipeline *pipeline;
+   switch (aspects) {
+   case VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT:
+      pipeline = device->meta_state.clear.depthstencil_pipeline;
+      break;
+   case VK_IMAGE_ASPECT_DEPTH_BIT:
+      pipeline = device->meta_state.clear.depth_only_pipeline;
+      break;
+   case VK_IMAGE_ASPECT_STENCIL_BIT:
+      pipeline = device->meta_state.clear.stencil_only_pipeline;
+      break;
+   default:
+      unreachable("expected depth or stencil aspect");
+   }
+
+   if (cmd_buffer->state.pipeline != pipeline) {
+      ANV_CALL(CmdBindPipeline)(cmd_buffer_h, VK_PIPELINE_BIND_POINT_GRAPHICS,
+                                anv_pipeline_to_handle(pipeline));
+   }
+
+   ANV_CALL(CmdDraw)(cmd_buffer_h, 3, 1, 0, 0);
+}
+
+static VkResult
+init_depthstencil_pipelines(struct anv_device *device)
+{
+   VkResult result;
+   struct anv_meta_state *state = &device->meta_state;
+
+   result =
+      create_depthstencil_pipeline(device, VK_IMAGE_ASPECT_DEPTH_BIT,
+                                   &state->clear.depth_only_pipeline);
+   if (result != VK_SUCCESS)
+      goto fail;
+
+   result =
+      create_depthstencil_pipeline(device, VK_IMAGE_ASPECT_STENCIL_BIT,
+                                   &state->clear.stencil_only_pipeline);
+   if (result != VK_SUCCESS)
+      goto fail_depth_only;
+
+   result =
+      create_depthstencil_pipeline(device,
+                                   VK_IMAGE_ASPECT_DEPTH_BIT |
+                                   VK_IMAGE_ASPECT_STENCIL_BIT,
+                                   &state->clear.depthstencil_pipeline);
+   if (result != VK_SUCCESS)
+      goto fail_stencil_only;
+
+   return result;
+
+ fail_stencil_only:
+   anv_DestroyPipeline(anv_device_to_handle(device),
+                       anv_pipeline_to_handle(state->clear.stencil_only_pipeline),
+                       NULL);
+ fail_depth_only:
+   anv_DestroyPipeline(anv_device_to_handle(device),
+                       anv_pipeline_to_handle(state->clear.depth_only_pipeline),
+                       NULL);
+ fail:
+   return result;
+}
+
+VkResult
+anv_device_init_meta_clear_state(struct anv_device *device)
+{
+   VkResult result;
+
+   result = init_color_pipeline(device);
+   if (result != VK_SUCCESS)
+      goto fail;
+
+   result = init_depthstencil_pipelines(device);
+   if (result != VK_SUCCESS)
+      goto fail_color_pipeline;
+
+   return VK_SUCCESS;
+
+ fail_color_pipeline:
+   anv_DestroyPipeline(anv_device_to_handle(device),
+                       anv_pipeline_to_handle(device->meta_state.clear.color_pipeline),
+                       NULL);
+ fail:
+    return result;
+}
+
+void
+anv_device_finish_meta_clear_state(struct anv_device *device)
+{
+   VkDevice device_h = anv_device_to_handle(device);
+
+   ANV_CALL(DestroyPipeline)(device_h,
+      anv_pipeline_to_handle(device->meta_state.clear.color_pipeline),
+      NULL);
+   ANV_CALL(DestroyPipeline)(device_h,
+      anv_pipeline_to_handle(device->meta_state.clear.depth_only_pipeline),
+      NULL);
+   ANV_CALL(DestroyPipeline)(device_h,
+      anv_pipeline_to_handle(device->meta_state.clear.stencil_only_pipeline),
+      NULL);
+   ANV_CALL(DestroyPipeline)(device_h,
+      anv_pipeline_to_handle(device->meta_state.clear.depthstencil_pipeline),
+      NULL);
+}
+
+void
+anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
+                                 struct anv_render_pass *pass,
+                                 const VkClearValue *clear_values)
+{
+   struct anv_meta_saved_state saved_state;
+
+   /* Figure out whether or not we actually need to clear anything to avoid
+    * trashing state when clearing is a no-op.
+    */
+   bool needs_clear = false;
+   for (uint32_t a = 0; a < pass->attachment_count; ++a) {
+      struct anv_render_pass_attachment *att = &pass->attachments[a];
+
+      if (anv_format_is_color(att->format)) {
+         if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
+            needs_clear = true;
+            break;
+         }
+      } else {
+         if ((att->format->depth_format &&
+              att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) ||
+             (att->format->has_stencil &&
+              att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR)) {
+            needs_clear = true;
+            break;
+         }
+      }
+   }
+
+   if (!needs_clear)
+      return;
+
+   meta_clear_begin(&saved_state, cmd_buffer);
+
+   for (uint32_t a = 0; a < pass->attachment_count; ++a) {
+      struct anv_render_pass_attachment *att = &pass->attachments[a];
+
+      if (anv_format_is_color(att->format)) {
+         if (att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
+            emit_load_color_clear(cmd_buffer, a, clear_values[a].color);
+         }
+      } else {
+         VkImageAspectFlags clear_aspects = 0;
+
+         if (att->format->depth_format &&
+             att->load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
+            clear_aspects |= VK_IMAGE_ASPECT_DEPTH_BIT;
+         }
+
+         if (att->format->has_stencil &&
+             att->stencil_load_op == VK_ATTACHMENT_LOAD_OP_CLEAR) {
+            clear_aspects |= VK_IMAGE_ASPECT_STENCIL_BIT;
+         }
 
-   anv_DestroyShader(anv_device_to_handle(device), vs);
-   anv_DestroyShader(anv_device_to_handle(device), fs);
-   ralloc_free(vsm.nir);
-   ralloc_free(fsm.nir);
+         if (clear_aspects) {
+            emit_load_depthstencil_clear(cmd_buffer, a, clear_aspects,
+                                         clear_values[a].depthStencil);
+         }
+      }
+   }
+
+   meta_clear_end(&saved_state, cmd_buffer);
 }
 
 void anv_CmdClearColorImage(
-    VkCmdBuffer                                 cmdBuffer,
+    VkCommandBuffer                             commandBuffer,
     VkImage                                     _image,
     VkImageLayout                               imageLayout,
     const VkClearColorValue*                    pColor,
     uint32_t                                    rangeCount,
     const VkImageSubresourceRange*              pRanges)
 {
-   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, cmdBuffer);
+   ANV_FROM_HANDLE(anv_cmd_buffer, cmd_buffer, commandBuffer);
    ANV_FROM_HANDLE(anv_image, image, _image);
    struct anv_meta_saved_state saved_state;
 
-   anv_meta_save(&saved_state, cmd_buffer,
-                 (1 << VK_DYNAMIC_STATE_VIEWPORT));
-   cmd_buffer->state.dynamic.viewport.count = 0;
+   meta_clear_begin(&saved_state, cmd_buffer);
 
    for (uint32_t r = 0; r < rangeCount; r++) {
-      for (uint32_t l = 0; l < pRanges[r].mipLevels; l++) {
-         for (uint32_t s = 0; s < pRanges[r].arraySize; s++) {
+      for (uint32_t l = 0; l < pRanges[r].levelCount; l++) {
+         for (uint32_t s = 0; s < pRanges[r].layerCount; s++) {
             struct anv_image_view iview;
             anv_image_view_init(&iview, cmd_buffer->device,
                &(VkImageViewCreateInfo) {
                   .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
                   .image = _image,
-                  .viewType = VK_IMAGE_VIEW_TYPE_2D,
-                  .format = image->format->vk_format,
-                  .channels = {
-                     VK_CHANNEL_SWIZZLE_R,
-                     VK_CHANNEL_SWIZZLE_G,
-                     VK_CHANNEL_SWIZZLE_B,
-                     VK_CHANNEL_SWIZZLE_A
-                  },
+                  .viewType = anv_meta_get_view_type(image),
+                  .format = image->vk_format,
                   .subresourceRange = {
                      .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
                      .baseMipLevel = pRanges[r].baseMipLevel + l,
-                     .mipLevels = 1,
+                     .levelCount = 1,
                      .baseArrayLayer = pRanges[r].baseArrayLayer + s,
-                     .arraySize = 1
+                     .layerCount = 1
                   },
                },
                cmd_buffer);
@@ -441,7 +752,7 @@ void anv_CmdClearColorImage(
                   .width = iview.extent.width,
                   .height = iview.extent.height,
                   .layers = 1
-               }, &fb);
+               }, &cmd_buffer->pool->alloc, &fb);
 
             VkRenderPass pass;
             anv_CreateRenderPass(anv_device_to_handle(cmd_buffer->device),
@@ -449,8 +760,7 @@ void anv_CmdClearColorImage(
                   .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
                   .attachmentCount = 1,
                   .pAttachments = &(VkAttachmentDescription) {
-                     .sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION,
-                     .format = iview.format->vk_format,
+                     .format = iview.vk_format,
                      .loadOp = VK_ATTACHMENT_LOAD_OP_LOAD,
                      .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
                      .initialLayout = VK_IMAGE_LAYOUT_GENERAL,
@@ -458,27 +768,26 @@ void anv_CmdClearColorImage(
                   },
                   .subpassCount = 1,
                   .pSubpasses = &(VkSubpassDescription) {
-                     .sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION,
                      .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
-                     .inputCount = 0,
-                     .colorCount = 1,
+                     .inputAttachmentCount = 0,
+                     .colorAttachmentCount = 1,
                      .pColorAttachments = &(VkAttachmentReference) {
                         .attachment = 0,
                         .layout = VK_IMAGE_LAYOUT_GENERAL,
                      },
                      .pResolveAttachments = NULL,
-                     .depthStencilAttachment = (VkAttachmentReference) {
+                     .pDepthStencilAttachment = &(VkAttachmentReference) {
                         .attachment = VK_ATTACHMENT_UNUSED,
                         .layout = VK_IMAGE_LAYOUT_GENERAL,
                      },
-                     .preserveCount = 1,
+                     .preserveAttachmentCount = 1,
                      .pPreserveAttachments = &(VkAttachmentReference) {
                         .attachment = 0,
                         .layout = VK_IMAGE_LAYOUT_GENERAL,
                      },
                   },
                   .dependencyCount = 0,
-               }, &pass);
+               }, &cmd_buffer->pool->alloc, &pass);
 
             ANV_CALL(CmdBeginRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer),
                &(VkRenderPassBeginInfo) {
@@ -493,31 +802,23 @@ void anv_CmdClearColorImage(
                   .renderPass = pass,
                   .framebuffer = fb,
                   .clearValueCount = 1,
-                  .pClearValues = NULL,
-               }, VK_RENDER_PASS_CONTENTS_INLINE);
-
-            struct clear_instance_data instance_data = {
-               .vue_header = {
-                  .RTAIndex = 0,
-                  .ViewportIndex = 0,
-                  .PointWidth = 0.0
-               },
-               .color = *pColor,
-            };
-
-            meta_emit_clear(cmd_buffer, 1, &instance_data,
-                            (VkClearDepthStencilValue) {0});
+                  .pClearValues = (VkClearValue[]) {
+                     { .color = *pColor },
+                  },
+               }, VK_SUBPASS_CONTENTS_INLINE);
 
             ANV_CALL(CmdEndRenderPass)(anv_cmd_buffer_to_handle(cmd_buffer));
+
+            /* XXX: We're leaking the render pass and framebuffer */
          }
       }
    }
 
-   anv_meta_restore(&saved_state, cmd_buffer);
+   meta_clear_end(&saved_state, cmd_buffer);
 }
 
 void anv_CmdClearDepthStencilImage(
-    VkCmdBuffer                                 cmdBuffer,
+    VkCommandBuffer                             commandBuffer,
     VkImage                                     image,
     VkImageLayout                               imageLayout,
     const VkClearDepthStencilValue*             pDepthStencil,
@@ -527,24 +828,12 @@ void anv_CmdClearDepthStencilImage(
    stub();
 }
 
-void anv_CmdClearColorAttachment(
-    VkCmdBuffer                                 cmdBuffer,
-    uint32_t                                    colorAttachment,
-    VkImageLayout                               imageLayout,
-    const VkClearColorValue*                    pColor,
-    uint32_t                                    rectCount,
-    const VkRect3D*                             pRects)
-{
-   stub();
-}
-
-void anv_CmdClearDepthStencilAttachment(
-    VkCmdBuffer                                 cmdBuffer,
-    VkImageAspectFlags                          aspectMask,
-    VkImageLayout                               imageLayout,
-    const VkClearDepthStencilValue*             pDepthStencil,
+void anv_CmdClearAttachments(
+    VkCommandBuffer                             commandBuffer,
+    uint32_t                                    attachmentCount,
+    const VkClearAttachment*                    pAttachments,
     uint32_t                                    rectCount,
-    const VkRect3D*                             pRects)
+    const VkClearRect*                          pRects)
 {
    stub();
 }