Initial RHI implementation

This commit is contained in:
2026-07-06 00:07:01 +01:00
parent cb3ef2be1c
commit 49aba1eb3c
13 changed files with 9349 additions and 81 deletions
+15 -1
View File
@@ -25,12 +25,17 @@
#include "pr_rhi_types.h"
#ifdef __cplusplus
extern "C" {
#endif
// ======================================================================
// Instance
// ======================================================================
PrRhiInstance *prRhiCreateInstance(PrRhiInstanceDesc desc, WpAllocator *alloc);
void prRhiDestroyInstance(PrRhiInstance *inst, WpAllocator *alloc);
void *prRhiGetNativeInstanceHandle(PrRhiInstance *inst);
// ======================================================================
// Physical device enumeration
@@ -39,6 +44,8 @@ void prRhiDestroyInstance(PrRhiInstance *inst, WpAllocator *alloc);
PrRhiPhysicalDeviceArray prRhiGetPhysicalDevices(PrRhiInstance *inst, const WpAllocator *scratch);
void prRhiGetPhysicalDeviceName(PrRhiPhysicalDevice *pdev, WpStr8 *out);
void prRhiGetPhysicalDeviceDriverInfo(PrRhiPhysicalDevice *pdev, WpStr8 *out);
void prRhiGetPhysicalDeviceProperties(PrRhiPhysicalDevice *pdev,
PrRhiPhysicalDeviceProperties *out);
// ======================================================================
// Surface (platform-specific)
@@ -57,6 +64,7 @@ PrRhiDevice *prRhiCreateDevice(PrRhiPhysicalDevice *pdev, PrRhiSurface *surface,
PrRhiDeviceDesc desc, WpAllocator *alloc);
void prRhiDestroyDevice(PrRhiDevice *device, WpAllocator *alloc);
void prRhiDeviceWaitIdle(PrRhiDevice *device);
u32 prRhiGetQueueFamilyIndex(PrRhiDevice *device);
// ======================================================================
// Swapchain
@@ -78,6 +86,7 @@ void prRhiRecreateSwapchain(PrRhiDevice *device, PrRhiSwapchain
PrRhiTexture *prRhiGetSwapchainTexture(PrRhiSwapchain *swapchain, u32 image_index);
PrRhiTexture *prRhiGetSwapchainDepthTexture(PrRhiSwapchain *swapchain);
PrRhiFormat prRhiGetSwapchainFormat(PrRhiSwapchain *swapchain);
// ======================================================================
// Buffers
@@ -258,7 +267,8 @@ void prRhiCmdDrawIndexed(PrRhiCommandBuffer *cb, u32 index_count, u32 instance_c
// --- Copy ---
void prRhiCmdCopyBufferToImage(PrRhiCommandBuffer *cb, PrRhiBuffer *src, PrRhiTexture *dst);
void prRhiCmdCopyBufferToImage(PrRhiCommandBuffer *cb, PrRhiBuffer *src, PrRhiTexture *dst,
PrRhiBufferImageCopyArray copies);
// ======================================================================
// Queue submission
@@ -282,5 +292,9 @@ void prRhiQueueSubmit(PrRhiDevice *device, PrRhiCommandBuffer *cb,
# error "Define one of: PR_RHI_VULKAN, PR_RHI_D3D12, PR_RHI_METAL"
#endif
#ifdef __cplusplus
}
#endif
#endif
+22
View File
@@ -212,14 +212,29 @@ typedef struct PrRhiImageMemoryBarrier {
PrRhiTexture *texture;
PrRhiImageLayout old_layout;
PrRhiImageLayout new_layout;
u64 src_stage_mask;
u64 src_access_mask;
u64 dst_stage_mask;
u64 dst_access_mask;
} PrRhiImageMemoryBarrier;
typedef struct PrRhiBufferMemoryBarrier {
PrRhiBuffer *buffer;
u64 offset;
u64 size;
u64 src_stage_mask;
u64 src_access_mask;
u64 dst_stage_mask;
u64 dst_access_mask;
} PrRhiBufferMemoryBarrier;
typedef struct PrRhiBufferImageCopy {
u64 buffer_offset;
u32 mip_level;
u32 width;
u32 height;
} PrRhiBufferImageCopy;
typedef struct PrRhiColorAttachment {
PrRhiTexture *texture;
PrRhiImageLayout layout;
@@ -255,6 +270,7 @@ typedef PrRhiDescriptorBufferInfo *PrRhiDescriptorBufferInfoArray;
typedef PrRhiImageMemoryBarrier *PrRhiImageMemoryBarrierArray;
typedef PrRhiBufferMemoryBarrier *PrRhiBufferMemoryBarrierArray;
typedef PrRhiColorAttachment *PrRhiColorAttachmentArray;
typedef PrRhiBufferImageCopy *PrRhiBufferImageCopyArray;
typedef struct PrRhiWriteDescriptorSet *PrRhiWriteDescriptorSetArray;
// ============================================================================
@@ -367,6 +383,7 @@ typedef struct PrRhiSwapchainDesc {
u32 width;
u32 height;
b8 has_depth;
PrRhiFormat depth_format; // PR_RHI_FORMAT_UNDEFINED = auto-pick
} PrRhiSwapchainDesc;
typedef struct PrRhiSurfaceCapabilities {
@@ -380,6 +397,11 @@ typedef struct PrRhiSurfaceCapabilities {
u32 max_height;
} PrRhiSurfaceCapabilities;
typedef struct PrRhiPhysicalDeviceProperties {
u32 api_version;
u32 device_type; // VkPhysicalDeviceType
} PrRhiPhysicalDeviceProperties;
typedef u64 PrRhiDeviceAddress;
// --- Command buffer types ---
+118 -68
View File
@@ -71,6 +71,22 @@ void _checkVkBool(VkResult res, const char *site) {
if (res < VK_SUCCESS) _abort(site);
}
PrRhiFormat _fromVkFormat(VkFormat fmt) {
switch (fmt) {
case VK_FORMAT_R8G8B8A8_SRGB: return PR_RHI_FORMAT_R8G8B8A8_SRGB;
case VK_FORMAT_R8G8B8A8_UNORM: return PR_RHI_FORMAT_R8G8B8A8_UNORM;
case VK_FORMAT_R16G16B16A16_SFLOAT: return PR_RHI_FORMAT_R16G16B16A16_SFLOAT;
case VK_FORMAT_R32G32B32A32_SFLOAT: return PR_RHI_FORMAT_R32G32B32A32_SFLOAT;
case VK_FORMAT_R32G32B32_SFLOAT: return PR_RHI_FORMAT_R32G32B32_SFLOAT;
case VK_FORMAT_R32G32_SFLOAT: return PR_RHI_FORMAT_R32G32_SFLOAT;
case VK_FORMAT_R32_SFLOAT: return PR_RHI_FORMAT_R32_SFLOAT;
case VK_FORMAT_D24_UNORM_S8_UINT: return PR_RHI_FORMAT_D24_UNORM_S8_UINT;
case VK_FORMAT_D32_SFLOAT_S8_UINT: return PR_RHI_FORMAT_D32_SFLOAT_S8_UINT;
case VK_FORMAT_B8G8R8A8_SRGB: return PR_RHI_FORMAT_B8G8R8A8_SRGB;
default: return PR_RHI_FORMAT_UNDEFINED;
}
}
VkFormat _toVkFormat(PrRhiFormat fmt) {
switch (fmt) {
case PR_RHI_FORMAT_R8G8B8A8_SRGB: return VK_FORMAT_R8G8B8A8_SRGB;
@@ -253,6 +269,8 @@ VkSamplerAddressMode _toVkAddressMode(PrRhiAddressMode m) {
PrRhiInstance *prRhiCreateInstanceVk(PrRhiInstanceDesc desc, WpAllocator *alloc) {
(void)desc;
volkInitialize();
VkBool32 supported = VK_FALSE;
_checkVk(vpGetInstanceProfileSupport(NULL, &_profile, &supported), "vpGetInstanceProfileSupport");
if (!supported) _abort("VP_PRISM_DESKTOP_2026 not supported at instance level");
@@ -296,6 +314,10 @@ void prRhiDestroyInstanceVk(PrRhiInstance *inst, WpAllocator *alloc) {
wpMemAllocatorFree(alloc, (void**)&inst, sizeof(PrRhiInstance));
}
void *prRhiGetNativeInstanceHandleVk(PrRhiInstance *inst) {
return inst->handle;
}
// ============================================================================
// Physical device enumeration
// ============================================================================
@@ -321,6 +343,14 @@ PrRhiPhysicalDeviceArray prRhiGetPhysicalDevicesVk(PrRhiInstance *inst, const Wp
if (!pdev) _abort("alloc failed for PrRhiPhysicalDevice");
pdev->handle = vk_devices[i];
pdev->instance = inst;
VkPhysicalDeviceProperties2 props = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
VkPhysicalDeviceDriverProperties driver = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES };
props.pNext = &driver;
vkGetPhysicalDeviceProperties2(vk_devices[i], &props);
memcpy(pdev->device_name, props.properties.deviceName, sizeof(pdev->device_name));
memcpy(pdev->driver_info, driver.driverInfo, sizeof(pdev->driver_info));
wpArrayAppendCapped(PrRhiPhysicalDevice *, pdevs, &pdev);
}
@@ -328,24 +358,25 @@ PrRhiPhysicalDeviceArray prRhiGetPhysicalDevicesVk(PrRhiInstance *inst, const Wp
}
void prRhiGetPhysicalDeviceNameVk(PrRhiPhysicalDevice *pdev, WpStr8 *out) {
VkPhysicalDeviceProperties2 props = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
vkGetPhysicalDeviceProperties2((VkPhysicalDevice)pdev->handle, &props);
const char *name = props.properties.deviceName;
out->buf = (c8*)name;
out->size = strlen(name);
out->buf = pdev->device_name;
out->size = strlen((const char *)pdev->device_name);
out->capacity = 0;
}
void prRhiGetPhysicalDeviceDriverInfoVk(PrRhiPhysicalDevice *pdev, WpStr8 *out) {
VkPhysicalDeviceDriverProperties driver_props = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES };
VkPhysicalDeviceProperties2 props = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, .pNext = &driver_props };
vkGetPhysicalDeviceProperties2((VkPhysicalDevice)pdev->handle, &props);
const char *info = driver_props.driverInfo;
out->buf = (c8*)info;
out->size = strlen(info);
out->buf = pdev->driver_info;
out->size = strlen((const char *)pdev->driver_info);
out->capacity = 0;
}
void prRhiGetPhysicalDevicePropertiesVk(PrRhiPhysicalDevice *pdev,
PrRhiPhysicalDeviceProperties *out) {
VkPhysicalDeviceProperties2 props = { .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
vkGetPhysicalDeviceProperties2((VkPhysicalDevice)pdev->handle, &props);
out->api_version = props.properties.apiVersion;
out->device_type = (u32)props.properties.deviceType;
}
// ============================================================================
// Surface
// ============================================================================
@@ -501,6 +532,10 @@ void prRhiDeviceWaitIdleVk(PrRhiDevice *device) {
_checkVk(vkDeviceWaitIdle((VkDevice)device->handle), "vkDeviceWaitIdle");
}
u32 prRhiGetQueueFamilyIndexVk(PrRhiDevice *device) {
return device->queue_family_index;
}
// ============================================================================
// Swapchain
// ============================================================================
@@ -606,19 +641,25 @@ PrRhiSwapchain *prRhiCreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchainDesc d
swapchain->images = images;
swapchain->depth = NULL;
swapchain->format = _default_image_format;
swapchain->depth_format = VK_FORMAT_UNDEFINED;
swapchain->width = extent.width;
swapchain->height = extent.height;
swapchain->current_image_index = 0;
// Create depth texture if requested
VkFormat depth_pick_fmt = VK_FORMAT_UNDEFINED;
if (desc.has_depth) {
VkFormat depth_fmt = _pickDepthFormat(vk_pdev);
if (depth_fmt == VK_FORMAT_UNDEFINED) _abort("no suitable depth format");
if (desc.depth_format != PR_RHI_FORMAT_UNDEFINED) {
depth_pick_fmt = _toVkFormat(desc.depth_format);
} else {
depth_pick_fmt = _pickDepthFormat(vk_pdev);
}
if (depth_pick_fmt == VK_FORMAT_UNDEFINED) _abort("no suitable depth format");
VkImageCreateInfo depth_img_info = {};
depth_img_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
depth_img_info.imageType = VK_IMAGE_TYPE_2D;
depth_img_info.format = depth_fmt;
depth_img_info.format = depth_pick_fmt;
depth_img_info.extent.width = extent.width;
depth_img_info.extent.height = extent.height;
depth_img_info.extent.depth = 1;
@@ -643,7 +684,7 @@ PrRhiSwapchain *prRhiCreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchainDesc d
depth_view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
depth_view_info.image = depth_image;
depth_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
depth_view_info.format = depth_fmt;
depth_view_info.format = depth_pick_fmt;
depth_view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
depth_view_info.subresourceRange.levelCount = 1;
depth_view_info.subresourceRange.layerCount = 1;
@@ -658,9 +699,10 @@ PrRhiSwapchain *prRhiCreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchainDesc d
depth_tex->allocation = depth_allocation;
depth_tex->width = extent.width;
depth_tex->height = extent.height;
depth_tex->format = depth_fmt;
depth_tex->format = depth_pick_fmt;
swapchain->depth = depth_tex;
swapchain->depth = depth_tex;
swapchain->depth_format = depth_pick_fmt;
}
return swapchain;
@@ -809,7 +851,7 @@ void prRhiRecreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchain **swapchain,
// Recreate depth
PrRhiTexture *new_depth = NULL;
{
VkFormat depth_fmt = _pickDepthFormat(vk_pdev);
VkFormat depth_fmt = old->depth_format ? (VkFormat)old->depth_format : _pickDepthFormat(vk_pdev);
if (depth_fmt == VK_FORMAT_UNDEFINED) _abort("no suitable depth format (recreate)");
VkImageCreateInfo depth_img_info = {};
@@ -853,9 +895,10 @@ void prRhiRecreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchain **swapchain,
new_depth->image = depth_image;
new_depth->view = depth_view;
new_depth->allocation = depth_allocation;
new_depth->width = extent.width;
new_depth->height = extent.height;
new_depth->format = depth_fmt;
new_depth->width = extent.width;
new_depth->height = extent.height;
new_depth->format = depth_fmt;
old->depth_format = depth_fmt;
}
// Update old swapchain in-place
@@ -879,6 +922,10 @@ PrRhiTexture *prRhiGetSwapchainDepthTextureVk(PrRhiSwapchain *swapchain) {
return swapchain->depth;
}
PrRhiFormat prRhiGetSwapchainFormatVk(PrRhiSwapchain *swapchain) {
return _fromVkFormat((VkFormat)swapchain->format);
}
// ============================================================================
// Buffers
// ============================================================================
@@ -1097,7 +1144,7 @@ PrRhiPipelineLayout *prRhiCreatePipelineLayoutVk(PrRhiDevice *device,
// Gather descriptor set layouts
u32 layout_count = (desc.set_layouts && wpArrayCount(desc.set_layouts) > 0)
? (u32)wpArrayCount(desc.set_layouts) : 0;
VkDescriptorSetLayout vk_layouts_stack[8];
VkDescriptorSetLayout vk_layouts_stack[8] = {0};
VkDescriptorSetLayout *vk_layouts = vk_layouts_stack;
if (layout_count > 8) {
@@ -1111,7 +1158,7 @@ PrRhiPipelineLayout *prRhiCreatePipelineLayoutVk(PrRhiDevice *device,
// Gather push constant ranges
u32 pc_count = (desc.push_constant_ranges && wpArrayCount(desc.push_constant_ranges) > 0)
? (u32)wpArrayCount(desc.push_constant_ranges) : 0;
VkPushConstantRange pc_ranges_stack[8];
VkPushConstantRange pc_ranges_stack[8] = {0};
VkPushConstantRange *pc_ranges = pc_ranges_stack;
if (pc_count > 8) {
@@ -1184,7 +1231,7 @@ PrRhiPipeline *prRhiCreateGraphicsPipelineVk(PrRhiDevice *device,
}
// Vertex input state
VkVertexInputBindingDescription vk_bindings[8];
VkVertexInputBindingDescription vk_bindings[8] = {0};
u32 binding_count = 0;
if (desc.vertex_bindings && wpArrayCount(desc.vertex_bindings) > 0) {
binding_count = (u32)wpArrayCount(desc.vertex_bindings);
@@ -1195,7 +1242,7 @@ PrRhiPipeline *prRhiCreateGraphicsPipelineVk(PrRhiDevice *device,
}
}
VkVertexInputAttributeDescription vk_attrs[16];
VkVertexInputAttributeDescription vk_attrs[16] = {0};
u32 attr_count = 0;
if (desc.vertex_attributes && wpArrayCount(desc.vertex_attributes) > 0) {
attr_count = (u32)wpArrayCount(desc.vertex_attributes);
@@ -1225,7 +1272,7 @@ PrRhiPipeline *prRhiCreateGraphicsPipelineVk(PrRhiDevice *device,
viewport_state.viewportCount = 1;
viewport_state.scissorCount = 1;
VkDynamicState dynamic_states_stack[2];
VkDynamicState dynamic_states_stack[2] = {0};
u32 dynamic_count = 0;
if (desc.dynamic_viewport) {
dynamic_states_stack[dynamic_count++] = VK_DYNAMIC_STATE_VIEWPORT;
@@ -1253,7 +1300,7 @@ PrRhiPipeline *prRhiCreateGraphicsPipelineVk(PrRhiDevice *device,
rendering_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
rendering_info.depthAttachmentFormat = _toVkFormat(desc.depth_attachment_format);
VkFormat vk_color_formats[8];
VkFormat vk_color_formats[8] = {0};
u32 color_format_count = 0;
if (desc.color_attachment_formats && wpArrayCount(desc.color_attachment_formats) > 0) {
color_format_count = (u32)wpArrayCount(desc.color_attachment_formats);
@@ -1265,7 +1312,7 @@ PrRhiPipeline *prRhiCreateGraphicsPipelineVk(PrRhiDevice *device,
rendering_info.pColorAttachmentFormats = vk_color_formats;
// Blending
VkPipelineColorBlendAttachmentState blend_attachments[8];
VkPipelineColorBlendAttachmentState blend_attachments[8] = {0};
u32 blend_count = 0;
if (desc.blend_attachments && wpArrayCount(desc.blend_attachments) > 0) {
blend_count = (u32)wpArrayCount(desc.blend_attachments);
@@ -1365,8 +1412,8 @@ PrRhiDescriptorSetLayout *prRhiCreateDescriptorSetLayoutVk(PrRhiDevice *device,
u32 binding_count = (desc.bindings && wpArrayCount(desc.bindings) > 0)
? (u32)wpArrayCount(desc.bindings) : 0;
VkDescriptorSetLayoutBinding vk_bindings[16];
VkDescriptorBindingFlags vk_flags[16];
VkDescriptorSetLayoutBinding vk_bindings[16] = {0};
VkDescriptorBindingFlags vk_flags[16] = {0};
for (u32 i = 0; i < binding_count && i < 16; ++i) {
vk_bindings[i].binding = i;
@@ -1417,7 +1464,7 @@ PrRhiDescriptorPool *prRhiCreateDescriptorPoolVk(PrRhiDevice *device,
u32 pool_size_count = (desc.pool_sizes && wpArrayCount(desc.pool_sizes) > 0)
? (u32)wpArrayCount(desc.pool_sizes) : 0;
VkDescriptorPoolSize vk_sizes[8];
VkDescriptorPoolSize vk_sizes[8] = {0};
for (u32 i = 0; i < pool_size_count && i < 8; ++i) {
vk_sizes[i].type = _toVkDescriptorType(desc.pool_sizes[i].type);
vk_sizes[i].descriptorCount = desc.pool_sizes[i].descriptor_count;
@@ -1504,7 +1551,7 @@ void prRhiUpdateDescriptorSetVk(PrRhiDevice *device, PrRhiWriteDescriptorSetArra
if (w->image_info && wpArrayCount(w->image_info) > 0) {
u32 img_count = (u32)wpArrayCount(w->image_info);
VkDescriptorImageInfo vk_img_info[16];
VkDescriptorImageInfo vk_img_info[16] = {0};
u32 img_max = img_count > 16 ? 16 : img_count;
for (u32 j = 0; j < img_max; ++j) {
PrRhiDescriptorImageInfo *src = &w->image_info[j];
@@ -1526,7 +1573,7 @@ void prRhiUpdateDescriptorSetVk(PrRhiDevice *device, PrRhiWriteDescriptorSetArra
if (w->buffer_info && wpArrayCount(w->buffer_info) > 0) {
u32 buf_count = (u32)wpArrayCount(w->buffer_info);
VkDescriptorBufferInfo vk_buf_info[16];
VkDescriptorBufferInfo vk_buf_info[16] = {0};
u32 buf_max = buf_count > 16 ? 16 : buf_count;
for (u32 j = 0; j < buf_max; ++j) {
PrRhiDescriptorBufferInfo *src = &w->buffer_info[j];
@@ -1576,7 +1623,7 @@ void prRhiDestroyFenceVk(PrRhiDevice *device, PrRhiFence *fence, WpAllocator *al
void prRhiWaitForFencesVk(PrRhiDevice *device, PrRhiFenceArray fences, u32 count,
b8 wait_all, u64 timeout_ns) {
VkFence vk_fences[16];
VkFence vk_fences[16] = {0};
u32 real_count = count < 16 ? count : 16;
for (u32 i = 0; i < real_count; ++i) {
vk_fences[i] = (VkFence)fences[i]->handle;
@@ -1587,7 +1634,7 @@ void prRhiWaitForFencesVk(PrRhiDevice *device, PrRhiFenceArray fences, u32 count
}
void prRhiResetFencesVk(PrRhiDevice *device, PrRhiFenceArray fences, u32 count) {
VkFence vk_fences[16];
VkFence vk_fences[16] = {0};
u32 real_count = count < 16 ? count : 16;
for (u32 i = 0; i < real_count; ++i) {
vk_fences[i] = (VkFence)fences[i]->handle;
@@ -1655,7 +1702,7 @@ PrRhiCommandBufferArray prRhiAllocateCommandBuffersVk(PrRhiDevice *device, PrRhi
info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
info.commandBufferCount = count;
VkCommandBuffer vk_cbs[16];
VkCommandBuffer vk_cbs[16] = {0};
u32 real_count = count < 16 ? count : 16;
VkCommandBufferArray vk_cb_array = wpArrayAllocCapacity(VkCommandBuffer, alloc, real_count, WP_ARRAY_INIT_NONE);
@@ -1690,7 +1737,7 @@ void prRhiFreeCommandBuffersVk(PrRhiDevice *device, PrRhiCommandPool *pool,
VkDevice vk_device = (VkDevice)device->handle;
VkCommandPool vk_pool = (VkCommandPool)pool->handle;
VkCommandBuffer vk_cbs[16];
VkCommandBuffer vk_cbs[16] = {0};
u32 real_count = count < 16 ? count : 16;
for (u32 i = 0; i < real_count; ++i) {
vk_cbs[i] = (VkCommandBuffer)buffers[i]->handle;
@@ -1743,7 +1790,7 @@ void prRhiCmdPipelineBarrierVk(PrRhiCommandBuffer *cb,
if (img_count == 0 && buf_count == 0) return;
VkImageMemoryBarrier2 vk_img_barriers[16];
VkImageMemoryBarrier2 vk_img_barriers[16] = {0};
for (u32 i = 0; i < img_count && i < 16; ++i) {
PrRhiImageMemoryBarrier *src = &image_barriers[i];
VkImageLayout old_layout = _toVkImageLayout(src->old_layout);
@@ -1756,16 +1803,11 @@ void prRhiCmdPipelineBarrierVk(PrRhiCommandBuffer *cb,
aspect = VK_IMAGE_ASPECT_DEPTH_BIT;
}
VkPipelineStageFlags2 src_stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
VkPipelineStageFlags2 dst_stage = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
VkAccessFlags2 src_access = 0;
VkAccessFlags2 dst_access = 0;
vk_img_barriers[i].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
vk_img_barriers[i].srcStageMask = src_stage;
vk_img_barriers[i].srcAccessMask = src_access;
vk_img_barriers[i].dstStageMask = dst_stage;
vk_img_barriers[i].dstAccessMask = dst_access;
vk_img_barriers[i].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
vk_img_barriers[i].srcStageMask = (VkPipelineStageFlags2)src->src_stage_mask;
vk_img_barriers[i].srcAccessMask = (VkAccessFlags2)src->src_access_mask;
vk_img_barriers[i].dstStageMask = (VkPipelineStageFlags2)src->dst_stage_mask;
vk_img_barriers[i].dstAccessMask = (VkAccessFlags2)src->dst_access_mask;
vk_img_barriers[i].oldLayout = old_layout;
vk_img_barriers[i].newLayout = new_layout;
vk_img_barriers[i].image = src->texture ? (VkImage)src->texture->image : VK_NULL_HANDLE;
@@ -1774,15 +1816,15 @@ void prRhiCmdPipelineBarrierVk(PrRhiCommandBuffer *cb,
vk_img_barriers[i].subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
}
VkBufferMemoryBarrier2 vk_buf_barriers[16];
VkBufferMemoryBarrier2 vk_buf_barriers[16] = {0};
for (u32 i = 0; i < buf_count && i < 16; ++i) {
PrRhiBufferMemoryBarrier *src = &buffer_barriers[i];
vk_buf_barriers[i].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER_2;
vk_buf_barriers[i].srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
vk_buf_barriers[i].srcAccessMask = 0;
vk_buf_barriers[i].dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT;
vk_buf_barriers[i].dstAccessMask = 0;
vk_buf_barriers[i].srcStageMask = (VkPipelineStageFlags2)src->src_stage_mask;
vk_buf_barriers[i].srcAccessMask = (VkAccessFlags2)src->src_access_mask;
vk_buf_barriers[i].dstStageMask = (VkPipelineStageFlags2)src->dst_stage_mask;
vk_buf_barriers[i].dstAccessMask = (VkAccessFlags2)src->dst_access_mask;
vk_buf_barriers[i].buffer = src->buffer ? (VkBuffer)src->buffer->handle : VK_NULL_HANDLE;
vk_buf_barriers[i].offset = src->offset;
vk_buf_barriers[i].size = src->size;
@@ -1810,7 +1852,7 @@ void prRhiCmdBeginRenderingVk(PrRhiCommandBuffer *cb,
u32 color_count = (color_attachments && wpArrayCount(color_attachments) > 0)
? (u32)wpArrayCount(color_attachments) : 0;
VkRenderingAttachmentInfo vk_color[8];
VkRenderingAttachmentInfo vk_color[8] = {0};
u32 width = 0, height = 0;
for (u32 i = 0; i < color_count && i < 8; ++i) {
@@ -1900,7 +1942,7 @@ void prRhiCmdBindDescriptorSetsVk(PrRhiCommandBuffer *cb, PrRhiPipelineBindPoint
VkPipelineLayout vk_layout = layout ? (VkPipelineLayout)layout->handle : VK_NULL_HANDLE;
u32 set_count = sets ? (u32)wpArrayCount(sets) : 0;
VkDescriptorSet vk_sets[16];
VkDescriptorSet vk_sets[16] = {0};
u32 real_count = set_count < 16 ? set_count : 16;
for (u32 i = 0; i < real_count; ++i) {
vk_sets[i] = (VkDescriptorSet)sets[i]->handle;
@@ -1920,7 +1962,7 @@ void prRhiCmdPushConstantsVk(PrRhiCommandBuffer *cb, PrRhiPipelineLayout *layout
void prRhiCmdBindVertexBuffersVk(PrRhiCommandBuffer *cb, u32 first_binding,
PrRhiBufferArray buffers, const u64 *offsets, u32 count) {
VkBuffer vk_bufs[16];
VkBuffer vk_bufs[16] = {0};
u32 real_count = count < 16 ? count : 16;
for (u32 i = 0; i < real_count; ++i) {
vk_bufs[i] = (VkBuffer)buffers[i]->handle;
@@ -1955,23 +1997,31 @@ void prRhiCmdDrawIndexedVk(PrRhiCommandBuffer *cb, u32 index_count, u32 instance
// Copy
// ============================================================================
void prRhiCmdCopyBufferToImageVk(PrRhiCommandBuffer *cb, PrRhiBuffer *src, PrRhiTexture *dst) {
VkBufferImageCopy region = {};
region.bufferOffset = 0;
region.bufferRowLength = 0;
region.bufferImageHeight = 0;
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
region.imageSubresource.mipLevel = 0;
region.imageSubresource.layerCount = 1;
region.imageExtent.width = dst->width;
region.imageExtent.height = dst->height;
region.imageExtent.depth = 1;
void prRhiCmdCopyBufferToImageVk(PrRhiCommandBuffer *cb, PrRhiBuffer *src, PrRhiTexture *dst,
PrRhiBufferImageCopyArray copies) {
u32 copy_count = (copies && wpArrayCount(copies) > 0) ? (u32)wpArrayCount(copies) : 0;
if (copy_count == 0) return;
VkBufferImageCopy vk_regions[16] = {0};
u32 real_count = copy_count < 16 ? copy_count : 16;
for (u32 i = 0; i < real_count; ++i) {
vk_regions[i].bufferOffset = copies[i].buffer_offset;
vk_regions[i].bufferRowLength = 0;
vk_regions[i].bufferImageHeight = 0;
vk_regions[i].imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
vk_regions[i].imageSubresource.mipLevel = copies[i].mip_level;
vk_regions[i].imageSubresource.baseArrayLayer = 0;
vk_regions[i].imageSubresource.layerCount = 1;
vk_regions[i].imageExtent.width = copies[i].width;
vk_regions[i].imageExtent.height = copies[i].height;
vk_regions[i].imageExtent.depth = 1;
}
vkCmdCopyBufferToImage((VkCommandBuffer)cb->handle,
(VkBuffer)src->handle,
(VkImage)dst->image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1, &region);
real_count, vk_regions);
}
// ============================================================================
+11 -2
View File
@@ -21,6 +21,8 @@ struct PrRhiInstance {
struct PrRhiPhysicalDevice {
void *handle; // VkPhysicalDevice
PrRhiInstance *instance;
c8 device_name[256];
c8 driver_info[256];
};
struct PrRhiDevice {
@@ -43,7 +45,8 @@ struct PrRhiSwapchain {
u32 image_count;
PrRhiTexture **images;
PrRhiTexture *depth;
u32 format; // VkFormat
u32 format; // VkFormat (color)
u32 depth_format; // VkFormat (depth)
u32 width;
u32 height;
u32 current_image_index;
@@ -116,10 +119,13 @@ struct PrRhiSemaphore {
PrRhiInstance *prRhiCreateInstanceVk(PrRhiInstanceDesc desc, WpAllocator *alloc);
void prRhiDestroyInstanceVk(PrRhiInstance *inst, WpAllocator *alloc);
void *prRhiGetNativeInstanceHandleVk(PrRhiInstance *inst);
PrRhiPhysicalDeviceArray prRhiGetPhysicalDevicesVk(PrRhiInstance *inst, const WpAllocator *scratch);
void prRhiGetPhysicalDeviceNameVk(PrRhiPhysicalDevice *pdev, WpStr8 *out);
void prRhiGetPhysicalDeviceDriverInfoVk(PrRhiPhysicalDevice *pdev, WpStr8 *out);
void prRhiGetPhysicalDevicePropertiesVk(PrRhiPhysicalDevice *pdev,
PrRhiPhysicalDeviceProperties *out);
PrRhiSurface *prRhiCreateSurfaceVk(PrRhiInstance *inst, void *window_handle, WpAllocator *alloc);
void prRhiDestroySurfaceVk(PrRhiInstance *inst, PrRhiSurface *surface, WpAllocator *alloc);
@@ -131,6 +137,7 @@ PrRhiDevice *prRhiCreateDeviceVk(PrRhiPhysicalDevice *pdev, PrRhiSurface *surfac
PrRhiDeviceDesc desc, WpAllocator *alloc);
void prRhiDestroyDeviceVk(PrRhiDevice *device, WpAllocator *alloc);
void prRhiDeviceWaitIdleVk(PrRhiDevice *device);
u32 prRhiGetQueueFamilyIndexVk(PrRhiDevice *device);
PrRhiSwapchain *prRhiCreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchainDesc desc,
WpAllocator *alloc);
@@ -144,6 +151,7 @@ void prRhiRecreateSwapchainVk(PrRhiDevice *device, PrRhiSwapchain **swapchain,
u32 width, u32 height, WpAllocator *alloc);
PrRhiTexture *prRhiGetSwapchainTextureVk(PrRhiSwapchain *swapchain, u32 image_index);
PrRhiTexture *prRhiGetSwapchainDepthTextureVk(PrRhiSwapchain *swapchain);
PrRhiFormat prRhiGetSwapchainFormatVk(PrRhiSwapchain *swapchain);
PrRhiBuffer *prRhiCreateBufferVk(PrRhiDevice *device, PrRhiBufferDesc desc,
WpAllocator *alloc);
@@ -260,7 +268,8 @@ void prRhiCmdDrawVk(PrRhiCommandBuffer *cb, u32 vertex_count, u32 instance_count
void prRhiCmdDrawIndexedVk(PrRhiCommandBuffer *cb, u32 index_count, u32 instance_count,
u32 first_index, i32 vertex_offset, u32 first_instance);
void prRhiCmdCopyBufferToImageVk(PrRhiCommandBuffer *cb, PrRhiBuffer *src, PrRhiTexture *dst);
void prRhiCmdCopyBufferToImageVk(PrRhiCommandBuffer *cb, PrRhiBuffer *src, PrRhiTexture *dst,
PrRhiBufferImageCopyArray copies);
void prRhiQueueSubmitVk(PrRhiDevice *device, PrRhiCommandBuffer *cb,
PrRhiSemaphore *wait_semaphore,
+4
View File
@@ -7,15 +7,18 @@
#define prRhiCreateInstance prRhiCreateInstanceVk
#define prRhiDestroyInstance prRhiDestroyInstanceVk
#define prRhiGetNativeInstanceHandle prRhiGetNativeInstanceHandleVk
#define prRhiGetPhysicalDevices prRhiGetPhysicalDevicesVk
#define prRhiGetPhysicalDeviceName prRhiGetPhysicalDeviceNameVk
#define prRhiGetPhysicalDeviceDriverInfo prRhiGetPhysicalDeviceDriverInfoVk
#define prRhiGetPhysicalDeviceProperties prRhiGetPhysicalDevicePropertiesVk
#define prRhiCreateSurface prRhiCreateSurfaceVk
#define prRhiDestroySurface prRhiDestroySurfaceVk
#define prRhiGetSurfaceCapabilities prRhiGetSurfaceCapabilitiesVk
#define prRhiCreateDevice prRhiCreateDeviceVk
#define prRhiDestroyDevice prRhiDestroyDeviceVk
#define prRhiDeviceWaitIdle prRhiDeviceWaitIdleVk
#define prRhiGetQueueFamilyIndex prRhiGetQueueFamilyIndexVk
#define prRhiCreateSwapchain prRhiCreateSwapchainVk
#define prRhiDestroySwapchain prRhiDestroySwapchainVk
#define prRhiAcquireNextImage prRhiAcquireNextImageVk
@@ -23,6 +26,7 @@
#define prRhiRecreateSwapchain prRhiRecreateSwapchainVk
#define prRhiGetSwapchainTexture prRhiGetSwapchainTextureVk
#define prRhiGetSwapchainDepthTexture prRhiGetSwapchainDepthTextureVk
#define prRhiGetSwapchainFormat prRhiGetSwapchainFormatVk
#define prRhiCreateBuffer prRhiCreateBufferVk
#define prRhiDestroyBuffer prRhiDestroyBufferVk
#define prRhiBufferMap prRhiBufferMapVk