vk: Add an initial implementation of the actual Khronos WSI extension
[mesa.git] / src / vulkan / anv_private.h
index dc14dd2818a1970237ff3e70ad2f35d686c2585d..06ef4e123f35399d2a260745bd4b8f4672b0008e 100644 (file)
@@ -46,7 +46,8 @@
 #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"
 
@@ -120,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);
@@ -505,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);
 
@@ -550,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,   \
@@ -569,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];
 
 };
 
@@ -623,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 {
@@ -665,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;
@@ -672,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;
@@ -685,6 +716,13 @@ 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 {
@@ -761,6 +799,21 @@ 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 *
@@ -774,14 +827,29 @@ 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);
 
@@ -832,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;
@@ -859,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);
@@ -879,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.
@@ -912,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;
 
@@ -924,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 {
@@ -941,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 {
@@ -970,17 +1119,8 @@ 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 {
@@ -994,18 +1134,57 @@ 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);
 
@@ -1035,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;
@@ -1045,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);
 
@@ -1090,7 +1285,6 @@ 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)