Renderer: Add backbuffer format to base class

This commit is contained in:
Stenzek 2017-09-09 16:09:24 +10:00
parent 1adcd47dcb
commit 2644e920cc
10 changed files with 75 additions and 46 deletions

View File

@ -63,7 +63,7 @@ typedef struct _Nv_Stereo_Image_Header
#define NVSTEREO_IMAGE_SIGNATURE 0x4433564e
Renderer::Renderer(int backbuffer_width, int backbuffer_height)
: ::Renderer(backbuffer_width, backbuffer_height)
: ::Renderer(backbuffer_width, backbuffer_height, AbstractTextureFormat::RGBA8)
{
m_last_multisamples = g_ActiveConfig.iMultisamples;
m_last_stereo_mode = g_ActiveConfig.stereo_mode != StereoMode::Off;

View File

@ -14,7 +14,7 @@
namespace Null
{
// Init functions
Renderer::Renderer() : ::Renderer(1, 1)
Renderer::Renderer() : ::Renderer(1, 1, AbstractTextureFormat::RGBA8)
{
UpdateActiveConfig();
}

View File

@ -355,7 +355,8 @@ static void InitDriverInfo()
// Init functions
Renderer::Renderer(std::unique_ptr<GLContext> main_gl_context)
: ::Renderer(static_cast<int>(std::max(main_gl_context->GetBackBufferWidth(), 1u)),
static_cast<int>(std::max(main_gl_context->GetBackBufferHeight(), 1u))),
static_cast<int>(std::max(main_gl_context->GetBackBufferHeight(), 1u)),
AbstractTextureFormat::RGBA8),
m_main_gl_context(std::move(main_gl_context))
{
bool bSuccess = true;

View File

@ -25,7 +25,8 @@
#include "VideoCommon/VideoConfig.h"
SWRenderer::SWRenderer(std::unique_ptr<SWOGLWindow> window)
: ::Renderer(static_cast<int>(MAX_XFB_WIDTH), static_cast<int>(MAX_XFB_HEIGHT)),
: ::Renderer(static_cast<int>(MAX_XFB_WIDTH), static_cast<int>(MAX_XFB_HEIGHT),
AbstractTextureFormat::RGBA8),
m_window(std::move(window))
{
}

View File

@ -49,7 +49,8 @@ namespace Vulkan
{
Renderer::Renderer(std::unique_ptr<SwapChain> swap_chain)
: ::Renderer(swap_chain ? static_cast<int>(swap_chain->GetWidth()) : 1,
swap_chain ? static_cast<int>(swap_chain->GetHeight()) : 0),
swap_chain ? static_cast<int>(swap_chain->GetHeight()) : 0,
swap_chain ? swap_chain->GetTextureFormat() : AbstractTextureFormat::Undefined),
m_swap_chain(std::move(swap_chain))
{
UpdateActiveConfig();
@ -95,6 +96,23 @@ bool Renderer::Initialize()
return false;
}
// Swap chain render pass.
if (m_swap_chain)
{
m_swap_chain_render_pass =
g_object_cache->GetRenderPass(m_swap_chain->GetSurfaceFormat().format, VK_FORMAT_UNDEFINED,
1, VK_ATTACHMENT_LOAD_OP_LOAD);
m_swap_chain_clear_render_pass =
g_object_cache->GetRenderPass(m_swap_chain->GetSurfaceFormat().format, VK_FORMAT_UNDEFINED,
1, VK_ATTACHMENT_LOAD_OP_CLEAR);
if (m_swap_chain_render_pass == VK_NULL_HANDLE ||
m_swap_chain_clear_render_pass == VK_NULL_HANDLE)
{
PanicAlert("Failed to create swap chain render passes.");
return false;
}
}
m_bounding_box = std::make_unique<BoundingBox>();
if (!m_bounding_box->Initialize())
{
@ -398,7 +416,7 @@ void Renderer::RenderText(const std::string& text, int left, int top, u32 color)
u32 backbuffer_width = m_swap_chain->GetWidth();
u32 backbuffer_height = m_swap_chain->GetHeight();
m_raster_font->PrintMultiLineText(m_swap_chain->GetRenderPass(), text,
m_raster_font->PrintMultiLineText(m_swap_chain_render_pass, text,
left * 2.0f / static_cast<float>(backbuffer_width) - 1,
1 - top * 2.0f / static_cast<float>(backbuffer_height),
backbuffer_width, backbuffer_height, color);
@ -816,20 +834,18 @@ void Renderer::DrawScreen(VKTexture* xfb_texture, const EFBRectangle& xfb_region
m_current_framebuffer_width = backbuffer->GetWidth();
m_current_framebuffer_height = backbuffer->GetHeight();
// Draw to the backbuffer.
VkRect2D region = {{0, 0}, {backbuffer->GetWidth(), backbuffer->GetHeight()}};
StateTracker::GetInstance()->SetRenderPass(m_swap_chain_render_pass,
m_swap_chain_clear_render_pass);
StateTracker::GetInstance()->SetFramebuffer(m_swap_chain->GetCurrentFramebuffer(), region);
// Begin render pass for rendering to the swap chain.
VkClearValue clear_value = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
VkRenderPassBeginInfo info = {VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
nullptr,
m_swap_chain->GetRenderPass(),
m_swap_chain->GetCurrentFramebuffer(),
{{0, 0}, {backbuffer->GetWidth(), backbuffer->GetHeight()}},
1,
&clear_value};
vkCmdBeginRenderPass(g_command_buffer_mgr->GetCurrentCommandBuffer(), &info,
VK_SUBPASS_CONTENTS_INLINE);
StateTracker::GetInstance()->BeginClearRenderPass(region, &clear_value, 1);
// Draw
BlitScreen(m_swap_chain->GetRenderPass(), GetTargetRectangle(), xfb_region,
BlitScreen(m_swap_chain_render_pass, GetTargetRectangle(), xfb_region,
xfb_texture->GetRawTexIdentifier());
// Draw OSD
@ -840,7 +856,8 @@ void Renderer::DrawScreen(VKTexture* xfb_texture, const EFBRectangle& xfb_region
OSD::DrawMessages();
// End drawing to backbuffer
vkCmdEndRenderPass(g_command_buffer_mgr->GetCurrentCommandBuffer());
StateTracker::GetInstance()->EndRenderPass();
BindEFBToStateTracker();
// Transition the backbuffer to PRESENT_SRC to ensure all commands drawing
// to it have finished before present.

View File

@ -125,6 +125,8 @@ private:
VkSemaphore m_image_available_semaphore = VK_NULL_HANDLE;
VkSemaphore m_rendering_finished_semaphore = VK_NULL_HANDLE;
VkRenderPass m_swap_chain_render_pass = VK_NULL_HANDLE;
VkRenderPass m_swap_chain_clear_render_pass = VK_NULL_HANDLE;
std::unique_ptr<SwapChain> m_swap_chain;
std::unique_ptr<BoundingBox> m_bounding_box;

View File

@ -142,11 +142,8 @@ std::unique_ptr<SwapChain> SwapChain::Create(void* display_handle, void* native_
std::unique_ptr<SwapChain> swap_chain =
std::make_unique<SwapChain>(display_handle, native_handle, surface, vsync);
if (!swap_chain->CreateSwapChain() || !swap_chain->CreateRenderPass() ||
!swap_chain->SetupSwapChainImages())
{
if (!swap_chain->CreateSwapChain() || !swap_chain->SetupSwapChainImages())
return nullptr;
}
return swap_chain;
}
@ -175,13 +172,27 @@ bool SwapChain::SelectSurfaceFormat()
return true;
}
// Use the first surface format, just use what it prefers.
// Some drivers seem to return a SRGB format here (Intel Mesa).
// This results in gamma correction when presenting to the screen, which we don't want.
// Use a linear format instead, if this is the case.
m_surface_format.format = Util::GetLinearFormat(surface_formats[0].format);
m_surface_format.colorSpace = surface_formats[0].colorSpace;
return true;
// Try to find a suitable format.
for (const VkSurfaceFormatKHR& surface_format : surface_formats)
{
// Some drivers seem to return a SRGB format here (Intel Mesa).
// This results in gamma correction when presenting to the screen, which we don't want.
// Use a linear format instead, if this is the case.
VkFormat format = Util::GetLinearFormat(surface_format.format);
if (format == VK_FORMAT_R8G8B8A8_UNORM)
m_texture_format = AbstractTextureFormat::RGBA8;
else if (format == VK_FORMAT_B8G8R8A8_UNORM)
m_texture_format = AbstractTextureFormat::BGRA8;
else
continue;
m_surface_format.format = format;
m_surface_format.colorSpace = surface_format.colorSpace;
return true;
}
PanicAlert("Failed to find a suitable format for swap chain buffers.");
return false;
}
bool SwapChain::SelectPresentMode()
@ -236,14 +247,6 @@ bool SwapChain::SelectPresentMode()
return true;
}
bool SwapChain::CreateRenderPass()
{
// render pass for rendering to the swap chain
m_render_pass = g_object_cache->GetRenderPass(m_surface_format.format, VK_FORMAT_UNDEFINED, 1,
VK_ATTACHMENT_LOAD_OP_CLEAR);
return m_render_pass != VK_NULL_HANDLE;
}
bool SwapChain::CreateSwapChain()
{
// Look up surface properties to determine image count and dimensions
@ -367,6 +370,9 @@ bool SwapChain::SetupSwapChainImages()
images.data());
ASSERT(res == VK_SUCCESS);
VkRenderPass render_pass = g_object_cache->GetRenderPass(
m_surface_format.format, VK_FORMAT_UNDEFINED, 1, VK_ATTACHMENT_LOAD_OP_CLEAR);
m_swap_chain_images.reserve(image_count);
for (uint32_t i = 0; i < image_count; i++)
{
@ -382,7 +388,7 @@ bool SwapChain::SetupSwapChainImages()
VkFramebufferCreateInfo framebuffer_info = {VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
nullptr,
0,
m_render_pass,
render_pass,
1,
&view,
m_width,
@ -499,7 +505,7 @@ bool SwapChain::RecreateSurface(void* native_handle)
}
// Finally re-create the swap chain
if (!CreateSwapChain() || !SetupSwapChainImages() || !CreateRenderPass())
if (!CreateSwapChain() || !SetupSwapChainImages())
return false;
return true;

View File

@ -10,6 +10,7 @@
#include "Common/CommonTypes.h"
#include "VideoBackends/Vulkan/Constants.h"
#include "VideoBackends/Vulkan/Texture2D.h"
#include "VideoCommon/TextureConfig.h"
namespace Vulkan
{
@ -33,10 +34,10 @@ public:
void* GetNativeHandle() const { return m_native_handle; }
VkSurfaceKHR GetSurface() const { return m_surface; }
VkSurfaceFormatKHR GetSurfaceFormat() const { return m_surface_format; }
AbstractTextureFormat GetTextureFormat() const { return m_texture_format; }
bool IsVSyncEnabled() const { return m_vsync_enabled; }
bool IsStereoEnabled() const { return m_layers == 2; }
VkSwapchainKHR GetSwapChain() const { return m_swap_chain; }
VkRenderPass GetRenderPass() const { return m_render_pass; }
u32 GetWidth() const { return m_width; }
u32 GetHeight() const { return m_height; }
u32 GetCurrentImageIndex() const { return m_current_swap_chain_image_index; }
@ -69,8 +70,6 @@ private:
bool CreateSwapChain();
void DestroySwapChain();
bool CreateRenderPass();
bool SetupSwapChainImages();
void DestroySwapChainImages();
@ -88,14 +87,13 @@ private:
VkSurfaceKHR m_surface = VK_NULL_HANDLE;
VkSurfaceFormatKHR m_surface_format = {};
VkPresentModeKHR m_present_mode = VK_PRESENT_MODE_RANGE_SIZE_KHR;
AbstractTextureFormat m_texture_format = AbstractTextureFormat::Undefined;
bool m_vsync_enabled;
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 = VK_NULL_HANDLE;
u32 m_width = 0;
u32 m_height = 0;
u32 m_layers = 0;

View File

@ -78,8 +78,10 @@ static float AspectToWidescreen(float aspect)
return aspect * ((16.0f / 9.0f) / (4.0f / 3.0f));
}
Renderer::Renderer(int backbuffer_width, int backbuffer_height)
: m_backbuffer_width(backbuffer_width), m_backbuffer_height(backbuffer_height)
Renderer::Renderer(int backbuffer_width, int backbuffer_height,
AbstractTextureFormat backbuffer_format)
: m_backbuffer_width(backbuffer_width), m_backbuffer_height(backbuffer_height),
m_backbuffer_format(backbuffer_format)
{
UpdateActiveConfig();
UpdateDrawRectangle();

View File

@ -32,6 +32,7 @@
#include "VideoCommon/BPMemory.h"
#include "VideoCommon/FPSCounter.h"
#include "VideoCommon/RenderState.h"
#include "VideoCommon/TextureConfig.h"
#include "VideoCommon/VideoCommon.h"
class AbstractFramebuffer;
@ -72,7 +73,7 @@ enum class OSDMessage : s32
class Renderer
{
public:
Renderer(int backbuffer_width, int backbuffer_height);
Renderer(int backbuffer_width, int backbuffer_height, AbstractTextureFormat backbuffer_format);
virtual ~Renderer();
using ClearColor = std::array<float, 4>;
@ -230,6 +231,7 @@ protected:
// Backbuffer (window) size and render area
int m_backbuffer_width = 0;
int m_backbuffer_height = 0;
AbstractTextureFormat m_backbuffer_format = AbstractTextureFormat::Undefined;
TargetRectangle m_target_rectangle = {};
FPSCounter m_fps_counter;