Skip to content

Commit

Permalink
Vulkan: Fix compilation on 32-bit targets
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Oct 3, 2016
1 parent d139659 commit 1286c30
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 179 deletions.
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/Vulkan/BoundingBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class BoundingBox
void Readback(StateTracker* state_tracker);

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 BUFFER_SIZE = sizeof(u32) * NUM_VALUES;
Expand Down
52 changes: 47 additions & 5 deletions Source/Core/VideoBackends/Vulkan/CommandBufferManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void CommandBufferManager::DestroyCommandPool()
if (m_command_pool)
{
vkDestroyCommandPool(g_vulkan_context->GetDevice(), m_command_pool, nullptr);
m_command_pool = nullptr;
m_command_pool = VK_NULL_HANDLE;
}
}

Expand Down Expand Up @@ -141,8 +141,8 @@ void CommandBufferManager::DestroyCommandBuffers()

for (FrameResources& resources : m_frame_resources)
{
for (const auto& it : resources.cleanup_resources)
it.destroy_callback(device, it.object);
for (auto& it : resources.cleanup_resources)
it();
resources.cleanup_resources.clear();

if (resources.fence != VK_NULL_HANDLE)
Expand Down Expand Up @@ -385,8 +385,8 @@ void CommandBufferManager::OnCommandBufferExecuted(size_t index)
iter.second.second(resources.fence);

// Clean up all objects pending destruction on this command buffer
for (const auto& it : resources.cleanup_resources)
it.destroy_callback(g_vulkan_context->GetDevice(), it.object);
for (auto& it : resources.cleanup_resources)
it();
resources.cleanup_resources.clear();
}

Expand Down Expand Up @@ -446,6 +446,48 @@ void CommandBufferManager::ExecuteCommandBuffer(bool submit_off_thread, bool wai
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(
const void* key, const CommandBufferQueuedCallback& queued_callback,
const CommandBufferExecutedCallback& executed_callback)
Expand Down
14 changes: 7 additions & 7 deletions Source/Core/VideoBackends/Vulkan/CommandBufferManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ class CommandBufferManager

// 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.
template <typename T>
void DeferResourceDestruction(T object)
{
DeferredResourceDestruction wrapper = DeferredResourceDestruction::Wrapper<T>(object);
m_frame_resources[m_current_frame].cleanup_resources.push_back(wrapper);
}
void DeferBufferDestruction(VkBuffer object);
void DeferBufferViewDestruction(VkBufferView object);
void DeferDeviceMemoryDestruction(VkDeviceMemory object);
void DeferFramebufferDestruction(VkFramebuffer object);
void DeferImageDestruction(VkImage object);
void DeferImageViewDestruction(VkImageView object);

// 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
Expand Down Expand Up @@ -124,7 +124,7 @@ class CommandBufferManager
bool init_command_buffer_used;
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 = {};
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/VideoBackends/Vulkan/RasterFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ RasterFont::RasterFont()
RasterFont::~RasterFont()
{
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)
g_command_buffer_mgr->DeferResourceDestruction(m_fragment_shader);
vkDestroyShaderModule(g_vulkan_context->GetDevice(), m_fragment_shader, nullptr);
}

bool RasterFont::Initialize()
Expand Down Expand Up @@ -279,8 +279,8 @@ bool RasterFont::CreateTexture()
// Free temp buffers after command buffer executes
m_texture->TransitionToLayout(g_command_buffer_mgr->GetCurrentInitCommandBuffer(),
VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
g_command_buffer_mgr->DeferResourceDestruction(temp_buffer);
g_command_buffer_mgr->DeferResourceDestruction(temp_buffer_memory);
g_command_buffer_mgr->DeferBufferDestruction(temp_buffer);
g_command_buffer_mgr->DeferDeviceMemoryDestruction(temp_buffer_memory);
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoBackends/Vulkan/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ void Renderer::DestroySemaphores()
if (m_image_available_semaphore)
{
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)
{
vkDestroySemaphore(g_vulkan_context->GetDevice(), m_rendering_finished_semaphore, nullptr);
m_rendering_finished_semaphore = nullptr;
m_rendering_finished_semaphore = VK_NULL_HANDLE;
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoBackends/Vulkan/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ class Renderer : public ::Renderer

FramebufferManager* m_framebuffer_mgr = nullptr;

VkSemaphore m_image_available_semaphore = nullptr;
VkSemaphore m_rendering_finished_semaphore = nullptr;
VkSemaphore m_image_available_semaphore = VK_NULL_HANDLE;
VkSemaphore m_rendering_finished_semaphore = VK_NULL_HANDLE;

std::unique_ptr<SwapChain> m_swap_chain;
std::unique_ptr<StateTracker> m_state_tracker;
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoBackends/Vulkan/StagingBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ StagingBuffer::~StagingBuffer()
if (m_map_pointer)
Unmap();

g_command_buffer_mgr->DeferResourceDestruction(m_memory);
g_command_buffer_mgr->DeferResourceDestruction(m_buffer);
g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
}

bool StagingBuffer::Map(VkDeviceSize offset, VkDeviceSize size)
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/VideoBackends/Vulkan/StagingTexture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ StagingTexture2DLinear::~StagingTexture2DLinear()
if (m_map_pointer)
Unmap();

g_command_buffer_mgr->DeferResourceDestruction(m_memory);
g_command_buffer_mgr->DeferResourceDestruction(m_image);
g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
g_command_buffer_mgr->DeferImageDestruction(m_image);
}

void StagingTexture2DLinear::CopyFromImage(VkCommandBuffer command_buffer, VkImage image,
Expand Down Expand Up @@ -362,8 +362,8 @@ StagingTexture2DBuffer::~StagingTexture2DBuffer()
if (m_map_pointer)
Unmap();

g_command_buffer_mgr->DeferResourceDestruction(m_memory);
g_command_buffer_mgr->DeferResourceDestruction(m_buffer);
g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);
g_command_buffer_mgr->DeferBufferDestruction(m_buffer);
}

void StagingTexture2DBuffer::CopyFromImage(VkCommandBuffer command_buffer, VkImage image,
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/Vulkan/StateTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ void StateTracker::EndRenderPass()
return;

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])
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/VideoBackends/Vulkan/StreamBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ StreamBuffer::~StreamBuffer()
vkUnmapMemory(g_vulkan_context->GetDevice(), m_memory);

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)
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,
Expand Down Expand Up @@ -124,9 +124,9 @@ bool StreamBuffer::ResizeBuffer(size_t size)

// Destroy the backings for the buffer after the command buffer executes
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)
g_command_buffer_mgr->DeferResourceDestruction(m_memory);
g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_memory);

// Replace with the new buffer
m_buffer = buffer;
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/VideoBackends/Vulkan/SwapChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ void SwapChain::DestroyRenderPass()
if (!m_render_pass)
return;

g_command_buffer_mgr->DeferResourceDestruction(m_render_pass);
m_render_pass = nullptr;
vkDestroyRenderPass(g_vulkan_context->GetDevice(), m_render_pass, nullptr);
m_render_pass = VK_NULL_HANDLE;
}

bool SwapChain::CreateSwapChain()
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/VideoBackends/Vulkan/SwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ class SwapChain
};

void* m_native_handle = nullptr;
VkSurfaceKHR m_surface = nullptr;
VkSurfaceKHR m_surface = VK_NULL_HANDLE;
VkSurfaceFormatKHR m_surface_format = {};
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;
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_height = 0;
Expand Down
10 changes: 6 additions & 4 deletions Source/Core/VideoBackends/Vulkan/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ Texture2D::Texture2D(u32 width, u32 height, u32 levels, u32 layers, VkFormat for

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 (m_device_memory != VK_NULL_HANDLE)
{
g_command_buffer_mgr->DeferResourceDestruction(m_image);
g_command_buffer_mgr->DeferResourceDestruction(m_device_memory);
g_command_buffer_mgr->DeferImageDestruction(m_image);
g_command_buffer_mgr->DeferDeviceMemoryDestruction(m_device_memory);
}
}

Expand Down Expand Up @@ -134,6 +134,8 @@ std::unique_ptr<Texture2D> Texture2D::CreateFromExistingImage(u32 width, u32 hei
static_cast<VkImageAspectFlags>(VK_IMAGE_ASPECT_COLOR_BIT),
0, levels, 0, layers}};

// Memory is managed by the owner of the image.
VkDeviceMemory memory = VK_NULL_HANDLE;
VkImageView view = VK_NULL_HANDLE;
VkResult res = vkCreateImageView(g_vulkan_context->GetDevice(), &view_info, nullptr, &view);
if (res != VK_SUCCESS)
Expand All @@ -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,
existing_image, nullptr, view);
existing_image, memory, view);
}

void Texture2D::OverrideImageLayout(VkImageLayout new_layout)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/VideoBackends/Vulkan/TextureCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ TextureCache::TCacheEntry::~TCacheEntry()
m_parent->m_state_tracker->UnbindTexture(m_texture->GetView());

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,
Expand Down
Loading

0 comments on commit 1286c30

Please sign in to comment.