X-Git-Url: http://git.mutantstargoat.com?a=blobdiff_plain;f=src%2Fvk.h;h=772f2e3691d2e97d46244320b13768ab24aff214;hb=638b86fbbdb0307faf21c04532d8f5ceb3b29884;hp=140f525fc1773ace4ee1cc3cc0db35e45ffa7870;hpb=526b6a7f13b31bab2b022eb9f74ec653ce4d4cd0;p=vkrt diff --git a/src/vk.h b/src/vk.h index 140f525..772f2e3 100644 --- a/src/vk.h +++ b/src/vk.h @@ -5,240 +5,375 @@ #include #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) + +enum GEOM_BIND { + VERTEX_BIND, + NORMAL_BIND, + TEXCOORD_BIND, + TANGENT_BIND, + IDX_BIND +}; + +enum GEOM_LOC { + VERTEX_LOC, + NORMAL_LOC, + TANGENT_LOC, + IDX_LOC +}; + struct vk_ctx { - VkInstance inst; - VkPhysicalDevice pdev; - VkDevice dev; + VkInstance inst; - VkCommandPool cmd_pool; - VkCommandBuffer cmd_buf; + VkPhysicalDevice pdev; + VkDevice dev; - VkQueue queue; - int qfam_idx; + VkCommandPool cmd_pool; - uint8_t deviceUUID[VK_UUID_SIZE]; - uint8_t driverUUID[VK_UUID_SIZE]; -}; + VkQueue queue; + int qfam_idx; -struct vk_swapchain -{ - VkSwapchainKHR swapchain; - VkSurfaceKHR surface; - VkSurfaceFormatKHR surf_fmt; - uint32_t num_images; - VkExtent2D extent2d; + uint8_t deviceUUID[VK_UUID_SIZE]; + uint8_t driverUUID[VK_UUID_SIZE]; }; -struct vk_image_props +struct vk_att_props { - uint32_t w; - uint32_t h; - uint32_t depth; + uint32_t w; + uint32_t h; + uint32_t depth; - uint32_t num_samples; - uint32_t num_levels; - uint32_t num_layers; + uint32_t num_samples; + uint32_t num_levels; + uint32_t num_layers; - VkFormat format; - VkImageUsageFlagBits usage; - VkImageTiling tiling; + VkFormat format; + VkImageUsageFlagBits usage; + VkImageTiling tiling; - VkImageLayout in_layout; - VkImageLayout end_layout; + VkImageLayout in_layout; + VkImageLayout end_layout; + bool is_depth; + bool is_swapchain; bool need_export; }; +struct vk_swapchain +{ + VkSwapchainKHR swapchain; + VkSurfaceFormatKHR surf_fmt; + + /* image properties */ + /* FIXME: do I really need those 2?*/ + VkFormat image_fmt; + VkExtent2D extent2d; + + struct vk_att_props img_props; + + uint32_t num_images; + VkImage *images; + VkImageView *views; +}; + +/* for allocated images */ + struct vk_mem_obj { - VkDeviceMemory mem; - VkDeviceSize mem_sz; - bool dedicated; + VkDeviceMemory mem; + VkDeviceSize mem_sz; + + bool dedicated; }; struct vk_image_obj { - VkImage img; + VkImage img; VkImageView img_view; - struct vk_mem_obj mobj; + + struct vk_mem_obj mobj; }; -struct vk_image_att { - struct vk_image_obj obj; - struct vk_image_props props; +struct vk_attachment { + struct vk_image_obj obj; + struct vk_att_props props; }; -struct vk_vertex_info +struct vk_buf { - int num_verts; - int num_components; + VkBuffer buf; + struct vk_mem_obj mobj; +}; - VkPrimitiveTopology topology; +struct vk_vbo { + int num_vertices; + struct vk_buf vbo; + + /* int num_components; */ }; -struct vk_buf +struct vk_geometry { - VkBuffer buf; - struct vk_mem_obj mobj; + struct vk_vbo vertices; + struct vk_vbo normals; + struct vk_vbo tangents; + struct vk_vbo indices; + + VkPrimitiveTopology topo; }; struct vk_renderer { - VkPipeline pipeline; - VkPipelineLayout pipeline_layout; - VkRenderPass renderpass; - VkShaderModule vs; - VkShaderModule fs; - VkFramebuffer fb; - - struct vk_vertex_info vertex_info; + VkPipeline pipeline; + VkPipelineLayout pipeline_layout; + VkRenderPass renderpass; + VkShaderModule vs; + VkShaderModule fs; + + struct vk_geometry *geometry; }; struct vk_dims { - float w; - float h; + float w; + float h; }; struct vk_semaphores { - VkSemaphore frame_ready; - VkSemaphore frame_done; + VkSemaphore frame_ready; + VkSemaphore frame_done; }; +#ifdef __cplusplus +extern "C" { +#endif + /* context */ -bool vk_init_ctx(struct vk_ctx *ctx); -bool vk_init_ctx_for_rendering(struct vk_ctx *ctx); +bool vk_init_ctx(struct vk_ctx *ctx, + bool enable_layers); + +bool vk_init_ctx_for_rendering(struct vk_ctx *ctx, + bool enable_cache, + bool enable_layers); + void vk_cleanup_ctx(struct vk_ctx *ctx); /* images */ +bool +vk_create_ext_image(struct vk_ctx *ctx, + struct vk_att_props *props, + struct vk_image_obj *img); bool vk_create_image(struct vk_ctx *ctx, - struct vk_image_props *props, - struct vk_image_obj *img_obj); + struct vk_att_props *props, + struct vk_image_obj *img_obj); void vk_destroy_image(struct vk_ctx *ctx, - struct vk_image_obj *img_obj); + struct vk_image_obj *img_obj); +bool +vk_create_image_view(struct vk_ctx *ctx, + VkImage image, + VkImageViewType view_type, + VkFormat format, + bool is_swapchain, + VkImageView *image_view); bool -vk_fill_ext_image_props(struct vk_ctx *ctx, - uint32_t w, uint32_t h, - uint32_t depth, - uint32_t num_samples, - uint32_t num_levels, - uint32_t num_layers, - VkFormat format, - VkImageTiling tiling, - VkImageLayout in_layout, - VkImageLayout end_layout, - bool need_export, - struct vk_image_props *props); +vk_fill_image_props(struct vk_ctx *ctx, + uint32_t w, uint32_t h, + uint32_t depth, + uint32_t num_samples, + uint32_t num_levels, + uint32_t num_layers, + VkFormat format, + VkImageTiling tiling, + VkImageLayout in_layout, + VkImageLayout end_layout, + bool is_swapchain, + bool is_depth, + bool need_export, + struct vk_att_props *props); + +struct vk_attachment +vk_create_attachment_from_obj(struct vk_image_obj *obj, + struct vk_att_props *props); + +struct vk_attachment +vk_create_attachment(VkImage image, + VkImageView view, + struct vk_att_props *props); /* buffers */ bool vk_create_buffer(struct vk_ctx *ctx, bool is_external, - uint32_t sz, - VkBufferUsageFlagBits usage, - void *pnext, - struct vk_buf *bo); + uint32_t sz, + VkBufferUsageFlagBits usage, + void *pnext, + struct vk_buf *bo); void vk_destroy_buffer(struct vk_ctx *ctx, - struct vk_buf *bo); + struct vk_buf *bo); bool vk_update_buffer_data(struct vk_ctx *ctx, - void *data, - uint32_t data_sz, - struct vk_buf *bo); + void *data, + uint32_t data_sz, + struct vk_buf *bo); bool vk_create_ext_buffer(struct vk_ctx *ctx, - uint32_t sz, - VkBufferUsageFlagBits usage, - struct vk_buf *bo); + uint32_t sz, + VkBufferUsageFlagBits usage, + struct vk_buf *bo); -/* semaphores */ +/* semaphores and fences */ bool vk_create_semaphores(struct vk_ctx *ctx, - struct vk_semaphores *semaphores); + bool is_external, + struct vk_semaphores *semaphores); void vk_destroy_semaphores(struct vk_ctx *ctx, - struct vk_semaphores *semaphores); + struct vk_semaphores *semaphores); + +void +vk_destroy_fences(struct vk_ctx *ctc, + int num_fences, + VkFence *fences); /* renderer */ +bool +vk_create_framebuffer(struct vk_ctx *ctx, + int width, int height, + int num_color_atts, + VkImageView *color_views, + VkImageView *depth_view, + VkRenderPass rb, + VkFramebuffer *fb); + +bool +vk_create_renderpass(struct vk_ctx *ctx, + uint32_t num_color_atts, + struct vk_att_props *color_props, + struct vk_att_props *depth_props, + VkRenderPass *renderpass); bool vk_create_renderer(struct vk_ctx *ctx, - const char *vs_src, - unsigned int vs_size, - const char *fs_src, - unsigned int fs_size, - bool enable_depth, - bool enable_stencil, - struct vk_image_att *color_att, - struct vk_image_att *depth_att, - struct vk_vertex_info *vert_info, - struct vk_renderer *renderer); + const char *vs_src, + unsigned int vs_size, + const char *fs_src, + unsigned int fs_size, + int w, int h, + uint32_t num_samples, + bool enable_depth, + bool enable_stencil, + int num_color_att, + struct vk_att_props *color_props, + struct vk_att_props *depth_props, + struct vk_geometry *geometry, + struct vk_renderer *renderer); void vk_destroy_renderer(struct vk_ctx *ctx, - struct vk_renderer *pipeline); + struct vk_renderer *pipeline); -/* draw */ +/* fences and command buffers */ +bool +vk_create_fence(struct vk_ctx *ctx, + VkFence *fence); + +VkCommandBuffer +vk_create_cmd_buffer(struct vk_ctx *ctx); + +bool +vk_record_cmd_buffer(struct vk_ctx *ctx, + VkCommandBuffer cmd_buf, + struct vk_renderer *renderer, + uint32_t vk_fb_color_count, + float *vk_fb_color, + VkFramebuffer fb, + uint32_t num_atts, + struct vk_attachment *atts, + float x, float y, + float w, float h); + +void +vk_destroy_cmd_buffers(struct vk_ctx *ctx, + uint32_t num_buffers, + VkCommandBuffer *buffers); +/* draw */ void vk_draw(struct vk_ctx *ctx, - struct vk_buf *vbo, - struct vk_renderer *renderer, - float *vk_fb_color, - uint32_t vk_fb_color_count, - struct vk_semaphores *semaphores, - struct vk_image_att *attachments, - uint32_t n_attachments, - float x, float y, float w, float h); + struct vk_semaphores *semaphores, + uint32_t num_buffers, + VkCommandBuffer *cmd_buf); void vk_clear_color(struct vk_ctx *ctx, + VkCommandBuffer cmd_buf, struct vk_buf *vbo, struct vk_renderer *renderer, float *vk_fb_color, uint32_t vk_fb_color_count, + VkFramebuffer fb, struct vk_semaphores *semaphores, bool has_wait, bool has_signal, - struct vk_image_att *attachments, + struct vk_attachment *attachments, uint32_t n_attachments, float x, float y, float w, float h); +void +vk_set_viewport(struct vk_ctx *ctx, + VkCommandBuffer cmd_buf, + float x, float y, + float w, float h, + float near, float far); + /* swapchain */ bool vk_create_swapchain(struct vk_ctx *ctx, int width, int height, - int num_qfam, + bool vsync, + VkSurfaceKHR surf, + struct vk_swapchain *old_swapchain, struct vk_swapchain *swapchain); void vk_destroy_swapchain(struct vk_ctx *ctx, struct vk_swapchain *swapchain); +bool +vk_queue_present(struct vk_swapchain *swapchain, + VkQueue queue, + uint32_t image_idx, + VkSemaphore wait_sema); + /* transitions */ void vk_copy_image_to_buffer(struct vk_ctx *ctx, - struct vk_image_att *src_img, - struct vk_buf *dst_bo, - float w, float h); + VkCommandBuffer cmd_buf, + struct vk_attachment *src_img, + struct vk_buf *dst_bo, + float w, float h); void -vk_transition_image_layout(struct vk_image_att *img_att, - VkCommandBuffer cmd_buf, - VkImageLayout old_layout, - VkImageLayout new_layout, - uint32_t src_queue_family_index, - uint32_t dst_queue_family_index); +vk_transition_image_layout(struct vk_attachment *img_att, + VkCommandBuffer cmd_buf, + VkImageLayout old_layout, + VkImageLayout new_layout, + uint32_t src_queue_family_index, + uint32_t dst_queue_family_index); + + +#ifdef __cplusplus +} +#endif #endif /* VK_H */