anv: Only include the pack headers where needed
[mesa.git] / src / vulkan / anv_private.h
index 06ef4e123f35399d2a260745bd4b8f4672b0008e..a8ed5e8a7e266e18d3f3962b390a697204529ff1 100644 (file)
 #define VK_PROTOTYPES
 #include <vulkan/vulkan.h>
 #include <vulkan/vulkan_intel.h>
-#include <vulkan/vk_wsi_swapchain.h>
-#include <vulkan/vk_wsi_device_swapchain.h>
+#include <vulkan/vk_ext_khr_swapchain.h>
+#include <vulkan/vk_ext_khr_device_swapchain.h>
 
 #include "anv_entrypoints.h"
-
 #include "brw_context.h"
+#include "isl.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
+#define ICD_LOADER_MAGIC   0x01CDC0DE
+
+typedef union _VK_LOADER_DATA {
+  uintptr_t loaderMagic;
+  void *loaderData;
+} VK_LOADER_DATA;
+
 #define anv_noreturn __attribute__((__noreturn__))
 #define anv_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
 
@@ -108,6 +115,11 @@ anv_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
         (b) = __builtin_ffs(__dword) - 1, __dword;      \
         __dword &= ~(1 << (b)))
 
+#define typed_memcpy(dest, src, count) ({ \
+   static_assert(sizeof(*src) == sizeof(*dest), ""); \
+   memcpy((dest), (src), (count) * sizeof(*(src))); \
+})
+
 /* Define no kernel as 1, since that's an illegal offset for a kernel */
 #define NO_KERNEL 1
 
@@ -128,6 +140,7 @@ VkResult __vk_errorf(VkResult error, const char *file, int line, const char *for
 #define vk_errorf(error, format, ...) __vk_errorf(error, __FILE__, __LINE__, format, ## __VA_ARGS__);
 #else
 #define vk_error(error) error
+#define vk_errorf(error, format, ...) error
 #endif
 
 void __anv_finishme(const char *file, int line, const char *format, ...)
@@ -151,6 +164,16 @@ void anv_loge_v(const char *format, va_list va);
 #define anv_assert(x)
 #endif
 
+/**
+ * If a block of code is annotated with anv_validate, then the block runs only
+ * in debug builds.
+ */
+#ifdef DEBUG
+#define anv_validate if (1)
+#else
+#define anv_validate if (0)
+#endif
+
 void anv_abortf(const char *format, ...) anv_noreturn anv_printflike(1, 2);
 void anv_abortfv(const char *format, va_list va) anv_noreturn;
 
@@ -192,6 +215,21 @@ anv_vector_length(struct anv_vector *queue)
    return (queue->head - queue->tail) / queue->element_size;
 }
 
+static inline void *
+anv_vector_head(struct anv_vector *vector)
+{
+   assert(vector->tail < vector->head);
+   return (void *)((char *)vector->data +
+                   ((vector->head - vector->element_size) &
+                    (vector->size - 1)));
+}
+
+static inline void *
+anv_vector_tail(struct anv_vector *vector)
+{
+   return (void *)((char *)vector->data + (vector->tail & (vector->size - 1)));
+}
+
 static inline void
 anv_vector_finish(struct anv_vector *queue)
 {
@@ -206,8 +244,19 @@ anv_vector_finish(struct anv_vector *queue)
 
 struct anv_bo {
    int gem_handle;
+
+   /* Index into the current validation list.  This is used by the
+    * validation list building alrogithm to track which buffers are already
+    * in the validation list so that we can ensure uniqueness.
+    */
    uint32_t index;
+
+   /* Last known offset.  This value is provided by the kernel when we
+    * execbuf and is used as the presumed offset for the next bunch of
+    * relocations.
+    */
    uint64_t offset;
+
    uint64_t size;
    void *map;
 };
@@ -218,7 +267,7 @@ struct anv_bo {
  */
 union anv_free_list {
    struct {
-      uint32_t offset;
+      int32_t offset;
 
       /* A simple count that is incremented every time the head changes. */
       uint32_t count;
@@ -242,6 +291,22 @@ struct anv_block_pool {
    struct anv_device *device;
 
    struct anv_bo bo;
+
+   /* The offset from the start of the bo to the "center" of the block
+    * pool.  Pointers to allocated blocks are given by
+    * bo.map + center_bo_offset + offsets.
+    */
+   uint32_t center_bo_offset;
+
+   /* Current memory map of the block pool.  This pointer may or may not
+    * point to the actual beginning of the block pool memory.  If
+    * anv_block_pool_alloc_back has ever been called, then this pointer
+    * will point to the "center" position of the buffer and all offsets
+    * (negative or positive) given out by the block pool alloc functions
+    * will be valid relative to this pointer.
+    *
+    * In particular, map == bo.map + center_offset
+    */
    void *map;
    int fd;
 
@@ -255,16 +320,27 @@ struct anv_block_pool {
 
    union anv_free_list free_list;
    struct anv_block_state state;
+
+   union anv_free_list back_free_list;
+   struct anv_block_state back_state;
 };
 
+/* Block pools are backed by a fixed-size 2GB memfd */
+#define BLOCK_POOL_MEMFD_SIZE (1ull << 32)
+
+/* The center of the block pool is also the middle of the memfd.  This may
+ * change in the future if we decide differently for some reason.
+ */
+#define BLOCK_POOL_MEMFD_CENTER (BLOCK_POOL_MEMFD_SIZE / 2)
+
 static inline uint32_t
 anv_block_pool_size(struct anv_block_pool *pool)
 {
-   return pool->state.end;
+   return pool->state.end + pool->back_state.end;
 }
 
 struct anv_state {
-   uint32_t offset;
+   int32_t offset;
    uint32_t alloc_size;
    void *map;
 };
@@ -295,8 +371,9 @@ struct anv_state_stream {
 void anv_block_pool_init(struct anv_block_pool *pool,
                          struct anv_device *device, uint32_t block_size);
 void anv_block_pool_finish(struct anv_block_pool *pool);
-uint32_t anv_block_pool_alloc(struct anv_block_pool *pool);
-void anv_block_pool_free(struct anv_block_pool *pool, uint32_t offset);
+int32_t anv_block_pool_alloc(struct anv_block_pool *pool);
+int32_t anv_block_pool_alloc_back(struct anv_block_pool *pool);
+void anv_block_pool_free(struct anv_block_pool *pool, int32_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);
@@ -327,66 +404,89 @@ void anv_bo_pool_finish(struct anv_bo_pool *pool);
 VkResult anv_bo_pool_alloc(struct anv_bo_pool *pool, struct anv_bo *bo);
 void anv_bo_pool_free(struct anv_bo_pool *pool, const struct anv_bo *bo);
 
+
+void *anv_resolve_entrypoint(uint32_t index);
+
+extern struct anv_dispatch_table dtable;
+
+#define ANV_CALL(func) ({ \
+   if (dtable.func == NULL) { \
+      size_t idx = offsetof(struct anv_dispatch_table, func) / sizeof(void *); \
+      dtable.entrypoints[idx] = anv_resolve_entrypoint(idx); \
+   } \
+   dtable.func; \
+})
+
+
 struct anv_physical_device {
+    VK_LOADER_DATA                              _loader_data;
+
     struct anv_instance *                       instance;
     uint32_t                                    chipset_id;
     const char *                                path;
     const char *                                name;
     const struct brw_device_info *              info;
     uint64_t                                    aperture_size;
+    struct brw_compiler *                       compiler;
+    struct isl_device                           isl_dev;
 };
 
+bool anv_is_scalar_shader_stage(const struct brw_compiler *compiler,
+                                VkShaderStage stage);
+
 struct anv_instance {
+    VK_LOADER_DATA                              _loader_data;
+
     void *                                      pAllocUserData;
     PFN_vkAllocFunction                         pfnAlloc;
     PFN_vkFreeFunction                          pfnFree;
     uint32_t                                    apiVersion;
-    uint32_t                                    physicalDeviceCount;
+    int                                         physicalDeviceCount;
     struct anv_physical_device                  physicalDevice;
+
+    struct anv_wsi_implementation *             wsi_impl[VK_PLATFORM_NUM_KHR];
 };
 
+VkResult anv_init_wsi(struct anv_instance *instance);
+void anv_finish_wsi(struct anv_instance *instance);
+
 struct anv_meta_state {
    struct {
-      VkPipeline                                pipeline;
+      struct anv_pipeline *color_pipeline;
+      struct anv_pipeline *depth_only_pipeline;
+      struct anv_pipeline *stencil_only_pipeline;
+      struct anv_pipeline *depthstencil_pipeline;
    } clear;
 
    struct {
-      VkPipeline                                pipeline;
+      VkRenderPass render_pass;
+
+      /** Pipeline that blits from a 2D image. */
+      VkPipeline pipeline_2d_src;
+
+      /** Pipeline that blits from a 3D image. */
+      VkPipeline pipeline_3d_src;
+
       VkPipelineLayout                          pipeline_layout;
       VkDescriptorSetLayout                     ds_layout;
    } blit;
-
-   struct {
-      VkDynamicRasterState                      rs_state;
-      VkDynamicColorBlendState                  cb_state;
-      VkDynamicDepthStencilState                ds_state;
-   } shared;
 };
 
 struct anv_queue {
+    VK_LOADER_DATA                              _loader_data;
+
     struct anv_device *                         device;
 
     struct anv_state_pool *                     pool;
-
-    /**
-     * Serial number of the most recently completed batch executed on the
-     * engine.
-     */
-    struct anv_state                            completed_serial;
-
-    /**
-     * The next batch submitted to the engine will be assigned this serial
-     * number.
-     */
-    uint32_t                                    next_serial;
-
-    uint32_t                                    last_collected_serial;
 };
 
 struct anv_device {
+    VK_LOADER_DATA                              _loader_data;
+
     struct anv_instance *                       instance;
     uint32_t                                    chipset_id;
     struct brw_device_info                      info;
+    struct isl_device                           isl_dev;
     int                                         context_id;
     int                                         fd;
 
@@ -399,6 +499,8 @@ struct anv_device {
     struct anv_block_pool                       surface_state_block_pool;
     struct anv_state_pool                       surface_state_pool;
 
+    struct anv_bo                               workaround_bo;
+
     struct anv_meta_state                       meta_state;
 
     struct anv_state                            border_colors;
@@ -407,10 +509,19 @@ struct anv_device {
 
     struct anv_block_pool                       scratch_block_pool;
 
-    struct anv_compiler *                       compiler;
     pthread_mutex_t                             mutex;
 };
 
+void *
+anv_instance_alloc(struct anv_instance *        instance,
+                   size_t                       size,
+                   size_t                       alignment,
+                   VkSystemAllocType            allocType);
+
+void
+anv_instance_free(struct anv_instance *         instance,
+                  void *                        mem);
+
 void *
 anv_device_alloc(struct anv_device *            device,
                  size_t                         size,
@@ -468,6 +579,9 @@ struct anv_batch_bo {
    /* Bytes actually consumed in this batch BO */
    size_t                                       length;
 
+   /* Last seen surface state block pool bo offset */
+   uint32_t                                     last_ss_pool_bo_offset;
+
    struct anv_reloc_list                        relocs;
 };
 
@@ -513,11 +627,6 @@ __gen_combine_address(struct anv_batch *batch, void *location,
    }
 }
 
-#include "gen7_pack.h"
-#include "gen75_pack.h"
-#undef GEN8_3DSTATE_MULTISAMPLE
-#include "gen8_pack.h"
-
 #define anv_batch_emit(batch, cmd, ...) do {                            \
       void *__dst = anv_batch_emit_dwords(batch, cmd ## _length);       \
       struct cmd __template = {                                         \
@@ -550,11 +659,11 @@ __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 GEN7_MOCS (struct GEN7_MEMORY_OBJECT_CONTROL_STATE) {  \
+   .GraphicsDataTypeGFDT                        = 0,           \
+   .LLCCacheabilityControlLLCCC                 = 0,           \
+   .L3CacheabilityControlL3CC                   = 1,           \
+}
 
 #define GEN8_MOCS {                                     \
       .MemoryTypeLLCeLLCCacheabilityControl = WB,       \
@@ -568,65 +677,76 @@ struct anv_device_memory {
    void *                                       map;
 };
 
-struct anv_dynamic_vp_state {
-   struct anv_state sf_clip_vp;
-   struct anv_state cc_vp;
-   struct anv_state scissor;
+/**
+ * Header for Vertex URB Entry (VUE)
+ */
+struct anv_vue_header {
+   uint32_t Reserved;
+   uint32_t RTAIndex; /* RenderTargetArrayIndex */
+   uint32_t ViewportIndex;
+   float PointWidth;
 };
 
-struct anv_dynamic_rs_state {
-   struct {
-      uint32_t sf[GEN7_3DSTATE_SF_length];
-   } gen7;
+struct anv_descriptor_set_binding_layout {
+   /* Number of array elements in this binding */
+   uint16_t array_size;
 
-   struct {
-      uint32_t sf[GEN8_3DSTATE_SF_length];
-      uint32_t raster[GEN8_3DSTATE_RASTER_length];
-   } gen8;
-};
+   /* Index into the flattend descriptor set */
+   uint16_t descriptor_index;
 
-struct anv_dynamic_ds_state {
-   struct {
-      uint32_t depth_stencil_state[GEN7_DEPTH_STENCIL_STATE_length];
-      uint32_t color_calc_state[GEN8_COLOR_CALC_STATE_length];
-   } gen7;
+   /* Index into the dynamic state array for a dynamic buffer */
+   int16_t dynamic_offset_index;
 
    struct {
-      uint32_t wm_depth_stencil[GEN8_3DSTATE_WM_DEPTH_STENCIL_length];
-      uint32_t color_calc_state[GEN8_COLOR_CALC_STATE_length];
-   } gen8;
-};
+      /* Index into the binding table for the associated surface */
+      int16_t surface_index;
 
-struct anv_dynamic_cb_state {
-   uint32_t color_calc_state[GEN8_COLOR_CALC_STATE_length];
-
-};
+      /* Index into the sampler table for the associated sampler */
+      int16_t sampler_index;
+   } stage[VK_SHADER_STAGE_NUM];
 
-struct anv_descriptor_slot {
-   int8_t dynamic_slot;
-   uint8_t index;
+   /* Immutable samplers (or NULL if no immutable samplers) */
+   struct anv_sampler **immutable_samplers;
 };
 
 struct anv_descriptor_set_layout {
-   struct {
-      uint32_t surface_count;
-      struct anv_descriptor_slot *surface_start;
-      uint32_t sampler_count;
-      struct anv_descriptor_slot *sampler_start;
-   } stage[VK_SHADER_STAGE_NUM];
+   /* Number of bindings in this descriptor set */
+   uint16_t binding_count;
 
-   uint32_t count;
-   uint32_t num_dynamic_buffers;
-   uint32_t shader_stages;
-   struct anv_descriptor_slot entries[0];
+   /* Total size of the descriptor set with room for all array entries */
+   uint16_t size;
+
+   /* Shader stages affected by this descriptor set */
+   uint16_t shader_stages;
+
+   /* Number of dynamic offsets used by this descriptor set */
+   uint16_t dynamic_offset_count;
+
+   /* Bindings in this descriptor set */
+   struct anv_descriptor_set_binding_layout binding[0];
 };
 
 struct anv_descriptor {
-   struct anv_sampler *sampler;
-   struct anv_surface_view *view;
+   VkDescriptorType type;
+
+   union {
+      struct {
+         union {
+            struct anv_image_view *image_view;
+         };
+         struct anv_sampler *sampler;
+      };
+
+      struct {
+         struct anv_buffer *buffer;
+         uint64_t offset;
+         uint64_t range;
+      };
+   };
 };
 
 struct anv_descriptor_set {
+   const struct anv_descriptor_set_layout *layout;
    struct anv_descriptor descriptors[0];
 };
 
@@ -639,24 +759,44 @@ void
 anv_descriptor_set_destroy(struct anv_device *device,
                            struct anv_descriptor_set *set);
 
-#define MAX_VBS   32
-#define MAX_SETS   8
-#define MAX_RTS    8
+#define MAX_VBS         32
+#define MAX_SETS         8
+#define MAX_RTS          8
+#define MAX_VIEWPORTS   16
+#define MAX_SCISSORS    16
 #define MAX_PUSH_CONSTANTS_SIZE 128
+#define MAX_DYNAMIC_BUFFERS 16
+#define MAX_IMAGES 8
+
+struct anv_pipeline_binding {
+   /* The descriptor set this surface corresponds to */
+   uint16_t set;
+
+   /* Offset into the descriptor set */
+   uint16_t offset;
+};
 
 struct anv_pipeline_layout {
    struct {
       struct anv_descriptor_set_layout *layout;
-      uint32_t surface_start[VK_SHADER_STAGE_NUM];
-      uint32_t sampler_start[VK_SHADER_STAGE_NUM];
+      uint32_t dynamic_offset_start;
+      struct {
+         uint32_t surface_start;
+         uint32_t sampler_start;
+      } stage[VK_SHADER_STAGE_NUM];
    } set[MAX_SETS];
 
    uint32_t num_sets;
 
    struct {
+      bool has_dynamic_offsets;
       uint32_t surface_count;
+      struct anv_pipeline_binding *surface_to_descriptor;
       uint32_t sampler_count;
+      struct anv_pipeline_binding *sampler_to_descriptor;
    } stage[VK_SHADER_STAGE_NUM];
+
+   struct anv_pipeline_binding entries[0];
 };
 
 struct anv_buffer {
@@ -665,62 +805,128 @@ struct anv_buffer {
 
    /* Set when bound */
    struct anv_bo *                              bo;
-   VkDeviceSize                                 offset;   
+   VkDeviceSize                                 offset;
 };
 
-#define ANV_CMD_BUFFER_PIPELINE_DIRTY           (1 << 0)
-#define ANV_CMD_BUFFER_RS_DIRTY                 (1 << 2)
-#define ANV_CMD_BUFFER_DS_DIRTY                 (1 << 3)
-#define ANV_CMD_BUFFER_CB_DIRTY                 (1 << 4)
-#define ANV_CMD_BUFFER_VP_DIRTY                 (1 << 5)
-#define ANV_CMD_BUFFER_INDEX_BUFFER_DIRTY       (1 << 6)
+enum anv_cmd_dirty_bits {
+   ANV_CMD_DIRTY_DYNAMIC_VIEWPORT                  = 1 << 0, /* VK_DYNAMIC_STATE_VIEWPORT */
+   ANV_CMD_DIRTY_DYNAMIC_SCISSOR                   = 1 << 1, /* VK_DYNAMIC_STATE_SCISSOR */
+   ANV_CMD_DIRTY_DYNAMIC_LINE_WIDTH                = 1 << 2, /* VK_DYNAMIC_STATE_LINE_WIDTH */
+   ANV_CMD_DIRTY_DYNAMIC_DEPTH_BIAS                = 1 << 3, /* VK_DYNAMIC_STATE_DEPTH_BIAS */
+   ANV_CMD_DIRTY_DYNAMIC_BLEND_CONSTANTS           = 1 << 4, /* VK_DYNAMIC_STATE_BLEND_CONSTANTS */
+   ANV_CMD_DIRTY_DYNAMIC_DEPTH_BOUNDS              = 1 << 5, /* VK_DYNAMIC_STATE_DEPTH_BOUNDS */
+   ANV_CMD_DIRTY_DYNAMIC_STENCIL_COMPARE_MASK      = 1 << 6, /* VK_DYNAMIC_STATE_STENCIL_COMPARE_MASK */
+   ANV_CMD_DIRTY_DYNAMIC_STENCIL_WRITE_MASK        = 1 << 7, /* VK_DYNAMIC_STATE_STENCIL_WRITE_MASK */
+   ANV_CMD_DIRTY_DYNAMIC_STENCIL_REFERENCE         = 1 << 8, /* VK_DYNAMIC_STATE_STENCIL_REFERENCE */
+   ANV_CMD_DIRTY_DYNAMIC_ALL                       = (1 << 9) - 1,
+   ANV_CMD_DIRTY_PIPELINE                          = 1 << 9,
+   ANV_CMD_DIRTY_INDEX_BUFFER                      = 1 << 10,
+};
+typedef uint32_t anv_cmd_dirty_mask_t;
 
 struct anv_vertex_binding {
    struct anv_buffer *                          buffer;
    VkDeviceSize                                 offset;
 };
 
-struct anv_descriptor_set_binding {
-   struct anv_descriptor_set *                  set;
-   uint32_t                                     dynamic_offsets[128];
-};
+struct anv_push_constants {
+   /* Current allocated size of this push constants data structure.
+    * Because a decent chunk of it may not be used (images on SKL, for
+    * instance), we won't actually allocate the entire structure up-front.
+    */
+   uint32_t size;
+
+   /* Push constant data provided by the client through vkPushConstants */
+   uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
+
+   /* Our hardware only provides zero-based vertex and instance id so, in
+    * order to satisfy the vulkan requirements, we may have to push one or
+    * both of these into the shader.
+    */
+   uint32_t base_vertex;
+   uint32_t base_instance;
+
+   /* Offsets and ranges for dynamically bound buffers */
+   struct {
+      uint32_t offset;
+      uint32_t range;
+   } dynamic[MAX_DYNAMIC_BUFFERS];
 
-struct anv_push_constant_data {
-    uint8_t client_data[MAX_PUSH_CONSTANTS_SIZE];
-    uint8_t driver_data[0];
+   /* Image data for image_load_store on pre-SKL */
+   struct brw_image_param images[MAX_IMAGES];
 };
 
-struct anv_push_constants {
-    uint32_t driver_data_size;
-    struct anv_push_constant_data *data;
+struct anv_dynamic_state {
+   struct {
+      uint32_t                                  count;
+      VkViewport                                viewports[MAX_VIEWPORTS];
+   } viewport;
+
+   struct {
+      uint32_t                                  count;
+      VkRect2D                                  scissors[MAX_SCISSORS];
+   } scissor;
+
+   float                                        line_width;
+
+   struct {
+      float                                     bias;
+      float                                     clamp;
+      float                                     slope_scaled;
+   } depth_bias;
+
+   float                                        blend_constants[4];
+
+   struct {
+      float                                     min;
+      float                                     max;
+   } depth_bounds;
+
+   struct {
+      uint32_t                                  front;
+      uint32_t                                  back;
+   } stencil_compare_mask;
+
+   struct {
+      uint32_t                                  front;
+      uint32_t                                  back;
+   } stencil_write_mask;
+
+   struct {
+      uint32_t                                  front;
+      uint32_t                                  back;
+   } stencil_reference;
 };
 
+extern const struct anv_dynamic_state default_dynamic_state;
+
+void anv_dynamic_state_copy(struct anv_dynamic_state *dest,
+                            const struct anv_dynamic_state *src,
+                            uint32_t copy_mask);
+
 /** State required while building cmd buffer */
 struct anv_cmd_state {
    uint32_t                                     current_pipeline;
    uint32_t                                     vb_dirty;
-   uint32_t                                     dirty;
-   uint32_t                                     compute_dirty;
-   uint32_t                                     descriptors_dirty;
-   uint32_t                                     push_constants_dirty;
+   anv_cmd_dirty_mask_t                         dirty;
+   anv_cmd_dirty_mask_t                         compute_dirty;
+   VkShaderStageFlags                           descriptors_dirty;
+   VkShaderStageFlags                           push_constants_dirty;
    uint32_t                                     scratch_size;
    struct anv_pipeline *                        pipeline;
    struct anv_pipeline *                        compute_pipeline;
    struct anv_framebuffer *                     framebuffer;
    struct anv_render_pass *                     pass;
    struct anv_subpass *                         subpass;
-   struct anv_dynamic_rs_state *                rs_state;
-   struct anv_dynamic_ds_state *                ds_state;
-   struct anv_dynamic_vp_state *                vp_state;
-   struct anv_dynamic_cb_state *                cb_state;
-   uint32_t                                     state_vf[GEN8_3DSTATE_VF_length];
+   uint32_t                                     state_vf[2];
    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 anv_descriptor_set *                  descriptors[MAX_SETS];
+   struct anv_push_constants *                  push_constants[VK_SHADER_STAGE_NUM];
+   struct anv_dynamic_state                     dynamic;
 
    struct {
       struct anv_buffer *                       index_buffer;
-      uint32_t                                  index_type;
+      uint32_t                                  index_type; /**< 3DSTATE_INDEX_BUFFER.IndexFormat */
       uint32_t                                  index_offset;
    } gen7;
 };
@@ -739,6 +945,8 @@ enum anv_cmd_buffer_exec_mode {
 };
 
 struct anv_cmd_buffer {
+   VK_LOADER_DATA                               _loader_data;
+
    struct anv_device *                          device;
 
    struct list_head                             pool_link;
@@ -750,8 +958,6 @@ struct anv_cmd_buffer {
     * These fields are initialized by anv_cmd_buffer_init_batch_bo_chain().
     */
    struct list_head                             batch_bos;
-   struct list_head                             surface_bos;
-   uint32_t                                     surface_next;
    enum anv_cmd_buffer_exec_mode                exec_mode;
 
    /* A vector of anv_batch_bo pointers for every batch or surface buffer
@@ -761,6 +967,14 @@ struct anv_cmd_buffer {
     */
    struct anv_vector                            seen_bbos;
 
+   /* A vector of int32_t's for every block of binding tables.
+    *
+    * initialized by anv_cmd_buffer_init_batch_bo_chain()
+    */
+   struct anv_vector                            bt_blocks;
+   uint32_t                                     bt_next;
+   struct anv_reloc_list                        surface_relocs;
+
    /* Information needed for execbuf
     *
     * These fields are generated by anv_cmd_buffer_prepare_execbuf().
@@ -803,7 +1017,7 @@ 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);
+void gen7_cmd_buffer_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,
@@ -814,18 +1028,22 @@ struct anv_state anv_cmd_buffer_merge_dynamic(struct anv_cmd_buffer *cmd_buffer,
 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_address
+anv_cmd_buffer_surface_base_address(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);
+anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
+                                   uint32_t entries, uint32_t *state_offset);
+struct anv_state
+anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer);
 struct anv_state
 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
                                    uint32_t size, uint32_t alignment);
 
-VkResult anv_cmd_buffer_new_surface_state_bo(struct anv_cmd_buffer *cmd_buffer);
+VkResult
+anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer);
+
+void gen8_cmd_buffer_emit_viewport(struct anv_cmd_buffer *cmd_buffer);
+void gen7_cmd_buffer_emit_scissor(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);
@@ -848,7 +1066,7 @@ anv_cmd_buffer_push_constants(struct anv_cmd_buffer *cmd_buffer,
 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 *
+const struct anv_image_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);
@@ -860,7 +1078,11 @@ struct anv_fence {
    bool ready;
 };
 
+struct nir_shader;
+
 struct anv_shader_module {
+   struct nir_shader *                          nir;
+
    uint32_t                                     size;
    char                                         data[0];
 };
@@ -873,9 +1095,11 @@ struct anv_shader {
 struct anv_pipeline {
    struct anv_device *                          device;
    struct anv_batch                             batch;
-   uint32_t                                     batch_data[256];
+   uint32_t                                     batch_data[512];
    struct anv_reloc_list                        batch_relocs;
-   struct anv_shader *                          shaders[VK_SHADER_STAGE_NUM];
+   uint32_t                                     dynamic_state_mask;
+   struct anv_dynamic_state                     dynamic_state;
+
    struct anv_pipeline_layout *                 layout;
    bool                                         use_repclear;
 
@@ -896,7 +1120,7 @@ struct anv_pipeline {
       uint32_t                                  nr_gs_entries;
    } urb;
 
-   uint32_t                                     active_stages;
+   VkShaderStageFlags                           active_stages;
    struct anv_state_stream                      program_stream;
    struct anv_state                             blend_state;
    uint32_t                                     vs_simd8;
@@ -921,15 +1145,15 @@ struct anv_pipeline {
    uint32_t                                     cs_right_mask;
 
    struct {
-      uint32_t                                  sf[GEN7_3DSTATE_SF_length];
-      uint32_t                                  depth_stencil_state[GEN7_DEPTH_STENCIL_STATE_length];
+      uint32_t                                  sf[7];
+      uint32_t                                  depth_stencil_state[3];
    } 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];
+      uint32_t                                  sf[4];
+      uint32_t                                  vf[2];
+      uint32_t                                  raster[5];
+      uint32_t                                  wm_depth_stencil[3];
    } gen8;
 };
 
@@ -946,6 +1170,11 @@ anv_pipeline_init(struct anv_pipeline *pipeline, struct anv_device *device,
                   const VkGraphicsPipelineCreateInfo *pCreateInfo,
                   const struct anv_graphics_pipeline_create_info *extra);
 
+VkResult
+anv_pipeline_compile_cs(struct anv_pipeline *pipeline,
+                        const VkComputePipelineCreateInfo *info,
+                        struct anv_shader *shader);
+
 VkResult
 anv_graphics_pipeline_create(VkDevice device,
                              const VkGraphicsPipelineCreateInfo *pCreateInfo,
@@ -973,16 +1202,11 @@ 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);
-int anv_compiler_run(struct anv_compiler *compiler, struct anv_pipeline *pipeline);
-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. */
+   enum isl_format surface_format; /**< RENDER_SURFACE_STATE.SurfaceFormat */
+   const struct isl_format_layout *isl_layout;
    uint8_t num_channels;
    uint16_t depth_format; /**< 3DSTATE_DEPTH_BUFFER.SurfaceFormat */
    bool has_stencil;
@@ -1016,7 +1240,7 @@ struct anv_image_view_info {
    bool is_cube:1; /**< RENDER_SURFACE_STATE.CubeFaceEnable* */
 };
 
-const struct anv_image_view_info *
+struct anv_image_view_info
 anv_image_view_info_for_vk_image_view_type(VkImageViewType type);
 
 /**
@@ -1043,7 +1267,7 @@ struct anv_surface {
    uint8_t v_align; /**< RENDER_SURFACE_STATE.SurfaceVerticalAlignment */
    /** \} */
 
-   uint8_t tile_mode; /**< RENDER_SURFACE_STATE.TileMode */
+   enum isl_tiling tiling;
 };
 
 struct anv_image {
@@ -1052,6 +1276,7 @@ struct anv_image {
    VkExtent3D extent;
    uint32_t levels;
    uint32_t array_size;
+   VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
 
    VkDeviceSize size;
    uint32_t alignment;
@@ -1060,8 +1285,10 @@ struct anv_image {
    struct anv_bo *bo;
    VkDeviceSize offset;
 
-   /** RENDER_SURFACE_STATE.SurfaceType */
-   uint8_t surf_type;
+   uint8_t surface_type; /**< RENDER_SURFACE_STATE.SurfaceType */
+
+   bool needs_nonrt_surface_state:1;
+   bool needs_color_rt_surface_state:1;
 
    /**
     * Image subsurfaces
@@ -1085,48 +1312,24 @@ struct anv_image {
    };
 };
 
-struct anv_surface_view {
-   struct anv_state surface_state; /**< RENDER_SURFACE_STATE */
-   struct anv_bo *bo;
-   uint32_t offset; /**< VkBufferCreateInfo::offset */
-   uint32_t range; /**< VkBufferCreateInfo::range */
-   const struct anv_format *format; /**< VkBufferCreateInfo::format */
-};
-
-struct anv_buffer_view {
-   struct anv_surface_view view;
-};
-
 struct anv_image_view {
-   struct anv_surface_view view;
-   VkExtent3D extent;
-};
-
-enum anv_attachment_view_type {
-   ANV_ATTACHMENT_VIEW_TYPE_COLOR,
-   ANV_ATTACHMENT_VIEW_TYPE_DEPTH_STENCIL,
-};
-
-struct anv_attachment_view {
-   enum anv_attachment_view_type attachment_type;
-   VkExtent3D extent;
-};
+   const struct anv_image *image; /**< VkImageViewCreateInfo::image */
+   const struct anv_format *format; /**< VkImageViewCreateInfo::format */
+   struct anv_bo *bo;
+   uint32_t offset; /**< Offset into bo. */
+   VkExtent3D extent; /**< Extent of VkImageViewCreateInfo::baseMipLevel. */
 
-struct anv_color_attachment_view {
-   struct anv_attachment_view base;
-   struct anv_surface_view view;
-};
+   /** RENDER_SURFACE_STATE when using image as a color render target. */
+   struct anv_state color_rt_surface_state;
 
-struct anv_depth_stencil_view {
-   struct anv_attachment_view base;
-   const struct anv_image *image; /**< VkAttachmentViewCreateInfo::image */
-   const struct anv_format *format; /**< VkAttachmentViewCreateInfo::format */
+   /** RENDER_SURFACE_STATE when using image as a non render target. */
+   struct anv_state nonrt_surface_state;
 };
 
 struct anv_image_create_info {
    const VkImageCreateInfo *vk_info;
-   bool force_tile_mode;
-   uint8_t tile_mode;
+   bool force_tiling;
+   enum isl_tiling tiling;
    uint32_t stride;
 };
 
@@ -1135,10 +1338,8 @@ VkResult anv_image_create(VkDevice _device,
                           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);
+anv_image_get_surface_for_aspect_mask(struct anv_image *image,
+                                      VkImageAspectFlags aspect_mask);
 
 void anv_image_view_init(struct anv_image_view *view,
                          struct anv_device *device,
@@ -1157,36 +1358,17 @@ gen8_image_view_init(struct anv_image_view *iview,
                      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 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);
+                                   uint32_t offset, uint32_t range,
+                                   uint32_t stride);
 
 void gen7_fill_buffer_surface_state(void *state, const struct anv_format *format,
-                                    uint32_t offset, uint32_t range);
+                                    uint32_t offset, uint32_t range,
+                                    uint32_t stride);
 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);
+                                    uint32_t offset, uint32_t range,
+                                    uint32_t stride);
 
 struct anv_sampler {
    uint32_t state[4];
@@ -1197,11 +1379,8 @@ struct anv_framebuffer {
    uint32_t                                     height;
    uint32_t                                     layers;
 
-   /* Viewport for clears */
-   VkDynamicViewportState                       vp_state;
-
    uint32_t                                     attachment_count;
-   const struct anv_attachment_view *           attachments[0];
+   const struct anv_image_view *           attachments[0];
 };
 
 struct anv_subpass {
@@ -1223,15 +1402,12 @@ struct anv_render_pass_attachment {
 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];
 };
 
+extern struct anv_render_pass anv_meta_dummy_renderpass;
+
 struct anv_query_pool_slot {
    uint64_t begin;
    uint64_t end;
@@ -1249,6 +1425,10 @@ void anv_device_finish_meta(struct anv_device *device);
 
 void *anv_lookup_entrypoint(const char *name);
 
+void anv_dump_image_to_ppm(struct anv_device *device,
+                           struct anv_image *image, unsigned miplevel,
+                           unsigned array_layer, const char *filename);
+
 #define ANV_DEFINE_HANDLE_CASTS(__anv_type, __VkType)                      \
                                                                            \
    static inline struct __anv_type *                                       \
@@ -1287,16 +1467,10 @@ ANV_DEFINE_HANDLE_CASTS(anv_physical_device, VkPhysicalDevice)
 ANV_DEFINE_HANDLE_CASTS(anv_queue, VkQueue)
 
 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);
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set, VkDescriptorSet)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_descriptor_set_layout, VkDescriptorSetLayout)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_device_memory, VkDeviceMemory)
-ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_cb_state, VkDynamicColorBlendState)
-ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_ds_state, VkDynamicDepthStencilState)
-ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_rs_state, VkDynamicRasterState)
-ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_dynamic_vp_state, VkDynamicViewportState)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_fence, VkFence)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_framebuffer, VkFramebuffer)
 ANV_DEFINE_NONDISP_HANDLE_CASTS(anv_image, VkImage)