diff --git a/src/venus/vkr_buffer.h b/src/venus/vkr_buffer.h index 5f32842..d1575b5 100644 --- a/src/venus/vkr_buffer.h +++ b/src/venus/vkr_buffer.h @@ -11,10 +11,12 @@ struct vkr_buffer { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(buffer, VK_OBJECT_TYPE_BUFFER, VkBuffer) struct vkr_buffer_view { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(buffer_view, VK_OBJECT_TYPE_BUFFER_VIEW, VkBufferView) void vkr_context_init_buffer_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_command_buffer.h b/src/venus/vkr_command_buffer.h index bc16b7f..9f10a1e 100644 --- a/src/venus/vkr_command_buffer.h +++ b/src/venus/vkr_command_buffer.h @@ -13,12 +13,14 @@ struct vkr_command_pool { struct list_head command_buffers; }; +VKR_DEFINE_OBJECT_CAST(command_pool, VK_OBJECT_TYPE_COMMAND_POOL, VkCommandPool) struct vkr_command_buffer { struct vkr_object base; struct vkr_device *device; }; +VKR_DEFINE_OBJECT_CAST(command_buffer, VK_OBJECT_TYPE_COMMAND_BUFFER, VkCommandBuffer) void vkr_context_init_command_pool_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_common.h b/src/venus/vkr_common.h index 016c73a..9bc77ae 100644 --- a/src/venus/vkr_common.h +++ b/src/venus/vkr_common.h @@ -45,6 +45,20 @@ #define VKR_DEBUG(category) (unlikely(vkr_debug_flags & VKR_DEBUG_##category)) +/* define a type-safe cast function */ +#define VKR_DEFINE_OBJECT_CAST(vkr_type, vk_enum, vk_type) \ + static inline struct vkr_##vkr_type *vkr_##vkr_type##_from_handle(vk_type handle) \ + { \ + struct vkr_##vkr_type *obj = (struct vkr_##vkr_type *)(uintptr_t)handle; \ + if (obj) { \ + assert(obj->base.type == vk_enum); \ + assert(obj->base.id); \ + assert(obj->base.handle.vkr_type); \ + assert((uintptr_t)obj->base.handle.vkr_type == obj->base.handle.u64); \ + } \ + return obj; \ + } + /* * TODO Most of the functions are generated. Some of them are then * hand-edited. Find a better/cleaner way to reduce manual works. diff --git a/src/venus/vkr_descriptor_set.h b/src/venus/vkr_descriptor_set.h index dd15492..1c42e37 100644 --- a/src/venus/vkr_descriptor_set.h +++ b/src/venus/vkr_descriptor_set.h @@ -11,22 +11,30 @@ struct vkr_descriptor_set_layout { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(descriptor_set_layout, + VK_OBJECT_TYPE_DESCRIPTOR_SET_LAYOUT, + VkDescriptorSetLayout) struct vkr_descriptor_pool { struct vkr_object base; struct list_head descriptor_sets; }; +VKR_DEFINE_OBJECT_CAST(descriptor_pool, VK_OBJECT_TYPE_DESCRIPTOR_POOL, VkDescriptorPool) struct vkr_descriptor_set { struct vkr_object base; struct vkr_device *device; }; +VKR_DEFINE_OBJECT_CAST(descriptor_set, VK_OBJECT_TYPE_DESCRIPTOR_SET, VkDescriptorSet) struct vkr_descriptor_update_template { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(descriptor_update_template, + VK_OBJECT_TYPE_DESCRIPTOR_UPDATE_TEMPLATE, + VkDescriptorUpdateTemplate) void vkr_context_init_descriptor_set_layout_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_device.h b/src/venus/vkr_device.h index 9bd0e60..28d3dfe 100644 --- a/src/venus/vkr_device.h +++ b/src/venus/vkr_device.h @@ -48,6 +48,7 @@ struct vkr_device { struct list_head objects; }; +VKR_DEFINE_OBJECT_CAST(device, VK_OBJECT_TYPE_DEVICE, VkDevice) void vkr_context_init_device_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_device_memory.h b/src/venus/vkr_device_memory.h index 3faf435..7ae33de 100644 --- a/src/venus/vkr_device_memory.h +++ b/src/venus/vkr_device_memory.h @@ -19,6 +19,7 @@ struct vkr_device_memory { uint32_t exported_res_id; struct list_head exported_head; }; +VKR_DEFINE_OBJECT_CAST(device_memory, VK_OBJECT_TYPE_DEVICE_MEMORY, VkDeviceMemory) void vkr_context_init_device_memory_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_image.h b/src/venus/vkr_image.h index 58e2207..70a9800 100644 --- a/src/venus/vkr_image.h +++ b/src/venus/vkr_image.h @@ -11,18 +11,24 @@ struct vkr_image { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(image, VK_OBJECT_TYPE_IMAGE, VkImage) struct vkr_image_view { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(image_view, VK_OBJECT_TYPE_IMAGE_VIEW, VkImageView) struct vkr_sampler { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(sampler, VK_OBJECT_TYPE_SAMPLER, VkSampler) struct vkr_sampler_ycbcr_conversion { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(sampler_ycbcr_conversion, + VK_OBJECT_TYPE_SAMPLER_YCBCR_CONVERSION, + VkSamplerYcbcrConversion) void vkr_context_init_image_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_instance.h b/src/venus/vkr_instance.h index b3a9af6..38e3034 100644 --- a/src/venus/vkr_instance.h +++ b/src/venus/vkr_instance.h @@ -23,6 +23,7 @@ struct vkr_instance { VkPhysicalDevice *physical_device_handles; struct vkr_physical_device **physical_devices; }; +VKR_DEFINE_OBJECT_CAST(instance, VK_OBJECT_TYPE_INSTANCE, VkInstance) void vkr_context_init_instance_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_physical_device.h b/src/venus/vkr_physical_device.h index a309e49..0e84b5f 100644 --- a/src/venus/vkr_physical_device.h +++ b/src/venus/vkr_physical_device.h @@ -26,6 +26,7 @@ struct vkr_physical_device { struct list_head devices; }; +VKR_DEFINE_OBJECT_CAST(physical_device, VK_OBJECT_TYPE_PHYSICAL_DEVICE, VkPhysicalDevice) void vkr_context_init_physical_device_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_pipeline.h b/src/venus/vkr_pipeline.h index d6de882..a356ebf 100644 --- a/src/venus/vkr_pipeline.h +++ b/src/venus/vkr_pipeline.h @@ -11,18 +11,22 @@ struct vkr_shader_module { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(shader_module, VK_OBJECT_TYPE_SHADER_MODULE, VkShaderModule) struct vkr_pipeline_layout { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(pipeline_layout, VK_OBJECT_TYPE_PIPELINE_LAYOUT, VkPipelineLayout) struct vkr_pipeline_cache { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(pipeline_cache, VK_OBJECT_TYPE_PIPELINE_CACHE, VkPipelineCache) struct vkr_pipeline { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(pipeline, VK_OBJECT_TYPE_PIPELINE, VkPipeline) void vkr_context_init_shader_module_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_query_pool.h b/src/venus/vkr_query_pool.h index ad063c2..6bee13d 100644 --- a/src/venus/vkr_query_pool.h +++ b/src/venus/vkr_query_pool.h @@ -11,6 +11,7 @@ struct vkr_query_pool { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(query_pool, VK_OBJECT_TYPE_QUERY_POOL, VkQueryPool) void vkr_context_init_query_pool_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_queue.h b/src/venus/vkr_queue.h index 0ff76f1..b0dce2c 100644 --- a/src/venus/vkr_queue.h +++ b/src/venus/vkr_queue.h @@ -54,18 +54,22 @@ struct vkr_queue { struct list_head busy_head; }; +VKR_DEFINE_OBJECT_CAST(queue, VK_OBJECT_TYPE_QUEUE, VkQueue) struct vkr_fence { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(fence, VK_OBJECT_TYPE_FENCE, VkFence) struct vkr_semaphore { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(semaphore, VK_OBJECT_TYPE_SEMAPHORE, VkSemaphore) struct vkr_event { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(event, VK_OBJECT_TYPE_EVENT, VkEvent) void vkr_context_init_queue_dispatch(struct vkr_context *ctx); diff --git a/src/venus/vkr_render_pass.h b/src/venus/vkr_render_pass.h index ceb0066..a6331b4 100644 --- a/src/venus/vkr_render_pass.h +++ b/src/venus/vkr_render_pass.h @@ -11,10 +11,12 @@ struct vkr_render_pass { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(render_pass, VK_OBJECT_TYPE_RENDER_PASS, VkRenderPass) struct vkr_framebuffer { struct vkr_object base; }; +VKR_DEFINE_OBJECT_CAST(framebuffer, VK_OBJECT_TYPE_FRAMEBUFFER, VkFramebuffer) void vkr_context_init_render_pass_dispatch(struct vkr_context *ctx);