diff options
Diffstat (limited to 'drivers/vulkan')
-rw-r--r-- | drivers/vulkan/SCsub | 20 | ||||
-rw-r--r-- | drivers/vulkan/rendering_device_vulkan.cpp | 931 | ||||
-rw-r--r-- | drivers/vulkan/rendering_device_vulkan.h | 52 | ||||
-rw-r--r-- | drivers/vulkan/vulkan_context.cpp | 270 | ||||
-rw-r--r-- | drivers/vulkan/vulkan_context.h | 20 |
5 files changed, 870 insertions, 423 deletions
diff --git a/drivers/vulkan/SCsub b/drivers/vulkan/SCsub index 7ffdac27d5..91d0e42f80 100644 --- a/drivers/vulkan/SCsub +++ b/drivers/vulkan/SCsub @@ -4,7 +4,25 @@ Import("env") env.add_source_files(env.drivers_sources, "*.cpp") -if env["builtin_vulkan"]: +if env["platform"] == "android": + # Use NDK Vulkan headers + thirdparty_dir = env["ANDROID_NDK_ROOT"] + "/sources/third_party/vulkan/src" + thirdparty_includes = [ + thirdparty_dir, + thirdparty_dir + "/include", + thirdparty_dir + "/layers", + thirdparty_dir + "/layers/generated", + ] + env.Prepend(CPPPATH=thirdparty_includes) + + # Build Vulkan memory allocator + env_thirdparty = env.Clone() + env_thirdparty.disable_warnings() + + thirdparty_dir = "#thirdparty/vulkan" + vma_sources = [thirdparty_dir + "/android/vk_mem_alloc.cpp"] + env_thirdparty.add_source_files(env.drivers_sources, vma_sources) +elif env["builtin_vulkan"]: # Use bundled Vulkan headers thirdparty_dir = "#thirdparty/vulkan" env.Prepend(CPPPATH=[thirdparty_dir, thirdparty_dir + "/include", thirdparty_dir + "/loader"]) diff --git a/drivers/vulkan/rendering_device_vulkan.cpp b/drivers/vulkan/rendering_device_vulkan.cpp index 8ebf5b0f04..23e9227a39 100644 --- a/drivers/vulkan/rendering_device_vulkan.cpp +++ b/drivers/vulkan/rendering_device_vulkan.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "rendering_device_vulkan.h" + #include "core/hashfuncs.h" #include "core/os/file_access.h" #include "core/os/os.h" @@ -1234,13 +1235,13 @@ const VkImageType RenderingDeviceVulkan::vulkan_image_type[RenderingDevice::TEXT Error RenderingDeviceVulkan::_buffer_allocate(Buffer *p_buffer, uint32_t p_size, uint32_t p_usage, VmaMemoryUsage p_mapping) { VkBufferCreateInfo bufferInfo; bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; - bufferInfo.pNext = NULL; + bufferInfo.pNext = nullptr; bufferInfo.flags = 0; bufferInfo.size = p_size; bufferInfo.usage = p_usage; bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; bufferInfo.queueFamilyIndexCount = 0; - bufferInfo.pQueueFamilyIndices = 0; + bufferInfo.pQueueFamilyIndices = nullptr; VmaAllocationCreateInfo allocInfo; allocInfo.flags = 0; @@ -1248,10 +1249,10 @@ Error RenderingDeviceVulkan::_buffer_allocate(Buffer *p_buffer, uint32_t p_size, allocInfo.requiredFlags = 0; allocInfo.preferredFlags = 0; allocInfo.memoryTypeBits = 0; - allocInfo.pool = NULL; - allocInfo.pUserData = NULL; + allocInfo.pool = nullptr; + allocInfo.pUserData = nullptr; - VkResult err = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &p_buffer->buffer, &p_buffer->allocation, NULL); + VkResult err = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &p_buffer->buffer, &p_buffer->allocation, nullptr); ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "Can't create buffer of size: " + itos(p_size) + ", error " + itos(err) + "."); p_buffer->size = p_size; p_buffer->buffer_info.buffer = p_buffer->buffer; @@ -1265,8 +1266,8 @@ Error RenderingDeviceVulkan::_buffer_free(Buffer *p_buffer) { ERR_FAIL_COND_V(p_buffer->size == 0, ERR_INVALID_PARAMETER); vmaDestroyBuffer(allocator, p_buffer->buffer, p_buffer->allocation); - p_buffer->buffer = NULL; - p_buffer->allocation = NULL; + p_buffer->buffer = VK_NULL_HANDLE; + p_buffer->allocation = nullptr; p_buffer->size = 0; return OK; @@ -1276,13 +1277,13 @@ Error RenderingDeviceVulkan::_insert_staging_block() { VkBufferCreateInfo bufferInfo; bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; - bufferInfo.pNext = NULL; + bufferInfo.pNext = nullptr; bufferInfo.flags = 0; bufferInfo.size = staging_buffer_block_size; bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE; bufferInfo.queueFamilyIndexCount = 0; - bufferInfo.pQueueFamilyIndices = 0; + bufferInfo.pQueueFamilyIndices = nullptr; VmaAllocationCreateInfo allocInfo; allocInfo.flags = 0; @@ -1290,12 +1291,12 @@ Error RenderingDeviceVulkan::_insert_staging_block() { allocInfo.requiredFlags = 0; allocInfo.preferredFlags = 0; allocInfo.memoryTypeBits = 0; - allocInfo.pool = NULL; - allocInfo.pUserData = NULL; + allocInfo.pool = nullptr; + allocInfo.pUserData = nullptr; StagingBufferBlock block; - VkResult err = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &block.buffer, &block.allocation, NULL); + VkResult err = vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &block.buffer, &block.allocation, nullptr); ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "vmaCreateBuffer failed with error " + itos(err) + "."); block.frame_used = 0; @@ -1459,7 +1460,7 @@ Error RenderingDeviceVulkan::_buffer_update(Buffer *p_buffer, size_t p_offset, c //map staging buffer (It's CPU and coherent) - void *data_ptr = NULL; + void *data_ptr = nullptr; { VkResult vkerr = vmaMapMemory(allocator, staging_buffer_blocks[staging_buffer_current].allocation, &data_ptr); ERR_FAIL_COND_V_MSG(vkerr, ERR_CANT_CREATE, "vmaMapMemory failed with error " + itos(vkerr) + "."); @@ -1492,11 +1493,11 @@ void RenderingDeviceVulkan::_memory_barrier(VkPipelineStageFlags p_src_stage_mas VkMemoryBarrier mem_barrier; mem_barrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; - mem_barrier.pNext = NULL; + mem_barrier.pNext = nullptr; mem_barrier.srcAccessMask = p_src_access; mem_barrier.dstAccessMask = p_dst_sccess; - vkCmdPipelineBarrier(p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, p_src_stage_mask, p_dst_stage_mask, 0, 1, &mem_barrier, 0, NULL, 0, NULL); + vkCmdPipelineBarrier(p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, p_src_stage_mask, p_dst_stage_mask, 0, 1, &mem_barrier, 0, nullptr, 0, nullptr); } void RenderingDeviceVulkan::_full_barrier(bool p_sync_with_draw) { @@ -1539,7 +1540,7 @@ void RenderingDeviceVulkan::_buffer_memory_barrier(VkBuffer buffer, uint64_t p_f VkBufferMemoryBarrier buffer_mem_barrier; buffer_mem_barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER; - buffer_mem_barrier.pNext = NULL; + buffer_mem_barrier.pNext = nullptr; buffer_mem_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; buffer_mem_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; buffer_mem_barrier.srcAccessMask = p_src_access; @@ -1548,7 +1549,7 @@ void RenderingDeviceVulkan::_buffer_memory_barrier(VkBuffer buffer, uint64_t p_f buffer_mem_barrier.offset = p_from; buffer_mem_barrier.size = p_size; - vkCmdPipelineBarrier(p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, p_src_stage_mask, p_dst_stage_mask, 0, 0, NULL, 1, &buffer_mem_barrier, 0, NULL); + vkCmdPipelineBarrier(p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, p_src_stage_mask, p_dst_stage_mask, 0, 0, nullptr, 1, &buffer_mem_barrier, 0, nullptr); } /*****************/ @@ -1561,20 +1562,30 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T VkImageCreateInfo image_create_info; image_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; - image_create_info.pNext = NULL; + image_create_info.pNext = nullptr; image_create_info.flags = 0; - VkImageFormatListCreateInfoKHR format_list_create_info; - Vector<VkFormat> allowed_formats; +#ifndef _MSC_VER +#warning TODO check for support via RenderingDevice to enable on mobile when possible +#endif + +#ifndef ANDROID_ENABLED + // vkCreateImage fails with format list on Android (VK_ERROR_OUT_OF_HOST_MEMORY) + VkImageFormatListCreateInfoKHR format_list_create_info; //keep out of the if, needed for creation + Vector<VkFormat> allowed_formats; //keep out of the if, needed for creation +#endif if (p_format.shareable_formats.size()) { image_create_info.flags |= VK_IMAGE_CREATE_MUTABLE_FORMAT_BIT; + +#ifndef ANDROID_ENABLED + for (int i = 0; i < p_format.shareable_formats.size(); i++) { allowed_formats.push_back(vulkan_formats[p_format.shareable_formats[i]]); } format_list_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO_KHR; - format_list_create_info.pNext = NULL; + format_list_create_info.pNext = nullptr; format_list_create_info.viewFormatCount = allowed_formats.size(); format_list_create_info.pViewFormats = allowed_formats.ptr(); image_create_info.pNext = &format_list_create_info; @@ -1583,7 +1594,9 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T "If supplied a list of shareable formats, the current format must be present in the list"); ERR_FAIL_COND_V_MSG(p_view.format_override != DATA_FORMAT_MAX && p_format.shareable_formats.find(p_view.format_override) == -1, RID(), "If supplied a list of shareable formats, the current view format override must be present in the list"); +#endif } + if (p_format.type == TEXTURE_TYPE_CUBE || p_format.type == TEXTURE_TYPE_CUBE_ARRAY) { image_create_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT; } @@ -1665,7 +1678,7 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T image_create_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; image_create_info.queueFamilyIndexCount = 0; - image_create_info.pQueueFamilyIndices = NULL; + image_create_info.pQueueFamilyIndices = nullptr; image_create_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; uint32_t required_mipmaps = get_image_required_mipmaps(image_create_info.extent.width, image_create_info.extent.height, image_create_info.extent.depth); @@ -1744,8 +1757,8 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T allocInfo.requiredFlags = 0; allocInfo.preferredFlags = 0; allocInfo.memoryTypeBits = 0; - allocInfo.pool = NULL; - allocInfo.pUserData = NULL; + allocInfo.pool = nullptr; + allocInfo.pUserData = nullptr; Texture texture; @@ -1759,6 +1772,8 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T texture.depth = image_create_info.extent.depth; texture.layers = image_create_info.arrayLayers; texture.mipmaps = image_create_info.mipLevels; + texture.base_mipmap = 0; + texture.base_layer = 0; texture.usage_flags = p_format.usage_bits; texture.samples = p_format.samples; texture.allowed_shared_formats = p_format.shareable_formats; @@ -1806,7 +1821,7 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T VkImageViewCreateInfo image_view_create_info; image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - image_view_create_info.pNext = NULL; + image_view_create_info.pNext = nullptr; image_view_create_info.flags = 0; image_view_create_info.image = texture.image; @@ -1852,7 +1867,7 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T image_view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; } - err = vkCreateImageView(device, &image_view_create_info, NULL, &texture.view); + err = vkCreateImageView(device, &image_view_create_info, nullptr, &texture.view); if (err) { vmaDestroyImage(allocator, texture.image, texture.allocation); @@ -1863,7 +1878,7 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T { VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = 0; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; @@ -1877,7 +1892,7 @@ RID RenderingDeviceVulkan::texture_create(const TextureFormat &p_format, const T image_memory_barrier.subresourceRange.baseArrayLayer = 0; image_memory_barrier.subresourceRange.layerCount = image_create_info.arrayLayers; - vkCmdPipelineBarrier(frames[frame].setup_command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(frames[frame].setup_command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } RID id = texture_owner.make_rid(texture); @@ -1910,7 +1925,7 @@ RID RenderingDeviceVulkan::texture_create_shared(const TextureView &p_view, RID VkImageViewCreateInfo image_view_create_info; image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - image_view_create_info.pNext = NULL; + image_view_create_info.pNext = nullptr; image_view_create_info.flags = 0; image_view_create_info.image = texture.image; @@ -1961,7 +1976,7 @@ RID RenderingDeviceVulkan::texture_create_shared(const TextureView &p_view, RID image_view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; } - VkResult err = vkCreateImageView(device, &image_view_create_info, NULL, &texture.view); + VkResult err = vkCreateImageView(device, &image_view_create_info, nullptr, &texture.view); ERR_FAIL_COND_V_MSG(err, RID(), "vkCreateImageView failed with error " + itos(err) + "."); texture.owner = p_with_texture; @@ -1999,10 +2014,12 @@ RID RenderingDeviceVulkan::texture_create_shared_from_slice(const TextureView &p get_image_format_required_size(texture.format, texture.width, texture.height, texture.depth, p_mipmap + 1, &texture.width, &texture.height); texture.mipmaps = 1; texture.layers = p_slice_type == TEXTURE_SLICE_CUBEMAP ? 6 : 1; + texture.base_mipmap = p_mipmap; + texture.base_layer = p_layer; VkImageViewCreateInfo image_view_create_info; image_view_create_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; - image_view_create_info.pNext = NULL; + image_view_create_info.pNext = nullptr; image_view_create_info.flags = 0; image_view_create_info.image = texture.image; @@ -2059,7 +2076,7 @@ RID RenderingDeviceVulkan::texture_create_shared_from_slice(const TextureView &p image_view_create_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; } - VkResult err = vkCreateImageView(device, &image_view_create_info, NULL, &texture.view); + VkResult err = vkCreateImageView(device, &image_view_create_info, nullptr, &texture.view); ERR_FAIL_COND_V_MSG(err, RID(), "vkCreateImageView failed with error " + itos(err) + "."); texture.owner = p_with_texture; @@ -2121,7 +2138,7 @@ Error RenderingDeviceVulkan::texture_update(RID p_texture, uint32_t p_layer, con { VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = 0; image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.oldLayout = texture->layout; @@ -2136,7 +2153,7 @@ Error RenderingDeviceVulkan::texture_update(RID p_texture, uint32_t p_layer, con image_memory_barrier.subresourceRange.baseArrayLayer = p_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } uint32_t mipmap_offset = 0; @@ -2169,7 +2186,7 @@ Error RenderingDeviceVulkan::texture_update(RID p_texture, uint32_t p_layer, con uint8_t *write_ptr; { //map - void *data_ptr = NULL; + void *data_ptr = nullptr; VkResult vkerr = vmaMapMemory(allocator, staging_buffer_blocks[staging_buffer_current].allocation, &data_ptr); ERR_FAIL_COND_V_MSG(vkerr, ERR_CANT_CREATE, "vmaMapMemory failed with error " + itos(vkerr) + "."); write_ptr = (uint8_t *)data_ptr; @@ -2258,7 +2275,7 @@ Error RenderingDeviceVulkan::texture_update(RID p_texture, uint32_t p_layer, con { VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; @@ -2272,7 +2289,7 @@ Error RenderingDeviceVulkan::texture_update(RID p_texture, uint32_t p_layer, con image_memory_barrier.subresourceRange.baseArrayLayer = p_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } return OK; @@ -2380,7 +2397,7 @@ Vector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint32_t { //Source image barrier VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = 0; image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; image_memory_barrier.oldLayout = tex->layout; @@ -2395,7 +2412,7 @@ Vector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint32_t image_memory_barrier.subresourceRange.baseArrayLayer = p_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } uint32_t computed_w = tex->width; @@ -2437,7 +2454,7 @@ Vector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint32_t { //restore src VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; @@ -2451,7 +2468,7 @@ Vector<uint8_t> RenderingDeviceVulkan::texture_get_data(RID p_texture, uint32_t image_memory_barrier.subresourceRange.baseArrayLayer = p_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } _flush(true); @@ -2546,7 +2563,7 @@ Error RenderingDeviceVulkan::texture_copy(RID p_from_texture, RID p_to_texture, { //Source VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = 0; image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; image_memory_barrier.oldLayout = src_tex->layout; @@ -2561,12 +2578,12 @@ Error RenderingDeviceVulkan::texture_copy(RID p_from_texture, RID p_to_texture, image_memory_barrier.subresourceRange.baseArrayLayer = p_src_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } { //Dest VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = 0; image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.oldLayout = dst_tex->layout; @@ -2581,7 +2598,7 @@ Error RenderingDeviceVulkan::texture_copy(RID p_from_texture, RID p_to_texture, image_memory_barrier.subresourceRange.baseArrayLayer = p_dst_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } //COPY @@ -2617,7 +2634,7 @@ Error RenderingDeviceVulkan::texture_copy(RID p_from_texture, RID p_to_texture, { //restore src VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; @@ -2631,14 +2648,14 @@ Error RenderingDeviceVulkan::texture_copy(RID p_from_texture, RID p_to_texture, image_memory_barrier.subresourceRange.baseArrayLayer = p_src_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } { //make dst readable VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; @@ -2653,7 +2670,160 @@ Error RenderingDeviceVulkan::texture_copy(RID p_from_texture, RID p_to_texture, image_memory_barrier.subresourceRange.baseArrayLayer = p_src_layer; image_memory_barrier.subresourceRange.layerCount = 1; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); + } + } + + return OK; +} +Error RenderingDeviceVulkan::texture_resolve_multisample(RID p_from_texture, RID p_to_texture, bool p_sync_with_draw) { + _THREAD_SAFE_METHOD_ + + Texture *src_tex = texture_owner.getornull(p_from_texture); + ERR_FAIL_COND_V(!src_tex, ERR_INVALID_PARAMETER); + + ERR_FAIL_COND_V_MSG(p_sync_with_draw && src_tex->bound, ERR_INVALID_PARAMETER, + "Source texture can't be copied while a render pass that uses it is being created. Ensure render pass is finalized (and that it was created with RENDER_PASS_CONTENTS_FINISH) to unbind this texture."); + ERR_FAIL_COND_V_MSG(!(src_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_FROM_BIT), ERR_INVALID_PARAMETER, + "Source texture requires the TEXTURE_USAGE_CAN_COPY_FROM_BIT in order to be retrieved."); + + ERR_FAIL_COND_V_MSG(src_tex->type != TEXTURE_TYPE_2D, ERR_INVALID_PARAMETER, "Source texture must be 2D (or a slice of a 3D/Cube texture)"); + ERR_FAIL_COND_V_MSG(src_tex->samples == TEXTURE_SAMPLES_1, ERR_INVALID_PARAMETER, "Source texture must be multisampled."); + + Texture *dst_tex = texture_owner.getornull(p_to_texture); + ERR_FAIL_COND_V(!dst_tex, ERR_INVALID_PARAMETER); + + ERR_FAIL_COND_V_MSG(p_sync_with_draw && dst_tex->bound, ERR_INVALID_PARAMETER, + "Destination texture can't be copied while a render pass that uses it is being created. Ensure render pass is finalized (and that it was created with RENDER_PASS_CONTENTS_FINISH) to unbind this texture."); + ERR_FAIL_COND_V_MSG(!(dst_tex->usage_flags & TEXTURE_USAGE_CAN_COPY_TO_BIT), ERR_INVALID_PARAMETER, + "Destination texture requires the TEXTURE_USAGE_CAN_COPY_TO_BIT in order to be retrieved."); + + ERR_FAIL_COND_V_MSG(dst_tex->type != TEXTURE_TYPE_2D, ERR_INVALID_PARAMETER, "Destination texture must be 2D (or a slice of a 3D/Cube texture)."); + ERR_FAIL_COND_V_MSG(dst_tex->samples != TEXTURE_SAMPLES_1, ERR_INVALID_PARAMETER, "Destination texture must not be multisampled."); + + ERR_FAIL_COND_V_MSG(src_tex->format != dst_tex->format, ERR_INVALID_PARAMETER, "Source and Destionation textures must be the same format."); + ERR_FAIL_COND_V_MSG(src_tex->width != dst_tex->width && src_tex->height != dst_tex->height && src_tex->depth != dst_tex->depth, ERR_INVALID_PARAMETER, "Source and Destionation textures must have the same dimensions."); + + ERR_FAIL_COND_V_MSG(src_tex->read_aspect_mask != dst_tex->read_aspect_mask, ERR_INVALID_PARAMETER, + "Source and destination texture must be of the same type (color or depth)."); + + VkCommandBuffer command_buffer = p_sync_with_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer; + + { + + //PRE Copy the image + + { //Source + VkImageMemoryBarrier image_memory_barrier; + image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; + image_memory_barrier.pNext = nullptr; + image_memory_barrier.srcAccessMask = 0; + image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; + image_memory_barrier.oldLayout = src_tex->layout; + image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; + + image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.image = src_tex->image; + image_memory_barrier.subresourceRange.aspectMask = src_tex->barrier_aspect_mask; + image_memory_barrier.subresourceRange.baseMipLevel = src_tex->base_mipmap; + image_memory_barrier.subresourceRange.levelCount = 1; + image_memory_barrier.subresourceRange.baseArrayLayer = src_tex->base_layer; + image_memory_barrier.subresourceRange.layerCount = 1; + + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); + } + { //Dest + VkImageMemoryBarrier image_memory_barrier; + image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; + image_memory_barrier.pNext = nullptr; + image_memory_barrier.srcAccessMask = 0; + image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + image_memory_barrier.oldLayout = dst_tex->layout; + image_memory_barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; + + image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.image = dst_tex->image; + image_memory_barrier.subresourceRange.aspectMask = dst_tex->read_aspect_mask; + image_memory_barrier.subresourceRange.baseMipLevel = dst_tex->base_mipmap; + image_memory_barrier.subresourceRange.levelCount = 1; + image_memory_barrier.subresourceRange.baseArrayLayer = dst_tex->base_layer; + image_memory_barrier.subresourceRange.layerCount = 1; + + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); + } + + //COPY + + { + + VkImageResolve image_copy_region; + image_copy_region.srcSubresource.aspectMask = src_tex->read_aspect_mask; + image_copy_region.srcSubresource.baseArrayLayer = src_tex->base_layer; + image_copy_region.srcSubresource.layerCount = 1; + image_copy_region.srcSubresource.mipLevel = src_tex->base_mipmap; + image_copy_region.srcOffset.x = 0; + image_copy_region.srcOffset.y = 0; + image_copy_region.srcOffset.z = 0; + + image_copy_region.dstSubresource.aspectMask = dst_tex->read_aspect_mask; + image_copy_region.dstSubresource.baseArrayLayer = dst_tex->base_layer; + image_copy_region.dstSubresource.layerCount = 1; + image_copy_region.dstSubresource.mipLevel = dst_tex->base_mipmap; + image_copy_region.dstOffset.x = 0; + image_copy_region.dstOffset.y = 0; + image_copy_region.dstOffset.z = 0; + + image_copy_region.extent.width = src_tex->width; + image_copy_region.extent.height = src_tex->height; + image_copy_region.extent.depth = src_tex->depth; + + vkCmdResolveImage(command_buffer, src_tex->image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, dst_tex->image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &image_copy_region); + } + + // RESTORE LAYOUT for SRC and DST + + { //restore src + VkImageMemoryBarrier image_memory_barrier; + image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; + image_memory_barrier.pNext = nullptr; + image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT; + image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; + image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; + image_memory_barrier.newLayout = src_tex->layout; + image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.image = src_tex->image; + image_memory_barrier.subresourceRange.aspectMask = src_tex->barrier_aspect_mask; + image_memory_barrier.subresourceRange.baseMipLevel = src_tex->base_mipmap; + image_memory_barrier.subresourceRange.levelCount = 1; + image_memory_barrier.subresourceRange.baseArrayLayer = src_tex->base_layer; + image_memory_barrier.subresourceRange.layerCount = 1; + + vkCmdPipelineBarrier(command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); + } + + { //make dst readable + + VkImageMemoryBarrier image_memory_barrier; + image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; + image_memory_barrier.pNext = nullptr; + image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; + image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; + image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; + image_memory_barrier.newLayout = dst_tex->layout; + + image_memory_barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; + image_memory_barrier.image = dst_tex->image; + image_memory_barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; + image_memory_barrier.subresourceRange.baseMipLevel = dst_tex->base_mipmap; + image_memory_barrier.subresourceRange.levelCount = 1; + image_memory_barrier.subresourceRange.baseArrayLayer = dst_tex->base_layer; + image_memory_barrier.subresourceRange.layerCount = 1; + + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } } @@ -2691,7 +2861,7 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color, if (src_tex->layout != VK_IMAGE_LAYOUT_GENERAL) { //storage may be in general state VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = 0; image_memory_barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.oldLayout = src_tex->layout; @@ -2701,13 +2871,13 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color, image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; image_memory_barrier.image = src_tex->image; image_memory_barrier.subresourceRange.aspectMask = src_tex->read_aspect_mask; - image_memory_barrier.subresourceRange.baseMipLevel = p_base_mipmap; + image_memory_barrier.subresourceRange.baseMipLevel = src_tex->base_mipmap + p_base_mipmap; image_memory_barrier.subresourceRange.levelCount = p_mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = p_base_layer; + image_memory_barrier.subresourceRange.baseArrayLayer = src_tex->base_layer + p_base_layer; image_memory_barrier.subresourceRange.layerCount = p_layers; layout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } VkClearColorValue clear_color; @@ -2718,9 +2888,9 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color, VkImageSubresourceRange range; range.aspectMask = src_tex->read_aspect_mask; - range.baseArrayLayer = p_base_layer; + range.baseArrayLayer = src_tex->base_layer + p_base_layer; range.layerCount = p_layers; - range.baseMipLevel = p_base_mipmap; + range.baseMipLevel = src_tex->base_mipmap + p_base_mipmap; range.levelCount = p_mipmaps; vkCmdClearColorImage(command_buffer, src_tex->image, layout, &clear_color, 1, &range); @@ -2729,7 +2899,7 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color, VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; image_memory_barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; @@ -2739,12 +2909,12 @@ Error RenderingDeviceVulkan::texture_clear(RID p_texture, const Color &p_color, image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; image_memory_barrier.image = src_tex->image; image_memory_barrier.subresourceRange.aspectMask = src_tex->read_aspect_mask; - image_memory_barrier.subresourceRange.baseMipLevel = p_base_mipmap; + image_memory_barrier.subresourceRange.baseMipLevel = src_tex->base_mipmap + p_base_mipmap; image_memory_barrier.subresourceRange.levelCount = p_mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = p_base_layer; + image_memory_barrier.subresourceRange.baseArrayLayer = src_tex->base_layer + p_base_layer; image_memory_barrier.subresourceRange.layerCount = p_layers; - vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(command_buffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); } return OK; @@ -2839,6 +3009,21 @@ VkRenderPass RenderingDeviceVulkan::_render_pass_create(const Vector<AttachmentF description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //don't care what is there } } break; + case INITIAL_ACTION_DROP: { + if (p_format[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) { + description.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + description.initialLayout = is_sampled ? VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL : (is_storage ? VK_IMAGE_LAYOUT_GENERAL : VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); + description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + } else if (p_format[i].usage_flags & TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) { + description.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //don't care what is there + description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + } else { + description.loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + description.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + description.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; //don't care what is there + } + } break; case INITIAL_ACTION_CONTINUE: { if (p_format[i].usage_flags & TEXTURE_USAGE_COLOR_ATTACHMENT_BIT) { description.loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; @@ -2944,27 +3129,27 @@ VkRenderPass RenderingDeviceVulkan::_render_pass_create(const Vector<AttachmentF subpass.flags = 0; subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS; subpass.inputAttachmentCount = 0; //unsupported for now - subpass.pInputAttachments = NULL; + subpass.pInputAttachments = nullptr; subpass.colorAttachmentCount = color_references.size(); subpass.pColorAttachments = color_references.ptr(); subpass.pDepthStencilAttachment = depth_stencil_references.ptr(); subpass.pResolveAttachments = resolve_references.ptr(); subpass.preserveAttachmentCount = 0; - subpass.pPreserveAttachments = NULL; + subpass.pPreserveAttachments = nullptr; VkRenderPassCreateInfo render_pass_create_info; render_pass_create_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO; - render_pass_create_info.pNext = NULL; + render_pass_create_info.pNext = nullptr; render_pass_create_info.flags = 0; render_pass_create_info.attachmentCount = attachments.size(); render_pass_create_info.pAttachments = attachments.ptr(); render_pass_create_info.subpassCount = 1; render_pass_create_info.pSubpasses = &subpass; render_pass_create_info.dependencyCount = 0; - render_pass_create_info.pDependencies = NULL; + render_pass_create_info.pDependencies = nullptr; VkRenderPass render_pass; - VkResult res = vkCreateRenderPass(device, &render_pass_create_info, NULL, &render_pass); + VkResult res = vkCreateRenderPass(device, &render_pass_create_info, nullptr, &render_pass); ERR_FAIL_COND_V_MSG(res, VK_NULL_HANDLE, "vkCreateRenderPass failed with error " + itos(res) + "."); if (r_color_attachment_count) { @@ -3083,7 +3268,7 @@ RID RenderingDeviceVulkan::sampler_create(const SamplerState &p_state) { VkSamplerCreateInfo sampler_create_info; sampler_create_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO; - sampler_create_info.pNext = NULL; + sampler_create_info.pNext = nullptr; sampler_create_info.flags = 0; sampler_create_info.magFilter = p_state.mag_filter == SAMPLER_FILTER_LINEAR ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; sampler_create_info.minFilter = p_state.min_filter == SAMPLER_FILTER_LINEAR ? VK_FILTER_LINEAR : VK_FILTER_NEAREST; @@ -3113,7 +3298,7 @@ RID RenderingDeviceVulkan::sampler_create(const SamplerState &p_state) { sampler_create_info.unnormalizedCoordinates = p_state.unnormalized_uvw; VkSampler sampler; - VkResult res = vkCreateSampler(device, &sampler_create_info, NULL, &sampler); + VkResult res = vkCreateSampler(device, &sampler_create_info, nullptr, &sampler); ERR_FAIL_COND_V_MSG(res, RID(), "vkCreateSampler failed with error " + itos(res) + "."); return sampler_owner.make_rid(sampler); @@ -3142,7 +3327,7 @@ RID RenderingDeviceVulkan::vertex_buffer_create(uint32_t p_size_bytes, const Vec } // Internally reference counted, this ID is warranted to be unique for the same description, but needs to be freed as many times as it was allocated -RenderingDevice::VertexFormatID RenderingDeviceVulkan::vertex_format_create(const Vector<VertexDescription> &p_vertex_formats) { +RenderingDevice::VertexFormatID RenderingDeviceVulkan::vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats) { _THREAD_SAFE_METHOD_ @@ -3178,7 +3363,7 @@ RenderingDevice::VertexFormatID RenderingDeviceVulkan::vertex_format_create(cons } vdcache.create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - vdcache.create_info.pNext = NULL; + vdcache.create_info.pNext = nullptr; vdcache.create_info.flags = 0; vdcache.create_info.vertexAttributeDescriptionCount = p_vertex_formats.size(); @@ -3217,7 +3402,7 @@ RID RenderingDeviceVulkan::vertex_array_create(uint32_t p_vertex_count, VertexFo //validate with buffer { - const VertexDescription &atf = vd.vertex_formats[i]; + const VertexAttribute &atf = vd.vertex_formats[i]; uint32_t element_size = get_format_vertex_size(atf.format); ERR_FAIL_COND_V(element_size == 0, RID()); //should never happens since this was prevalidated @@ -3546,7 +3731,7 @@ bool RenderingDeviceVulkan::_uniform_add_binding(Vector<Vector<VkDescriptorSetLa } layout_binding.binding = binding; layout_binding.stageFlags = shader_stage_masks[p_stage]; - layout_binding.pImmutableSamplers = NULL; //no support for this yet + layout_binding.pImmutableSamplers = nullptr; //no support for this yet info.stages = 1 << p_stage; info.binding = binding; @@ -3600,7 +3785,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages "Reflection of SPIR-V shader stage '" + String(shader_stage_names[p_stages[i].shader_stage]) + "' failed parsing shader."); uint32_t binding_count = 0; - result = spvReflectEnumerateDescriptorBindings(&module, &binding_count, NULL); + result = spvReflectEnumerateDescriptorBindings(&module, &binding_count, nullptr); ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, RID(), "Reflection of SPIR-V shader stage '" + String(shader_stage_names[p_stages[i].shader_stage]) + "' failed enumerating descriptor bindings."); @@ -3742,7 +3927,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages layout_binding.binding = info.binding; layout_binding.stageFlags = shader_stage_masks[stage]; - layout_binding.pImmutableSamplers = NULL; //no support for this yet + layout_binding.pImmutableSamplers = nullptr; //no support for this yet info.stages = 1 << stage; info.binding = info.binding; @@ -3760,7 +3945,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages if (stage == SHADER_STAGE_VERTEX) { uint32_t iv_count = 0; - result = spvReflectEnumerateInputVariables(&module, &iv_count, NULL); + result = spvReflectEnumerateInputVariables(&module, &iv_count, nullptr); ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, RID(), "Reflection of SPIR-V shader stage '" + String(shader_stage_names[p_stages[i].shader_stage]) + "' failed enumerating input variables."); @@ -3783,7 +3968,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages if (stage == SHADER_STAGE_FRAGMENT) { uint32_t ov_count = 0; - result = spvReflectEnumerateOutputVariables(&module, &ov_count, NULL); + result = spvReflectEnumerateOutputVariables(&module, &ov_count, nullptr); ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, RID(), "Reflection of SPIR-V shader stage '" + String(shader_stage_names[p_stages[i].shader_stage]) + "' failed enumerating output variables."); @@ -3803,7 +3988,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages } } uint32_t pc_count = 0; - result = spvReflectEnumeratePushConstantBlocks(&module, &pc_count, NULL); + result = spvReflectEnumeratePushConstantBlocks(&module, &pc_count, nullptr); ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, RID(), "Reflection of SPIR-V shader stage '" + String(shader_stage_names[p_stages[i].shader_stage]) + "' failed enumerating push constants."); @@ -3817,7 +4002,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, RID(), "Reflection of SPIR-V shader stage '" + String(shader_stage_names[p_stages[i].shader_stage]) + "' failed obtaining push constants."); #if 0 - if (pconstants[0] == NULL) { + if (pconstants[0] == nullptr) { FileAccess *f = FileAccess::open("res://popo.spv", FileAccess::WRITE); f->store_buffer((const uint8_t *)&SpirV[0], SpirV.size() * sizeof(uint32_t)); memdelete(f); @@ -3857,7 +4042,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages for (int i = 0; i < p_stages.size(); i++) { VkShaderModuleCreateInfo shader_module_create_info; shader_module_create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; - shader_module_create_info.pNext = NULL; + shader_module_create_info.pNext = nullptr; shader_module_create_info.flags = 0; shader_module_create_info.codeSize = p_stages[i].spir_v.size(); const uint8_t *r = p_stages[i].spir_v.ptr(); @@ -3865,7 +4050,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages shader_module_create_info.pCode = (const uint32_t *)r; VkShaderModule module; - VkResult res = vkCreateShaderModule(device, &shader_module_create_info, NULL, &module); + VkResult res = vkCreateShaderModule(device, &shader_module_create_info, nullptr, &module); if (res) { success = false; error_text = "Error (" + itos(res) + ") creating shader module for stage: " + String(shader_stage_names[p_stages[i].shader_stage]); @@ -3882,12 +4067,12 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages VkPipelineShaderStageCreateInfo shader_stage; shader_stage.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO; - shader_stage.pNext = NULL; + shader_stage.pNext = nullptr; shader_stage.flags = 0; shader_stage.stage = shader_stage_bits[p_stages[i].shader_stage]; shader_stage.module = module; shader_stage.pName = "main"; - shader_stage.pSpecializationInfo = NULL; + shader_stage.pSpecializationInfo = nullptr; shader.pipeline_stages.push_back(shader_stage); } @@ -3900,13 +4085,13 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages //empty ones are fine if they were not used according to spec (binding count will be 0) VkDescriptorSetLayoutCreateInfo layout_create_info; layout_create_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO; - layout_create_info.pNext = NULL; + layout_create_info.pNext = nullptr; layout_create_info.flags = 0; layout_create_info.bindingCount = set_bindings[i].size(); layout_create_info.pBindings = set_bindings[i].ptr(); VkDescriptorSetLayout layout; - VkResult res = vkCreateDescriptorSetLayout(device, &layout_create_info, NULL, &layout); + VkResult res = vkCreateDescriptorSetLayout(device, &layout_create_info, nullptr, &layout); if (res) { error_text = "Error (" + itos(res) + ") creating descriptor set layout for set " + itos(i); success = false; @@ -3943,7 +4128,7 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages //create pipeline layout VkPipelineLayoutCreateInfo pipeline_layout_create_info; pipeline_layout_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; - pipeline_layout_create_info.pNext = NULL; + pipeline_layout_create_info.pNext = nullptr; pipeline_layout_create_info.flags = 0; pipeline_layout_create_info.setLayoutCount = shader.sets.size(); @@ -3965,10 +4150,10 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages pipeline_layout_create_info.pPushConstantRanges = &push_constant_range; } else { pipeline_layout_create_info.pushConstantRangeCount = 0; - pipeline_layout_create_info.pPushConstantRanges = NULL; + pipeline_layout_create_info.pPushConstantRanges = nullptr; } - VkResult err = vkCreatePipelineLayout(device, &pipeline_layout_create_info, NULL, &shader.pipeline_layout); + VkResult err = vkCreatePipelineLayout(device, &pipeline_layout_create_info, nullptr, &shader.pipeline_layout); if (err) { error_text = "Error (" + itos(err) + ") creating pipeline layout."; @@ -3979,11 +4164,11 @@ RID RenderingDeviceVulkan::shader_create(const Vector<ShaderStageData> &p_stages if (!success) { //clean up if failed for (int i = 0; i < shader.pipeline_stages.size(); i++) { - vkDestroyShaderModule(device, shader.pipeline_stages[i].module, NULL); + vkDestroyShaderModule(device, shader.pipeline_stages[i].module, nullptr); } for (int i = 0; i < shader.sets.size(); i++) { - vkDestroyDescriptorSetLayout(device, shader.sets[i].descriptor_set_layout, NULL); + vkDestroyDescriptorSetLayout(device, shader.sets[i].descriptor_set_layout, nullptr); } ERR_FAIL_V_MSG(RID(), error_text); @@ -4064,7 +4249,7 @@ RID RenderingDeviceVulkan::texture_buffer_create(uint32_t p_size_elements, DataF VkBufferViewCreateInfo view_create_info; view_create_info.sType = VK_STRUCTURE_TYPE_BUFFER_VIEW_CREATE_INFO; - view_create_info.pNext = NULL; + view_create_info.pNext = nullptr; view_create_info.flags = 0; view_create_info.buffer = texture_buffer.buffer.buffer; view_create_info.format = vulkan_formats[p_format]; @@ -4073,7 +4258,7 @@ RID RenderingDeviceVulkan::texture_buffer_create(uint32_t p_size_elements, DataF texture_buffer.view = VK_NULL_HANDLE; - VkResult res = vkCreateBufferView(device, &view_create_info, NULL, &texture_buffer.view); + VkResult res = vkCreateBufferView(device, &view_create_info, nullptr, &texture_buffer.view); if (res) { _buffer_free(&texture_buffer.buffer); ERR_FAIL_V_MSG(RID(), "Unable to create buffer view, error " + itos(res) + "."); @@ -4088,7 +4273,7 @@ RenderingDeviceVulkan::DescriptorPool *RenderingDeviceVulkan::_descriptor_pool_a descriptor_pools[p_key] = Set<DescriptorPool *>(); } - DescriptorPool *pool = NULL; + DescriptorPool *pool = nullptr; for (Set<DescriptorPool *>::Element *E = descriptor_pools[p_key].front(); E; E = E->next()) { if (E->get()->usage < max_descriptors_per_pool) { @@ -4104,7 +4289,7 @@ RenderingDeviceVulkan::DescriptorPool *RenderingDeviceVulkan::_descriptor_pool_a VkDescriptorPoolCreateInfo descriptor_pool_create_info; descriptor_pool_create_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO; - descriptor_pool_create_info.pNext = NULL; + descriptor_pool_create_info.pNext = nullptr; descriptor_pool_create_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT; // can't think how somebody may NOT need this flag.. descriptor_pool_create_info.maxSets = max_descriptors_per_pool; Vector<VkDescriptorPoolSize> sizes; @@ -4169,10 +4354,10 @@ RenderingDeviceVulkan::DescriptorPool *RenderingDeviceVulkan::_descriptor_pool_a descriptor_pool_create_info.poolSizeCount = sizes.size(); descriptor_pool_create_info.pPoolSizes = sizes.ptr(); - VkResult res = vkCreateDescriptorPool(device, &descriptor_pool_create_info, NULL, &pool->pool); + VkResult res = vkCreateDescriptorPool(device, &descriptor_pool_create_info, nullptr, &pool->pool); if (res) { memdelete(pool); - ERR_FAIL_COND_V_MSG(res, NULL, "vkCreateDescriptorPool failed with error " + itos(res) + "."); + ERR_FAIL_COND_V_MSG(res, nullptr, "vkCreateDescriptorPool failed with error " + itos(res) + "."); } descriptor_pools[p_key].insert(pool); } @@ -4189,7 +4374,7 @@ void RenderingDeviceVulkan::_descriptor_pool_free(const DescriptorPoolKey &p_key ERR_FAIL_COND(p_pool->usage == 0); p_pool->usage--; if (p_pool->usage == 0) { - vkDestroyDescriptorPool(device, p_pool->pool, NULL); + vkDestroyDescriptorPool(device, p_pool->pool, nullptr); descriptor_pools[p_key].erase(p_pool); memdelete(p_pool); if (descriptor_pools[p_key].empty()) { @@ -4249,8 +4434,8 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, VkWriteDescriptorSet write; //common header write.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET; - write.pNext = NULL; - write.dstSet = NULL; //will assign afterwards when everything is valid + write.pNext = nullptr; + write.dstSet = VK_NULL_HANDLE; //will assign afterwards when everything is valid write.dstBinding = set_uniform.binding; uint32_t type_size = 1; @@ -4282,8 +4467,8 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, write.descriptorCount = uniform.ids.size(); write.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLER; write.pImageInfo = image_infos.push_back(image_info)->get().ptr(); - write.pBufferInfo = NULL; - write.pTexelBufferView = NULL; + write.pBufferInfo = nullptr; + write.pTexelBufferView = nullptr; type_size = uniform.ids.size(); @@ -4318,6 +4503,10 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, attachable_textures.push_back(texture->owner.is_valid() ? texture->owner : uniform.ids[j + 1]); } + if (texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT) { + //can also be used as storage, add to mutable sampled + mutable_sampled_textures.push_back(texture); + } if (texture->owner.is_valid()) { texture = texture_owner.getornull(texture->owner); ERR_FAIL_COND_V(!texture, RID()); //bug, should never happen @@ -4326,19 +4515,14 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, img_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; image_info.push_back(img_info); - - if (texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT) { - //can also be used as storage, add to mutable sampled - mutable_sampled_textures.push_back(texture); - } } write.dstArrayElement = 0; write.descriptorCount = uniform.ids.size() / 2; write.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER; write.pImageInfo = image_infos.push_back(image_info)->get().ptr(); - write.pBufferInfo = NULL; - write.pTexelBufferView = NULL; + write.pBufferInfo = nullptr; + write.pTexelBufferView = nullptr; type_size = uniform.ids.size() / 2; @@ -4363,13 +4547,18 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, "Texture (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_SAMPLING_BIT usage flag set in order to be used as uniform."); VkDescriptorImageInfo img_info; - img_info.sampler = NULL; + img_info.sampler = VK_NULL_HANDLE; img_info.imageView = texture->view; if (texture->usage_flags & (TEXTURE_USAGE_COLOR_ATTACHMENT_BIT | TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | TEXTURE_USAGE_RESOLVE_ATTACHMENT_BIT)) { attachable_textures.push_back(texture->owner.is_valid() ? texture->owner : uniform.ids[j]); } + if (texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT) { + //can also be used as storage, add to mutable sampled + mutable_sampled_textures.push_back(texture); + } + if (texture->owner.is_valid()) { texture = texture_owner.getornull(texture->owner); ERR_FAIL_COND_V(!texture, RID()); //bug, should never happen @@ -4378,19 +4567,14 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, img_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; image_info.push_back(img_info); - - if (texture->usage_flags & TEXTURE_USAGE_STORAGE_BIT) { - //can also be used as storage, add to mutable sampled - mutable_sampled_textures.push_back(texture); - } } write.dstArrayElement = 0; write.descriptorCount = uniform.ids.size(); write.descriptorType = VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE; write.pImageInfo = image_infos.push_back(image_info)->get().ptr(); - write.pBufferInfo = NULL; - write.pTexelBufferView = NULL; + write.pBufferInfo = nullptr; + write.pTexelBufferView = nullptr; type_size = uniform.ids.size(); } break; @@ -4416,9 +4600,14 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, "Image (binding: " + itos(uniform.binding) + ", index " + itos(j) + ") needs the TEXTURE_USAGE_STORAGE_BIT usage flag set in order to be used as uniform."); VkDescriptorImageInfo img_info; - img_info.sampler = NULL; + img_info.sampler = VK_NULL_HANDLE; img_info.imageView = texture->view; + if (texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT) { + //can also be used as storage, add to mutable sampled + mutable_storage_textures.push_back(texture); + } + if (texture->owner.is_valid()) { texture = texture_owner.getornull(texture->owner); ERR_FAIL_COND_V(!texture, RID()); //bug, should never happen @@ -4427,19 +4616,14 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, img_info.imageLayout = VK_IMAGE_LAYOUT_GENERAL; image_info.push_back(img_info); - - if (texture->usage_flags & TEXTURE_USAGE_SAMPLING_BIT) { - //can also be used as storage, add to mutable sampled - mutable_storage_textures.push_back(texture); - } } write.dstArrayElement = 0; write.descriptorCount = uniform.ids.size(); write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE; write.pImageInfo = image_infos.push_back(image_info)->get().ptr(); - write.pBufferInfo = NULL; - write.pTexelBufferView = NULL; + write.pBufferInfo = nullptr; + write.pTexelBufferView = nullptr; type_size = uniform.ids.size(); @@ -4467,7 +4651,7 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, write.dstArrayElement = 0; write.descriptorCount = uniform.ids.size(); write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER; - write.pImageInfo = NULL; + write.pImageInfo = nullptr; write.pBufferInfo = buffer_infos.push_back(buffer_info)->get().ptr(); write.pTexelBufferView = buffer_views.push_back(buffer_view)->get().ptr(); @@ -4533,9 +4717,9 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, write.dstArrayElement = 0; write.descriptorCount = 1; write.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER; - write.pImageInfo = NULL; + write.pImageInfo = nullptr; write.pBufferInfo = &buffer->buffer_info; - write.pTexelBufferView = NULL; + write.pTexelBufferView = nullptr; } break; case UNIFORM_TYPE_STORAGE_BUFFER: { @@ -4552,9 +4736,9 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, write.dstArrayElement = 0; write.descriptorCount = 1; write.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER; - write.pImageInfo = NULL; + write.pImageInfo = nullptr; write.pBufferInfo = &buffer->buffer_info; - write.pTexelBufferView = NULL; + write.pTexelBufferView = nullptr; } break; case UNIFORM_TYPE_INPUT_ATTACHMENT: { @@ -4578,7 +4762,7 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, VkDescriptorSetAllocateInfo descriptor_set_allocate_info; descriptor_set_allocate_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; - descriptor_set_allocate_info.pNext = NULL; + descriptor_set_allocate_info.pNext = nullptr; descriptor_set_allocate_info.descriptorPool = pool->pool; descriptor_set_allocate_info.descriptorSetCount = 1; descriptor_set_allocate_info.pSetLayouts = &shader->sets[p_shader_set].descriptor_set_layout; @@ -4619,7 +4803,7 @@ RID RenderingDeviceVulkan::uniform_set_create(const Vector<Uniform> &p_uniforms, for (int i = 0; i < writes.size(); i++) { writes.write[i].dstSet = descriptor_set; } - vkUpdateDescriptorSets(device, writes.size(), writes.ptr(), 0, NULL); + vkUpdateDescriptorSets(device, writes.size(), writes.ptr(), 0, nullptr); } return id; @@ -4638,7 +4822,7 @@ Error RenderingDeviceVulkan::buffer_update(RID p_buffer, uint32_t p_offset, uint VkPipelineStageFlags dst_stage_mask; VkAccessFlags dst_access; - Buffer *buffer = NULL; + Buffer *buffer = nullptr; if (vertex_buffer_owner.owns(p_buffer)) { dst_stage_mask = VK_PIPELINE_STAGE_VERTEX_INPUT_BIT; dst_access = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT; @@ -4684,7 +4868,7 @@ Vector<uint8_t> RenderingDeviceVulkan::buffer_get_data(RID p_buffer) { _THREAD_SAFE_METHOD_ - Buffer *buffer = NULL; + Buffer *buffer = nullptr; if (vertex_buffer_owner.owns(p_buffer)) { buffer = vertex_buffer_owner.getornull(p_buffer); } else if (index_buffer_owner.owns(p_buffer)) { @@ -4783,12 +4967,12 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma } else { //does not use vertices pipeline_vertex_input_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO; - pipeline_vertex_input_state_create_info.pNext = NULL; + pipeline_vertex_input_state_create_info.pNext = nullptr; pipeline_vertex_input_state_create_info.flags = 0; pipeline_vertex_input_state_create_info.vertexBindingDescriptionCount = 0; - pipeline_vertex_input_state_create_info.pVertexBindingDescriptions = NULL; + pipeline_vertex_input_state_create_info.pVertexBindingDescriptions = nullptr; pipeline_vertex_input_state_create_info.vertexAttributeDescriptionCount = 0; - pipeline_vertex_input_state_create_info.pVertexAttributeDescriptions = NULL; + pipeline_vertex_input_state_create_info.pVertexAttributeDescriptions = nullptr; ERR_FAIL_COND_V_MSG(shader->vertex_input_mask != 0, RID(), "Shader contains vertex inputs, but no vertex input description was provided for pipeline creation."); @@ -4799,7 +4983,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma VkPipelineInputAssemblyStateCreateInfo input_assembly_create_info; input_assembly_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; - input_assembly_create_info.pNext = NULL; + input_assembly_create_info.pNext = nullptr; input_assembly_create_info.flags = 0; static const VkPrimitiveTopology topology_list[RENDER_PRIMITIVE_MAX] = { @@ -4822,24 +5006,24 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma //tesselation VkPipelineTessellationStateCreateInfo tesselation_create_info; tesselation_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_TESSELLATION_STATE_CREATE_INFO; - tesselation_create_info.pNext = NULL; + tesselation_create_info.pNext = nullptr; tesselation_create_info.flags = 0; ERR_FAIL_COND_V(p_rasterization_state.patch_control_points < 1 || p_rasterization_state.patch_control_points > limits.maxTessellationPatchSize, RID()); tesselation_create_info.patchControlPoints = p_rasterization_state.patch_control_points; VkPipelineViewportStateCreateInfo viewport_state_create_info; viewport_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO; - viewport_state_create_info.pNext = NULL; + viewport_state_create_info.pNext = nullptr; viewport_state_create_info.flags = 0; viewport_state_create_info.viewportCount = 1; //if VR extensions are supported at some point, this will have to be customizable in the framebuffer format - viewport_state_create_info.pViewports = NULL; + viewport_state_create_info.pViewports = nullptr; viewport_state_create_info.scissorCount = 1; - viewport_state_create_info.pScissors = NULL; + viewport_state_create_info.pScissors = nullptr; //rasterization VkPipelineRasterizationStateCreateInfo rasterization_state_create_info; rasterization_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO; - rasterization_state_create_info.pNext = NULL; + rasterization_state_create_info.pNext = nullptr; rasterization_state_create_info.flags = 0; rasterization_state_create_info.depthClampEnable = p_rasterization_state.enable_depth_clamp; rasterization_state_create_info.rasterizerDiscardEnable = p_rasterization_state.discard_primitives; @@ -4862,7 +5046,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma //multisample VkPipelineMultisampleStateCreateInfo multisample_state_create_info; multisample_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO; - multisample_state_create_info.pNext = NULL; + multisample_state_create_info.pNext = nullptr; multisample_state_create_info.flags = 0; multisample_state_create_info.rasterizationSamples = rasterization_sample_count[p_multisample_state.sample_count]; @@ -4882,7 +5066,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma } multisample_state_create_info.pSampleMask = sample_mask.ptr(); } else { - multisample_state_create_info.pSampleMask = NULL; + multisample_state_create_info.pSampleMask = nullptr; } multisample_state_create_info.alphaToCoverageEnable = p_multisample_state.enable_alpha_to_coverage; @@ -4892,7 +5076,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma VkPipelineDepthStencilStateCreateInfo depth_stencil_state_create_info; depth_stencil_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO; - depth_stencil_state_create_info.pNext = NULL; + depth_stencil_state_create_info.pNext = nullptr; depth_stencil_state_create_info.flags = 0; depth_stencil_state_create_info.depthTestEnable = p_depth_stencil_state.enable_depth_test; depth_stencil_state_create_info.depthWriteEnable = p_depth_stencil_state.enable_depth_write; @@ -4901,29 +5085,29 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma depth_stencil_state_create_info.depthBoundsTestEnable = p_depth_stencil_state.enable_depth_range; depth_stencil_state_create_info.stencilTestEnable = p_depth_stencil_state.enable_stencil; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_front.fail, STENCIL_OP_MAX, RID()); - depth_stencil_state_create_info.front.failOp = stencil_operations[p_depth_stencil_state.stencil_operation_front.fail]; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_front.pass, STENCIL_OP_MAX, RID()); - depth_stencil_state_create_info.front.passOp = stencil_operations[p_depth_stencil_state.stencil_operation_front.pass]; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_front.depth_fail, STENCIL_OP_MAX, RID()); - depth_stencil_state_create_info.front.depthFailOp = stencil_operations[p_depth_stencil_state.stencil_operation_front.depth_fail]; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_front.compare, COMPARE_OP_MAX, RID()); - depth_stencil_state_create_info.front.compareOp = compare_operators[p_depth_stencil_state.stencil_operation_front.compare]; - depth_stencil_state_create_info.front.compareMask = p_depth_stencil_state.stencil_operation_front.compare_mask; - depth_stencil_state_create_info.front.writeMask = p_depth_stencil_state.stencil_operation_front.write_mask; - depth_stencil_state_create_info.front.reference = p_depth_stencil_state.stencil_operation_front.reference; - - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_back.fail, STENCIL_OP_MAX, RID()); - depth_stencil_state_create_info.back.failOp = stencil_operations[p_depth_stencil_state.stencil_operation_back.fail]; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_back.pass, STENCIL_OP_MAX, RID()); - depth_stencil_state_create_info.back.passOp = stencil_operations[p_depth_stencil_state.stencil_operation_back.pass]; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_back.depth_fail, STENCIL_OP_MAX, RID()); - depth_stencil_state_create_info.back.depthFailOp = stencil_operations[p_depth_stencil_state.stencil_operation_back.depth_fail]; - ERR_FAIL_INDEX_V(p_depth_stencil_state.stencil_operation_back.compare, COMPARE_OP_MAX, RID()); - depth_stencil_state_create_info.back.compareOp = compare_operators[p_depth_stencil_state.stencil_operation_back.compare]; - depth_stencil_state_create_info.back.compareMask = p_depth_stencil_state.stencil_operation_back.compare_mask; - depth_stencil_state_create_info.back.writeMask = p_depth_stencil_state.stencil_operation_back.write_mask; - depth_stencil_state_create_info.back.reference = p_depth_stencil_state.stencil_operation_back.reference; + ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.fail, STENCIL_OP_MAX, RID()); + depth_stencil_state_create_info.front.failOp = stencil_operations[p_depth_stencil_state.front_op.fail]; + ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.pass, STENCIL_OP_MAX, RID()); + depth_stencil_state_create_info.front.passOp = stencil_operations[p_depth_stencil_state.front_op.pass]; + ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.depth_fail, STENCIL_OP_MAX, RID()); + depth_stencil_state_create_info.front.depthFailOp = stencil_operations[p_depth_stencil_state.front_op.depth_fail]; + ERR_FAIL_INDEX_V(p_depth_stencil_state.front_op.compare, COMPARE_OP_MAX, RID()); + depth_stencil_state_create_info.front.compareOp = compare_operators[p_depth_stencil_state.front_op.compare]; + depth_stencil_state_create_info.front.compareMask = p_depth_stencil_state.front_op.compare_mask; + depth_stencil_state_create_info.front.writeMask = p_depth_stencil_state.front_op.write_mask; + depth_stencil_state_create_info.front.reference = p_depth_stencil_state.front_op.reference; + + ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.fail, STENCIL_OP_MAX, RID()); + depth_stencil_state_create_info.back.failOp = stencil_operations[p_depth_stencil_state.back_op.fail]; + ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.pass, STENCIL_OP_MAX, RID()); + depth_stencil_state_create_info.back.passOp = stencil_operations[p_depth_stencil_state.back_op.pass]; + ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.depth_fail, STENCIL_OP_MAX, RID()); + depth_stencil_state_create_info.back.depthFailOp = stencil_operations[p_depth_stencil_state.back_op.depth_fail]; + ERR_FAIL_INDEX_V(p_depth_stencil_state.back_op.compare, COMPARE_OP_MAX, RID()); + depth_stencil_state_create_info.back.compareOp = compare_operators[p_depth_stencil_state.back_op.compare]; + depth_stencil_state_create_info.back.compareMask = p_depth_stencil_state.back_op.compare_mask; + depth_stencil_state_create_info.back.writeMask = p_depth_stencil_state.back_op.write_mask; + depth_stencil_state_create_info.back.reference = p_depth_stencil_state.back_op.reference; depth_stencil_state_create_info.minDepthBounds = p_depth_stencil_state.depth_range_min; depth_stencil_state_create_info.maxDepthBounds = p_depth_stencil_state.depth_range_max; @@ -4931,7 +5115,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma //blend state VkPipelineColorBlendStateCreateInfo color_blend_state_create_info; color_blend_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO; - color_blend_state_create_info.pNext = NULL; + color_blend_state_create_info.pNext = nullptr; color_blend_state_create_info.flags = 0; color_blend_state_create_info.logicOpEnable = p_blend_state.enable_logic_op; ERR_FAIL_INDEX_V(p_blend_state.logic_op, LOGIC_OP_MAX, RID()); @@ -4988,7 +5172,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma VkPipelineDynamicStateCreateInfo dynamic_state_create_info; dynamic_state_create_info.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO; - dynamic_state_create_info.pNext = NULL; + dynamic_state_create_info.pNext = nullptr; dynamic_state_create_info.flags = 0; Vector<VkDynamicState> dynamic_states; //vulkan is weird.. @@ -5030,7 +5214,7 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma VkGraphicsPipelineCreateInfo graphics_pipeline_create_info; graphics_pipeline_create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; - graphics_pipeline_create_info.pNext = NULL; + graphics_pipeline_create_info.pNext = nullptr; graphics_pipeline_create_info.flags = 0; graphics_pipeline_create_info.stageCount = shader->pipeline_stages.size(); @@ -5048,11 +5232,11 @@ RID RenderingDeviceVulkan::render_pipeline_create(RID p_shader, FramebufferForma graphics_pipeline_create_info.renderPass = fb_format.render_pass; graphics_pipeline_create_info.subpass = 0; - graphics_pipeline_create_info.basePipelineHandle = NULL; + graphics_pipeline_create_info.basePipelineHandle = VK_NULL_HANDLE; graphics_pipeline_create_info.basePipelineIndex = 0; RenderPipeline pipeline; - VkResult err = vkCreateGraphicsPipelines(device, NULL, 1, &graphics_pipeline_create_info, NULL, &pipeline.pipeline); + VkResult err = vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &graphics_pipeline_create_info, nullptr, &pipeline.pipeline); ERR_FAIL_COND_V_MSG(err, RID(), "vkCreateGraphicsPipelines failed with error " + itos(err) + "."); pipeline.set_formats = shader->set_formats; @@ -5116,16 +5300,16 @@ RID RenderingDeviceVulkan::compute_pipeline_create(RID p_shader) { VkComputePipelineCreateInfo compute_pipeline_create_info; compute_pipeline_create_info.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO; - compute_pipeline_create_info.pNext = NULL; + compute_pipeline_create_info.pNext = nullptr; compute_pipeline_create_info.flags = 0; compute_pipeline_create_info.stage = shader->pipeline_stages[0]; compute_pipeline_create_info.layout = shader->pipeline_layout; - compute_pipeline_create_info.basePipelineHandle = NULL; + compute_pipeline_create_info.basePipelineHandle = VK_NULL_HANDLE; compute_pipeline_create_info.basePipelineIndex = 0; ComputePipeline pipeline; - VkResult err = vkCreateComputePipelines(device, NULL, 1, &compute_pipeline_create_info, NULL, &pipeline.pipeline); + VkResult err = vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &compute_pipeline_create_info, nullptr, &pipeline.pipeline); ERR_FAIL_COND_V_MSG(err, RID(), "vkCreateComputePipelines failed with error " + itos(err) + "."); pipeline.set_formats = shader->set_formats; @@ -5152,17 +5336,19 @@ bool RenderingDeviceVulkan::compute_pipeline_is_valid(RID p_pipeline) { int RenderingDeviceVulkan::screen_get_width(DisplayServer::WindowID p_screen) const { _THREAD_SAFE_METHOD_ - + ERR_FAIL_COND_V_MSG(local_device.is_valid(), -1, "Local devices have no screen"); return context->window_get_width(p_screen); } int RenderingDeviceVulkan::screen_get_height(DisplayServer::WindowID p_screen) const { _THREAD_SAFE_METHOD_ + ERR_FAIL_COND_V_MSG(local_device.is_valid(), -1, "Local devices have no screen"); return context->window_get_height(p_screen); } RenderingDevice::FramebufferFormatID RenderingDeviceVulkan::screen_get_framebuffer_format() const { _THREAD_SAFE_METHOD_ + ERR_FAIL_COND_V_MSG(local_device.is_valid(), INVALID_ID, "Local devices have no screen"); //very hacky, but not used often per frame so I guess ok VkFormat vkformat = context->get_screen_format(); @@ -5192,9 +5378,10 @@ RenderingDevice::FramebufferFormatID RenderingDeviceVulkan::screen_get_framebuff RenderingDevice::DrawListID RenderingDeviceVulkan::draw_list_begin_for_screen(DisplayServer::WindowID p_screen, const Color &p_clear_color) { _THREAD_SAFE_METHOD_ + ERR_FAIL_COND_V_MSG(local_device.is_valid(), INVALID_ID, "Local devices have no screen"); - ERR_FAIL_COND_V_MSG(draw_list != NULL, INVALID_ID, "Only one draw list can be active at the same time."); - ERR_FAIL_COND_V_MSG(compute_list != NULL, INVALID_ID, "Only one draw/compute list can be active at the same time."); + ERR_FAIL_COND_V_MSG(draw_list != nullptr, INVALID_ID, "Only one draw list can be active at the same time."); + ERR_FAIL_COND_V_MSG(compute_list != nullptr, INVALID_ID, "Only one draw/compute list can be active at the same time."); VkCommandBuffer command_buffer = frames[frame].draw_command_buffer; draw_list = memnew(DrawList); @@ -5207,7 +5394,7 @@ RenderingDevice::DrawListID RenderingDeviceVulkan::draw_list_begin_for_screen(Di VkRenderPassBeginInfo render_pass_begin; render_pass_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - render_pass_begin.pNext = NULL; + render_pass_begin.pNext = nullptr; render_pass_begin.renderPass = context->window_get_render_pass(p_screen); render_pass_begin.framebuffer = context->window_get_framebuffer(p_screen); @@ -5268,7 +5455,7 @@ Error RenderingDeviceVulkan::_draw_list_setup_framebuffer(Framebuffer *p_framebu VkFramebufferCreateInfo framebuffer_create_info; framebuffer_create_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; - framebuffer_create_info.pNext = NULL; + framebuffer_create_info.pNext = nullptr; framebuffer_create_info.flags = 0; framebuffer_create_info.renderPass = version.render_pass; Vector<VkImageView> attachments; @@ -5285,7 +5472,7 @@ Error RenderingDeviceVulkan::_draw_list_setup_framebuffer(Framebuffer *p_framebu framebuffer_create_info.height = p_framebuffer->size.height; framebuffer_create_info.layers = 1; - VkResult err = vkCreateFramebuffer(device, &framebuffer_create_info, NULL, &version.framebuffer); + VkResult err = vkCreateFramebuffer(device, &framebuffer_create_info, nullptr, &version.framebuffer); ERR_FAIL_COND_V_MSG(err, ERR_CANT_CREATE, "vkCreateFramebuffer failed with error " + itos(err) + "."); p_framebuffer->framebuffers.insert(vk, version); @@ -5301,7 +5488,7 @@ Error RenderingDeviceVulkan::_draw_list_render_pass_begin(Framebuffer *framebuff VkRenderPassBeginInfo render_pass_begin; render_pass_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - render_pass_begin.pNext = NULL; + render_pass_begin.pNext = nullptr; render_pass_begin.renderPass = render_pass; render_pass_begin.framebuffer = vkframebuffer; @@ -5404,8 +5591,8 @@ RenderingDevice::DrawListID RenderingDeviceVulkan::draw_list_begin(RID p_framebu _THREAD_SAFE_METHOD_ - ERR_FAIL_COND_V_MSG(draw_list != NULL, INVALID_ID, "Only one draw list can be active at the same time."); - ERR_FAIL_COND_V_MSG(compute_list != NULL, INVALID_ID, "Only one draw/compute list can be active at the same time."); + ERR_FAIL_COND_V_MSG(draw_list != nullptr, INVALID_ID, "Only one draw list can be active at the same time."); + ERR_FAIL_COND_V_MSG(compute_list != nullptr, INVALID_ID, "Only one draw/compute list can be active at the same time."); Framebuffer *framebuffer = framebuffer_owner.getornull(p_framebuffer); ERR_FAIL_COND_V(!framebuffer, INVALID_ID); @@ -5542,11 +5729,11 @@ Error RenderingDeviceVulkan::draw_list_begin_split(RID p_framebuffer, uint32_t p VkCommandPoolCreateInfo cmd_pool_info; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - cmd_pool_info.pNext = NULL; + cmd_pool_info.pNext = nullptr; cmd_pool_info.queueFamilyIndex = context->get_graphics_queue(); cmd_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - VkResult res = vkCreateCommandPool(device, &cmd_pool_info, NULL, &split_draw_list_allocators.write[i].command_pool); + VkResult res = vkCreateCommandPool(device, &cmd_pool_info, nullptr, &split_draw_list_allocators.write[i].command_pool); ERR_FAIL_COND_V_MSG(res, ERR_CANT_CREATE, "vkCreateCommandPool failed with error " + itos(res) + "."); for (int j = 0; j < frame_count; j++) { @@ -5556,7 +5743,7 @@ Error RenderingDeviceVulkan::draw_list_begin_split(RID p_framebuffer, uint32_t p VkCommandBufferAllocateInfo cmdbuf; //no command buffer exists, create it. cmdbuf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - cmdbuf.pNext = NULL; + cmdbuf.pNext = nullptr; cmdbuf.commandPool = split_draw_list_allocators[i].command_pool; cmdbuf.level = VK_COMMAND_BUFFER_LEVEL_SECONDARY; cmdbuf.commandBufferCount = 1; @@ -5593,7 +5780,7 @@ Error RenderingDeviceVulkan::draw_list_begin_split(RID p_framebuffer, uint32_t p VkCommandBufferInheritanceInfo inheritance_info; inheritance_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO; - inheritance_info.pNext = NULL; + inheritance_info.pNext = nullptr; inheritance_info.renderPass = render_pass; inheritance_info.subpass = 0; inheritance_info.framebuffer = vkframebuffer; @@ -5603,21 +5790,21 @@ Error RenderingDeviceVulkan::draw_list_begin_split(RID p_framebuffer, uint32_t p VkCommandBufferBeginInfo cmdbuf_begin; cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - cmdbuf_begin.pNext = NULL; + cmdbuf_begin.pNext = nullptr; cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT | VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT; cmdbuf_begin.pInheritanceInfo = &inheritance_info; VkResult res = vkResetCommandBuffer(command_buffer, 0); if (res) { memdelete_arr(draw_list); - draw_list = NULL; + draw_list = nullptr; ERR_FAIL_V_MSG(ERR_CANT_CREATE, "vkResetCommandBuffer failed with error " + itos(res) + "."); } res = vkBeginCommandBuffer(command_buffer, &cmdbuf_begin); if (res) { memdelete_arr(draw_list); - draw_list = NULL; + draw_list = nullptr; ERR_FAIL_V_MSG(ERR_CANT_CREATE, "vkBeginCommandBuffer failed with error " + itos(res) + "."); } @@ -5658,30 +5845,30 @@ Error RenderingDeviceVulkan::draw_list_begin_split(RID p_framebuffer, uint32_t p RenderingDeviceVulkan::DrawList *RenderingDeviceVulkan::_get_draw_list_ptr(DrawListID p_id) { if (p_id < 0) { - return NULL; + return nullptr; } if (!draw_list) { - return NULL; + return nullptr; } else if (p_id == ID_TYPE_DRAW_LIST) { if (draw_list_split) { - return NULL; + return nullptr; } return draw_list; } else if (p_id >> DrawListID(ID_BASE_SHIFT) == ID_TYPE_SPLIT_DRAW_LIST) { if (!draw_list_split) { - return NULL; + return nullptr; } uint64_t index = p_id & ((DrawListID(1) << DrawListID(ID_BASE_SHIFT)) - 1); //mask if (index >= draw_list_count) { - return NULL; + return nullptr; } return &draw_list[index]; } else { - return NULL; + return nullptr; } } @@ -5856,7 +6043,7 @@ void RenderingDeviceVulkan::draw_list_set_line_width(DrawListID p_list, float p_ vkCmdSetLineWidth(dl->command_buffer, p_width); } -void RenderingDeviceVulkan::draw_list_set_push_constant(DrawListID p_list, void *p_data, uint32_t p_data_size) { +void RenderingDeviceVulkan::draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size) { DrawList *dl = _get_draw_list_ptr(p_list); ERR_FAIL_COND(!dl); @@ -5927,7 +6114,7 @@ void RenderingDeviceVulkan::draw_list_draw(DrawListID p_list, bool p_use_indices #endif if (!dl->state.sets[i].bound) { //All good, see if this requires re-binding - vkCmdBindDescriptorSets(dl->command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, dl->state.pipeline_layout, i, 1, &dl->state.sets[i].descriptor_set, 0, NULL); + vkCmdBindDescriptorSets(dl->command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, dl->state.pipeline_layout, i, 1, &dl->state.sets[i].descriptor_set, 0, nullptr); dl->state.sets[i].bound = true; } } @@ -6046,13 +6233,13 @@ void RenderingDeviceVulkan::draw_list_end() { vkCmdExecuteCommands(frames[frame].draw_command_buffer, draw_list_count, command_buffers); vkCmdEndRenderPass(frames[frame].draw_command_buffer); memdelete_arr(draw_list); - draw_list = NULL; + draw_list = nullptr; } else { //just end the list vkCmdEndRenderPass(draw_list->command_buffer); memdelete(draw_list); - draw_list = NULL; + draw_list = nullptr; } for (int i = 0; i < draw_list_bound_textures.size(); i++) { @@ -6085,8 +6272,8 @@ void RenderingDeviceVulkan::draw_list_end() { RenderingDevice::ComputeListID RenderingDeviceVulkan::compute_list_begin() { - ERR_FAIL_COND_V_MSG(draw_list != NULL, INVALID_ID, "Only one draw list can be active at the same time."); - ERR_FAIL_COND_V_MSG(compute_list != NULL, INVALID_ID, "Only one draw/compute list can be active at the same time."); + ERR_FAIL_COND_V_MSG(draw_list != nullptr, INVALID_ID, "Only one draw list can be active at the same time."); + ERR_FAIL_COND_V_MSG(compute_list != nullptr, INVALID_ID, "Only one draw/compute list can be active at the same time."); compute_list = memnew(ComputeList); compute_list->command_buffer = frames[frame].draw_command_buffer; @@ -6189,7 +6376,7 @@ void RenderingDeviceVulkan::compute_list_bind_uniform_set(ComputeListID p_list, VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.oldLayout = textures_to_sampled[i]->layout; @@ -6199,12 +6386,12 @@ void RenderingDeviceVulkan::compute_list_bind_uniform_set(ComputeListID p_list, image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; image_memory_barrier.image = textures_to_sampled[i]->image; image_memory_barrier.subresourceRange.aspectMask = textures_to_sampled[i]->read_aspect_mask; - image_memory_barrier.subresourceRange.baseMipLevel = 0; + image_memory_barrier.subresourceRange.baseMipLevel = textures_to_sampled[i]->base_mipmap; image_memory_barrier.subresourceRange.levelCount = textures_to_sampled[i]->mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = 0; + image_memory_barrier.subresourceRange.baseArrayLayer = textures_to_sampled[i]->base_layer; image_memory_barrier.subresourceRange.layerCount = textures_to_sampled[i]->layers; - vkCmdPipelineBarrier(cl->command_buffer, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(cl->command_buffer, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); textures_to_sampled[i]->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; @@ -6220,7 +6407,7 @@ void RenderingDeviceVulkan::compute_list_bind_uniform_set(ComputeListID p_list, VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.oldLayout = textures_to_storage[i]->layout; @@ -6230,12 +6417,12 @@ void RenderingDeviceVulkan::compute_list_bind_uniform_set(ComputeListID p_list, image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; image_memory_barrier.image = textures_to_storage[i]->image; image_memory_barrier.subresourceRange.aspectMask = textures_to_storage[i]->read_aspect_mask; - image_memory_barrier.subresourceRange.baseMipLevel = 0; + image_memory_barrier.subresourceRange.baseMipLevel = textures_to_storage[i]->base_mipmap; image_memory_barrier.subresourceRange.levelCount = textures_to_storage[i]->mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = 0; + image_memory_barrier.subresourceRange.baseArrayLayer = textures_to_storage[i]->base_layer; image_memory_barrier.subresourceRange.layerCount = textures_to_storage[i]->layers; - vkCmdPipelineBarrier(cl->command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(cl->command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); textures_to_storage[i]->layout = VK_IMAGE_LAYOUT_GENERAL; @@ -6259,7 +6446,7 @@ void RenderingDeviceVulkan::compute_list_bind_uniform_set(ComputeListID p_list, #endif } -void RenderingDeviceVulkan::compute_list_set_push_constant(ComputeListID p_list, void *p_data, uint32_t p_data_size) { +void RenderingDeviceVulkan::compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size) { ERR_FAIL_COND(p_list != ID_TYPE_COMPUTE_LIST); ERR_FAIL_COND(!compute_list); @@ -6329,7 +6516,7 @@ void RenderingDeviceVulkan::compute_list_dispatch(ComputeListID p_list, uint32_t #endif if (!cl->state.sets[i].bound) { //All good, see if this requires re-binding - vkCmdBindDescriptorSets(cl->command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, cl->state.pipeline_layout, i, 1, &cl->state.sets[i].descriptor_set, 0, NULL); + vkCmdBindDescriptorSets(cl->command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, cl->state.pipeline_layout, i, 1, &cl->state.sets[i].descriptor_set, 0, nullptr); cl->state.sets[i].bound = true; } } @@ -6352,7 +6539,7 @@ void RenderingDeviceVulkan::compute_list_end() { VkImageMemoryBarrier image_memory_barrier; image_memory_barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER; - image_memory_barrier.pNext = NULL; + image_memory_barrier.pNext = nullptr; image_memory_barrier.srcAccessMask = VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT; image_memory_barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT; image_memory_barrier.oldLayout = E->get()->layout; @@ -6362,18 +6549,18 @@ void RenderingDeviceVulkan::compute_list_end() { image_memory_barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; image_memory_barrier.image = E->get()->image; image_memory_barrier.subresourceRange.aspectMask = E->get()->read_aspect_mask; - image_memory_barrier.subresourceRange.baseMipLevel = 0; + image_memory_barrier.subresourceRange.baseMipLevel = E->get()->base_mipmap; image_memory_barrier.subresourceRange.levelCount = E->get()->mipmaps; - image_memory_barrier.subresourceRange.baseArrayLayer = 0; + image_memory_barrier.subresourceRange.baseArrayLayer = E->get()->base_layer; image_memory_barrier.subresourceRange.layerCount = E->get()->layers; - vkCmdPipelineBarrier(compute_list->command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, NULL, 0, NULL, 1, &image_memory_barrier); + vkCmdPipelineBarrier(compute_list->command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_memory_barrier); E->get()->layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL; } memdelete(compute_list); - compute_list = NULL; + compute_list = nullptr; #ifdef FORCE_FULL_BARRIER _full_barrier(true); #else @@ -6389,7 +6576,7 @@ void RenderingDeviceVulkan::draw_list_render_secondary_to_framebuffer(ID p_frame VkRenderPassBeginInfo render_pass_begin; render_pass_begin.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; - render_pass_begin.pNext = NULL; + render_pass_begin.pNext = nullptr; render_pass_begin.renderPass = context->get_render_pass(); render_pass_begin.framebuffer = context->get_frame_framebuffer(frame); @@ -6511,75 +6698,104 @@ void RenderingDeviceVulkan::free(RID p_id) { _free_dependencies(p_id); //recursively erase dependencies first, to avoid potential API problems _free_internal(p_id); } -void RenderingDeviceVulkan::swap_buffers() { - _THREAD_SAFE_METHOD_ +void RenderingDeviceVulkan::_finalize_command_bufers() { - { //finalize frame + if (draw_list) { + ERR_PRINT("Found open draw list at the end of the frame, this should never happen (further drawing will likely not work)."); + } - if (draw_list) { - ERR_PRINT("Found open draw list at the end of the frame, this should never happen (further drawing will likely not work)."); - } + if (compute_list) { + ERR_PRINT("Found open compute list at the end of the frame, this should never happen (further compute will likely not work)."); + } - if (compute_list) { - ERR_PRINT("Found open compute list at the end of the frame, this should never happen (further compute will likely not work)."); - } + { //complete the setup buffer (that needs to be processed before anything else) + vkEndCommandBuffer(frames[frame].setup_command_buffer); + vkEndCommandBuffer(frames[frame].draw_command_buffer); + } +} + +void RenderingDeviceVulkan::_begin_frame() { + + //erase pending resources + _free_pending_resources(frame); + + //create setup command buffer and set as the setup buffer + + { + VkCommandBufferBeginInfo cmdbuf_begin; + cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + cmdbuf_begin.pNext = nullptr; + cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + cmdbuf_begin.pInheritanceInfo = nullptr; + + VkResult err = vkResetCommandBuffer(frames[frame].setup_command_buffer, 0); + ERR_FAIL_COND_MSG(err, "vkResetCommandBuffer failed with error " + itos(err) + "."); - { //complete the setup buffer (that needs to be processed before anything else) - vkEndCommandBuffer(frames[frame].setup_command_buffer); - vkEndCommandBuffer(frames[frame].draw_command_buffer); + err = vkBeginCommandBuffer(frames[frame].setup_command_buffer, &cmdbuf_begin); + ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); + err = vkBeginCommandBuffer(frames[frame].draw_command_buffer, &cmdbuf_begin); + ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); + + if (local_device.is_null()) { + context->append_command_buffer(frames[frame].draw_command_buffer); + context->set_setup_buffer(frames[frame].setup_command_buffer); //append now so it's added before everything else } - screen_prepared = false; } - //swap buffers - context->swap_buffers(); + //advance current frame + frames_drawn++; + //advance staging buffer if used + if (staging_buffer_used) { + staging_buffer_current = (staging_buffer_current + 1) % staging_buffer_blocks.size(); + staging_buffer_used = false; + } - { //advance frame + if (frames[frame].timestamp_count) { + vkGetQueryPoolResults(device, frames[frame].timestamp_pool, 0, frames[frame].timestamp_count, sizeof(uint64_t) * max_timestamp_query_elements, frames[frame].timestamp_result_values, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); + SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names); + SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values); + } - frame = (frame + 1) % frame_count; + frames[frame].timestamp_result_count = frames[frame].timestamp_count; + frames[frame].timestamp_count = 0; + frames[frame].index = Engine::get_singleton()->get_frames_drawn(); +} - //erase pending resources - _free_pending_resources(frame); +void RenderingDeviceVulkan::swap_buffers() { - //create setup command buffer and set as the setup buffer + ERR_FAIL_COND_MSG(local_device.is_valid(), "Local devices can't swap buffers."); + _THREAD_SAFE_METHOD_ - { - VkCommandBufferBeginInfo cmdbuf_begin; - cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - cmdbuf_begin.pNext = NULL; - cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - cmdbuf_begin.pInheritanceInfo = NULL; + _finalize_command_bufers(); - VkResult err = vkResetCommandBuffer(frames[frame].setup_command_buffer, 0); - ERR_FAIL_COND_MSG(err, "vkResetCommandBuffer failed with error " + itos(err) + "."); + screen_prepared = false; + //swap buffers + context->swap_buffers(); - err = vkBeginCommandBuffer(frames[frame].setup_command_buffer, &cmdbuf_begin); - ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - context->set_setup_buffer(frames[frame].setup_command_buffer); //append now so it's added before everything else - err = vkBeginCommandBuffer(frames[frame].draw_command_buffer, &cmdbuf_begin); - ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - context->append_command_buffer(frames[frame].draw_command_buffer); - } + frame = (frame + 1) % frame_count; - //advance current frame - frames_drawn++; - //advance staging buffer if used - if (staging_buffer_used) { - staging_buffer_current = (staging_buffer_current + 1) % staging_buffer_blocks.size(); - staging_buffer_used = false; - } + _begin_frame(); +} - if (frames[frame].timestamp_count) { - vkGetQueryPoolResults(device, frames[frame].timestamp_pool, 0, frames[frame].timestamp_count, sizeof(uint64_t) * max_timestamp_query_elements, frames[frame].timestamp_result_values, sizeof(uint64_t), VK_QUERY_RESULT_64_BIT); - SWAP(frames[frame].timestamp_names, frames[frame].timestamp_result_names); - SWAP(frames[frame].timestamp_cpu_values, frames[frame].timestamp_cpu_result_values); - } +void RenderingDeviceVulkan::submit() { + ERR_FAIL_COND_MSG(local_device.is_null(), "Only local devices can submit and sync."); + ERR_FAIL_COND_MSG(local_device_processing, "device already submitted, call sync to wait until done."); - frames[frame].timestamp_result_count = frames[frame].timestamp_count; - frames[frame].timestamp_count = 0; - frames[frame].index = Engine::get_singleton()->get_frames_drawn(); - } + _finalize_command_bufers(); + + VkCommandBuffer command_buffers[2] = { frames[frame].setup_command_buffer, frames[frame].draw_command_buffer }; + context->local_device_push_command_buffers(local_device, command_buffers, 2); + local_device_processing = true; +} + +void RenderingDeviceVulkan::sync() { + + ERR_FAIL_COND_MSG(local_device.is_null(), "Only local devices can submit and sync."); + ERR_FAIL_COND_MSG(!local_device_processing, "sync can only be called after a submit"); + + context->local_device_sync(local_device); + _begin_frame(); } void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { @@ -6588,7 +6804,7 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { while (frames[p_frame].render_pipelines_to_dispose_of.front()) { RenderPipeline *pipeline = &frames[p_frame].render_pipelines_to_dispose_of.front()->get(); - vkDestroyPipeline(device, pipeline->pipeline, NULL); + vkDestroyPipeline(device, pipeline->pipeline, nullptr); frames[p_frame].render_pipelines_to_dispose_of.pop_front(); } @@ -6596,7 +6812,7 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { while (frames[p_frame].compute_pipelines_to_dispose_of.front()) { ComputePipeline *pipeline = &frames[p_frame].compute_pipelines_to_dispose_of.front()->get(); - vkDestroyPipeline(device, pipeline->pipeline, NULL); + vkDestroyPipeline(device, pipeline->pipeline, nullptr); frames[p_frame].compute_pipelines_to_dispose_of.pop_front(); } @@ -6615,7 +6831,7 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { while (frames[p_frame].buffer_views_to_dispose_of.front()) { VkBufferView buffer_view = frames[p_frame].buffer_views_to_dispose_of.front()->get(); - vkDestroyBufferView(device, buffer_view, NULL); + vkDestroyBufferView(device, buffer_view, nullptr); frames[p_frame].buffer_views_to_dispose_of.pop_front(); } @@ -6626,15 +6842,15 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { //descriptor set layout for each set for (int i = 0; i < shader->sets.size(); i++) { - vkDestroyDescriptorSetLayout(device, shader->sets[i].descriptor_set_layout, NULL); + vkDestroyDescriptorSetLayout(device, shader->sets[i].descriptor_set_layout, nullptr); } //pipeline layout - vkDestroyPipelineLayout(device, shader->pipeline_layout, NULL); + vkDestroyPipelineLayout(device, shader->pipeline_layout, nullptr); //shaders themselves for (int i = 0; i < shader->pipeline_stages.size(); i++) { - vkDestroyShaderModule(device, shader->pipeline_stages[i].module, NULL); + vkDestroyShaderModule(device, shader->pipeline_stages[i].module, nullptr); } frames[p_frame].shaders_to_dispose_of.pop_front(); @@ -6644,7 +6860,7 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { while (frames[p_frame].samplers_to_dispose_of.front()) { VkSampler sampler = frames[p_frame].samplers_to_dispose_of.front()->get(); - vkDestroySampler(device, sampler, NULL); + vkDestroySampler(device, sampler, nullptr); frames[p_frame].samplers_to_dispose_of.pop_front(); } @@ -6655,8 +6871,8 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { for (Map<Framebuffer::VersionKey, Framebuffer::Version>::Element *E = framebuffer->framebuffers.front(); E; E = E->next()) { //first framebuffer, then render pass because it depends on it - vkDestroyFramebuffer(device, E->get().framebuffer, NULL); - vkDestroyRenderPass(device, E->get().render_pass, NULL); + vkDestroyFramebuffer(device, E->get().framebuffer, nullptr); + vkDestroyRenderPass(device, E->get().render_pass, nullptr); } frames[p_frame].framebuffers_to_dispose_of.pop_front(); @@ -6669,7 +6885,7 @@ void RenderingDeviceVulkan::_free_pending_resources(int p_frame) { if (texture->bound) { WARN_PRINT("Deleted a texture while it was bound.."); } - vkDestroyImageView(device, texture->view, NULL); + vkDestroyImageView(device, texture->view, nullptr); if (texture->owner.is_null()) { //actually owns the image and the allocation too vmaDestroyImage(allocator, texture->image, texture->allocation); @@ -6698,43 +6914,71 @@ uint32_t RenderingDeviceVulkan::get_frame_delay() const { void RenderingDeviceVulkan::_flush(bool p_current_frame) { + if (local_device.is_valid() && !p_current_frame) { + return; //flushign previous frames has no effect with local device + } //not doing this crashes RADV (undefined behavior) if (p_current_frame) { vkEndCommandBuffer(frames[frame].setup_command_buffer); vkEndCommandBuffer(frames[frame].draw_command_buffer); } - context->flush(p_current_frame, p_current_frame); - //re-create the setup command - if (p_current_frame) { + + if (local_device.is_valid()) { + + VkCommandBuffer command_buffers[2] = { frames[frame].setup_command_buffer, frames[frame].draw_command_buffer }; + context->local_device_push_command_buffers(local_device, command_buffers, 2); + context->local_device_sync(local_device); + VkCommandBufferBeginInfo cmdbuf_begin; cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - cmdbuf_begin.pNext = NULL; + cmdbuf_begin.pNext = nullptr; cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - cmdbuf_begin.pInheritanceInfo = NULL; + cmdbuf_begin.pInheritanceInfo = nullptr; VkResult err = vkBeginCommandBuffer(frames[frame].setup_command_buffer, &cmdbuf_begin); ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - context->set_setup_buffer(frames[frame].setup_command_buffer); //append now so it's added before everything else - } + err = vkBeginCommandBuffer(frames[frame].draw_command_buffer, &cmdbuf_begin); + ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - if (p_current_frame) { - VkCommandBufferBeginInfo cmdbuf_begin; - cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - cmdbuf_begin.pNext = NULL; - cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - cmdbuf_begin.pInheritanceInfo = NULL; + } else { + context->flush(p_current_frame, p_current_frame); + //re-create the setup command + if (p_current_frame) { + VkCommandBufferBeginInfo cmdbuf_begin; + cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + cmdbuf_begin.pNext = nullptr; + cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + cmdbuf_begin.pInheritanceInfo = nullptr; - VkResult err = vkBeginCommandBuffer(frames[frame].draw_command_buffer, &cmdbuf_begin); - ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - context->append_command_buffer(frames[frame].draw_command_buffer); + VkResult err = vkBeginCommandBuffer(frames[frame].setup_command_buffer, &cmdbuf_begin); + ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); + context->set_setup_buffer(frames[frame].setup_command_buffer); //append now so it's added before everything else + } + + if (p_current_frame) { + VkCommandBufferBeginInfo cmdbuf_begin; + cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; + cmdbuf_begin.pNext = nullptr; + cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; + cmdbuf_begin.pInheritanceInfo = nullptr; + + VkResult err = vkBeginCommandBuffer(frames[frame].draw_command_buffer, &cmdbuf_begin); + ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); + context->append_command_buffer(frames[frame].draw_command_buffer); + } } } -void RenderingDeviceVulkan::initialize(VulkanContext *p_context) { +void RenderingDeviceVulkan::initialize(VulkanContext *p_context, bool p_local_device) { context = p_context; device = p_context->get_device(); - frame_count = p_context->get_swapchain_image_count() + 1; //always need one extra to ensure it's unused at any time, without having to use a fence for this. + if (p_local_device) { + frame_count = 1; + local_device = p_context->local_device_create(); + } else { + frame_count = p_context->get_swapchain_image_count() + 1; //always need one extra to ensure it's unused at any time, without having to use a fence for this. + } limits = p_context->get_device_limits(); max_timestamp_query_elements = 256; @@ -6757,11 +7001,11 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context) { { //create command pool, one per frame is recommended VkCommandPoolCreateInfo cmd_pool_info; cmd_pool_info.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; - cmd_pool_info.pNext = NULL; + cmd_pool_info.pNext = nullptr; cmd_pool_info.queueFamilyIndex = p_context->get_graphics_queue(); cmd_pool_info.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; - VkResult res = vkCreateCommandPool(device, &cmd_pool_info, NULL, &frames[i].command_pool); + VkResult res = vkCreateCommandPool(device, &cmd_pool_info, nullptr, &frames[i].command_pool); ERR_FAIL_COND_MSG(res, "vkCreateCommandPool failed with error " + itos(res) + "."); } @@ -6770,7 +7014,7 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context) { VkCommandBufferAllocateInfo cmdbuf; //no command buffer exists, create it. cmdbuf.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; - cmdbuf.pNext = NULL; + cmdbuf.pNext = nullptr; cmdbuf.commandPool = frames[i].command_pool; cmdbuf.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; cmdbuf.commandBufferCount = 1; @@ -6787,12 +7031,12 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context) { VkQueryPoolCreateInfo query_pool_create_info; query_pool_create_info.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO; query_pool_create_info.flags = 0; - query_pool_create_info.pNext = NULL; + query_pool_create_info.pNext = nullptr; query_pool_create_info.queryType = VK_QUERY_TYPE_TIMESTAMP; query_pool_create_info.queryCount = max_timestamp_query_elements; query_pool_create_info.pipelineStatistics = 0; - vkCreateQueryPool(device, &query_pool_create_info, NULL, &frames[i].timestamp_pool); + vkCreateQueryPool(device, &query_pool_create_info, nullptr, &frames[i].timestamp_pool); frames[i].timestamp_names = memnew_arr(String, max_timestamp_query_elements); frames[i].timestamp_cpu_values = memnew_arr(uint64_t, max_timestamp_query_elements); @@ -6809,17 +7053,19 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context) { //setting up things can be done in the meantime until swap_buffers(), which is called before advance. VkCommandBufferBeginInfo cmdbuf_begin; cmdbuf_begin.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; - cmdbuf_begin.pNext = NULL; + cmdbuf_begin.pNext = nullptr; cmdbuf_begin.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; - cmdbuf_begin.pInheritanceInfo = NULL; + cmdbuf_begin.pInheritanceInfo = nullptr; VkResult err = vkBeginCommandBuffer(frames[0].setup_command_buffer, &cmdbuf_begin); ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - context->set_setup_buffer(frames[0].setup_command_buffer); //append now so it's added before everything else err = vkBeginCommandBuffer(frames[0].draw_command_buffer, &cmdbuf_begin); ERR_FAIL_COND_MSG(err, "vkBeginCommandBuffer failed with error " + itos(err) + "."); - context->append_command_buffer(frames[0].draw_command_buffer); + if (local_device.is_null()) { + context->set_setup_buffer(frames[0].setup_command_buffer); //append now so it's added before everything else + context->append_command_buffer(frames[0].draw_command_buffer); + } } staging_buffer_block_size = GLOBAL_DEF("rendering/vulkan/staging_buffer/block_size_kb", 256); @@ -6853,11 +7099,11 @@ void RenderingDeviceVulkan::initialize(VulkanContext *p_context) { //check to make sure DescriptorPoolKey is good static_assert(sizeof(uint64_t) * 3 >= UNIFORM_TYPE_MAX * sizeof(uint16_t)); - draw_list = NULL; + draw_list = nullptr; draw_list_count = 0; draw_list_split = false; - compute_list = NULL; + compute_list = nullptr; } template <class T> @@ -6880,7 +7126,7 @@ void RenderingDeviceVulkan::capture_timestamp(const String &p_name, bool p_sync_ VkMemoryBarrier memoryBarrier; memoryBarrier.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER; - memoryBarrier.pNext = NULL; + memoryBarrier.pNext = nullptr; memoryBarrier.srcAccessMask = VK_ACCESS_INDIRECT_COMMAND_READ_BIT | VK_ACCESS_INDEX_READ_BIT | VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT | @@ -6912,7 +7158,7 @@ void RenderingDeviceVulkan::capture_timestamp(const String &p_name, bool p_sync_ VK_ACCESS_HOST_READ_BIT | VK_ACCESS_HOST_WRITE_BIT; - vkCmdPipelineBarrier(p_sync_to_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &memoryBarrier, 0, NULL, 0, NULL); + vkCmdPipelineBarrier(p_sync_to_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0, 1, &memoryBarrier, 0, nullptr, 0, nullptr); } vkCmdWriteTimestamp(p_sync_to_draw ? frames[frame].draw_command_buffer : frames[frame].setup_command_buffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, frames[frame].timestamp_pool, frames[frame].timestamp_count); frames[frame].timestamp_names[frames[frame].timestamp_count] = p_name; @@ -6928,9 +7174,42 @@ uint64_t RenderingDeviceVulkan::get_captured_timestamps_frame() const { return frames[frame].index; } +static void mult64to128(uint64_t u, uint64_t v, uint64_t &h, uint64_t &l) { + uint64_t u1 = (u & 0xffffffff); + uint64_t v1 = (v & 0xffffffff); + uint64_t t = (u1 * v1); + uint64_t w3 = (t & 0xffffffff); + uint64_t k = (t >> 32); + + u >>= 32; + t = (u * v1) + k; + k = (t & 0xffffffff); + uint64_t w1 = (t >> 32); + + v >>= 32; + t = (u1 * v) + k; + k = (t >> 32); + + h = (u * v) + w1 + k; + l = (t << 32) + w3; +} + uint64_t RenderingDeviceVulkan::get_captured_timestamp_gpu_time(uint32_t p_index) const { ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0); - return frames[frame].timestamp_result_values[p_index] * limits.timestampPeriod; + + // this sucks because timestampPeriod multiplier is a float, while the timestamp is 64 bits nanosecs. + // so, in cases like nvidia which give you enormous numbers and 1 as multiplier, multiplying is next to impossible + // need to do 128 bits fixed point multiplication to get the rigth value + + uint64_t shift_bits = 16; + + uint64_t h, l; + + mult64to128(frames[frame].timestamp_result_values[p_index], uint64_t(double(limits.timestampPeriod) * double(1 << shift_bits)), h, l); + l >>= shift_bits; + l |= h << (64 - shift_bits); + + return l; } uint64_t RenderingDeviceVulkan::get_captured_timestamp_cpu_time(uint32_t p_index) const { ERR_FAIL_UNSIGNED_INDEX_V(p_index, frames[frame].timestamp_result_count, 0); @@ -7030,8 +7309,8 @@ void RenderingDeviceVulkan::finalize() { for (int i = 0; i < frame_count; i++) { int f = (frame + i) % frame_count; _free_pending_resources(f); - vkDestroyCommandPool(device, frames[i].command_pool, NULL); - vkDestroyQueryPool(device, frames[i].timestamp_pool, NULL); + vkDestroyCommandPool(device, frames[i].command_pool, nullptr); + vkDestroyQueryPool(device, frames[i].timestamp_pool, nullptr); memdelete_arr(frames[i].timestamp_names); memdelete_arr(frames[i].timestamp_cpu_values); memdelete_arr(frames[i].timestamp_result_names); @@ -7040,7 +7319,7 @@ void RenderingDeviceVulkan::finalize() { } for (int i = 0; i < split_draw_list_allocators.size(); i++) { - vkDestroyCommandPool(device, split_draw_list_allocators[i].command_pool, NULL); + vkDestroyCommandPool(device, split_draw_list_allocators[i].command_pool, nullptr); } memdelete_arr(frames); @@ -7048,6 +7327,19 @@ void RenderingDeviceVulkan::finalize() { for (int i = 0; i < staging_buffer_blocks.size(); i++) { vmaDestroyBuffer(allocator, staging_buffer_blocks[i].buffer, staging_buffer_blocks[i].allocation); } + vmaDestroyAllocator(allocator); + + while (vertex_formats.size()) { + Map<VertexFormatID, VertexDescriptionCache>::Element *temp = vertex_formats.front(); + memdelete_arr(temp->get().bindings); + memdelete_arr(temp->get().attributes); + vertex_formats.erase(temp); + } + + for (int i = 0; i < framebuffer_formats.size(); i++) { + vkDestroyRenderPass(device, framebuffer_formats[i].render_pass, nullptr); + } + framebuffer_formats.clear(); //all these should be clear at this point ERR_FAIL_COND(descriptor_pools.size()); @@ -7055,6 +7347,19 @@ void RenderingDeviceVulkan::finalize() { ERR_FAIL_COND(reverse_dependency_map.size()); } +RenderingDevice *RenderingDeviceVulkan::create_local_device() { + RenderingDeviceVulkan *rd = memnew(RenderingDeviceVulkan); + rd->initialize(context, true); + return rd; +} + RenderingDeviceVulkan::RenderingDeviceVulkan() { screen_prepared = false; } + +RenderingDeviceVulkan::~RenderingDeviceVulkan() { + if (local_device.is_valid()) { + finalize(); + context->local_device_free(local_device); + } +} diff --git a/drivers/vulkan/rendering_device_vulkan.h b/drivers/vulkan/rendering_device_vulkan.h index f7ae9c28ea..6432946fbe 100644 --- a/drivers/vulkan/rendering_device_vulkan.h +++ b/drivers/vulkan/rendering_device_vulkan.h @@ -37,8 +37,10 @@ #include "servers/rendering/rendering_device.h" #ifdef DEBUG_ENABLED +#ifndef _DEBUG #define _DEBUG #endif +#endif #include "vk_mem_alloc.h" #include <vulkan/vulkan.h> //todo: @@ -78,7 +80,7 @@ class RenderingDeviceVulkan : public RenderingDevice { static void get_compressed_image_format_block_dimensions(DataFormat p_format, uint32_t &r_w, uint32_t &r_h); uint32_t get_compressed_image_format_block_byte_size(DataFormat p_format); static uint32_t get_compressed_image_format_pixel_rshift(DataFormat p_format); - static uint32_t get_image_format_required_size(DataFormat p_format, uint32_t p_width, uint32_t p_height, uint32_t p_depth, uint32_t p_mipmaps, uint32_t *r_blockw = NULL, uint32_t *r_blockh = NULL, uint32_t *r_depth = NULL); + static uint32_t get_image_format_required_size(DataFormat p_format, uint32_t p_width, uint32_t p_height, uint32_t p_depth, uint32_t p_mipmaps, uint32_t *r_blockw = nullptr, uint32_t *r_blockh = nullptr, uint32_t *r_depth = nullptr); static uint32_t get_image_required_mipmaps(uint32_t p_width, uint32_t p_height, uint32_t p_depth); static bool format_has_stencil(DataFormat p_format); @@ -136,6 +138,8 @@ class RenderingDeviceVulkan : public RenderingDevice { uint32_t layers; uint32_t mipmaps; uint32_t usage_flags; + uint32_t base_mipmap; + uint32_t base_layer; Vector<DataFormat> allowed_shared_formats; @@ -207,8 +211,8 @@ class RenderingDeviceVulkan : public RenderingDevice { VkDescriptorBufferInfo buffer_info; //used for binding Buffer() { size = 0; - buffer = NULL; - allocation = NULL; + buffer = VK_NULL_HANDLE; + allocation = nullptr; } }; @@ -260,7 +264,7 @@ class RenderingDeviceVulkan : public RenderingDevice { } }; - VkRenderPass _render_pass_create(const Vector<AttachmentFormat> &p_format, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depthcolor_action, int *r_color_attachment_count = NULL); + VkRenderPass _render_pass_create(const Vector<AttachmentFormat> &p_format, InitialAction p_initial_action, FinalAction p_final_action, InitialAction p_initial_depth_action, FinalAction p_final_depthcolor_action, int *r_color_attachment_count = nullptr); // This is a cache and it's never freed, it ensures // IDs for a given format are always unique. @@ -328,7 +332,7 @@ class RenderingDeviceVulkan : public RenderingDevice { RID_Owner<Buffer, true> vertex_buffer_owner; struct VertexDescriptionKey { - Vector<VertexDescription> vertex_formats; + Vector<VertexAttribute> vertex_formats; bool operator==(const VertexDescriptionKey &p_key) const { int vdc = vertex_formats.size(); int vdck = p_key.vertex_formats.size(); @@ -336,11 +340,11 @@ class RenderingDeviceVulkan : public RenderingDevice { if (vdc != vdck) { return false; } else { - const VertexDescription *a_ptr = vertex_formats.ptr(); - const VertexDescription *b_ptr = p_key.vertex_formats.ptr(); + const VertexAttribute *a_ptr = vertex_formats.ptr(); + const VertexAttribute *b_ptr = p_key.vertex_formats.ptr(); for (int i = 0; i < vdc; i++) { - const VertexDescription &a = a_ptr[i]; - const VertexDescription &b = b_ptr[i]; + const VertexAttribute &a = a_ptr[i]; + const VertexAttribute &b = b_ptr[i]; if (a.location != b.location) { return false; @@ -365,9 +369,9 @@ class RenderingDeviceVulkan : public RenderingDevice { uint32_t hash() const { int vdc = vertex_formats.size(); uint32_t h = hash_djb2_one_32(vdc); - const VertexDescription *ptr = vertex_formats.ptr(); + const VertexAttribute *ptr = vertex_formats.ptr(); for (int i = 0; i < vdc; i++) { - const VertexDescription &vd = ptr[i]; + const VertexAttribute &vd = ptr[i]; h = hash_djb2_one_32(vd.location, h); h = hash_djb2_one_32(vd.offset, h); h = hash_djb2_one_32(vd.format, h); @@ -389,7 +393,7 @@ class RenderingDeviceVulkan : public RenderingDevice { HashMap<VertexDescriptionKey, VertexFormatID, VertexDescriptionHash> vertex_format_cache; struct VertexDescriptionCache { - Vector<VertexDescription> vertex_formats; + Vector<VertexAttribute> vertex_formats; VkVertexInputBindingDescription *bindings; VkVertexInputAttributeDescription *attributes; VkPipelineVertexInputStateCreateInfo create_info; @@ -948,10 +952,12 @@ class RenderingDeviceVulkan : public RenderingDevice { uint32_t max_timestamp_query_elements; - Frame *frames; //frames available, they are cycled (usually 3) + Frame *frames; //frames available, for main device they are cycled (usually 3), for local devices only 1 int frame; //current frame int frame_count; //total amount of frames uint64_t frames_drawn; + RID local_device; + bool local_device_processing = false; void _free_pending_resources(int p_frame); @@ -967,6 +973,9 @@ class RenderingDeviceVulkan : public RenderingDevice { template <class T> void _free_rids(T &p_owner, const char *p_type); + void _finalize_command_bufers(); + void _begin_frame(); + public: virtual RID texture_create(const TextureFormat &p_format, const TextureView &p_view, const Vector<Vector<uint8_t>> &p_data = Vector<Vector<uint8_t>>()); virtual RID texture_create_shared(const TextureView &p_view, RID p_with_texture); @@ -981,6 +990,7 @@ public: virtual Error texture_copy(RID p_from_texture, RID p_to_texture, const Vector3 &p_from, const Vector3 &p_to, const Vector3 &p_size, uint32_t p_src_mipmap, uint32_t p_dst_mipmap, uint32_t p_src_layer, uint32_t p_dst_layer, bool p_sync_with_draw = false); virtual Error texture_clear(RID p_texture, const Color &p_color, uint32_t p_base_mipmap, uint32_t p_mipmaps, uint32_t p_base_layer, uint32_t p_layers, bool p_sync_with_draw = false); + virtual Error texture_resolve_multisample(RID p_from_texture, RID p_to_texture, bool p_sync_with_draw = false); /*********************/ /**** FRAMEBUFFER ****/ @@ -1006,7 +1016,7 @@ public: virtual RID vertex_buffer_create(uint32_t p_size_bytes, const Vector<uint8_t> &p_data = Vector<uint8_t>()); // Internally reference counted, this ID is warranted to be unique for the same description, but needs to be freed as many times as it was allocated - virtual VertexFormatID vertex_format_create(const Vector<VertexDescription> &p_vertex_formats); + virtual VertexFormatID vertex_format_create(const Vector<VertexAttribute> &p_vertex_formats); virtual RID vertex_array_create(uint32_t p_vertex_count, VertexFormatID p_vertex_format, const Vector<RID> &p_src_buffers); virtual RID index_buffer_create(uint32_t p_size_indices, IndexBufferFormat p_format, const Vector<uint8_t> &p_data = Vector<uint8_t>(), bool p_use_restart_indices = false); @@ -1070,7 +1080,7 @@ public: virtual void draw_list_bind_vertex_array(DrawListID p_list, RID p_vertex_array); virtual void draw_list_bind_index_array(DrawListID p_list, RID p_index_array); virtual void draw_list_set_line_width(DrawListID p_list, float p_width); - virtual void draw_list_set_push_constant(DrawListID p_list, void *p_data, uint32_t p_data_size); + virtual void draw_list_set_push_constant(DrawListID p_list, const void *p_data, uint32_t p_data_size); virtual void draw_list_draw(DrawListID p_list, bool p_use_indices, uint32_t p_instances = 1, uint32_t p_procedural_vertices = 0); @@ -1086,7 +1096,7 @@ public: virtual ComputeListID compute_list_begin(); virtual void compute_list_bind_compute_pipeline(ComputeListID p_list, RID p_compute_pipeline); virtual void compute_list_bind_uniform_set(ComputeListID p_list, RID p_uniform_set, uint32_t p_index); - virtual void compute_list_set_push_constant(ComputeListID p_list, void *p_data, uint32_t p_data_size); + virtual void compute_list_set_push_constant(ComputeListID p_list, const void *p_data, uint32_t p_data_size); virtual void compute_list_add_barrier(ComputeListID p_list); virtual void compute_list_dispatch(ComputeListID p_list, uint32_t p_x_groups, uint32_t p_y_groups, uint32_t p_z_groups); @@ -1116,14 +1126,20 @@ public: virtual int limit_get(Limit p_limit); virtual void prepare_screen_for_drawing(); - void initialize(VulkanContext *p_context); + void initialize(VulkanContext *p_context, bool p_local_device = false); void finalize(); - virtual void swap_buffers(); + virtual void swap_buffers(); //for main device + + virtual void submit(); //for local device + virtual void sync(); //for local device virtual uint32_t get_frame_delay() const; + virtual RenderingDevice *create_local_device(); + RenderingDeviceVulkan(); + ~RenderingDeviceVulkan(); }; #endif // RENDERING_DEVICE_VULKAN_H diff --git a/drivers/vulkan/vulkan_context.cpp b/drivers/vulkan/vulkan_context.cpp index 494c64ff55..083b822676 100644 --- a/drivers/vulkan/vulkan_context.cpp +++ b/drivers/vulkan/vulkan_context.cpp @@ -51,20 +51,20 @@ VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_messenger_callback( void *pUserData) { // This error needs to be ignored because the AMD allocator will mix up memory types on IGP processors. - if (strstr(pCallbackData->pMessage, "Mapping an image with layout") != NULL && - strstr(pCallbackData->pMessage, "can result in undefined behavior if this memory is used by the device") != NULL) { + if (strstr(pCallbackData->pMessage, "Mapping an image with layout") != nullptr && + strstr(pCallbackData->pMessage, "can result in undefined behavior if this memory is used by the device") != nullptr) { return VK_FALSE; } // This needs to be ignored because Validator is wrong here. - if (strstr(pCallbackData->pMessage, "SPIR-V module not valid: Pointer operand") != NULL && - strstr(pCallbackData->pMessage, "must be a memory object") != NULL) { + if (strstr(pCallbackData->pMessage, "SPIR-V module not valid: Pointer operand") != nullptr && + strstr(pCallbackData->pMessage, "must be a memory object") != nullptr) { return VK_FALSE; } // Workaround for Vulkan-Loader usability bug: https://github.com/KhronosGroup/Vulkan-Loader/issues/262. - if (strstr(pCallbackData->pMessage, "wrong ELF class: ELFCLASS32") != NULL) { + if (strstr(pCallbackData->pMessage, "wrong ELF class: ELFCLASS32") != nullptr) { return VK_FALSE; } - if (pCallbackData->pMessageIdName && strstr(pCallbackData->pMessageIdName, "UNASSIGNED-CoreValidation-DrawState-ClearCmdBeforeDraw") != NULL) { + if (pCallbackData->pMessageIdName && strstr(pCallbackData->pMessageIdName, "UNASSIGNED-CoreValidation-DrawState-ClearCmdBeforeDraw") != nullptr) { return VK_FALSE; } @@ -92,7 +92,7 @@ VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::_debug_messenger_callback( "\n\t\tObject[" + String::num_int64(object) + "]" + " - " + string_VkObjectType(pCallbackData->pObjects[object].objectType) + ", Handle " + String::num_int64(pCallbackData->pObjects[object].objectHandle); - if (NULL != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) { + if (nullptr != pCallbackData->pObjects[object].pObjectName && strlen(pCallbackData->pObjects[object].pObjectName) > 0) { objects_string += ", Name \"" + String(pCallbackData->pObjects[object].pObjectName) + "\""; } } @@ -172,7 +172,7 @@ Error VulkanContext::_create_validation_layers() { "VK_LAYER_LUNARG_object_tracker", "VK_LAYER_LUNARG_core_validation", "VK_LAYER_GOOGLE_unique_objects" }; VkBool32 validation_found = 0; - err = vkEnumerateInstanceLayerProperties(&instance_layer_count, NULL); + err = vkEnumerateInstanceLayerProperties(&instance_layer_count, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); const char **instance_validation_layers = instance_validation_layers_alt1; if (instance_layer_count > 0) { @@ -222,12 +222,12 @@ Error VulkanContext::_initialize_extensions() { VkBool32 platformSurfaceExtFound = 0; memset(extension_names, 0, sizeof(extension_names)); - err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, NULL); + err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); if (instance_extension_count > 0) { VkExtensionProperties *instance_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * instance_extension_count); - err = vkEnumerateInstanceExtensionProperties(NULL, &instance_extension_count, instance_extensions); + err = vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, instance_extensions); if (err) { free(instance_extensions); ERR_FAIL_V(ERR_CANT_CREATE); @@ -286,7 +286,7 @@ Error VulkanContext::_create_physical_device() { CharString namecs = name.utf8(); const VkApplicationInfo app = { /*sType*/ VK_STRUCTURE_TYPE_APPLICATION_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*pApplicationName*/ cs.get_data(), /*applicationVersion*/ 0, /*pEngineName*/ namecs.get_data(), @@ -295,7 +295,7 @@ Error VulkanContext::_create_physical_device() { }; VkInstanceCreateInfo inst_info = { /*sType*/ VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*pApplicationInfo*/ &app, /*enabledLayerCount*/ enabled_layer_count, @@ -313,7 +313,7 @@ Error VulkanContext::_create_physical_device() { if (use_validation_layers) { // VK_EXT_debug_utils style dbg_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; - dbg_messenger_create_info.pNext = NULL; + dbg_messenger_create_info.pNext = nullptr; dbg_messenger_create_info.flags = 0; dbg_messenger_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; @@ -327,7 +327,7 @@ Error VulkanContext::_create_physical_device() { uint32_t gpu_count; - VkResult err = vkCreateInstance(&inst_info, NULL, &inst); + VkResult err = vkCreateInstance(&inst_info, nullptr, &inst); ERR_FAIL_COND_V_MSG(err == VK_ERROR_INCOMPATIBLE_DRIVER, ERR_CANT_CREATE, "Cannot find a compatible Vulkan installable client driver (ICD).\n\n" "vkCreateInstance Failure"); @@ -342,7 +342,7 @@ Error VulkanContext::_create_physical_device() { "vkCreateInstance Failure"); /* Make initial call to query gpu_count, then second call for gpu info*/ - err = vkEnumeratePhysicalDevices(inst, &gpu_count, NULL); + err = vkEnumeratePhysicalDevices(inst, &gpu_count, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); ERR_FAIL_COND_V_MSG(gpu_count == 0, ERR_CANT_CREATE, @@ -366,12 +366,12 @@ Error VulkanContext::_create_physical_device() { enabled_extension_count = 0; memset(extension_names, 0, sizeof(extension_names)); - err = vkEnumerateDeviceExtensionProperties(gpu, NULL, &device_extension_count, NULL); + err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); if (device_extension_count > 0) { VkExtensionProperties *device_extensions = (VkExtensionProperties *)malloc(sizeof(VkExtensionProperties) * device_extension_count); - err = vkEnumerateDeviceExtensionProperties(gpu, NULL, &device_extension_count, device_extensions); + err = vkEnumerateDeviceExtensionProperties(gpu, nullptr, &device_extension_count, device_extensions); if (err) { free(device_extensions); ERR_FAIL_V(ERR_CANT_CREATE); @@ -449,16 +449,16 @@ Error VulkanContext::_create_physical_device() { (PFN_vkCmdInsertDebugUtilsLabelEXT)vkGetInstanceProcAddr(inst, "vkCmdInsertDebugUtilsLabelEXT"); SetDebugUtilsObjectNameEXT = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(inst, "vkSetDebugUtilsObjectNameEXT"); - if (NULL == CreateDebugUtilsMessengerEXT || NULL == DestroyDebugUtilsMessengerEXT || - NULL == SubmitDebugUtilsMessageEXT || NULL == CmdBeginDebugUtilsLabelEXT || - NULL == CmdEndDebugUtilsLabelEXT || NULL == CmdInsertDebugUtilsLabelEXT || - NULL == SetDebugUtilsObjectNameEXT) { + if (nullptr == CreateDebugUtilsMessengerEXT || nullptr == DestroyDebugUtilsMessengerEXT || + nullptr == SubmitDebugUtilsMessageEXT || nullptr == CmdBeginDebugUtilsLabelEXT || + nullptr == CmdEndDebugUtilsLabelEXT || nullptr == CmdInsertDebugUtilsLabelEXT || + nullptr == SetDebugUtilsObjectNameEXT) { ERR_FAIL_V_MSG(ERR_CANT_CREATE, "GetProcAddr: Failed to init VK_EXT_debug_utils\n" "GetProcAddr: Failure"); } - err = CreateDebugUtilsMessengerEXT(inst, &dbg_messenger_create_info, NULL, &dbg_messenger); + err = CreateDebugUtilsMessengerEXT(inst, &dbg_messenger_create_info, nullptr, &dbg_messenger); switch (err) { case VK_SUCCESS: break; @@ -478,7 +478,7 @@ Error VulkanContext::_create_physical_device() { vkGetPhysicalDeviceProperties(gpu, &gpu_props); /* Call with NULL data to get count */ - vkGetPhysicalDeviceQueueFamilyProperties(gpu, &queue_family_count, NULL); + vkGetPhysicalDeviceQueueFamilyProperties(gpu, &queue_family_count, nullptr); ERR_FAIL_COND_V(queue_family_count == 0, ERR_CANT_CREATE); queue_props = (VkQueueFamilyProperties *)malloc(queue_family_count * sizeof(VkQueueFamilyProperties)); @@ -492,7 +492,7 @@ Error VulkanContext::_create_physical_device() { #define GET_INSTANCE_PROC_ADDR(inst, entrypoint) \ { \ fp##entrypoint = (PFN_vk##entrypoint)vkGetInstanceProcAddr(inst, "vk" #entrypoint); \ - ERR_FAIL_COND_V_MSG(fp##entrypoint == NULL, ERR_CANT_CREATE, \ + ERR_FAIL_COND_V_MSG(fp##entrypoint == nullptr, ERR_CANT_CREATE, \ "vkGetInstanceProcAddr failed to find vk" #entrypoint); \ } @@ -511,7 +511,7 @@ Error VulkanContext::_create_device() { float queue_priorities[1] = { 0.0 }; VkDeviceQueueCreateInfo queues[2]; queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - queues[0].pNext = NULL; + queues[0].pNext = nullptr; queues[0].queueFamilyIndex = graphics_queue_family_index; queues[0].queueCount = 1; queues[0].pQueuePriorities = queue_priorities; @@ -519,12 +519,12 @@ Error VulkanContext::_create_device() { VkDeviceCreateInfo sdevice = { /*sType*/ VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*queueCreateInfoCount*/ 1, /*pQueueCreateInfos*/ queues, /*enabledLayerCount*/ 0, - /*ppEnabledLayerNames*/ NULL, + /*ppEnabledLayerNames*/ nullptr, /*enabledExtensionCount*/ enabled_extension_count, /*ppEnabledExtensionNames*/ (const char *const *)extension_names, /*pEnabledFeatures*/ &physical_device_features, // If specific features are required, pass them in here @@ -532,14 +532,14 @@ Error VulkanContext::_create_device() { }; if (separate_present_queue) { queues[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; - queues[1].pNext = NULL; + queues[1].pNext = nullptr; queues[1].queueFamilyIndex = present_queue_family_index; queues[1].queueCount = 1; queues[1].pQueuePriorities = queue_priorities; queues[1].flags = 0; sdevice.queueCreateInfoCount = 2; } - err = vkCreateDevice(gpu, &sdevice, NULL, &device); + err = vkCreateDevice(gpu, &sdevice, nullptr, &device); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); return OK; } @@ -593,12 +593,12 @@ Error VulkanContext::_initialize_queues(VkSurfaceKHR surface) { _create_device(); - static PFN_vkGetDeviceProcAddr g_gdpa = NULL; + static PFN_vkGetDeviceProcAddr g_gdpa = nullptr; #define GET_DEVICE_PROC_ADDR(dev, entrypoint) \ { \ if (!g_gdpa) g_gdpa = (PFN_vkGetDeviceProcAddr)vkGetInstanceProcAddr(inst, "vkGetDeviceProcAddr"); \ fp##entrypoint = (PFN_vk##entrypoint)g_gdpa(dev, "vk" #entrypoint); \ - ERR_FAIL_COND_V_MSG(fp##entrypoint == NULL, ERR_CANT_CREATE, \ + ERR_FAIL_COND_V_MSG(fp##entrypoint == nullptr, ERR_CANT_CREATE, \ "vkGetDeviceProcAddr failed to find vk" #entrypoint); \ } @@ -622,7 +622,7 @@ Error VulkanContext::_initialize_queues(VkSurfaceKHR surface) { // Get the list of VkFormat's that are supported: uint32_t formatCount; - VkResult err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &formatCount, NULL); + VkResult err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &formatCount, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); VkSurfaceFormatKHR *surfFormats = (VkSurfaceFormatKHR *)malloc(formatCount * sizeof(VkSurfaceFormatKHR)); err = fpGetPhysicalDeviceSurfaceFormatsKHR(gpu, surface, &formatCount, surfFormats); @@ -662,7 +662,7 @@ Error VulkanContext::_create_semaphores() { // rendering and waiting for drawing to be complete before presenting VkSemaphoreCreateInfo semaphoreCreateInfo = { /*sType*/ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, }; @@ -670,21 +670,21 @@ Error VulkanContext::_create_semaphores() { // ahead of the image presents VkFenceCreateInfo fence_ci = { /*sType*/ VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ VK_FENCE_CREATE_SIGNALED_BIT }; for (uint32_t i = 0; i < FRAME_LAG; i++) { - err = vkCreateFence(device, &fence_ci, NULL, &fences[i]); + err = vkCreateFence(device, &fence_ci, nullptr, &fences[i]); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); - err = vkCreateSemaphore(device, &semaphoreCreateInfo, NULL, &image_acquired_semaphores[i]); + err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &image_acquired_semaphores[i]); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); - err = vkCreateSemaphore(device, &semaphoreCreateInfo, NULL, &draw_complete_semaphores[i]); + err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &draw_complete_semaphores[i]); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); if (separate_present_queue) { - err = vkCreateSemaphore(device, &semaphoreCreateInfo, NULL, &image_ownership_semaphores[i]); + err = vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &image_ownership_semaphores[i]); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); } } @@ -753,7 +753,7 @@ VkFramebuffer VulkanContext::window_get_framebuffer(DisplayServer::WindowID p_wi void VulkanContext::window_destroy(DisplayServer::WindowID p_window_id) { ERR_FAIL_COND(!windows.has(p_window_id)); _clean_up_swap_chain(&windows[p_window_id]); - vkDestroySurfaceKHR(inst, windows[p_window_id].surface, NULL); + vkDestroySurfaceKHR(inst, windows[p_window_id].surface, nullptr); windows.erase(p_window_id); } @@ -765,20 +765,20 @@ Error VulkanContext::_clean_up_swap_chain(Window *window) { vkDeviceWaitIdle(device); //this destroys images associated it seems - fpDestroySwapchainKHR(device, window->swapchain, NULL); + fpDestroySwapchainKHR(device, window->swapchain, nullptr); window->swapchain = VK_NULL_HANDLE; - vkDestroyRenderPass(device, window->render_pass, NULL); + vkDestroyRenderPass(device, window->render_pass, nullptr); if (window->swapchain_image_resources) { for (uint32_t i = 0; i < swapchainImageCount; i++) { - vkDestroyImageView(device, window->swapchain_image_resources[i].view, NULL); - vkDestroyFramebuffer(device, window->swapchain_image_resources[i].framebuffer, NULL); + vkDestroyImageView(device, window->swapchain_image_resources[i].view, nullptr); + vkDestroyFramebuffer(device, window->swapchain_image_resources[i].framebuffer, nullptr); } free(window->swapchain_image_resources); - window->swapchain_image_resources = NULL; + window->swapchain_image_resources = nullptr; } if (separate_present_queue) { - vkDestroyCommandPool(device, window->present_cmd_pool, NULL); + vkDestroyCommandPool(device, window->present_cmd_pool, nullptr); } return OK; } @@ -796,7 +796,7 @@ Error VulkanContext::_update_swap_chain(Window *window) { ERR_FAIL_COND_V(err, ERR_CANT_CREATE); uint32_t presentModeCount; - err = fpGetPhysicalDeviceSurfacePresentModesKHR(gpu, window->surface, &presentModeCount, NULL); + err = fpGetPhysicalDeviceSurfacePresentModesKHR(gpu, window->surface, &presentModeCount, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); VkPresentModeKHR *presentModes = (VkPresentModeKHR *)malloc(presentModeCount * sizeof(VkPresentModeKHR)); ERR_FAIL_COND_V(!presentModes, ERR_CANT_CREATE); @@ -918,7 +918,7 @@ Error VulkanContext::_update_swap_chain(Window *window) { VkSwapchainCreateInfoKHR swapchain_ci = { /*sType*/ VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*surface*/ window->surface, /*minImageCount*/ desiredNumOfSwapchainImages, @@ -932,19 +932,19 @@ Error VulkanContext::_update_swap_chain(Window *window) { /*imageUsage*/ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, /*imageSharingMode*/ VK_SHARING_MODE_EXCLUSIVE, /*queueFamilyIndexCount*/ 0, - /*pQueueFamilyIndices*/ NULL, + /*pQueueFamilyIndices*/ nullptr, /*preTransform*/ (VkSurfaceTransformFlagBitsKHR)preTransform, /*compositeAlpha*/ compositeAlpha, /*presentMode*/ swapchainPresentMode, /*clipped*/ true, - /*oldSwapchain*/ NULL, + /*oldSwapchain*/ VK_NULL_HANDLE, }; - err = fpCreateSwapchainKHR(device, &swapchain_ci, NULL, &window->swapchain); + err = fpCreateSwapchainKHR(device, &swapchain_ci, nullptr, &window->swapchain); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); uint32_t sp_image_count; - err = fpGetSwapchainImagesKHR(device, window->swapchain, &sp_image_count, NULL); + err = fpGetSwapchainImagesKHR(device, window->swapchain, &sp_image_count, nullptr); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); if (swapchainImageCount == 0) { @@ -972,7 +972,7 @@ Error VulkanContext::_update_swap_chain(Window *window) { for (uint32_t i = 0; i < swapchainImageCount; i++) { VkImageViewCreateInfo color_image_view = { /*sType*/ VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*image*/ swapchainImages[i], /*viewType*/ VK_IMAGE_VIEW_TYPE_2D, @@ -994,7 +994,7 @@ Error VulkanContext::_update_swap_chain(Window *window) { color_image_view.image = window->swapchain_image_resources[i].image; - err = vkCreateImageView(device, &color_image_view, NULL, &window->swapchain_image_resources[i].view); + err = vkCreateImageView(device, &color_image_view, nullptr, &window->swapchain_image_resources[i].view); if (err) { free(swapchainImages); ERR_FAIL_V(ERR_CANT_CREATE); @@ -1028,33 +1028,33 @@ Error VulkanContext::_update_swap_chain(Window *window) { /*flags*/ 0, /*pipelineBindPoint*/ VK_PIPELINE_BIND_POINT_GRAPHICS, /*inputAttachmentCount*/ 0, - /*pInputAttachments*/ NULL, + /*pInputAttachments*/ nullptr, /*colorAttachmentCount*/ 1, /*pColorAttachments*/ &color_reference, - /*pResolveAttachments*/ NULL, - /*pDepthStencilAttachment*/ NULL, + /*pResolveAttachments*/ nullptr, + /*pDepthStencilAttachment*/ nullptr, /*preserveAttachmentCount*/ 0, - /*pPreserveAttachments*/ NULL, + /*pPreserveAttachments*/ nullptr, }; const VkRenderPassCreateInfo rp_info = { /*sTyp*/ VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*attachmentCount*/ 1, /*pAttachments*/ &attachment, /*subpassCount*/ 1, /*pSubpasses*/ &subpass, /*dependencyCount*/ 0, - /*pDependencies*/ NULL, + /*pDependencies*/ nullptr, }; - err = vkCreateRenderPass(device, &rp_info, NULL, &window->render_pass); + err = vkCreateRenderPass(device, &rp_info, nullptr, &window->render_pass); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); for (uint32_t i = 0; i < swapchainImageCount; i++) { const VkFramebufferCreateInfo fb_info = { /*sType*/ VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*renderPass*/ window->render_pass, /*attachmentCount*/ 1, @@ -1064,7 +1064,7 @@ Error VulkanContext::_update_swap_chain(Window *window) { /*layers*/ 1, }; - err = vkCreateFramebuffer(device, &fb_info, NULL, &window->swapchain_image_resources[i].framebuffer); + err = vkCreateFramebuffer(device, &fb_info, nullptr, &window->swapchain_image_resources[i].framebuffer); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); } } @@ -1074,15 +1074,15 @@ Error VulkanContext::_update_swap_chain(Window *window) { if (separate_present_queue) { const VkCommandPoolCreateInfo present_cmd_pool_info = { /*sType*/ VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ 0, /*queueFamilyIndex*/ present_queue_family_index, }; - err = vkCreateCommandPool(device, &present_cmd_pool_info, NULL, &window->present_cmd_pool); + err = vkCreateCommandPool(device, &present_cmd_pool_info, nullptr, &window->present_cmd_pool); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); const VkCommandBufferAllocateInfo present_cmd_info = { /*sType*/ VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*commandPool*/ window->present_cmd_pool, /*level*/ VK_COMMAND_BUFFER_LEVEL_PRIMARY, /*commandBufferCount*/ 1, @@ -1094,16 +1094,16 @@ Error VulkanContext::_update_swap_chain(Window *window) { const VkCommandBufferBeginInfo cmd_buf_info = { /*sType*/ VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*flags*/ VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT, - /*pInheritanceInfo*/ NULL, + /*pInheritanceInfo*/ nullptr, }; err = vkBeginCommandBuffer(window->swapchain_image_resources[i].graphics_to_present_cmd, &cmd_buf_info); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); VkImageMemoryBarrier image_ownership_barrier = { /*sType*/ VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*srcAccessMask*/ 0, /*dstAccessMask*/ VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, /*oldLayout*/ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, @@ -1115,7 +1115,7 @@ Error VulkanContext::_update_swap_chain(Window *window) { }; vkCmdPipelineBarrier(window->swapchain_image_resources[i].graphics_to_present_cmd, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, - VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, NULL, 0, NULL, 1, &image_ownership_barrier); + VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 0, nullptr, 0, nullptr, 1, &image_ownership_barrier); err = vkEndCommandBuffer(window->swapchain_image_resources[i].graphics_to_present_cmd); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); } @@ -1163,16 +1163,16 @@ void VulkanContext::flush(bool p_flush_setup, bool p_flush_pending) { //use a fence to wait for everything done VkSubmitInfo submit_info; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submit_info.pNext = NULL; - submit_info.pWaitDstStageMask = NULL; + submit_info.pNext = nullptr; + submit_info.pWaitDstStageMask = nullptr; submit_info.waitSemaphoreCount = 0; - submit_info.pWaitSemaphores = NULL; + submit_info.pWaitSemaphores = nullptr; submit_info.commandBufferCount = 1; submit_info.pCommandBuffers = command_buffer_queue.ptr(); submit_info.signalSemaphoreCount = 0; - submit_info.pSignalSemaphores = NULL; + submit_info.pSignalSemaphores = nullptr; VkResult err = vkQueueSubmit(graphics_queue, 1, &submit_info, VK_NULL_HANDLE); - command_buffer_queue.write[0] = NULL; + command_buffer_queue.write[0] = nullptr; ERR_FAIL_COND(err); vkDeviceWaitIdle(device); } @@ -1183,14 +1183,14 @@ void VulkanContext::flush(bool p_flush_setup, bool p_flush_pending) { VkSubmitInfo submit_info; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submit_info.pNext = NULL; - submit_info.pWaitDstStageMask = NULL; + submit_info.pNext = nullptr; + submit_info.pWaitDstStageMask = nullptr; submit_info.waitSemaphoreCount = 0; - submit_info.pWaitSemaphores = NULL; + submit_info.pWaitSemaphores = nullptr; submit_info.commandBufferCount = command_buffer_count - 1; submit_info.pCommandBuffers = command_buffer_queue.ptr() + 1; submit_info.signalSemaphoreCount = 0; - submit_info.pSignalSemaphores = NULL; + submit_info.pSignalSemaphores = nullptr; VkResult err = vkQueueSubmit(graphics_queue, 1, &submit_info, VK_NULL_HANDLE); ERR_FAIL_COND(err); vkDeviceWaitIdle(device); @@ -1274,10 +1274,10 @@ Error VulkanContext::swap_buffers() { // engine has fully released ownership to the application, and it is // okay to render to the image. - const VkCommandBuffer *commands_ptr = NULL; + const VkCommandBuffer *commands_ptr = nullptr; uint32_t commands_to_submit = 0; - if (command_buffer_queue[0] == NULL) { + if (command_buffer_queue[0] == nullptr) { //no setup command, but commands to submit, submit from the first and skip command if (command_buffer_count > 1) { commands_ptr = command_buffer_queue.ptr() + 1; @@ -1291,7 +1291,7 @@ Error VulkanContext::swap_buffers() { VkPipelineStageFlags pipe_stage_flags; VkSubmitInfo submit_info; submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; - submit_info.pNext = NULL; + submit_info.pNext = nullptr; submit_info.pWaitDstStageMask = &pipe_stage_flags; pipe_stage_flags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; submit_info.waitSemaphoreCount = 1; @@ -1303,7 +1303,7 @@ Error VulkanContext::swap_buffers() { err = vkQueueSubmit(graphics_queue, 1, &submit_info, fences[frame_index]); ERR_FAIL_COND_V(err, ERR_CANT_CREATE); - command_buffer_queue.write[0] = NULL; + command_buffer_queue.write[0] = nullptr; command_buffer_count = 1; if (separate_present_queue) { @@ -1339,13 +1339,13 @@ Error VulkanContext::swap_buffers() { // otherwise wait for draw complete VkPresentInfoKHR present = { /*sType*/ VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, - /*pNext*/ NULL, + /*pNext*/ nullptr, /*waitSemaphoreCount*/ 1, /*pWaitSemaphores*/ (separate_present_queue) ? &image_ownership_semaphores[frame_index] : &draw_complete_semaphores[frame_index], /*swapchainCount*/ 0, - /*pSwapchain*/ NULL, - /*pImageIndices*/ NULL, - /*pResults*/ NULL, + /*pSwapchain*/ nullptr, + /*pImageIndices*/ nullptr, + /*pResults*/ nullptr, }; VkSwapchainKHR *pSwapchains = (VkSwapchainKHR *)alloca(sizeof(VkSwapchainKHR *) * windows.size()); @@ -1483,15 +1483,15 @@ VkPhysicalDeviceLimits VulkanContext::get_device_limits() const { } VulkanContext::VulkanContext() { - queue_props = NULL; + queue_props = nullptr; command_buffer_count = 0; - instance_validation_layers = NULL; + instance_validation_layers = nullptr; use_validation_layers = true; VK_KHR_incremental_present_enabled = true; VK_GOOGLE_display_timing_enabled = true; command_buffer_queue.resize(1); //first one is the setup command always - command_buffer_queue.write[0] = NULL; + command_buffer_queue.write[0] = nullptr; command_buffer_count = 1; queues_initialized = false; @@ -1499,8 +1499,100 @@ VulkanContext::VulkanContext() { swapchainImageCount = 0; } +RID VulkanContext::local_device_create() { + LocalDevice ld; + + { //create device + VkResult err; + float queue_priorities[1] = { 0.0 }; + VkDeviceQueueCreateInfo queues[2]; + queues[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; + queues[0].pNext = nullptr; + queues[0].queueFamilyIndex = graphics_queue_family_index; + queues[0].queueCount = 1; + queues[0].pQueuePriorities = queue_priorities; + queues[0].flags = 0; + + VkDeviceCreateInfo sdevice = { + /*sType =*/VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, + /*pNext */ nullptr, + /*flags */ 0, + /*queueCreateInfoCount */ 1, + /*pQueueCreateInfos */ queues, + /*enabledLayerCount */ 0, + /*ppEnabledLayerNames */ nullptr, + /*enabledExtensionCount */ enabled_extension_count, + /*ppEnabledExtensionNames */ (const char *const *)extension_names, + /*pEnabledFeatures */ &physical_device_features, // If specific features are required, pass them in here + }; + err = vkCreateDevice(gpu, &sdevice, nullptr, &ld.device); + ERR_FAIL_COND_V(err, RID()); + } + + { //create graphics queue + + vkGetDeviceQueue(ld.device, graphics_queue_family_index, 0, &ld.queue); + } + + return local_device_owner.make_rid(ld); +} + +VkDevice VulkanContext::local_device_get_vk_device(RID p_local_device) { + LocalDevice *ld = local_device_owner.getornull(p_local_device); + return ld->device; +} + +void VulkanContext::local_device_push_command_buffers(RID p_local_device, const VkCommandBuffer *p_buffers, int p_count) { + + LocalDevice *ld = local_device_owner.getornull(p_local_device); + ERR_FAIL_COND(ld->waiting); + + VkSubmitInfo submit_info; + submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; + submit_info.pNext = nullptr; + submit_info.pWaitDstStageMask = nullptr; + submit_info.waitSemaphoreCount = 0; + submit_info.pWaitSemaphores = nullptr; + submit_info.commandBufferCount = p_count; + submit_info.pCommandBuffers = p_buffers; + submit_info.signalSemaphoreCount = 0; + submit_info.pSignalSemaphores = nullptr; + + VkResult err = vkQueueSubmit(ld->queue, 1, &submit_info, VK_NULL_HANDLE); + ERR_FAIL_COND(err); + + ld->waiting = true; +} + +void VulkanContext::local_device_sync(RID p_local_device) { + + LocalDevice *ld = local_device_owner.getornull(p_local_device); + ERR_FAIL_COND(!ld->waiting); + + vkDeviceWaitIdle(ld->device); + ld->waiting = false; +} + +void VulkanContext::local_device_free(RID p_local_device) { + + LocalDevice *ld = local_device_owner.getornull(p_local_device); + vkDestroyDevice(ld->device, nullptr); + local_device_owner.free(p_local_device); +} + VulkanContext::~VulkanContext() { if (queue_props) { free(queue_props); } + for (uint32_t i = 0; i < FRAME_LAG; i++) { + vkDestroyFence(device, fences[i], nullptr); + vkDestroySemaphore(device, image_acquired_semaphores[i], nullptr); + vkDestroySemaphore(device, draw_complete_semaphores[i], nullptr); + if (separate_present_queue) { + vkDestroySemaphore(device, image_ownership_semaphores[i], nullptr); + } + } + DestroyDebugUtilsMessengerEXT(inst, dbg_messenger, nullptr); + vkDestroyDevice(device, nullptr); + vkDestroyInstance(inst, nullptr); } diff --git a/drivers/vulkan/vulkan_context.h b/drivers/vulkan/vulkan_context.h index 2f10fbfdef..51c3febb47 100644 --- a/drivers/vulkan/vulkan_context.h +++ b/drivers/vulkan/vulkan_context.h @@ -33,6 +33,8 @@ #include "core/error_list.h" #include "core/map.h" +#include "core/os/mutex.h" +#include "core/rid_owner.h" #include "core/ustring.h" #include "servers/display_server.h" #include <vulkan/vulkan.h> @@ -45,8 +47,6 @@ class VulkanContext { FRAME_LAG = 2 }; - bool use_validation_layers; - VkInstance inst; VkSurfaceKHR surface; VkPhysicalDevice gpu; @@ -107,6 +107,14 @@ class VulkanContext { } }; + struct LocalDevice { + bool waiting = false; + VkDevice device; + VkQueue queue; + }; + + RID_Owner<LocalDevice, true> local_device_owner; + Map<DisplayServer::WindowID, Window> windows; uint32_t swapchainImageCount; @@ -181,6 +189,8 @@ protected: bool buffers_prepared; + bool use_validation_layers; + public: VkDevice get_device(); VkPhysicalDevice get_physical_device(); @@ -194,6 +204,12 @@ public: VkFramebuffer window_get_framebuffer(DisplayServer::WindowID p_window = 0); VkRenderPass window_get_render_pass(DisplayServer::WindowID p_window = 0); + RID local_device_create(); + VkDevice local_device_get_vk_device(RID p_local_device); + void local_device_push_command_buffers(RID p_local_device, const VkCommandBuffer *p_buffers, int p_count); + void local_device_sync(RID p_local_device); + void local_device_free(RID p_local_device); + VkFormat get_screen_format() const; VkPhysicalDeviceLimits get_device_limits() const; |