At this commit driver skeleton is able to dump spirv and nir
[mesa.git] / src / libre-soc / vulkan / libresoc_private.h
index 60f12937448a0dc5dbfb7a0887d65c8b962d7eaa..55a9cbb609076d8543b67c7240ace0600c93622f 100644 (file)
 #include "libresoc_constants.h"
 #include "libresoc_debug.h"
 
+#include "wsi_common.h"
 #define LIBRESOC_MAX_QUEUE_FAMILIES 1 
+
+static inline gl_shader_stage
+vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
+{
+       assert(__builtin_popcount(vk_stage) == 1);
+       return ffs(vk_stage) - 1;
+}
+
+static inline VkShaderStageFlagBits
+mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
+{
+       return (1 << mesa_stage);
+}
+
+static inline uint32_t
+align_u32(uint32_t v, uint32_t a)
+{
+       assert(a != 0 && a == (a & -a));
+       return (v + a - 1) & ~(a - 1);
+}
+
+static inline uint32_t
+align_u32_npot(uint32_t v, uint32_t a)
+{
+       return (v + a - 1) / a * a;
+}
+
+static inline uint64_t
+align_u64(uint64_t v, uint64_t a)
+{
+       assert(a != 0 && a == (a & -a));
+       return (v + a - 1) & ~(a - 1);
+}
+
+static inline int32_t
+align_i32(int32_t v, int32_t a)
+{
+       assert(a != 0 && a == (a & -a));
+       return (v + a - 1) & ~(a - 1);
+}
+
+/** Alignment must be a power of 2. */
+static inline bool
+libresoc_is_aligned(uintmax_t n, uintmax_t a)
+{
+       assert(a == (a & -a));
+       return (n & (a - 1)) == 0;
+}
+
+static inline uint32_t
+round_up_u32(uint32_t v, uint32_t a)
+{
+       return (v + a - 1) / a;
+}
+
+static inline uint64_t
+round_up_u64(uint64_t v, uint64_t a)
+{
+       return (v + a - 1) / a;
+}
+
+static inline uint32_t
+libresoc_minify(uint32_t n, uint32_t levels)
+{
+       if (unlikely(n == 0))
+               return 0;
+       else
+               return MAX2(n >> levels, 1);
+}
+static inline float
+libresoc_clamp_f(float f, float min, float max)
+{
+       assert(min < max);
+
+       if (f > max)
+               return max;
+       else if (f < min)
+               return min;
+       else
+               return f;
+}
+
+static inline bool
+libresoc_clear_mask(uint32_t *inout_mask, uint32_t clear_mask)
+{
+       if (*inout_mask & clear_mask) {
+               *inout_mask &= ~clear_mask;
+               return true;
+       } else {
+               return false;
+       }
+}
+
+struct libresoc_fence {
+       struct vk_object_base base;
+};
+
+struct libresoc_image_create_info {
+       const VkImageCreateInfo *vk_info;
+       bool scanout;
+       bool no_metadata_planes;
+};
+
+struct libresoc_image {
+       struct vk_object_base base;
+       VkImageType type;
+       /* The original VkFormat provided by the client.  This may not match any
+        * of the actual surface formats.
+        */
+       VkFormat vk_format;
+       VkImageAspectFlags aspects;
+       VkImageUsageFlags usage; /**< Superset of VkImageCreateInfo::usage. */
+       VkImageTiling tiling; /** VkImageCreateInfo::tiling */
+       VkImageCreateFlags flags; /** VkImageCreateInfo::flags */
+
+       VkDeviceSize size;
+       uint32_t alignment;
+
+       unsigned queue_family_mask;
+       bool exclusive;
+       bool shareable;
+
+};
+
+VkResult libresoc_image_create(VkDevice _device,
+                          const struct libresoc_image_create_info *info,
+                          const VkAllocationCallbacks* alloc,
+                          VkImage *pImage);
+
+struct libresoc_cmd_pool {
+       struct vk_object_base                        base;
+       VkAllocationCallbacks                        alloc;
+       struct list_head                             cmd_buffers;
+       struct list_head                             free_cmd_buffers;
+       uint32_t queue_family_index;
+};
+
+struct libresoc_semaphore {
+       struct vk_object_base base;
+};
+
 struct libresoc_instance;
 struct libresoc_device;
 struct cache_entry;
@@ -70,6 +212,14 @@ struct libresoc_pipeline_cache {
 struct libresoc_shader_binary;
 struct libresoc_shader_variant;
 
+struct libresoc_pipeline {
+       struct vk_object_base                         base;
+       struct libresoc_device *                          device;
+
+
+       VkShaderStageFlags                           active_stages;
+
+};
 void
 libresoc_pipeline_cache_init(struct libresoc_pipeline_cache *cache,
                         struct libresoc_device *device);
@@ -92,6 +242,10 @@ libresoc_pipeline_cache_insert_shaders(struct libresoc_device *device,
                                   const unsigned char *sha1,
                                   struct libresoc_shader_variant **variants,
                                   struct libresoc_shader_binary *const *binaries);
+
+VkResult libresoc_init_wsi(struct libresoc_physical_device *physical_device);
+void libresoc_finish_wsi(struct libresoc_physical_device *physical_device);
+
 struct libresoc_device {
 
    struct vk_device vk;
@@ -112,6 +266,10 @@ struct libresoc_device {
    /* Condition variable for legacy timelines, to notify waiters when a
     * new point gets submitted. */
    pthread_cond_t timeline_cond;
+   /* Overallocation. */
+   bool overallocation_disallowed;
+   uint64_t allocated_memory_size[VK_MAX_MEMORY_HEAPS];
+   mtx_t overallocation_mutex;
    /* FIXME: stub */
 };
 
@@ -121,7 +279,7 @@ struct libresoc_physical_device {
 
    struct list_head                            link;
    struct libresoc_instance *instance;
-
+   struct wsi_device wsi_device;
    struct libresoc_device_extension_table supported_extensions;
    struct libresoc_physical_device_dispatch_table dispatch;
 
@@ -129,6 +287,9 @@ struct libresoc_physical_device {
    uint8_t                                     driver_uuid[VK_UUID_SIZE];
    uint8_t                                     device_uuid[VK_UUID_SIZE];
    uint8_t                                     cache_uuid[VK_UUID_SIZE];
+   int local_fd;
+   int master_fd;
+   VkPhysicalDeviceMemoryProperties memory_properties;
    /* FIXME: stub */
 };
 
@@ -160,9 +321,12 @@ struct libresoc_instance {
    int physical_device_count;
    struct list_head physical_devices;
 
+   struct driOptionCache dri_options;
+   struct driOptionCache available_dri_options;
    struct vk_debug_report_instance debug_report_callbacks;
 };
 
+struct libresoc_deferred_queue_submission;
 struct libresoc_queue {
    VK_LOADER_DATA _loader_data;
 
@@ -174,16 +338,83 @@ struct libresoc_queue {
 
    struct list_head pending_submissions;
    pthread_mutex_t pending_mutex;
+   pthread_mutex_t thread_mutex;
+   pthread_cond_t thread_cond;
+   //struct libresoc_deferred_queue_submission *thread_submission;
+   pthread_t submission_thread;
+   bool thread_exit;
+   bool thread_running;
    /* FIXME: stub */
 };
 
+struct libresoc_cmd_buffer_upload {
+       uint8_t *map;
+       unsigned offset;
+       uint64_t size;
+       struct list_head list;
+};
+
+enum libresoc_cmd_buffer_status {
+       LIBRESOC_CMD_BUFFER_STATUS_INVALID,
+       LIBRESOC_CMD_BUFFER_STATUS_INITIAL,
+       LIBRESOC_CMD_BUFFER_STATUS_RECORDING,
+       LIBRESOC_CMD_BUFFER_STATUS_EXECUTABLE,
+       LIBRESOC_CMD_BUFFER_STATUS_PENDING,
+};
+
 struct libresoc_cmd_buffer {
+       struct vk_object_base                         base;
 
-   struct libresoc_device *device;
+       struct libresoc_device *                          device;
 
-   /* FIXME: stub */
+       struct libresoc_cmd_pool *                        pool;
+       struct list_head                             pool_link;
+
+       VkCommandBufferUsageFlags                    usage_flags;
+       VkCommandBufferLevel                         level;
+       enum libresoc_cmd_buffer_status status;
+       //struct radeon_cmdbuf *cs;
+       // struct libresoc_cmd_state state;
+       // struct libresoc_vertex_binding                   vertex_bindings[MAX_VBS];
+       // struct libresoc_streamout_binding                streamout_bindings[MAX_SO_BUFFERS];
+       uint32_t queue_family_index;
+
+       uint8_t push_constants[MAX_PUSH_CONSTANTS_SIZE];
+       VkShaderStageFlags push_constant_stages;
+       // struct libresoc_descriptor_set meta_push_descriptors;
+
+       // struct libresoc_descriptor_state descriptors[MAX_BIND_POINTS];
+
+       struct libresoc_cmd_buffer_upload upload;
+
+       uint32_t scratch_size_per_wave_needed;
+       uint32_t scratch_waves_wanted;
+       uint32_t compute_scratch_size_per_wave_needed;
+       uint32_t compute_scratch_waves_wanted;
+       uint32_t esgs_ring_size_needed;
+       uint32_t gsvs_ring_size_needed;
+       bool tess_rings_needed;
+       bool sample_positions_needed;
+
+       VkResult record_result;
+
+};
+
+struct libresoc_device_memory {
+       struct vk_object_base                        base;
+       /* for dedicated allocations */
+       struct libresoc_image                            *image;
+       //struct libresoc_buffer                           *buffer;
+       uint32_t                                     heap_index;
+       uint64_t                                     alloc_size;
+       void *                                       map;
+       void *                                       user_ptr;
 };
 
+void libresoc_free_memory(struct libresoc_device *device,
+                     const VkAllocationCallbacks* pAllocator,
+                     struct libresoc_device_memory *mem);
+
 uint32_t libresoc_physical_device_api_version(struct libresoc_physical_device *dev);
 
 int libresoc_get_instance_entrypoint_index(const char *name);
@@ -210,6 +441,71 @@ void *libresoc_lookup_entrypoint(const char *name);
 const char *
 libresoc_get_debug_option_name(int id);
 
+struct libresoc_binning_settings {
+       unsigned context_states_per_bin; /* allowed range: [1, 6] */
+       unsigned persistent_states_per_bin; /* allowed range: [1, 32] */
+       unsigned fpovs_per_batch; /* allowed range: [0, 255], 0 = unlimited */
+};
+
+struct libresoc_binning_settings
+libresoc_get_binning_settings(const struct libresoc_physical_device *pdev);
+
+struct vk_format_description;
+uint32_t libresoc_translate_buffer_dataformat(const struct vk_format_description *desc,
+                                         int first_non_void);
+uint32_t libresoc_translate_buffer_numformat(const struct vk_format_description *desc,
+                                        int first_non_void);
+bool libresoc_is_buffer_format_supported(VkFormat format, bool *scaled);
+uint32_t libresoc_translate_colorformat(VkFormat format);
+uint32_t libresoc_translate_color_numformat(VkFormat format,
+                                       const struct vk_format_description *desc,
+                                       int first_non_void);
+uint32_t libresoc_colorformat_endian_swap(uint32_t colorformat);
+unsigned libresoc_translate_colorswap(VkFormat format, bool do_endian_swap);
+uint32_t libresoc_translate_dbformat(VkFormat format);
+uint32_t libresoc_translate_tex_dataformat(VkFormat format,
+                                      const struct vk_format_description *desc,
+                                      int first_non_void);
+uint32_t libresoc_translate_tex_numformat(VkFormat format,
+                                     const struct vk_format_description *desc,
+                                     int first_non_void);
+bool libresoc_format_pack_clear_color(VkFormat format,
+                                 uint32_t clear_vals[2],
+                                 VkClearColorValue *value);
+bool libresoc_is_colorbuffer_format_supported(VkFormat format, bool *blendable);
+bool libresoc_dcc_formats_compatible(VkFormat format1,
+                                 VkFormat format2);
+bool libresoc_device_supports_etc(struct libresoc_physical_device *physical_device);
+
+
+/* Whether the image has a htile  that is known consistent with the contents of
+ * the image and is allowed to be in compressed form.
+ *
+ * If this is false reads that don't use the htile should be able to return
+ * correct results.
+ */
+bool libresoc_layout_is_htile_compressed(const struct libresoc_image *image,
+                                     VkImageLayout layout,
+                                     bool in_render_loop,
+                                     unsigned queue_mask);
+
+bool libresoc_layout_can_fast_clear(const struct libresoc_image *image,
+                               VkImageLayout layout,
+                               bool in_render_loop,
+                               unsigned queue_mask);
+
+bool libresoc_layout_dcc_compressed(const struct libresoc_device *device,
+                               const struct libresoc_image *image,
+                               VkImageLayout layout,
+                               bool in_render_loop,
+                               unsigned queue_mask);
+VkResult
+libresoc_graphics_pipeline_create(VkDevice device,
+                             VkPipelineCache cache,
+                             const VkGraphicsPipelineCreateInfo *pCreateInfo,
+                             const VkAllocationCallbacks *alloc,
+                             VkPipeline *pPipeline);
+
 #define libresoc_printflike(a, b) __attribute__((__format__(__printf__, a, b)))
 
 VkResult __vk_errorf(struct libresoc_instance *instance, VkResult error,
@@ -259,27 +555,27 @@ LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_instance, VkInstance)
 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_physical_device, VkPhysicalDevice)
 LIBRESOC_DEFINE_HANDLE_CASTS(libresoc_queue, VkQueue)
 
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_cmd_pool, VkCommandPool)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_cmd_pool, VkCommandPool)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_buffer, VkBuffer)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_buffer_view, VkBufferView)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_pool, VkDescriptorPool)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_set, VkDescriptorSet)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_set_layout, VkDescriptorSetLayout)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_descriptor_update_template, VkDescriptorUpdateTemplate)
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_device_memory, VkDeviceMemory)
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_fence, VkFence)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_device_memory, VkDeviceMemory)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_fence, VkFence)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_event, VkEvent)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_framebuffer, VkFramebuffer)
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_image, VkImage)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_image, VkImage)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_image_view, VkImageView);
 LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline_cache, VkPipelineCache)
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline, VkPipeline)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline, VkPipeline)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_pipeline_layout, VkPipelineLayout)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_query_pool, VkQueryPool)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_render_pass, VkRenderPass)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_sampler, VkSampler)
 //LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_sampler_ycbcr_conversion, VkSamplerYcbcrConversion)
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_shader_module, VkShaderModule)
-//LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_semaphore, VkSemaphore)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_shader_module, VkShaderModule)
+LIBRESOC_DEFINE_NONDISP_HANDLE_CASTS(libresoc_semaphore, VkSemaphore)
 
 #endif /* LIBRESOC_PRIVATE_H */