HostDisplay: Use streaming for sw renderer display

This commit is contained in:
Connor McLaughlin
2022-09-11 01:54:01 +10:00
parent c27026aed5
commit 0b3461338c
16 changed files with 495 additions and 541 deletions

View File

@ -23,6 +23,16 @@ Texture::~Texture()
Destroy();
}
bool Texture::UseTextureStorage(bool multisampled)
{
return GLAD_GL_ARB_texture_storage || (multisampled ? GLAD_GL_ES_VERSION_3_1 : GLAD_GL_ES_VERSION_3_0);
}
bool Texture::UseTextureStorage() const
{
return UseTextureStorage(IsMultisampled());
}
bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format, GLenum format, GLenum type,
const void* data, bool linear_filter, bool wrap)
{
@ -37,17 +47,23 @@ bool Texture::Create(u32 width, u32 height, u32 samples, GLenum internal_format,
if (samples > 1)
{
Assert(!data);
if (GLAD_GL_ARB_texture_storage || GLAD_GL_ES_VERSION_3_1)
if (UseTextureStorage(true))
glTexStorage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
else
glTexImage2DMultisample(target, samples, internal_format, width, height, GL_FALSE);
}
else
{
if ((GLAD_GL_ARB_texture_storage || GLAD_GL_ES_VERSION_3_0) && !data)
if (UseTextureStorage(false))
{
glTexStorage2D(target, 1, internal_format, width, height);
if (data)
glTexSubImage2D(target, 0, 0, 0, width, height, format, type, data);
}
else
{
glTexImage2D(target, 0, internal_format, width, height, 0, format, type, data);
}
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, linear_filter ? GL_LINEAR : GL_NEAREST);

View File

@ -10,6 +10,8 @@ public:
Texture(Texture&& moved);
~Texture();
static bool UseTextureStorage(bool multisampled);
bool Create(u32 width, u32 height, u32 samples, GLenum internal_format, GLenum format, GLenum type,
const void* data = nullptr, bool linear_filter = false, bool wrap = false);
void Replace(u32 width, u32 height, GLenum internal_format, GLenum format, GLenum type, const void* data);
@ -17,6 +19,7 @@ public:
void Destroy();
bool UseTextureStorage() const;
void SetLinearFilter(bool enabled);
bool IsValid() const { return m_id != 0; }

View File

@ -19,8 +19,13 @@ std::unique_ptr<Vulkan::Context> g_vulkan_context;
namespace Vulkan {
enum : u32
{
TEXTURE_BUFFER_SIZE = 16 * 1024 * 1024,
};
Context::Context(VkInstance instance, VkPhysicalDevice physical_device, bool owns_device)
: m_instance(instance), m_physical_device(physical_device), m_owns_device(owns_device)
: m_instance(instance), m_physical_device(physical_device)
{
// Read device physical memory properties, we need it for allocating buffers
vkGetPhysicalDeviceProperties(physical_device, &m_device_properties);
@ -37,29 +42,7 @@ Context::Context(VkInstance instance, VkPhysicalDevice physical_device, bool own
std::max(m_device_properties.limits.optimalBufferCopyRowPitchAlignment, static_cast<VkDeviceSize>(1));
}
Context::~Context()
{
StopPresentThread();
if (m_device != VK_NULL_HANDLE)
WaitForGPUIdle();
DestroyRenderPassCache();
DestroyGlobalDescriptorPool();
DestroyCommandBuffers();
if (m_owns_device && m_device != VK_NULL_HANDLE)
vkDestroyDevice(m_device, nullptr);
if (m_debug_messenger_callback != VK_NULL_HANDLE)
DisableDebugUtils();
if (m_owns_device)
{
vkDestroyInstance(m_instance, nullptr);
Vulkan::UnloadVulkanLibrary();
}
}
Context::~Context() = default;
bool Context::CheckValidationLayerAvailablility()
{
@ -369,6 +352,7 @@ bool Context::Create(std::string_view gpu_name, const WindowInfo* wi, std::uniqu
// Attempt to create the device.
if (!g_vulkan_context->CreateDevice(surface, enable_validation_layer, nullptr, 0, nullptr, 0, nullptr) ||
!g_vulkan_context->CreateGlobalDescriptorPool() || !g_vulkan_context->CreateCommandBuffers() ||
!g_vulkan_context->CreateTextureStreamBuffer() ||
(enable_surface && (*out_swap_chain = SwapChain::Create(wi_copy, surface, true)) == nullptr))
{
// Since we are destroying the instance, we're also responsible for destroying the surface.
@ -415,6 +399,29 @@ bool Context::CreateFromExistingInstance(VkInstance instance, VkPhysicalDevice g
void Context::Destroy()
{
AssertMsg(g_vulkan_context, "Has context");
g_vulkan_context->StopPresentThread();
if (g_vulkan_context->m_device != VK_NULL_HANDLE)
g_vulkan_context->WaitForGPUIdle();
g_vulkan_context->m_texture_upload_buffer.Destroy(false);
g_vulkan_context->DestroyRenderPassCache();
g_vulkan_context->DestroyGlobalDescriptorPool();
g_vulkan_context->DestroyCommandBuffers();
if (g_vulkan_context->m_device != VK_NULL_HANDLE)
vkDestroyDevice(g_vulkan_context->m_device, nullptr);
if (g_vulkan_context->m_debug_messenger_callback != VK_NULL_HANDLE)
g_vulkan_context->DisableDebugUtils();
if (g_vulkan_context->m_instance != VK_NULL_HANDLE)
vkDestroyInstance(g_vulkan_context->m_instance, nullptr);
Vulkan::UnloadVulkanLibrary();
g_vulkan_context.reset();
}
@ -785,6 +792,17 @@ void Context::DestroyGlobalDescriptorPool()
}
}
bool Context::CreateTextureStreamBuffer()
{
if (!m_texture_upload_buffer.Create(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, TEXTURE_BUFFER_SIZE))
{
Log_ErrorPrintf("Failed to allocate texture upload buffer");
return false;
}
return true;
}
void Context::DestroyRenderPassCache()
{
for (auto& it : m_render_pass_cache)
@ -1105,7 +1123,8 @@ void Context::ActivateCommandBuffer(u32 index)
{
const double ns_diff =
(timestamps[1] - timestamps[0]) * static_cast<double>(m_device_properties.limits.timestampPeriod);
m_accumulated_gpu_time = static_cast<float>(static_cast<double>(m_accumulated_gpu_time) + (ns_diff / 1000000.0));
m_accumulated_gpu_time =
static_cast<float>(static_cast<double>(m_accumulated_gpu_time) + (ns_diff / 1000000.0));
}
}
else

View File

@ -7,6 +7,7 @@
#include "../types.h"
#include "loader.h"
#include "stream_buffer.h"
#include <array>
#include <atomic>
#include <condition_variable>
@ -91,22 +92,31 @@ public:
ALWAYS_INLINE bool SupportsDualSourceBlend() const { return m_device_features.dualSrcBlend == VK_TRUE; }
// Helpers for getting constants
ALWAYS_INLINE VkDeviceSize GetUniformBufferAlignment() const
ALWAYS_INLINE u32 GetUniformBufferAlignment() const
{
return m_device_properties.limits.minUniformBufferOffsetAlignment;
return static_cast<u32>(m_device_properties.limits.minUniformBufferOffsetAlignment);
}
ALWAYS_INLINE VkDeviceSize GetTexelBufferAlignment() const
ALWAYS_INLINE u32 GetTexelBufferAlignment() const
{
return m_device_properties.limits.minTexelBufferOffsetAlignment;
return static_cast<u32>(m_device_properties.limits.minTexelBufferOffsetAlignment);
}
ALWAYS_INLINE VkDeviceSize GetStorageBufferAlignment() const
ALWAYS_INLINE u32 GetStorageBufferAlignment() const
{
return m_device_properties.limits.minStorageBufferOffsetAlignment;
return static_cast<u32>(m_device_properties.limits.minStorageBufferOffsetAlignment);
}
ALWAYS_INLINE VkDeviceSize GetBufferImageGranularity() const
ALWAYS_INLINE u32 GetBufferImageGranularity() const
{
return m_device_properties.limits.bufferImageGranularity;
return static_cast<u32>(m_device_properties.limits.bufferImageGranularity);
}
ALWAYS_INLINE u32 GetBufferCopyOffsetAlignment() const
{
return static_cast<u32>(m_device_properties.limits.optimalBufferCopyOffsetAlignment);
}
ALWAYS_INLINE u32 GetBufferCopyRowPitchAlignment() const
{
return static_cast<u32>(m_device_properties.limits.optimalBufferCopyRowPitchAlignment);
}
ALWAYS_INLINE u32 GetMaxImageDimension2D() const { return m_device_properties.limits.maxImageDimension2D; }
// Finds a memory type index for the specified memory properties and the bits returned by
// vkGetImageMemoryRequirements
@ -125,6 +135,7 @@ public:
// is submitted, after that you should call these functions again.
ALWAYS_INLINE VkDescriptorPool GetGlobalDescriptorPool() const { return m_global_descriptor_pool; }
ALWAYS_INLINE VkCommandBuffer GetCurrentCommandBuffer() const { return m_current_command_buffer; }
ALWAYS_INLINE StreamBuffer& GetTextureUploadBuffer() { return m_texture_upload_buffer; }
ALWAYS_INLINE VkDescriptorPool GetCurrentDescriptorPool() const
{
return m_frame_resources[m_current_frame].descriptor_pool;
@ -198,6 +209,7 @@ private:
void DestroyCommandBuffers();
bool CreateGlobalDescriptorPool();
void DestroyGlobalDescriptorPool();
bool CreateTextureStreamBuffer();
void DestroyRenderPassCache();
void ActivateCommandBuffer(u32 index);
@ -247,7 +259,7 @@ private:
u64 m_completed_fence_counter = 0;
u32 m_current_frame;
bool m_owns_device = false;
StreamBuffer m_texture_upload_buffer;
std::atomic_bool m_last_present_failed{false};
std::atomic_bool m_present_done{true};