vk: Add an initial implementation of the actual Khronos WSI extension
[mesa.git] / src / vulkan / anv_private.h
index 1ddc2722ceaa4cc0fd3e1d5d419a0b53667b98e8..06ef4e123f35399d2a260745bd4b8f4672b0008e 100644 (file)
 
 #include "brw_device_info.h"
 #include "util/macros.h"
+#include "util/list.h"
 
 #define VK_PROTOTYPES
 #include <vulkan/vulkan.h>
 #include <vulkan/vulkan_intel.h>
-#include <vulkan/vk_wsi_lunarg.h>
+#include <vulkan/vk_wsi_swapchain.h>
+#include <vulkan/vk_wsi_device_swapchain.h>
 
 #include "anv_entrypoints.h"
 
@@ -119,16 +121,15 @@ struct anv_common {
  * propagating errors. Might be useful to plug in a stack trace here.
  */
 
-static inline VkResult
-vk_error(VkResult error)
-{
+VkResult __vk_errorf(VkResult error, const char *file, int line, const char *format, ...);
+
 #ifdef DEBUG
-   fprintf(stderr, "vk_error: %x\n", error);
+#define vk_error(error) __vk_errorf(error, __FILE__, __LINE__, NULL);
+#define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
+#else
+#define vk_error(error) error
 #endif
 
-   return error;
-}
-
 void __anv_finishme(const char *file, int line, const char *format, ...)
    anv_printflike(3, 4);
 void anv_loge(const char *format, ...) anv_printflike(1, 2);
@@ -208,11 +209,6 @@ struct anv_bo {
    uint32_t index;
    uint64_t offset;
    uint64_t size;
-
-   /* This field is here for the benefit of the aub dumper.  It can (and for
-    * userptr bos it must) be set to the cpu map of the buffer.  Destroying
-    * the bo won't clean up the mmap, it's still the responsibility of the bo
-    * user to do that. */
    void *map;
 };
 
@@ -232,13 +228,22 @@ union anv_free_list {
 
 #define ANV_FREE_LIST_EMPTY ((union anv_free_list) { { 1, 0 } })
 
+struct anv_block_state {
+   union {
+      struct {
+         uint32_t next;
+         uint32_t end;
+      };
+      uint64_t u64;
+   };
+};
+
 struct anv_block_pool {
    struct anv_device *device;
 
    struct anv_bo bo;
    void *map;
    int fd;
-   uint32_t size;
 
    /**
     * Array of mmaps and gem handles owned by the block pool, reclaimed when
@@ -248,19 +253,15 @@ struct anv_block_pool {
 
    uint32_t block_size;
 
-   uint32_t next_block;
    union anv_free_list free_list;
+   struct anv_block_state state;
 };
 
-struct anv_block_state {
-   union {
-      struct {
-         uint32_t next;
-         uint32_t end;
-      };
-      uint64_t u64;
-   };
-};
+static inline uint32_t
+anv_block_pool_size(struct anv_block_pool *pool)
+{
+   return pool->state.end;
+}
 
 struct anv_state {
    uint32_t offset;
@@ -298,6 +299,7 @@ uint32_t anv_block_pool_alloc(struct anv_block_pool *pool);
 void anv_block_pool_free(struct anv_block_pool *pool, uint32_t offset);
 void anv_state_pool_init(struct anv_state_pool *pool,
                          struct anv_block_pool *block_pool);
+void anv_state_pool_finish(struct anv_state_pool *pool);
 struct anv_state anv_state_pool_alloc(struct anv_state_pool *pool,
                                       size_t state_size, size_t alignment);
 void anv_state_pool_free(struct anv_state_pool *pool, struct anv_state state);
@@ -328,11 +330,10 @@ void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
 struct anv_physical_device {
     struct anv_instance *                       instance;
     uint32_t                                    chipset_id;
-    bool                                        no_hw;
     const char *                                path;
     const char *                                name;
     const struct brw_device_info *              info;
-    int                                         fd;
+    uint64_t                                    aperture_size;
 };
 
 struct anv_instance {
@@ -388,8 +389,6 @@ struct anv_device {
     struct brw_device_info                      info;
     int                                         context_id;
     int                                         fd;
-    bool                                        no_hw;
-    bool                                        dump_aub;
 
     struct anv_bo_pool                          batch_bo_pool;
 
@@ -409,7 +408,6 @@ struct anv_device {
     struct anv_block_pool                       scratch_block_pool;
 
     struct anv_compiler *                       compiler;
-    struct anv_aub_writer *                     aub_writer;
     pthread_mutex_t                             mutex;
 };
 
@@ -437,7 +435,7 @@ int anv_gem_set_tiling(struct anv_device *device, int gem_handle,
 int anv_gem_create_context(struct anv_device *device);
 int anv_gem_destroy_context(struct anv_device *device, int context);
 int anv_gem_get_param(int fd, uint32_t param);
-int anv_gem_get_aperture(struct anv_physical_device *physical_dev, uint64_t *size);
+int anv_gem_get_aperture(int fd, uint64_t *size);
 int anv_gem_handle_to_fd(struct anv_device *device, int gem_handle);
 int anv_gem_fd_to_handle(struct anv_device *device, int fd);
 int anv_gem_userptr(struct anv_device *device, void *mem, size_t size);
@@ -462,16 +460,15 @@ uint64_t anv_reloc_list_add(struct anv_reloc_list *list,
                             uint32_t delta);
 
 struct anv_batch_bo {
+   /* Link in the anv_cmd_buffer.owned_batch_bos list */
+   struct list_head                             link;
+
    struct anv_bo                                bo;
 
    /* Bytes actually consumed in this batch BO */
    size_t                                       length;
 
-   /* These offsets reference the per-batch reloc list */
-   size_t                                       first_reloc;
-   size_t                                       num_relocs;
-
-   struct anv_batch_bo *                        prev_batch_bo;
+   struct anv_reloc_list                        relocs;
 };
 
 struct anv_batch {
@@ -481,7 +478,7 @@ struct anv_batch {
    void *                                       end;
    void *                                       next;
 
-   struct anv_reloc_list                        relocs;
+   struct anv_reloc_list *                      relocs;
 
    /* This callback is called (with the associated user data) in the event
     * that the batch runs out of space.
@@ -508,7 +505,7 @@ __gen_combine_address(struct anv_batch *batch, void *location,
                       const struct anv_address address, uint32_t delta)
 {
    if (address.bo == NULL) {
-      return delta;
+      return address.offset + delta;
    } else {
       assert(batch->start <= location && location < batch->end);
 
@@ -522,22 +519,22 @@ __gen_combine_address(struct anv_batch *batch, void *location,
 #include "gen8_pack.h"
 
 #define anv_batch_emit(batch, cmd, ...) do {                            \
+      void *__dst = anv_batch_emit_dwords(batch, cmd ## _length);       \
       struct cmd __template = {                                         \
          cmd ## _header,                                                \
          __VA_ARGS__                                                    \
       };                                                                \
-      void *__dst = anv_batch_emit_dwords(batch, cmd ## _length);       \
       cmd ## _pack(batch, __dst, &__template);                          \
       VG(VALGRIND_CHECK_MEM_IS_DEFINED(__dst, cmd ## _length * 4));     \
    } while (0)
 
 #define anv_batch_emitn(batch, n, cmd, ...) ({          \
+      void *__dst = anv_batch_emit_dwords(batch, n);    \
       struct cmd __template = {                         \
          cmd ## _header,                                \
         .DwordLength = n - cmd ## _length_bias,         \
          __VA_ARGS__                                    \
       };                                                \
-      void *__dst = anv_batch_emit_dwords(batch, n);    \
       cmd ## _pack(batch, __dst, &__template);          \
       __dst;                                            \
    })
@@ -553,6 +550,12 @@ __gen_combine_address(struct anv_batch *batch, void *location,
       VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, ARRAY_SIZE(dwords0) * 4));\
    } while (0)
 
+static const struct GEN7_MEMORY_OBJECT_CONTROL_STATE GEN7_MOCS = {
+   .GraphicsDataTypeGFDT                        = 0,
+   .LLCCacheabilityControlLLCCC                 = 0,
+   .L3CacheabilityControlL3CC                   = 1
+};
+
 #define GEN8_MOCS {                                     \
       .MemoryTypeLLCeLLCCacheabilityControl = WB,       \
       .TargetCache = L3DefertoPATforLLCeLLCselection,   \
@@ -572,17 +575,30 @@ struct anv_dynamic_vp_state {
 };
 
 struct anv_dynamic_rs_state {
-   uint32_t state_sf[GEN8_3DSTATE_SF_length];
-   uint32_t state_raster[GEN8_3DSTATE_RASTER_length];
+   struct {
+      uint32_t sf[GEN7_3DSTATE_SF_length];
+   } gen7;
+
+   struct {
+      uint32_t sf[GEN8_3DSTATE_SF_length];
+      uint32_t raster[GEN8_3DSTATE_RASTER_length];
+   } gen8;
 };
 
 struct anv_dynamic_ds_state {
-   uint32_t state_wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
-   uint32_t state_color_calc[GEN8_COLOR_CALC_STATE_length];
+   struct {
+      uint32_t depth_stencil_state[GEN7_DEPTH_STENCIL_STATE_length];
+      uint32_t color_calc_state[GEN8_COLOR_CALC_STATE_length];
+   } gen7;
+
+   struct {
+      uint32_t wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
+      uint32_t color_calc_state[GEN8_COLOR_CALC_STATE_length];
+   } gen8;
 };
 
 struct anv_dynamic_cb_state {
-   uint32_t state_color_calc[GEN8_COLOR_CALC_STATE_length];
+   uint32_t color_calc_state[GEN8_COLOR_CALC_STATE_length];
 
 };
 
@@ -626,6 +642,7 @@ anv_descriptor_set_destroy(struct anv_device *device,
 #define MAX_VBS   32
 #define MAX_SETS   8
 #define MAX_RTS    8
+#define MAX_PUSH_CONSTANTS_SIZE 128
 
 struct anv_pipeline_layout {
    struct {
@@ -668,6 +685,16 @@ struct anv_descriptor_set_binding {
    uint32_t                                     dynamic_offsets[128];
 };
 
+struct anv_push_constant_data {
+    uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
+    uint8_t driver_data[0];
+};
+
+struct anv_push_constants {
+    uint32_t driver_data_size;
+    struct anv_push_constant_data *data;
+};
+
 /** State required while building cmd buffer */
 struct anv_cmd_state {
    uint32_t                                     current_pipeline;
@@ -675,6 +702,7 @@ struct anv_cmd_state {
    uint32_t                                     dirty;
    uint32_t                                     compute_dirty;
    uint32_t                                     descriptors_dirty;
+   uint32_t                                     push_constants_dirty;
    uint32_t                                     scratch_size;
    struct anv_pipeline *                        pipeline;
    struct anv_pipeline *                        compute_pipeline;
@@ -688,48 +716,108 @@ struct anv_cmd_state {
    uint32_t                                     state_vf[GEN8_3DSTATE_VF_length];
    struct anv_vertex_binding                    vertex_bindings[MAX_VBS];
    struct anv_descriptor_set_binding            descriptors[MAX_SETS];
+   struct anv_push_constants                    push_constants[VK_SHADER_STAGE_NUM];
+
+   struct {
+      struct anv_buffer *                       index_buffer;
+      uint32_t                                  index_type;
+      uint32_t                                  index_offset;
+   } gen7;
+};
+
+struct anv_cmd_pool {
+   struct list_head                             cmd_buffers;
 };
 
-VkResult anv_cmd_state_init(struct anv_cmd_state *state);
-void anv_cmd_state_fini(struct anv_cmd_state *state);
+#define ANV_CMD_BUFFER_BATCH_SIZE 8192
+
+enum anv_cmd_buffer_exec_mode {
+   ANV_CMD_BUFFER_EXEC_MODE_PRIMARY,
+   ANV_CMD_BUFFER_EXEC_MODE_EMIT,
+   ANV_CMD_BUFFER_EXEC_MODE_CHAIN,
+   ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN,
+};
 
 struct anv_cmd_buffer {
    struct anv_device *                          device;
 
+   struct list_head                             pool_link;
+
+   struct anv_batch                             batch;
+
    /* Fields required for the actual chain of anv_batch_bo's.
     *
     * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
     */
-   struct anv_batch                             batch;
-   struct anv_batch_bo *                        last_batch_bo;
-   struct anv_batch_bo *                        surface_batch_bo;
+   struct list_head                             batch_bos;
+   struct list_head                             surface_bos;
    uint32_t                                     surface_next;
-   struct anv_reloc_list                        surface_relocs;
+   enum anv_cmd_buffer_exec_mode                exec_mode;
 
-   /* Information needed for execbuf that's generated when the command
-    * buffer is ended.
+   /* A vector of anv_batch_bo pointers for every batch or surface buffer
+    * referenced by this command buffer
+    *
+    * initialized by anv_cmd_buffer_init_batch_bo_chain()
+    */
+   struct anv_vector                            seen_bbos;
+
+   /* Information needed for execbuf
+    *
+    * These fields are generated by anv_cmd_buffer_prepare_execbuf().
     */
-   struct drm_i915_gem_execbuffer2              execbuf;
-   struct drm_i915_gem_exec_object2 *           exec2_objects;
-   uint32_t                                     exec2_bo_count;
-   struct anv_bo **                             exec2_bos;
-   uint32_t                                     exec2_array_length;
-   bool                                         need_reloc;
+   struct {
+      struct drm_i915_gem_execbuffer2           execbuf;
+
+      struct drm_i915_gem_exec_object2 *        objects;
+      uint32_t                                  bo_count;
+      struct anv_bo **                          bos;
+
+      /* Allocated length of the 'objects' and 'bos' arrays */
+      uint32_t                                  array_length;
+
+      bool                                      need_reloc;
+   } execbuf2;
+
+   /* Serial for tracking buffer completion */
    uint32_t                                     serial;
 
    /* Stream objects for storing temporary data */
    struct anv_state_stream                      surface_state_stream;
    struct anv_state_stream                      dynamic_state_stream;
 
+   VkCmdBufferOptimizeFlags                     opt_flags;
+   VkCmdBufferLevel                             level;
+
    struct anv_cmd_state                         state;
 };
 
 VkResult anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
 void anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
 void anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer);
-void anv_cmd_buffer_emit_batch_buffer_end(struct anv_cmd_buffer *cmd_buffer);
-void anv_cmd_buffer_compute_validate_list(struct anv_cmd_buffer *cmd_buffer);
+void anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer);
+void anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
+                                  struct anv_cmd_buffer *secondary);
+void anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer);
+
+VkResult anv_cmd_buffer_emit_binding_table(struct anv_cmd_buffer *cmd_buffer,
+                                           unsigned stage, struct anv_state *bt_state);
+VkResult anv_cmd_buffer_emit_samplers(struct anv_cmd_buffer *cmd_buffer,
+                                      unsigned stage, struct anv_state *state);
+void anv_flush_descriptor_sets(struct anv_cmd_buffer *cmd_buffer);
+
+struct anv_state anv_cmd_buffer_emit_dynamic(struct anv_cmd_buffer *cmd_buffer,
+                                             uint32_t *a, uint32_t dwords,
+                                             uint32_t alignment);
+struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
+                                              uint32_t *a, uint32_t *b,
+                                              uint32_t dwords, uint32_t alignment);
+void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
+                                  struct anv_subpass *subpass);
 
+struct anv_bo *
+anv_cmd_buffer_current_surface_bo(struct anv_cmd_buffer *cmd_buffer);
+struct anv_reloc_list *
+anv_cmd_buffer_current_surface_relocs(struct anv_cmd_buffer *cmd_buffer);
 struct anv_state
 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer,
                                    uint32_t size, uint32_t alignment);
@@ -739,17 +827,31 @@ anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
 
 VkResult anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer *cmd_buffer);
 
+void gen7_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
+void gen8_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
+
 void anv_cmd_buffer_emit_state_base_address(struct anv_cmd_buffer *cmd_buffer);
 
+void gen7_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
+                                   struct anv_subpass *subpass);
+
+void gen8_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
+                                   struct anv_subpass *subpass);
+
 void anv_cmd_buffer_begin_subpass(struct anv_cmd_buffer *cmd_buffer,
                                   struct anv_subpass *subpass);
 
+struct anv_state
+anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
+                              VkShaderStage stage);
+
 void anv_cmd_buffer_clear_attachments(struct anv_cmd_buffer *cmd_buffer,
                                       struct anv_render_pass *pass,
                                       const VkClearValue *clear_values);
+const struct anv_depth_stencil_view *
+anv_cmd_buffer_get_depth_stencil_view(const struct anv_cmd_buffer *cmd_buffer);
 
 void anv_cmd_buffer_dump(struct anv_cmd_buffer *cmd_buffer);
-void anv_aub_writer_destroy(struct anv_aub_writer *writer);
 
 struct anv_fence {
    struct anv_bo bo;
@@ -772,6 +874,7 @@ struct anv_pipeline {
    struct anv_device *                          device;
    struct anv_batch                             batch;
    uint32_t                                     batch_data[256];
+   struct anv_reloc_list                        batch_relocs;
    struct anv_shader *                          shaders[VK_SHADER_STAGE_NUM];
    struct anv_pipeline_layout *                 layout;
    bool                                         use_repclear;
@@ -797,25 +900,40 @@ struct anv_pipeline {
    struct anv_state_stream                      program_stream;
    struct anv_state                             blend_state;
    uint32_t                                     vs_simd8;
+   uint32_t                                     vs_vec4;
    uint32_t                                     ps_simd8;
    uint32_t                                     ps_simd16;
+   uint32_t                                     ps_ksp0;
+   uint32_t                                     ps_ksp2;
+   uint32_t                                     ps_grf_start0;
+   uint32_t                                     ps_grf_start2;
    uint32_t                                     gs_vec4;
    uint32_t                                     gs_vertex_count;
    uint32_t                                     cs_simd;
 
    uint32_t                                     vb_used;
    uint32_t                                     binding_stride[MAX_VBS];
-
-   uint32_t                                     state_sf[GEN8_3DSTATE_SF_length];
-   uint32_t                                     state_vf[GEN8_3DSTATE_VF_length];
-   uint32_t                                     state_raster[GEN8_3DSTATE_RASTER_length];
-   uint32_t                                     state_wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
+   bool                                         instancing_enable[MAX_VBS];
+   bool                                         primitive_restart;
+   uint32_t                                     topology;
 
    uint32_t                                     cs_thread_width_max;
    uint32_t                                     cs_right_mask;
+
+   struct {
+      uint32_t                                  sf[GEN7_3DSTATE_SF_length];
+      uint32_t                                  depth_stencil_state[GEN7_DEPTH_STENCIL_STATE_length];
+   } gen7;
+
+   struct {
+      uint32_t                                  sf[GEN8_3DSTATE_SF_length];
+      uint32_t                                  vf[GEN8_3DSTATE_VF_length];
+      uint32_t                                  raster[GEN8_3DSTATE_RASTER_length];
+      uint32_t                                  wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
+   } gen8;
 };
 
-struct anv_pipeline_create_info {
+struct anv_graphics_pipeline_create_info {
    bool                                         use_repclear;
    bool                                         disable_viewport;
    bool                                         disable_scissor;
@@ -824,10 +942,36 @@ struct anv_pipeline_create_info {
 };
 
 VkResult
-anv_pipeline_create(VkDevice device,
-                    const VkGraphicsPipelineCreateInfo *pCreateInfo,
-                    const struct anv_pipeline_create_info *extra,
-                    VkPipeline *pPipeline);
+anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
+                  const VkGraphicsPipelineCreateInfo *pCreateInfo,
+                  const struct anv_graphics_pipeline_create_info *extra);
+
+VkResult
+anv_graphics_pipeline_create(VkDevice device,
+                             const VkGraphicsPipelineCreateInfo *pCreateInfo,
+                             const struct anv_graphics_pipeline_create_info *extra,
+                             VkPipeline *pPipeline);
+
+VkResult
+gen7_graphics_pipeline_create(VkDevice _device,
+                              const VkGraphicsPipelineCreateInfo *pCreateInfo,
+                              const struct anv_graphics_pipeline_create_info *extra,
+                              VkPipeline *pPipeline);
+
+VkResult
+gen8_graphics_pipeline_create(VkDevice _device,
+                              const VkGraphicsPipelineCreateInfo *pCreateInfo,
+                              const struct anv_graphics_pipeline_create_info *extra,
+                              VkPipeline *pPipeline);
+VkResult
+gen7_compute_pipeline_create(VkDevice _device,
+                             const VkComputePipelineCreateInfo *pCreateInfo,
+                             VkPipeline *pPipeline);
+
+VkResult
+gen8_compute_pipeline_create(VkDevice _device,
+                             const VkComputePipelineCreateInfo *pCreateInfo,
+                             VkPipeline *pPipeline);
 
 struct anv_compiler *anv_compiler_create(struct anv_device *device);
 void anv_compiler_destroy(struct anv_compiler *compiler);
@@ -835,6 +979,7 @@ int anv_compiler_run(struct anv_compiler *compiler, struct anv_pipeline *pipelin
 void anv_compiler_free(struct anv_pipeline *pipeline);
 
 struct anv_format {
+   const VkFormat vk_format;
    const char *name;
    uint16_t surface_format; /**< RENDER_SURFACE_STATE.SurfaceFormat */
    uint8_t cpp; /**< Bytes-per-pixel of anv_format::surface_format. */
@@ -843,9 +988,36 @@ struct anv_format {
    bool has_stencil;
 };
 
+/**
+ * Stencil formats are often a special case. To reduce the number of lookups
+ * into the VkFormat-to-anv_format translation table when working with
+ * stencil, here is the handle to the table's entry for VK_FORMAT_S8_UINT.
+ */
+extern const struct anv_format *const anv_format_s8_uint;
+
 const struct anv_format *
 anv_format_for_vk_format(VkFormat format);
-bool anv_is_vk_format_depth_or_stencil(VkFormat format);
+
+static inline bool
+anv_format_is_color(const struct anv_format *format)
+{
+   return !format->depth_format && !format->has_stencil;
+}
+
+static inline bool
+anv_format_is_depth_or_stencil(const struct anv_format *format)
+{
+   return format->depth_format || format->has_stencil;
+}
+
+struct anv_image_view_info {
+   uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
+   bool is_array:1; /**< RENDER_SURFACE_STATE.SurfaceArray */
+   bool is_cube:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */
+};
+
+const struct anv_image_view_info *
+anv_image_view_info_for_vk_image_view_type(VkImageViewType type);
 
 /**
  * A proxy for the color surfaces, depth surfaces, and stencil surfaces.
@@ -876,8 +1048,8 @@ struct anv_surface {
 
 struct anv_image {
    VkImageType type;
+   const struct anv_format *format;
    VkExtent3D extent;
-   VkFormat format;
    uint32_t levels;
    uint32_t array_size;
 
@@ -888,16 +1060,29 @@ struct anv_image {
    struct anv_bo *bo;
    VkDeviceSize offset;
 
-   struct anv_swap_chain *swap_chain;
-
    /** RENDER_SURFACE_STATE.SurfaceType */
    uint8_t surf_type;
 
-   /** Primary surface is either color or depth. */
-   struct anv_surface primary_surface;
+   /**
+    * Image subsurfaces
+    *
+    * For each foo, anv_image::foo_surface is valid if and only if
+    * anv_image::format has a foo aspect.
+    *
+    * The hardware requires that the depth buffer and stencil buffer be
+    * separate surfaces.  From Vulkan's perspective, though, depth and stencil
+    * reside in the same VkImage.  To satisfy both the hardware and Vulkan, we
+    * allocate the depth and stencil buffers as separate surfaces in the same
+    * bo.
+    */
+   union {
+      struct anv_surface color_surface;
 
-   /** Stencil surface is optional. */
-   struct anv_surface stencil_surface;
+      struct {
+         struct anv_surface depth_surface;
+         struct anv_surface stencil_surface;
+      };
+   };
 };
 
 struct anv_surface_view {
@@ -905,7 +1090,7 @@ struct anv_surface_view {
    struct anv_bo *bo;
    uint32_t offset; /**< VkBufferCreateInfo::offset */
    uint32_t range; /**< VkBufferCreateInfo::range */
-   VkFormat format; /**< VkBufferCreateInfo::format */
+   const struct anv_format *format; /**< VkBufferCreateInfo::format */
 };
 
 struct anv_buffer_view {
@@ -934,41 +1119,72 @@ struct anv_color_attachment_view {
 
 struct anv_depth_stencil_view {
    struct anv_attachment_view base;
-
-   struct anv_bo *bo;
-
-   uint32_t depth_offset; /**< Offset into bo. */
-   uint32_t depth_stride; /**< 3DSTATE_DEPTH_BUFFER.SurfacePitch */
-   uint32_t depth_format; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
-   uint16_t depth_qpitch; /**< 3DSTATE_DEPTH_BUFFER.SurfaceQPitch */
-
-   uint32_t stencil_offset; /**< Offset into bo. */
-   uint32_t stencil_stride; /**< 3DSTATE_STENCIL_BUFFER.SurfacePitch */
-   uint16_t stencil_qpitch; /**< 3DSTATE_STENCIL_BUFFER.SurfaceQPitch */
+   const struct anv_image *image; /**< VkAttachmentViewCreateInfo::image */
+   const struct anv_format *format; /**< VkAttachmentViewCreateInfo::format */
 };
 
 struct anv_image_create_info {
    const VkImageCreateInfo *vk_info;
    bool force_tile_mode;
    uint8_t tile_mode;
+   uint32_t stride;
 };
 
 VkResult anv_image_create(VkDevice _device,
                           const struct anv_image_create_info *info,
                           VkImage *pImage);
 
+struct anv_surface *
+anv_image_get_surface_for_aspect(struct anv_image *image, VkImageAspect aspect);
+
+struct anv_surface *
+anv_image_get_surface_for_color_attachment(struct anv_image *image);
+
 void anv_image_view_init(struct anv_image_view *view,
                          struct anv_device *device,
                          const VkImageViewCreateInfo* pCreateInfo,
                          struct anv_cmd_buffer *cmd_buffer);
 
+void
+gen7_image_view_init(struct anv_image_view *iview,
+                     struct anv_device *device,
+                     const VkImageViewCreateInfo* pCreateInfo,
+                     struct anv_cmd_buffer *cmd_buffer);
+
+void
+gen8_image_view_init(struct anv_image_view *iview,
+                     struct anv_device *device,
+                     const VkImageViewCreateInfo* pCreateInfo,
+                     struct anv_cmd_buffer *cmd_buffer);
+
 void anv_color_attachment_view_init(struct anv_color_attachment_view *view,
                                     struct anv_device *device,
                                     const VkAttachmentViewCreateInfo* pCreateInfo,
                                     struct anv_cmd_buffer *cmd_buffer);
-void anv_fill_buffer_surface_state(void *state, VkFormat format,
+
+void gen7_color_attachment_view_init(struct anv_color_attachment_view *aview,
+                                     struct anv_device *device,
+                                     const VkAttachmentViewCreateInfo* pCreateInfo,
+                                     struct anv_cmd_buffer *cmd_buffer);
+
+void gen8_color_attachment_view_init(struct anv_color_attachment_view *aview,
+                                     struct anv_device *device,
+                                     const VkAttachmentViewCreateInfo* pCreateInfo,
+                                     struct anv_cmd_buffer *cmd_buffer);
+
+VkResult anv_buffer_view_create(struct anv_device *device,
+                                const VkBufferViewCreateInfo *pCreateInfo,
+                                struct anv_buffer_view **view_out);
+
+void anv_fill_buffer_surface_state(struct anv_device *device, void *state,
+                                   const struct anv_format *format,
                                    uint32_t offset, uint32_t range);
 
+void gen7_fill_buffer_surface_state(void *state, const struct anv_format *format,
+                                    uint32_t offset, uint32_t range);
+void gen8_fill_buffer_surface_state(void *state, const struct anv_format *format,
+                                    uint32_t offset, uint32_t range);
+
 void anv_surface_view_fini(struct anv_device *device,
                            struct anv_surface_view *view);
 
@@ -998,7 +1214,7 @@ struct anv_subpass {
 };
 
 struct anv_render_pass_attachment {
-   VkFormat                                     format;
+   const struct anv_format                      *format;
    uint32_t                                     samples;
    VkAttachmentLoadOp                           load_op;
    VkAttachmentLoadOp                           stencil_load_op;
@@ -1008,10 +1224,26 @@ struct anv_render_pass {
    uint32_t                                     attachment_count;
    uint32_t                                     subpass_count;
 
+   uint32_t                                     num_color_clear_attachments;
+   bool                                         has_depth_clear_attachment;
+   bool                                         has_stencil_clear_attachment;
+
    struct anv_render_pass_attachment *          attachments;
    struct anv_subpass                           subpasses[0];
 };
 
+struct anv_query_pool_slot {
+   uint64_t begin;
+   uint64_t end;
+   uint64_t available;
+};
+
+struct anv_query_pool {
+   VkQueryType                                  type;
+   uint32_t                                     slots;
+   struct anv_bo                                bo;
+};
+
 void anv_device_init_meta(struct anv_device *device);
 void anv_device_finish_meta(struct anv_device *device);
 
@@ -1053,8 +1285,8 @@ ANV_DEFINE_HANDLE_CASTS(anv_device, VkDevice)
 ANV_DEFINE_HANDLE_CASTS(anv_instance, VkInstance)
 ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
-ANV_DEFINE_HANDLE_CASTS(anv_swap_chain, VkSwapChainWSI);
 
+ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_cmd_pool, VkCmdPool)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_attachment_view, VkAttachmentView)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer, VkBuffer)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_buffer_view, VkBufferView);