Vulkan: Fix compilation on 32-bit targets

This commit is contained in:
Stenzek 2016-10-01 10:40:44 +10:00
parent d139659ea2
commit 1286c309e3
17 changed files with 88 additions and 179 deletions

View File

@ -40,7 +40,7 @@ private:
void Readback(StateTracker* state_tracker); void Readback(StateTracker* state_tracker);
VkBuffer m_gpu_buffer = VK_NULL_HANDLE; VkBuffer m_gpu_buffer = VK_NULL_HANDLE;
VkDeviceMemory m_gpu_memory = nullptr; VkDeviceMemory m_gpu_memory = VK_NULL_HANDLE;
static const size_t NUM_VALUES = 4; static const size_t NUM_VALUES = 4;
static const size_t BUFFER_SIZE = sizeof(u32) * NUM_VALUES; static const size_t BUFFER_SIZE = sizeof(u32) * NUM_VALUES;

View File

@ -73,7 +73,7 @@ void CommandBufferManager::DestroyCommandPool()
if (m_command_pool) if (m_command_pool)
{ {
vkDestroyCommandPool(g_vulkan_context->GetDevice(), m_command_pool, nullptr); vkDestroyCommandPool(g_vulkan_context->GetDevice(), m_command_pool, nullptr);
m_command_pool = nullptr; m_command_pool = VK_NULL_HANDLE;
} }
} }
@ -141,8 +141,8 @@ void CommandBufferManager::DestroyCommandBuffers()
for (FrameResources& resources : m_frame_resources) for (FrameResources& resources : m_frame_resources)
{ {
for (const auto& it : resources.cleanup_resources) for (auto& it : resources.cleanup_resources)
it.destroy_callback(device, it.object); it();
resources.cleanup_resources.clear(); resources.cleanup_resources.clear();
if (resources.fence != VK_NULL_HANDLE) if (resources.fence != VK_NULL_HANDLE)
@ -385,8 +385,8 @@ void CommandBufferManager::OnCommandBufferExecuted(size_t index)
iter.second.second(resources.fence); iter.second.second(resources.fence);
// Clean up all objects pending destruction on this command buffer // Clean up all objects pending destruction on this command buffer
for (const auto& it : resources.cleanup_resources) for (auto& it : resources.cleanup_resources)
it.destroy_callback(g_vulkan_context->GetDevice(), it.object); it();
resources.cleanup_resources.clear(); resources.cleanup_resources.clear();
} }
@ -446,6 +446,48 @@ void CommandBufferManager::ExecuteCommandBuffer(bool submit_off_thread, bool wai
WaitForFence(pending_fence); WaitForFence(pending_fence);
} }
void CommandBufferManager::DeferBufferDestruction(VkBuffer object)
{
FrameResources& resources = m_frame_resources[m_current_frame];
resources.cleanup_resources.push_back(
[object]() { vkDestroyBuffer(g_vulkan_context->GetDevice(), object, nullptr); });
}
void CommandBufferManager::DeferBufferViewDestruction(VkBufferView object)
{
FrameResources& resources = m_frame_resources[m_current_frame];
resources.cleanup_resources.push_back(
[object]() { vkDestroyBufferView(g_vulkan_context->GetDevice(), object, nullptr); });
}
void CommandBufferManager::DeferDeviceMemoryDestruction(VkDeviceMemory object)
{
FrameResources& resources = m_frame_resources[m_current_frame];
resources.cleanup_resources.push_back(
[object]() { vkFreeMemory(g_vulkan_context->GetDevice(), object, nullptr); });
}
void CommandBufferManager::DeferFramebufferDestruction(VkFramebuffer object)
{
FrameResources& resources = m_frame_resources[m_current_frame];
resources.cleanup_resources.push_back(
[object]() { vkDestroyFramebuffer(g_vulkan_context->GetDevice(), object, nullptr); });
}
void CommandBufferManager::DeferImageDestruction(VkImage object)
{
FrameResources& resources = m_frame_resources[m_current_frame];
resources.cleanup_resources.push_back(
[object]() { vkDestroyImage(g_vulkan_context->GetDevice(), object, nullptr); });
}
void CommandBufferManager::DeferImageViewDestruction(VkImageView object)
{
FrameResources& resources = m_frame_resources[m_current_frame];
resources.cleanup_resources.push_back(
[object]() { vkDestroyImageView(g_vulkan_context->GetDevice(), object, nullptr); });
}
void CommandBufferManager::AddFencePointCallback( void CommandBufferManager::AddFencePointCallback(
const void* key, const CommandBufferQueuedCallback& queued_callback, const void* key, const CommandBufferQueuedCallback& queued_callback,
const CommandBufferExecutedCallback& executed_callback) const CommandBufferExecutedCallback& executed_callback)

View File

@ -81,12 +81,12 @@ public:
// Schedule a vulkan resource for destruction later on. This will occur when the command buffer // Schedule a vulkan resource for destruction later on. This will occur when the command buffer
// is next re-used, and the GPU has finished working with the specified resource. // is next re-used, and the GPU has finished working with the specified resource.
template <typename T> void DeferBufferDestruction(VkBuffer object);
void DeferResourceDestruction(T object) void DeferBufferViewDestruction(VkBufferView object);
{ void DeferDeviceMemoryDestruction(VkDeviceMemory object);
DeferredResourceDestruction wrapper = DeferredResourceDestruction::Wrapper<T>(object); void DeferFramebufferDestruction(VkFramebuffer object);
m_frame_resources[m_current_frame].cleanup_resources.push_back(wrapper); void DeferImageDestruction(VkImage object);
} void DeferImageViewDestruction(VkImageView object);
// Instruct the manager to fire the specified callback when a fence is flagged to be signaled. // Instruct the manager to fire the specified callback when a fence is flagged to be signaled.
// This happens when command buffers are executed, and can be tested if signaled, which means // This happens when command buffers are executed, and can be tested if signaled, which means
@ -124,7 +124,7 @@ private:
bool init_command_buffer_used; bool init_command_buffer_used;
bool needs_fence_wait; bool needs_fence_wait;
std::vector<DeferredResourceDestruction> cleanup_resources; std::vector<std::function<void()>> cleanup_resources;
}; };
std::array<FrameResources, NUM_COMMAND_BUFFERS> m_frame_resources = {}; std::array<FrameResources, NUM_COMMAND_BUFFERS> m_frame_resources = {};

View File

@ -170,9 +170,9 @@ RasterFont::RasterFont()
RasterFont::~RasterFont() RasterFont::~RasterFont()
{ {
if (m_vertex_shader != VK_NULL_HANDLE) if (m_vertex_shader != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_vertex_shader); vkDestroyShaderModule(g_vulkan_context->GetDevice(), m_vertex_shader, nullptr);
if (m_fragment_shader != VK_NULL_HANDLE) if (m_fragment_shader != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_fragment_shader); vkDestroyShaderModule(g_vulkan_context->GetDevice(), m_fragment_shader, nullptr);
} }
bool RasterFont::Initialize() bool RasterFont::Initialize()
@ -279,8 +279,8 @@ bool RasterFont::CreateTexture()
// Free temp buffers after command buffer executes // Free temp buffers after command buffer executes
m_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentInitCommandBuffer(), m_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentInitCommandBuffer(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
g_command_buffer_mgr->DeferResourceDestruction(temp_buffer); g_command_buffer_mgr->DeferBufferDestruction(temp_buffer);
g_command_buffer_mgr->DeferResourceDestruction(temp_buffer_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(temp_buffer_memory);
return true; return true;
} }

View File

@ -164,13 +164,13 @@ void Renderer::DestroySemaphores()
if (m_image_available_semaphore) if (m_image_available_semaphore)
{ {
vkDestroySemaphore(g_vulkan_context->GetDevice(), m_image_available_semaphore, nullptr); vkDestroySemaphore(g_vulkan_context->GetDevice(), m_image_available_semaphore, nullptr);
m_image_available_semaphore = nullptr; m_image_available_semaphore = VK_NULL_HANDLE;
} }
if (m_rendering_finished_semaphore) if (m_rendering_finished_semaphore)
{ {
vkDestroySemaphore(g_vulkan_context->GetDevice(), m_rendering_finished_semaphore, nullptr); vkDestroySemaphore(g_vulkan_context->GetDevice(), m_rendering_finished_semaphore, nullptr);
m_rendering_finished_semaphore = nullptr; m_rendering_finished_semaphore = VK_NULL_HANDLE;
} }
} }

View File

@ -105,8 +105,8 @@ private:
FramebufferManager* m_framebuffer_mgr = nullptr; FramebufferManager* m_framebuffer_mgr = nullptr;
VkSemaphore m_image_available_semaphore = nullptr; VkSemaphore m_image_available_semaphore = VK_NULL_HANDLE;
VkSemaphore m_rendering_finished_semaphore = nullptr; VkSemaphore m_rendering_finished_semaphore = VK_NULL_HANDLE;
std::unique_ptr<SwapChain> m_swap_chain; std::unique_ptr<SwapChain> m_swap_chain;
std::unique_ptr<StateTracker> m_state_tracker; std::unique_ptr<StateTracker> m_state_tracker;

View File

@ -26,8 +26,8 @@ StagingBuffer::~StagingBuffer()
if (m_map_pointer) if (m_map_pointer)
Unmap(); Unmap();
g_command_buffer_mgr->DeferResourceDestruction(m_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
g_command_buffer_mgr->DeferResourceDestruction(m_buffer); g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
} }
bool StagingBuffer::Map(VkDeviceSize offset, VkDeviceSize size) bool StagingBuffer::Map(VkDeviceSize offset, VkDeviceSize size)

View File

@ -133,8 +133,8 @@ StagingTexture2DLinear::~StagingTexture2DLinear()
if (m_map_pointer) if (m_map_pointer)
Unmap(); Unmap();
g_command_buffer_mgr->DeferResourceDestruction(m_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
g_command_buffer_mgr->DeferResourceDestruction(m_image); g_command_buffer_mgr->DeferImageDestruction(m_image);
} }
void StagingTexture2DLinear::CopyFromImage(VkCommandBuffer command_buffer, VkImage image, void StagingTexture2DLinear::CopyFromImage(VkCommandBuffer command_buffer, VkImage image,
@ -362,8 +362,8 @@ StagingTexture2DBuffer::~StagingTexture2DBuffer()
if (m_map_pointer) if (m_map_pointer)
Unmap(); Unmap();
g_command_buffer_mgr->DeferResourceDestruction(m_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
g_command_buffer_mgr->DeferResourceDestruction(m_buffer); g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
} }
void StagingTexture2DBuffer::CopyFromImage(VkCommandBuffer command_buffer, VkImage image, void StagingTexture2DBuffer::CopyFromImage(VkCommandBuffer command_buffer, VkImage image,

View File

@ -539,7 +539,7 @@ void StateTracker::EndRenderPass()
return; return;
vkCmdEndRenderPass(g_command_buffer_mgr->GetCurrentCommandBuffer()); vkCmdEndRenderPass(g_command_buffer_mgr->GetCurrentCommandBuffer());
m_current_render_pass = nullptr; m_current_render_pass = VK_NULL_HANDLE;
} }
void StateTracker::BeginClearRenderPass(const VkRect2D& area, const VkClearValue clear_values[2]) void StateTracker::BeginClearRenderPass(const VkRect2D& area, const VkClearValue clear_values[2])

View File

@ -35,9 +35,9 @@ StreamBuffer::~StreamBuffer()
vkUnmapMemory(g_vulkan_context->GetDevice(), m_memory); vkUnmapMemory(g_vulkan_context->GetDevice(), m_memory);
if (m_buffer != VK_NULL_HANDLE) if (m_buffer != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_buffer); g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
if (m_memory != VK_NULL_HANDLE) if (m_memory != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
} }
std::unique_ptr<StreamBuffer> StreamBuffer::Create(VkBufferUsageFlags usage, size_t initial_size, std::unique_ptr<StreamBuffer> StreamBuffer::Create(VkBufferUsageFlags usage, size_t initial_size,
@ -124,9 +124,9 @@ bool StreamBuffer::ResizeBuffer(size_t size)
// Destroy the backings for the buffer after the command buffer executes // Destroy the backings for the buffer after the command buffer executes
if (m_buffer != VK_NULL_HANDLE) if (m_buffer != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_buffer); g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
if (m_memory != VK_NULL_HANDLE) if (m_memory != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
// Replace with the new buffer // Replace with the new buffer
m_buffer = buffer; m_buffer = buffer;

View File

@ -280,8 +280,8 @@ void SwapChain::DestroyRenderPass()
if (!m_render_pass) if (!m_render_pass)
return; return;
g_command_buffer_mgr->DeferResourceDestruction(m_render_pass); vkDestroyRenderPass(g_vulkan_context->GetDevice(), m_render_pass, nullptr);
m_render_pass = nullptr; m_render_pass = VK_NULL_HANDLE;
} }
bool SwapChain::CreateSwapChain() bool SwapChain::CreateSwapChain()

View File

@ -77,15 +77,15 @@ private:
}; };
void* m_native_handle = nullptr; void* m_native_handle = nullptr;
VkSurfaceKHR m_surface = nullptr; VkSurfaceKHR m_surface = VK_NULL_HANDLE;
VkSurfaceFormatKHR m_surface_format = {}; VkSurfaceFormatKHR m_surface_format = {};
VkPresentModeKHR m_present_mode = VK_PRESENT_MODE_RANGE_SIZE_KHR; VkPresentModeKHR m_present_mode = VK_PRESENT_MODE_RANGE_SIZE_KHR;
VkSwapchainKHR m_swap_chain = nullptr; VkSwapchainKHR m_swap_chain = VK_NULL_HANDLE;
std::vector<SwapChainImage> m_swap_chain_images; std::vector<SwapChainImage> m_swap_chain_images;
u32 m_current_swap_chain_image_index = 0; u32 m_current_swap_chain_image_index = 0;
VkRenderPass m_render_pass = nullptr; VkRenderPass m_render_pass = VK_NULL_HANDLE;
u32 m_width = 0; u32 m_width = 0;
u32 m_height = 0; u32 m_height = 0;

View File

@ -21,13 +21,13 @@ Texture2D::Texture2D(u32 width, u32 height, u32 levels, u32 layers, VkFormat for
Texture2D::~Texture2D() Texture2D::~Texture2D()
{ {
g_command_buffer_mgr->DeferResourceDestruction(m_view); g_command_buffer_mgr->DeferImageViewDestruction(m_view);
// If we don't have device memory allocated, the image is not owned by us (e.g. swapchain) // If we don't have device memory allocated, the image is not owned by us (e.g. swapchain)
if (m_device_memory != VK_NULL_HANDLE) if (m_device_memory != VK_NULL_HANDLE)
{ {
g_command_buffer_mgr->DeferResourceDestruction(m_image); g_command_buffer_mgr->DeferImageDestruction(m_image);
g_command_buffer_mgr->DeferResourceDestruction(m_device_memory); g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_device_memory);
} }
} }
@ -134,6 +134,8 @@ std::unique_ptr<Texture2D> Texture2D::CreateFromExistingImage(u32 width, u32 hei
static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_COLOR_BIT), static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_COLOR_BIT),
0, levels, 0, layers}}; 0, levels, 0, layers}};
// Memory is managed by the owner of the image.
VkDeviceMemory memory = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE; VkImageView view = VK_NULL_HANDLE;
VkResult res = vkCreateImageView(g_vulkan_context->GetDevice(), &view_info, nullptr, &view); VkResult res = vkCreateImageView(g_vulkan_context->GetDevice(), &view_info, nullptr, &view);
if (res != VK_SUCCESS) if (res != VK_SUCCESS)
@ -143,7 +145,7 @@ std::unique_ptr<Texture2D> Texture2D::CreateFromExistingImage(u32 width, u32 hei
} }
return std::make_unique<Texture2D>(width, height, levels, layers, format, samples, view_type, return std::make_unique<Texture2D>(width, height, levels, layers, format, samples, view_type,
existing_image, nullptr, view); existing_image, memory, view);
} }
void Texture2D::OverrideImageLayout(VkImageLayout new_layout) void Texture2D::OverrideImageLayout(VkImageLayout new_layout)

View File

@ -316,7 +316,7 @@ TextureCache::TCacheEntry::~TCacheEntry()
m_parent->m_state_tracker->UnbindTexture(m_texture->GetView()); m_parent->m_state_tracker->UnbindTexture(m_texture->GetView());
if (m_framebuffer != VK_NULL_HANDLE) if (m_framebuffer != VK_NULL_HANDLE)
g_command_buffer_mgr->DeferResourceDestruction(m_framebuffer); g_command_buffer_mgr->DeferFramebufferDestruction(m_framebuffer);
} }
void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height, void TextureCache::TCacheEntry::Load(unsigned int width, unsigned int height,

View File

@ -260,120 +260,6 @@ VkShaderModule CompileAndCreateFragmentShader(const std::string& source_code, bo
} // namespace Util } // namespace Util
template <>
DeferredResourceDestruction
DeferredResourceDestruction::Wrapper<VkCommandPool>(VkCommandPool object)
{
DeferredResourceDestruction ret;
ret.object.command_pool = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyCommandPool(device, obj.command_pool, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction
DeferredResourceDestruction::Wrapper<VkDeviceMemory>(VkDeviceMemory object)
{
DeferredResourceDestruction ret;
ret.object.device_memory = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkFreeMemory(device, obj.device_memory, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction DeferredResourceDestruction::Wrapper<VkBuffer>(VkBuffer object)
{
DeferredResourceDestruction ret;
ret.object.buffer = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyBuffer(device, obj.buffer, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction DeferredResourceDestruction::Wrapper<VkBufferView>(VkBufferView object)
{
DeferredResourceDestruction ret;
ret.object.buffer_view = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyBufferView(device, obj.buffer_view, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction DeferredResourceDestruction::Wrapper<VkImage>(VkImage object)
{
DeferredResourceDestruction ret;
ret.object.image = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyImage(device, obj.image, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction DeferredResourceDestruction::Wrapper<VkImageView>(VkImageView object)
{
DeferredResourceDestruction ret;
ret.object.image_view = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyImageView(device, obj.image_view, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction DeferredResourceDestruction::Wrapper<VkRenderPass>(VkRenderPass object)
{
DeferredResourceDestruction ret;
ret.object.render_pass = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyRenderPass(device, obj.render_pass, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction
DeferredResourceDestruction::Wrapper<VkFramebuffer>(VkFramebuffer object)
{
DeferredResourceDestruction ret;
ret.object.framebuffer = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyFramebuffer(device, obj.framebuffer, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction
DeferredResourceDestruction::Wrapper<VkShaderModule>(VkShaderModule object)
{
DeferredResourceDestruction ret;
ret.object.shader_module = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyShaderModule(device, obj.shader_module, nullptr);
};
return ret;
}
template <>
DeferredResourceDestruction DeferredResourceDestruction::Wrapper<VkPipeline>(VkPipeline object)
{
DeferredResourceDestruction ret;
ret.object.pipeline = object;
ret.destroy_callback = [](VkDevice device, const Object& obj) {
vkDestroyPipeline(device, obj.pipeline, nullptr);
};
return ret;
}
UtilityShaderDraw::UtilityShaderDraw(VkCommandBuffer command_buffer, UtilityShaderDraw::UtilityShaderDraw(VkCommandBuffer command_buffer,
VkPipelineLayout pipeline_layout, VkRenderPass render_pass, VkPipelineLayout pipeline_layout, VkRenderPass render_pass,
VkShaderModule vertex_shader, VkShaderModule geometry_shader, VkShaderModule vertex_shader, VkShaderModule geometry_shader,

View File

@ -66,28 +66,6 @@ VkShaderModule CompileAndCreateFragmentShader(const std::string& source_code,
bool prepend_header = true); bool prepend_header = true);
} }
// Helper methods for cleaning up device objects, used by deferred destruction
struct DeferredResourceDestruction
{
union Object {
VkCommandPool command_pool;
VkDeviceMemory device_memory;
VkBuffer buffer;
VkBufferView buffer_view;
VkImage image;
VkImageView image_view;
VkRenderPass render_pass;
VkFramebuffer framebuffer;
VkShaderModule shader_module;
VkPipeline pipeline;
} object;
void (*destroy_callback)(VkDevice device, const Object& object);
template <typename T>
static DeferredResourceDestruction Wrapper(T object);
};
// Utility shader vertex format // Utility shader vertex format
#pragma pack(push, 1) #pragma pack(push, 1)
struct UtilityShaderVertex struct UtilityShaderVertex

View File

@ -526,7 +526,8 @@ bool VulkanContext::CreateDevice(VkSurfaceKHR surface, bool enable_validation_la
device_info.pQueueCreateInfos = &queue_info; device_info.pQueueCreateInfos = &queue_info;
ExtensionList enabled_extensions; ExtensionList enabled_extensions;
if (!SelectDeviceExtensions(&enabled_extensions, (surface != nullptr), enable_validation_layer)) if (!SelectDeviceExtensions(&enabled_extensions, (surface != VK_NULL_HANDLE),
enable_validation_layer))
return false; return false;
device_info.enabledLayerCount = 0; device_info.enabledLayerCount = 0;