Skip to content

Commit

Permalink
HostDisplay: Use streaming for sw renderer display
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Sep 13, 2022
1 parent c27026a commit 0b34613
Show file tree
Hide file tree
Showing 16 changed files with 495 additions and 541 deletions.
20 changes: 18 additions & 2 deletions src/common/gl/texture.cpp
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/common/gl/texture.h
Expand Up @@ -10,13 +10,16 @@ class Texture
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);
bool CreateFramebuffer();

void Destroy();

bool UseTextureStorage() const;
void SetLinearFilter(bool enabled);

bool IsValid() const { return m_id != 0; }
Expand Down
69 changes: 44 additions & 25 deletions src/common/vulkan/context.cpp
Expand Up @@ -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);
Expand All @@ -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()
{
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
30 changes: 21 additions & 9 deletions src/common/vulkan/context.h
Expand Up @@ -7,6 +7,7 @@

#include "../types.h"
#include "loader.h"
#include "stream_buffer.h"
#include <array>
#include <atomic>
#include <condition_variable>
Expand Down Expand Up @@ -91,22 +92,31 @@ class Context
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
Expand All @@ -125,6 +135,7 @@ class Context
// 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;
Expand Down Expand Up @@ -198,6 +209,7 @@ class Context
void DestroyCommandBuffers();
bool CreateGlobalDescriptorPool();
void DestroyGlobalDescriptorPool();
bool CreateTextureStreamBuffer();
void DestroyRenderPassCache();

void ActivateCommandBuffer(u32 index);
Expand Down Expand Up @@ -247,7 +259,7 @@ class Context
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};
Expand Down
53 changes: 33 additions & 20 deletions src/core/gpu_sw.cpp
Expand Up @@ -103,6 +103,21 @@ void GPU_SW::UpdateSettings()
m_backend.UpdateSettings();
}

HostDisplayTexture* GPU_SW::GetDisplayTexture(u32 width, u32 height, HostDisplayPixelFormat format)
{
if (!m_display_texture || m_display_texture->GetWidth() != width || m_display_texture->GetHeight() != height ||
m_display_texture->GetFormat() != format)
{
g_host_display->ClearDisplayTexture();
m_display_texture.reset();
m_display_texture = g_host_display->CreateTexture(width, height, 1, 1, 1, format, nullptr, 0, true);
if (!m_display_texture)
Log_ErrorPrintf("Failed to create %ux%u %u texture", width, height, static_cast<u32>(format));
}

return m_display_texture.get();
}

template<HostDisplayPixelFormat out_format, typename out_type>
static void CopyOutRow16(const u16* src_ptr, out_type* dst_ptr, u32 width);

Expand Down Expand Up @@ -240,13 +255,14 @@ void GPU_SW::CopyOut15Bit(u32 src_x, u32 src_y, u32 width, u32 height, u32 field
using OutputPixelType = std::conditional_t<
display_format == HostDisplayPixelFormat::RGBA8 || display_format == HostDisplayPixelFormat::BGRA8, u32, u16>;

HostDisplayTexture* texture = GetDisplayTexture(width, height, display_format);
if (!texture)
return;

if (!interlaced)
{
if (!g_host_display->BeginSetDisplayPixels(display_format, width, height, reinterpret_cast<void**>(&dst_ptr),
&dst_stride))
{
if (!texture->BeginUpdate(width, height, reinterpret_cast<void**>(&dst_ptr), &dst_stride))
return;
}
}
else
{
Expand Down Expand Up @@ -293,13 +309,11 @@ void GPU_SW::CopyOut15Bit(u32 src_x, u32 src_y, u32 width, u32 height, u32 field
}

if (!interlaced)
{
g_host_display->EndSetDisplayPixels();
}
texture->EndUpdate(0, 0, width, height);
else
{
g_host_display->SetDisplayPixels(display_format, width, height, m_display_texture_buffer.data(), output_stride);
}
texture->Update(0, 0, width, height, m_display_texture_buffer.data(), output_stride);

g_host_display->SetDisplayTexture(texture->GetHandle(), display_format, width, height, 0, 0, width, height);
}

void GPU_SW::CopyOut15Bit(HostDisplayPixelFormat display_format, u32 src_x, u32 src_y, u32 width, u32 height, u32 field,
Expand Down Expand Up @@ -334,13 +348,14 @@ void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 heigh
using OutputPixelType = std::conditional_t<
display_format == HostDisplayPixelFormat::RGBA8 || display_format == HostDisplayPixelFormat::BGRA8, u32, u16>;

HostDisplayTexture* texture = GetDisplayTexture(width, height, display_format);
if (!texture)
return;

if (!interlaced)
{
if (!g_host_display->BeginSetDisplayPixels(display_format, width, height, reinterpret_cast<void**>(&dst_ptr),
&dst_stride))
{
if (!texture->BeginUpdate(width, height, reinterpret_cast<void**>(&dst_ptr), &dst_stride))
return;
}
}
else
{
Expand Down Expand Up @@ -451,13 +466,11 @@ void GPU_SW::CopyOut24Bit(u32 src_x, u32 src_y, u32 skip_x, u32 width, u32 heigh
}

if (!interlaced)
{
g_host_display->EndSetDisplayPixels();
}
texture->EndUpdate(0, 0, width, height);
else
{
g_host_display->SetDisplayPixels(display_format, width, height, m_display_texture_buffer.data(), output_stride);
}
texture->Update(0, 0, width, height, m_display_texture_buffer.data(), output_stride);

g_host_display->SetDisplayTexture(texture->GetHandle(), display_format, width, height, 0, 0, width, height);
}

void GPU_SW::CopyOut24Bit(HostDisplayPixelFormat display_format, u32 src_x, u32 src_y, u32 skip_x, u32 width,
Expand Down
3 changes: 3 additions & 0 deletions src/core/gpu_sw.h
Expand Up @@ -55,9 +55,12 @@ class GPU_SW final : public GPU
void FillBackendCommandParameters(GPUBackendCommand* cmd) const;
void FillDrawCommand(GPUBackendDrawCommand* cmd, GPURenderCommand rc) const;

HostDisplayTexture* GetDisplayTexture(u32 width, u32 height, HostDisplayPixelFormat format);

HeapArray<u8, GPU_MAX_DISPLAY_WIDTH * GPU_MAX_DISPLAY_HEIGHT * sizeof(u32)> m_display_texture_buffer;
HostDisplayPixelFormat m_16bit_display_format = HostDisplayPixelFormat::RGB565;
HostDisplayPixelFormat m_24bit_display_format = HostDisplayPixelFormat::RGBA8;
std::unique_ptr<HostDisplayTexture> m_display_texture;

GPU_SW_Backend m_backend;
};

0 comments on commit 0b34613

Please sign in to comment.