mirror of https://github.com/auygun/kaliber.git
Cleanup
This commit is contained in:
parent
bb4c6032fb
commit
3a5572c8d0
|
@ -335,7 +335,8 @@ bool RendererOpenGL::StartRenderThread() {
|
||||||
|
|
||||||
void RendererOpenGL::TerminateRenderThread() {
|
void RendererOpenGL::TerminateRenderThread() {
|
||||||
#ifdef THREADED_RENDERING
|
#ifdef THREADED_RENDERING
|
||||||
DCHECK(!terminate_render_thread_);
|
if (terminate_render_thread_)
|
||||||
|
return;
|
||||||
|
|
||||||
// Notify worker thread and wait for it to terminate.
|
// Notify worker thread and wait for it to terminate.
|
||||||
{
|
{
|
||||||
|
|
|
@ -15,11 +15,7 @@ bool RendererOpenGL::Initialize(ANativeWindow* window) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererOpenGL::Shutdown() {
|
void RendererOpenGL::Shutdown() {
|
||||||
if (terminate_render_thread_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
LOG << "Shutting down renderer.";
|
LOG << "Shutting down renderer.";
|
||||||
|
|
||||||
TerminateRenderThread();
|
TerminateRenderThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
#include "renderer_vulkan.h"
|
#include "renderer_vulkan.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <unordered_set>
|
|
||||||
|
|
||||||
#include "../../../base/log.h"
|
#include "../../../base/log.h"
|
||||||
#include "../../../base/vecmath.h"
|
#include "../../../base/vecmath.h"
|
||||||
|
@ -786,20 +784,27 @@ bool RendererVulkan::InitializeInternal() {
|
||||||
|
|
||||||
// Use a background thread for filling up staging buffers and recording setup
|
// Use a background thread for filling up staging buffers and recording setup
|
||||||
// commands.
|
// commands.
|
||||||
|
quit_.store(false, std::memory_order_relaxed);
|
||||||
setup_thread_ =
|
setup_thread_ =
|
||||||
std::thread(&RendererVulkan::SetupThreadMain, this, frame_count);
|
std::thread(&RendererVulkan::SetupThreadMain, this, frame_count);
|
||||||
|
|
||||||
|
if (context_lost_cb_) {
|
||||||
|
LOG << "Context lost.";
|
||||||
|
context_lost_cb_();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererVulkan::Shutdown() {
|
void RendererVulkan::Shutdown() {
|
||||||
LOG << "Shutting down renderer.";
|
LOG << "Shutting down renderer.";
|
||||||
vkDeviceWaitIdle(device_);
|
InvalidateAllResources();
|
||||||
|
|
||||||
quit_.store(true, std::memory_order_relaxed);
|
quit_.store(true, std::memory_order_relaxed);
|
||||||
semaphore_.Release();
|
semaphore_.Release();
|
||||||
setup_thread_.join();
|
setup_thread_.join();
|
||||||
|
|
||||||
|
vkDeviceWaitIdle(device_);
|
||||||
|
|
||||||
for (int i = 0; i < frames_.size(); ++i) {
|
for (int i = 0; i < frames_.size(); ++i) {
|
||||||
FreePendingResources(i);
|
FreePendingResources(i);
|
||||||
vkDestroyCommandPool(device_, frames_[i].setup_command_pool, nullptr);
|
vkDestroyCommandPool(device_, frames_[i].setup_command_pool, nullptr);
|
||||||
|
@ -812,6 +817,14 @@ void RendererVulkan::Shutdown() {
|
||||||
vkDestroySampler(device_, sampler_, nullptr);
|
vkDestroySampler(device_, sampler_, nullptr);
|
||||||
|
|
||||||
device_ = VK_NULL_HANDLE;
|
device_ = VK_NULL_HANDLE;
|
||||||
|
frames_drawn_ = 0;
|
||||||
|
frames_.clear();
|
||||||
|
current_frame_ = 0;
|
||||||
|
|
||||||
|
staging_buffers_.clear();
|
||||||
|
current_staging_buffer_ = 0;
|
||||||
|
staging_buffer_used_ = false;
|
||||||
|
|
||||||
context_.DestroyWindow();
|
context_.DestroyWindow();
|
||||||
context_.Shutdown();
|
context_.Shutdown();
|
||||||
|
|
||||||
|
@ -995,8 +1008,8 @@ bool RendererVulkan::AllocateStagingBuffer(uint32_t amount,
|
||||||
staging_buffers_[current_staging_buffer_].frame_used =
|
staging_buffers_[current_staging_buffer_].frame_used =
|
||||||
frames_drawn_;
|
frames_drawn_;
|
||||||
} else {
|
} else {
|
||||||
// Worst case scenario, all the staging buffers belong to this frame
|
// All the staging buffers belong to this frame and we can't create
|
||||||
// and this frame is not even done. Flush everything.
|
// more. Flush setup buffer to make room.
|
||||||
FlushSetupBuffer();
|
FlushSetupBuffer();
|
||||||
|
|
||||||
// Clear the whole staging buffer.
|
// Clear the whole staging buffer.
|
||||||
|
@ -1205,12 +1218,12 @@ void RendererVulkan::UpdateBuffer(VkBuffer buffer,
|
||||||
size_t submit_from = 0;
|
size_t submit_from = 0;
|
||||||
|
|
||||||
while (to_submit > 0) {
|
while (to_submit > 0) {
|
||||||
uint32_t block_write_offset;
|
uint32_t write_offset;
|
||||||
uint32_t block_write_amount;
|
uint32_t write_amount;
|
||||||
|
|
||||||
if (!AllocateStagingBuffer(
|
if (!AllocateStagingBuffer(
|
||||||
std::min((uint32_t)to_submit, staging_buffer_size_), 32,
|
std::min((uint32_t)to_submit, staging_buffer_size_), 32,
|
||||||
block_write_offset, block_write_amount))
|
write_offset, write_amount))
|
||||||
return;
|
return;
|
||||||
Buffer<VkBuffer> staging_buffer =
|
Buffer<VkBuffer> staging_buffer =
|
||||||
staging_buffers_[current_staging_buffer_].buffer;
|
staging_buffers_[current_staging_buffer_].buffer;
|
||||||
|
@ -1218,20 +1231,20 @@ void RendererVulkan::UpdateBuffer(VkBuffer buffer,
|
||||||
// Copy to staging buffer.
|
// Copy to staging buffer.
|
||||||
void* data_ptr =
|
void* data_ptr =
|
||||||
staging_buffers_[current_staging_buffer_].alloc_info.pMappedData;
|
staging_buffers_[current_staging_buffer_].alloc_info.pMappedData;
|
||||||
memcpy(((uint8_t*)data_ptr) + block_write_offset, (char*)data + submit_from,
|
memcpy(((uint8_t*)data_ptr) + write_offset, (char*)data + submit_from,
|
||||||
block_write_amount);
|
write_amount);
|
||||||
|
|
||||||
// Insert a command to copy to GPU buffer.
|
// Insert a command to copy to GPU buffer.
|
||||||
VkBufferCopy region;
|
VkBufferCopy region;
|
||||||
region.srcOffset = block_write_offset;
|
region.srcOffset = write_offset;
|
||||||
region.dstOffset = submit_from + offset;
|
region.dstOffset = submit_from + offset;
|
||||||
region.size = block_write_amount;
|
region.size = write_amount;
|
||||||
|
|
||||||
vkCmdCopyBuffer(frames_[current_frame_].setup_command_buffer,
|
vkCmdCopyBuffer(frames_[current_frame_].setup_command_buffer,
|
||||||
std::get<0>(staging_buffer), buffer, 1, ®ion);
|
std::get<0>(staging_buffer), buffer, 1, ®ion);
|
||||||
|
|
||||||
to_submit -= block_write_amount;
|
to_submit -= write_amount;
|
||||||
submit_from += block_write_amount;
|
submit_from += write_amount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1405,10 +1418,10 @@ void RendererVulkan::UpdateImage(VkImage image,
|
||||||
DCHECK(staging_buffer_size_ >= segment);
|
DCHECK(staging_buffer_size_ >= segment);
|
||||||
|
|
||||||
while (to_submit > 0) {
|
while (to_submit > 0) {
|
||||||
uint32_t block_write_offset;
|
uint32_t write_offset;
|
||||||
uint32_t block_write_amount;
|
uint32_t write_amount;
|
||||||
if (!AllocateStagingBuffer(std::min((uint32_t)to_submit, max_size), segment,
|
if (!AllocateStagingBuffer(std::min((uint32_t)to_submit, max_size), segment,
|
||||||
block_write_offset, block_write_amount))
|
write_offset, write_amount))
|
||||||
return;
|
return;
|
||||||
Buffer<VkBuffer> staging_buffer =
|
Buffer<VkBuffer> staging_buffer =
|
||||||
staging_buffers_[current_staging_buffer_].buffer;
|
staging_buffers_[current_staging_buffer_].buffer;
|
||||||
|
@ -1416,14 +1429,14 @@ void RendererVulkan::UpdateImage(VkImage image,
|
||||||
// Copy to staging buffer.
|
// Copy to staging buffer.
|
||||||
void* data_ptr =
|
void* data_ptr =
|
||||||
staging_buffers_[current_staging_buffer_].alloc_info.pMappedData;
|
staging_buffers_[current_staging_buffer_].alloc_info.pMappedData;
|
||||||
memcpy(((uint8_t*)data_ptr) + block_write_offset, (char*)data + submit_from,
|
memcpy(((uint8_t*)data_ptr) + write_offset, (char*)data + submit_from,
|
||||||
block_write_amount);
|
write_amount);
|
||||||
|
|
||||||
uint32_t region_height = (block_write_amount / segment) * block_height;
|
uint32_t region_height = (write_amount / segment) * block_height;
|
||||||
|
|
||||||
// Insert a command to copy to GPU buffer.
|
// Insert a command to copy to GPU buffer.
|
||||||
VkBufferImageCopy buffer_image_copy;
|
VkBufferImageCopy buffer_image_copy;
|
||||||
buffer_image_copy.bufferOffset = block_write_offset;
|
buffer_image_copy.bufferOffset = write_offset;
|
||||||
buffer_image_copy.bufferRowLength = 0;
|
buffer_image_copy.bufferRowLength = 0;
|
||||||
buffer_image_copy.bufferImageHeight = 0;
|
buffer_image_copy.bufferImageHeight = 0;
|
||||||
buffer_image_copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
buffer_image_copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||||
|
@ -1442,8 +1455,8 @@ void RendererVulkan::UpdateImage(VkImage image,
|
||||||
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1,
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1,
|
||||||
&buffer_image_copy);
|
&buffer_image_copy);
|
||||||
|
|
||||||
to_submit -= block_write_amount;
|
to_submit -= write_amount;
|
||||||
submit_from += block_write_amount;
|
submit_from += write_amount;
|
||||||
region_offset_y += region_height;
|
region_offset_y += region_height;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1826,12 +1839,6 @@ bool RendererVulkan::IsFormatSupported(VkFormat format) {
|
||||||
return properties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
|
return properties.optimalTilingFeatures & VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void RendererVulkan::ContextLost() {
|
|
||||||
LOG << "Context lost.";
|
|
||||||
InvalidateAllResources();
|
|
||||||
context_lost_cb_();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<RenderResource> RendererVulkan::CreateResource(
|
std::unique_ptr<RenderResource> RendererVulkan::CreateResource(
|
||||||
RenderResourceFactoryBase& factory) {
|
RenderResourceFactoryBase& factory) {
|
||||||
static unsigned last_id = 0;
|
static unsigned last_id = 0;
|
||||||
|
|
|
@ -184,13 +184,6 @@ class RendererVulkan : public Renderer {
|
||||||
base::Semaphore semaphore_;
|
base::Semaphore semaphore_;
|
||||||
std::atomic<bool> quit_{false};
|
std::atomic<bool> quit_{false};
|
||||||
|
|
||||||
#if defined(__ANDROID__)
|
|
||||||
ANativeWindow* window_;
|
|
||||||
#elif defined(__linux__)
|
|
||||||
Display* display_ = NULL;
|
|
||||||
Window window_ = 0;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool InitializeInternal();
|
bool InitializeInternal();
|
||||||
|
|
||||||
void BeginFrame();
|
void BeginFrame();
|
||||||
|
@ -269,8 +262,6 @@ class RendererVulkan : public Renderer {
|
||||||
|
|
||||||
bool IsFormatSupported(VkFormat format);
|
bool IsFormatSupported(VkFormat format);
|
||||||
|
|
||||||
void ContextLost();
|
|
||||||
|
|
||||||
void InvalidateAllResources();
|
void InvalidateAllResources();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,8 @@ namespace eng {
|
||||||
bool RendererVulkan::Initialize(Display* display, Window window) {
|
bool RendererVulkan::Initialize(Display* display, Window window) {
|
||||||
LOG << "Initializing renderer.";
|
LOG << "Initializing renderer.";
|
||||||
|
|
||||||
display_ = display;
|
|
||||||
window_ = window;
|
|
||||||
|
|
||||||
XWindowAttributes xwa;
|
XWindowAttributes xwa;
|
||||||
XGetWindowAttributes(display_, window_, &xwa);
|
XGetWindowAttributes(display, window, &xwa);
|
||||||
screen_width_ = xwa.width;
|
screen_width_ = xwa.width;
|
||||||
screen_height_ = xwa.height;
|
screen_height_ = xwa.height;
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,12 @@ void VulkanContext::Shutdown() {
|
||||||
vkDestroyDevice(device_, nullptr);
|
vkDestroyDevice(device_, nullptr);
|
||||||
device_ = VK_NULL_HANDLE;
|
device_ = VK_NULL_HANDLE;
|
||||||
}
|
}
|
||||||
|
buffers_prepared_ = false;
|
||||||
|
queues_initialized_ = false;
|
||||||
|
separate_present_queue_ = false;
|
||||||
|
swapchain_image_count_ = 0;
|
||||||
|
command_buffers_.clear();
|
||||||
|
window_ = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::DebugMessengerCallback(
|
VKAPI_ATTR VkBool32 VKAPI_CALL VulkanContext::DebugMessengerCallback(
|
||||||
|
@ -392,23 +398,25 @@ bool VulkanContext::CreatePhysicalDevice() {
|
||||||
|
|
||||||
uint32_t gpu_count;
|
uint32_t gpu_count;
|
||||||
|
|
||||||
VkResult err = vkCreateInstance(&inst_info, nullptr, &instance_);
|
if (instance_ == VK_NULL_HANDLE) {
|
||||||
if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
|
VkResult err = vkCreateInstance(&inst_info, nullptr, &instance_);
|
||||||
DLOG << "Cannot find a compatible Vulkan installable client driver (ICD).";
|
if (err == VK_ERROR_INCOMPATIBLE_DRIVER) {
|
||||||
return false;
|
DLOG << "Cannot find a compatible Vulkan installable client driver (ICD).";
|
||||||
}
|
return false;
|
||||||
if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
|
}
|
||||||
DLOG << "Cannot find a specified extension library. Make sure your layers "
|
if (err == VK_ERROR_EXTENSION_NOT_PRESENT) {
|
||||||
"path is set appropriately. ";
|
DLOG << "Cannot find a specified extension library. Make sure your layers "
|
||||||
return false;
|
"path is set appropriately. ";
|
||||||
}
|
return false;
|
||||||
if (err) {
|
}
|
||||||
DLOG << "vkCreateInstance failed. Error: " << err;
|
if (err) {
|
||||||
return false;
|
DLOG << "vkCreateInstance failed. Error: " << err;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make initial call to query gpu_count.
|
// Make initial call to query gpu_count.
|
||||||
err = vkEnumeratePhysicalDevices(instance_, &gpu_count, nullptr);
|
VkResult err = vkEnumeratePhysicalDevices(instance_, &gpu_count, nullptr);
|
||||||
if (err) {
|
if (err) {
|
||||||
DLOG << "vkEnumeratePhysicalDevices failed. Error: " << err;
|
DLOG << "vkEnumeratePhysicalDevices failed. Error: " << err;
|
||||||
return false;
|
return false;
|
||||||
|
@ -919,7 +927,7 @@ bool VulkanContext::UpdateSwapChain(Window* window) {
|
||||||
// between VSync signals, which is 16.6ms at a rate of 60 fps. The rendered
|
// between VSync signals, which is 16.6ms at a rate of 60 fps. The rendered
|
||||||
// image is presented to the swapchain, and the previously presented one is
|
// image is presented to the swapchain, and the previously presented one is
|
||||||
// made available to the application again. If the GPU cannot process frames
|
// made available to the application again. If the GPU cannot process frames
|
||||||
// fast enough, VSync will be missed and the application wait have to wait for
|
// fast enough, VSync will be missed and the application will have to wait for
|
||||||
// another whole VSync cycle, which caps framerate at 30 fps. This may be ok,
|
// another whole VSync cycle, which caps framerate at 30 fps. This may be ok,
|
||||||
// but triple buffering can deliver higher framerate.
|
// but triple buffering can deliver higher framerate.
|
||||||
uint32_t desired_num_of_swapchain_images = 3;
|
uint32_t desired_num_of_swapchain_images = 3;
|
||||||
|
|
Loading…
Reference in New Issue