2016-08-13 12:57:50 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
#include <algorithm>
|
2016-10-01 03:07:50 +00:00
|
|
|
#include <cstddef>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <limits>
|
2016-10-01 03:07:50 +00:00
|
|
|
#include <string>
|
2017-04-09 19:05:24 +00:00
|
|
|
#include <tuple>
|
2016-10-01 03:07:50 +00:00
|
|
|
|
2018-01-21 13:13:25 +00:00
|
|
|
#include "Common/Assert.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-10-01 03:07:50 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
#include "Common/MsgHandler.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-10-22 12:41:42 +00:00
|
|
|
#include "Core/Core.h"
|
2016-08-13 14:40:04 +00:00
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/BoundingBox.h"
|
|
|
|
#include "VideoBackends/Vulkan/CommandBufferManager.h"
|
|
|
|
#include "VideoBackends/Vulkan/FramebufferManager.h"
|
|
|
|
#include "VideoBackends/Vulkan/ObjectCache.h"
|
2017-04-21 13:33:58 +00:00
|
|
|
#include "VideoBackends/Vulkan/PostProcessing.h"
|
2017-04-23 04:44:34 +00:00
|
|
|
#include "VideoBackends/Vulkan/Renderer.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/StateTracker.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "VideoBackends/Vulkan/StreamBuffer.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/SwapChain.h"
|
2016-10-01 12:54:02 +00:00
|
|
|
#include "VideoBackends/Vulkan/TextureCache.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/Util.h"
|
2017-09-08 09:42:56 +00:00
|
|
|
#include "VideoBackends/Vulkan/VKPipeline.h"
|
|
|
|
#include "VideoBackends/Vulkan/VKShader.h"
|
2017-04-23 04:44:34 +00:00
|
|
|
#include "VideoBackends/Vulkan/VKTexture.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/VulkanContext.h"
|
|
|
|
|
|
|
|
#include "VideoCommon/BPFunctions.h"
|
|
|
|
#include "VideoCommon/BPMemory.h"
|
2017-05-25 13:59:23 +00:00
|
|
|
#include "VideoCommon/DriverDetails.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
|
|
|
#include "VideoCommon/PixelEngine.h"
|
2017-04-17 13:14:17 +00:00
|
|
|
#include "VideoCommon/RenderState.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoCommon/TextureCacheBase.h"
|
2017-01-23 16:20:20 +00:00
|
|
|
#include "VideoCommon/VideoBackendBase.h"
|
2017-05-29 22:02:09 +00:00
|
|
|
#include "VideoCommon/VideoCommon.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2017-02-01 15:56:13 +00:00
|
|
|
#include "VideoCommon/XFMemory.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
2019-01-18 14:35:00 +00:00
|
|
|
Renderer::Renderer(std::unique_ptr<SwapChain> swap_chain, float backbuffer_scale)
|
2017-03-04 06:40:08 +00:00
|
|
|
: ::Renderer(swap_chain ? static_cast<int>(swap_chain->GetWidth()) : 1,
|
2019-01-18 14:35:00 +00:00
|
|
|
swap_chain ? static_cast<int>(swap_chain->GetHeight()) : 0, backbuffer_scale,
|
2017-09-09 06:09:24 +00:00
|
|
|
swap_chain ? swap_chain->GetTextureFormat() : AbstractTextureFormat::Undefined),
|
2017-03-04 06:40:08 +00:00
|
|
|
m_swap_chain(std::move(swap_chain))
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2016-10-02 11:37:24 +00:00
|
|
|
UpdateActiveConfig();
|
2016-08-13 12:57:50 +00:00
|
|
|
for (size_t i = 0; i < m_sampler_states.size(); i++)
|
2017-09-09 08:30:15 +00:00
|
|
|
m_sampler_states[i].hex = RenderState::GetPointSamplerState().hex;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 10:52:25 +00:00
|
|
|
Renderer::~Renderer() = default;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-10-22 10:50:36 +00:00
|
|
|
Renderer* Renderer::GetInstance()
|
|
|
|
{
|
|
|
|
return static_cast<Renderer*>(g_renderer.get());
|
|
|
|
}
|
|
|
|
|
2018-10-03 13:03:13 +00:00
|
|
|
bool Renderer::IsHeadless() const
|
|
|
|
{
|
|
|
|
return m_swap_chain == nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-22 10:50:36 +00:00
|
|
|
bool Renderer::Initialize()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-02-09 10:52:25 +00:00
|
|
|
if (!::Renderer::Initialize())
|
|
|
|
return false;
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
BindEFBToStateTracker();
|
|
|
|
|
|
|
|
m_bounding_box = std::make_unique<BoundingBox>();
|
|
|
|
if (!m_bounding_box->Initialize())
|
|
|
|
{
|
|
|
|
PanicAlert("Failed to initialize bounding box.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_vulkan_context->SupportsBoundingBox())
|
|
|
|
{
|
|
|
|
// Bind bounding box to state tracker
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->SetBBoxBuffer(m_bounding_box->GetGPUBuffer(),
|
|
|
|
m_bounding_box->GetGPUBufferOffset(),
|
|
|
|
m_bounding_box->GetGPUBufferSize());
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2017-04-21 13:33:58 +00:00
|
|
|
// Initialize post processing.
|
|
|
|
m_post_processor = std::make_unique<VulkanPostProcessing>();
|
2018-10-09 14:34:26 +00:00
|
|
|
if (!static_cast<VulkanPostProcessing*>(m_post_processor.get())->Initialize())
|
2017-04-21 13:33:58 +00:00
|
|
|
{
|
|
|
|
PanicAlert("failed to initialize post processor.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// Various initialization routines will have executed commands on the command buffer.
|
|
|
|
// Execute what we have done before beginning the first frame.
|
|
|
|
g_command_buffer_mgr->PrepareToSubmitCommandBuffer();
|
|
|
|
g_command_buffer_mgr->SubmitCommandBuffer(false);
|
|
|
|
BeginFrame();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:52:25 +00:00
|
|
|
void Renderer::Shutdown()
|
|
|
|
{
|
|
|
|
::Renderer::Shutdown();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 06:25:36 +00:00
|
|
|
std::unique_ptr<AbstractTexture> Renderer::CreateTexture(const TextureConfig& config)
|
|
|
|
{
|
|
|
|
return VKTexture::Create(config);
|
|
|
|
}
|
|
|
|
|
2017-10-21 14:49:40 +00:00
|
|
|
std::unique_ptr<AbstractStagingTexture> Renderer::CreateStagingTexture(StagingTextureType type,
|
|
|
|
const TextureConfig& config)
|
|
|
|
{
|
|
|
|
return VKStagingTexture::Create(type, config);
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
std::unique_ptr<AbstractShader> Renderer::CreateShaderFromSource(ShaderStage stage,
|
|
|
|
const char* source, size_t length)
|
|
|
|
{
|
|
|
|
return VKShader::CreateFromSource(stage, source, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<AbstractShader> Renderer::CreateShaderFromBinary(ShaderStage stage,
|
|
|
|
const void* data, size_t length)
|
|
|
|
{
|
|
|
|
return VKShader::CreateFromBinary(stage, data, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<AbstractPipeline> Renderer::CreatePipeline(const AbstractPipelineConfig& config)
|
|
|
|
{
|
|
|
|
return VKPipeline::Create(config);
|
|
|
|
}
|
|
|
|
|
2018-01-21 10:22:45 +00:00
|
|
|
std::unique_ptr<AbstractFramebuffer>
|
|
|
|
Renderer::CreateFramebuffer(const AbstractTexture* color_attachment,
|
|
|
|
const AbstractTexture* depth_attachment)
|
|
|
|
{
|
|
|
|
return VKFramebuffer::Create(static_cast<const VKTexture*>(color_attachment),
|
|
|
|
static_cast<const VKTexture*>(depth_attachment));
|
|
|
|
}
|
|
|
|
|
2017-09-08 09:42:56 +00:00
|
|
|
void Renderer::SetPipeline(const AbstractPipeline* pipeline)
|
|
|
|
{
|
2018-02-24 15:15:35 +00:00
|
|
|
StateTracker::GetInstance()->SetPipeline(static_cast<const VKPipeline*>(pipeline));
|
2017-09-08 09:42:56 +00:00
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
u32 Renderer::AccessEFB(EFBAccessType type, u32 x, u32 y, u32 poke_data)
|
|
|
|
{
|
2017-01-23 07:51:46 +00:00
|
|
|
if (type == EFBAccessType::PeekColor)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
u32 color = FramebufferManager::GetInstance()->PeekEFBColor(x, y);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// a little-endian value is expected to be returned
|
|
|
|
color = ((color & 0xFF00FF00) | ((color >> 16) & 0xFF) | ((color << 16) & 0xFF0000));
|
|
|
|
|
|
|
|
// check what to do with the alpha channel (GX_PokeAlphaRead)
|
|
|
|
PixelEngine::UPEAlphaReadReg alpha_read_mode = PixelEngine::GetAlphaReadMode();
|
|
|
|
|
|
|
|
if (bpmem.zcontrol.pixel_format == PEControl::RGBA6_Z24)
|
|
|
|
{
|
|
|
|
color = RGBA8ToRGBA6ToRGBA8(color);
|
|
|
|
}
|
|
|
|
else if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16)
|
|
|
|
{
|
|
|
|
color = RGBA8ToRGB565ToRGBA8(color);
|
|
|
|
}
|
|
|
|
if (bpmem.zcontrol.pixel_format != PEControl::RGBA6_Z24)
|
|
|
|
{
|
|
|
|
color |= 0xFF000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha_read_mode.ReadMode == 2)
|
|
|
|
{
|
|
|
|
return color; // GX_READ_NONE
|
|
|
|
}
|
|
|
|
else if (alpha_read_mode.ReadMode == 1)
|
|
|
|
{
|
|
|
|
return color | 0xFF000000; // GX_READ_FF
|
|
|
|
}
|
|
|
|
else /*if(alpha_read_mode.ReadMode == 0)*/
|
|
|
|
{
|
|
|
|
return color & 0x00FFFFFF; // GX_READ_00
|
|
|
|
}
|
|
|
|
}
|
2017-01-23 07:51:46 +00:00
|
|
|
else // if (type == EFBAccessType::PeekZ)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Depth buffer is inverted for improved precision near far plane
|
2016-10-22 10:50:36 +00:00
|
|
|
float depth = 1.0f - FramebufferManager::GetInstance()->PeekEFBDepth(x, y);
|
2016-08-13 12:57:50 +00:00
|
|
|
u32 ret = 0;
|
|
|
|
|
|
|
|
if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16)
|
|
|
|
{
|
|
|
|
// if Z is in 16 bit format you must return a 16 bit integer
|
|
|
|
ret = MathUtil::Clamp<u32>(static_cast<u32>(depth * 65536.0f), 0, 0xFFFF);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = MathUtil::Clamp<u32>(static_cast<u32>(depth * 16777216.0f), 0, 0xFFFFFF);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::PokeEFB(EFBAccessType type, const EfbPokeData* points, size_t num_points)
|
|
|
|
{
|
2017-01-23 07:51:46 +00:00
|
|
|
if (type == EFBAccessType::PokeColor)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < num_points; i++)
|
|
|
|
{
|
|
|
|
// Convert to expected format (BGRA->RGBA)
|
|
|
|
// TODO: Check alpha, depending on mode?
|
|
|
|
const EfbPokeData& point = points[i];
|
|
|
|
u32 color = ((point.data & 0xFF00FF00) | ((point.data >> 16) & 0xFF) |
|
|
|
|
((point.data << 16) & 0xFF0000));
|
2016-10-22 10:50:36 +00:00
|
|
|
FramebufferManager::GetInstance()->PokeEFBColor(point.x, point.y, color);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-23 07:51:46 +00:00
|
|
|
else // if (type == EFBAccessType::PokeZ)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < num_points; i++)
|
|
|
|
{
|
|
|
|
// Convert to floating-point depth.
|
|
|
|
const EfbPokeData& point = points[i];
|
|
|
|
float depth = (1.0f - float(point.data & 0xFFFFFF) / 16777216.0f);
|
2016-10-22 10:50:36 +00:00
|
|
|
FramebufferManager::GetInstance()->PokeEFBDepth(point.x, point.y, depth);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 Renderer::BBoxRead(int index)
|
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
s32 value = m_bounding_box->Get(static_cast<size_t>(index));
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Here we get the min/max value of the truncated position of the upscaled framebuffer.
|
|
|
|
// So we have to correct them to the unscaled EFB sizes.
|
|
|
|
if (index < 2)
|
|
|
|
{
|
|
|
|
// left/right
|
2017-03-04 06:42:21 +00:00
|
|
|
value = value * EFB_WIDTH / m_target_width;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// up/down
|
2017-03-04 06:42:21 +00:00
|
|
|
value = value * EFB_HEIGHT / m_target_height;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fix max values to describe the outer border
|
|
|
|
if (index & 1)
|
|
|
|
value++;
|
|
|
|
|
|
|
|
return static_cast<u16>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::BBoxWrite(int index, u16 value)
|
|
|
|
{
|
|
|
|
s32 scaled_value = static_cast<s32>(value);
|
|
|
|
|
|
|
|
// fix max values to describe the outer border
|
|
|
|
if (index & 1)
|
|
|
|
scaled_value--;
|
|
|
|
|
|
|
|
// scale to internal resolution
|
|
|
|
if (index < 2)
|
|
|
|
{
|
|
|
|
// left/right
|
2017-03-04 06:42:21 +00:00
|
|
|
scaled_value = scaled_value * m_target_width / EFB_WIDTH;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// up/down
|
2017-03-04 06:42:21 +00:00
|
|
|
scaled_value = scaled_value * m_target_height / EFB_HEIGHT;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 10:50:36 +00:00
|
|
|
m_bounding_box->Set(static_cast<size_t>(index), scaled_value);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TargetRectangle Renderer::ConvertEFBRectangle(const EFBRectangle& rc)
|
|
|
|
{
|
|
|
|
TargetRectangle result;
|
|
|
|
result.left = EFBToScaledX(rc.left);
|
|
|
|
result.top = EFBToScaledY(rc.top);
|
|
|
|
result.right = EFBToScaledX(rc.right);
|
|
|
|
result.bottom = EFBToScaledY(rc.bottom);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::BeginFrame()
|
|
|
|
{
|
|
|
|
// Activate a new command list, and restore state ready for the next draw
|
|
|
|
g_command_buffer_mgr->ActivateCommandBuffer();
|
|
|
|
|
|
|
|
// Ensure that the state tracker rebinds everything, and allocates a new set
|
|
|
|
// of descriptors out of the next pool.
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->InvalidateDescriptorSets();
|
2016-11-30 12:34:36 +00:00
|
|
|
StateTracker::GetInstance()->InvalidateConstants();
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->SetPendingRebind();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::ClearScreen(const EFBRectangle& rc, bool color_enable, bool alpha_enable,
|
|
|
|
bool z_enable, u32 color, u32 z)
|
|
|
|
{
|
|
|
|
// Native -> EFB coordinates
|
|
|
|
TargetRectangle target_rc = Renderer::ConvertEFBRectangle(rc);
|
2017-04-14 08:10:53 +00:00
|
|
|
|
|
|
|
// Size we pass this size to vkBeginRenderPass, it has to be clamped to the framebuffer
|
|
|
|
// dimensions. The other backends just silently ignore this case.
|
|
|
|
target_rc.ClampUL(0, 0, m_target_width, m_target_height);
|
|
|
|
|
2016-09-11 06:37:41 +00:00
|
|
|
VkRect2D target_vk_rc = {
|
|
|
|
{target_rc.left, target_rc.top},
|
|
|
|
{static_cast<uint32_t>(target_rc.GetWidth()), static_cast<uint32_t>(target_rc.GetHeight())}};
|
|
|
|
|
2016-09-11 05:18:23 +00:00
|
|
|
// Determine whether the EFB has an alpha channel. If it doesn't, we can clear the alpha
|
|
|
|
// channel to 0xFF. This hopefully allows us to use the fast path in most cases.
|
|
|
|
if (bpmem.zcontrol.pixel_format == PEControl::RGB565_Z16 ||
|
|
|
|
bpmem.zcontrol.pixel_format == PEControl::RGB8_Z24 ||
|
|
|
|
bpmem.zcontrol.pixel_format == PEControl::Z24)
|
|
|
|
{
|
2016-12-27 02:59:52 +00:00
|
|
|
// Force alpha writes, and clear the alpha channel. This is different to the other backends,
|
|
|
|
// where the existing values of the alpha channel are preserved.
|
2016-09-11 05:18:23 +00:00
|
|
|
alpha_enable = true;
|
2016-12-27 02:59:52 +00:00
|
|
|
color &= 0x00FFFFFF;
|
2016-09-11 05:18:23 +00:00
|
|
|
}
|
|
|
|
|
2016-10-23 11:00:20 +00:00
|
|
|
// Convert RGBA8 -> floating-point values.
|
|
|
|
VkClearValue clear_color_value = {};
|
|
|
|
VkClearValue clear_depth_value = {};
|
|
|
|
clear_color_value.color.float32[0] = static_cast<float>((color >> 16) & 0xFF) / 255.0f;
|
|
|
|
clear_color_value.color.float32[1] = static_cast<float>((color >> 8) & 0xFF) / 255.0f;
|
|
|
|
clear_color_value.color.float32[2] = static_cast<float>((color >> 0) & 0xFF) / 255.0f;
|
|
|
|
clear_color_value.color.float32[3] = static_cast<float>((color >> 24) & 0xFF) / 255.0f;
|
|
|
|
clear_depth_value.depthStencil.depth = (1.0f - (static_cast<float>(z & 0xFFFFFF) / 16777216.0f));
|
|
|
|
|
2016-09-11 06:37:41 +00:00
|
|
|
// If we're not in a render pass (start of the frame), we can use a clear render pass
|
|
|
|
// to discard the data, rather than loading and then clearing.
|
2017-09-03 04:04:14 +00:00
|
|
|
bool use_clear_attachments = (color_enable && alpha_enable) || z_enable;
|
|
|
|
bool use_clear_render_pass =
|
|
|
|
!StateTracker::GetInstance()->InRenderPass() && color_enable && alpha_enable && z_enable;
|
|
|
|
|
|
|
|
// The NVIDIA Vulkan driver causes the GPU to lock up, or throw exceptions if MSAA is enabled,
|
|
|
|
// a non-full clear rect is specified, and a clear loadop or vkCmdClearAttachments is used.
|
|
|
|
if (g_ActiveConfig.iMultisamples > 1 &&
|
|
|
|
DriverDetails::HasBug(DriverDetails::BUG_BROKEN_MSAA_CLEAR))
|
2017-06-20 01:46:17 +00:00
|
|
|
{
|
|
|
|
use_clear_render_pass = false;
|
2017-09-03 04:04:14 +00:00
|
|
|
use_clear_attachments = false;
|
2017-06-20 01:46:17 +00:00
|
|
|
}
|
2017-09-03 04:04:14 +00:00
|
|
|
|
|
|
|
// This path cannot be used if the driver implementation doesn't guarantee pixels with no drawn
|
|
|
|
// geometry in "this" renderpass won't be cleared
|
|
|
|
if (DriverDetails::HasBug(DriverDetails::BUG_BROKEN_CLEAR_LOADOP_RENDERPASS))
|
2016-09-11 06:37:41 +00:00
|
|
|
use_clear_render_pass = false;
|
|
|
|
|
|
|
|
// Fastest path: Use a render pass to clear the buffers.
|
|
|
|
if (use_clear_render_pass)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-01-21 10:22:45 +00:00
|
|
|
const std::array<VkClearValue, 2> clear_values = {{clear_color_value, clear_depth_value}};
|
|
|
|
StateTracker::GetInstance()->BeginClearRenderPass(target_vk_rc, clear_values.data(),
|
|
|
|
static_cast<u32>(clear_values.size()));
|
2016-09-11 06:37:41 +00:00
|
|
|
return;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 06:37:41 +00:00
|
|
|
// Fast path: Use vkCmdClearAttachments to clear the buffers within a render path
|
|
|
|
// We can't use this when preserving alpha but clearing color.
|
2017-09-03 04:04:14 +00:00
|
|
|
if (use_clear_attachments)
|
2016-09-11 06:37:41 +00:00
|
|
|
{
|
|
|
|
VkClearAttachment clear_attachments[2];
|
|
|
|
uint32_t num_clear_attachments = 0;
|
|
|
|
if (color_enable && alpha_enable)
|
|
|
|
{
|
|
|
|
clear_attachments[num_clear_attachments].aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
|
|
clear_attachments[num_clear_attachments].colorAttachment = 0;
|
|
|
|
clear_attachments[num_clear_attachments].clearValue = clear_color_value;
|
|
|
|
num_clear_attachments++;
|
|
|
|
color_enable = false;
|
|
|
|
alpha_enable = false;
|
|
|
|
}
|
|
|
|
if (z_enable)
|
|
|
|
{
|
|
|
|
clear_attachments[num_clear_attachments].aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
|
|
|
clear_attachments[num_clear_attachments].colorAttachment = 0;
|
|
|
|
clear_attachments[num_clear_attachments].clearValue = clear_depth_value;
|
|
|
|
num_clear_attachments++;
|
|
|
|
z_enable = false;
|
|
|
|
}
|
|
|
|
if (num_clear_attachments > 0)
|
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
VkClearRect vk_rect = {target_vk_rc, 0, FramebufferManager::GetInstance()->GetEFBLayers()};
|
|
|
|
if (!StateTracker::GetInstance()->IsWithinRenderArea(
|
|
|
|
target_vk_rc.offset.x, target_vk_rc.offset.y, target_vk_rc.extent.width,
|
|
|
|
target_vk_rc.extent.height))
|
2016-09-11 06:37:41 +00:00
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->EndClearRenderPass();
|
2016-09-11 06:37:41 +00:00
|
|
|
}
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->BeginRenderPass();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-09-11 06:37:41 +00:00
|
|
|
vkCmdClearAttachments(g_command_buffer_mgr->GetCurrentCommandBuffer(), num_clear_attachments,
|
2016-10-22 10:50:36 +00:00
|
|
|
clear_attachments, 1, &vk_rect);
|
2016-09-11 06:37:41 +00:00
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-11 06:37:41 +00:00
|
|
|
// Anything left over for the slow path?
|
2016-08-13 12:57:50 +00:00
|
|
|
if (!color_enable && !alpha_enable && !z_enable)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Clearing must occur within a render pass.
|
2016-10-22 10:50:36 +00:00
|
|
|
if (!StateTracker::GetInstance()->IsWithinRenderArea(target_vk_rc.offset.x, target_vk_rc.offset.y,
|
|
|
|
target_vk_rc.extent.width,
|
|
|
|
target_vk_rc.extent.height))
|
2016-09-11 06:37:41 +00:00
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->EndClearRenderPass();
|
2016-09-11 06:37:41 +00:00
|
|
|
}
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->BeginRenderPass();
|
|
|
|
StateTracker::GetInstance()->SetPendingRebind();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Mask away the appropriate colors and use a shader
|
2017-09-08 14:10:40 +00:00
|
|
|
BlendingState blend_state = RenderState::GetNoBlendingBlendState();
|
2017-04-17 13:14:17 +00:00
|
|
|
blend_state.colorupdate = color_enable;
|
|
|
|
blend_state.alphaupdate = alpha_enable;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2017-09-08 14:10:40 +00:00
|
|
|
DepthState depth_state = RenderState::GetNoDepthTestingDepthStencilState();
|
2017-04-30 05:54:45 +00:00
|
|
|
depth_state.testenable = z_enable;
|
|
|
|
depth_state.updateenable = z_enable;
|
|
|
|
depth_state.func = ZMode::ALWAYS;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// No need to start a new render pass, but we do need to restore viewport state
|
2016-10-22 10:50:36 +00:00
|
|
|
UtilityShaderDraw draw(g_command_buffer_mgr->GetCurrentCommandBuffer(),
|
2016-11-13 05:24:55 +00:00
|
|
|
g_object_cache->GetPipelineLayout(PIPELINE_LAYOUT_STANDARD),
|
2016-10-22 10:50:36 +00:00
|
|
|
FramebufferManager::GetInstance()->GetEFBLoadRenderPass(),
|
2017-07-20 05:25:33 +00:00
|
|
|
g_shader_cache->GetPassthroughVertexShader(),
|
2019-01-27 03:07:35 +00:00
|
|
|
g_shader_cache->GetPassthroughGeometryShader(),
|
|
|
|
g_shader_cache->GetClearFragmentShader());
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2017-04-30 08:07:57 +00:00
|
|
|
draw.SetMultisamplingState(FramebufferManager::GetInstance()->GetEFBMultisamplingState());
|
2017-04-30 05:54:45 +00:00
|
|
|
draw.SetDepthState(depth_state);
|
2016-08-13 12:57:50 +00:00
|
|
|
draw.SetBlendState(blend_state);
|
|
|
|
|
|
|
|
draw.DrawColoredQuad(target_rc.left, target_rc.top, target_rc.GetWidth(), target_rc.GetHeight(),
|
2016-09-11 06:37:41 +00:00
|
|
|
clear_color_value.color.float32[0], clear_color_value.color.float32[1],
|
|
|
|
clear_color_value.color.float32[2], clear_color_value.color.float32[3],
|
|
|
|
clear_depth_value.depthStencil.depth);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::ReinterpretPixelData(unsigned int convtype)
|
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
|
|
|
StateTracker::GetInstance()->SetPendingRebind();
|
|
|
|
FramebufferManager::GetInstance()->ReinterpretPixelData(convtype);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// EFB framebuffer has now changed, so update accordingly.
|
|
|
|
BindEFBToStateTracker();
|
|
|
|
}
|
|
|
|
|
2018-11-28 04:30:47 +00:00
|
|
|
void Renderer::Flush()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-11-28 04:30:47 +00:00
|
|
|
Util::ExecuteCurrentCommandsAndRestoreState(true, false);
|
|
|
|
}
|
2017-05-29 22:02:09 +00:00
|
|
|
|
2018-11-28 04:30:47 +00:00
|
|
|
void Renderer::BindBackbuffer(const ClearColor& clear_color)
|
|
|
|
{
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
// Handle host window resizes.
|
|
|
|
CheckForSurfaceChange();
|
|
|
|
CheckForSurfaceResize();
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// Ensure the worker thread is not still submitting a previous command buffer.
|
|
|
|
// In other words, the last frame has been submitted (otherwise the next call would
|
|
|
|
// be a race, as the image may not have been consumed yet).
|
|
|
|
g_command_buffer_mgr->PrepareToSubmitCommandBuffer();
|
|
|
|
|
2017-09-16 05:53:08 +00:00
|
|
|
VkResult res;
|
2017-09-16 06:15:20 +00:00
|
|
|
if (!g_command_buffer_mgr->CheckLastPresentFail())
|
2017-09-16 05:53:08 +00:00
|
|
|
{
|
|
|
|
// Grab the next image from the swap chain in preparation for drawing the window.
|
2019-01-27 02:59:57 +00:00
|
|
|
res = m_swap_chain->AcquireNextImage();
|
2017-09-16 05:53:08 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If the last present failed, we need to recreate the swap chain.
|
|
|
|
res = VK_ERROR_OUT_OF_DATE_KHR;
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
if (res == VK_SUBOPTIMAL_KHR || res == VK_ERROR_OUT_OF_DATE_KHR)
|
|
|
|
{
|
2016-12-03 14:23:37 +00:00
|
|
|
// There's an issue here. We can't resize the swap chain while the GPU is still busy with it,
|
|
|
|
// but calling WaitForGPUIdle would create a deadlock as PrepareToSubmitCommandBuffer has been
|
|
|
|
// called by SwapImpl. WaitForGPUIdle waits on the semaphore, which PrepareToSubmitCommandBuffer
|
|
|
|
// has already done, so it blocks indefinitely. To work around this, we submit the current
|
|
|
|
// command buffer, resize the swap chain (which calls WaitForGPUIdle), and then finally call
|
|
|
|
// PrepareToSubmitCommandBuffer to return to the state that the caller expects.
|
|
|
|
g_command_buffer_mgr->SubmitCommandBuffer(false);
|
2017-09-16 06:15:20 +00:00
|
|
|
m_swap_chain->ResizeSwapChain();
|
2017-09-16 05:52:12 +00:00
|
|
|
BeginFrame();
|
2016-12-03 14:23:37 +00:00
|
|
|
g_command_buffer_mgr->PrepareToSubmitCommandBuffer();
|
2019-01-27 02:59:57 +00:00
|
|
|
res = m_swap_chain->AcquireNextImage();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
if (res != VK_SUCCESS)
|
|
|
|
PanicAlert("Failed to grab image from swap chain");
|
|
|
|
|
|
|
|
// Transition from undefined (or present src, but it can be substituted) to
|
|
|
|
// color attachment ready for writing. These transitions must occur outside
|
|
|
|
// a render pass, unless the render pass declares a self-dependency.
|
|
|
|
Texture2D* backbuffer = m_swap_chain->GetCurrentTexture();
|
|
|
|
backbuffer->OverrideImageLayout(VK_IMAGE_LAYOUT_UNDEFINED);
|
|
|
|
backbuffer->TransitionToLayout(g_command_buffer_mgr->GetCurrentCommandBuffer(),
|
|
|
|
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
2018-01-21 10:22:45 +00:00
|
|
|
m_current_framebuffer = nullptr;
|
|
|
|
m_current_framebuffer_width = backbuffer->GetWidth();
|
|
|
|
m_current_framebuffer_height = backbuffer->GetHeight();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2017-09-09 06:09:24 +00:00
|
|
|
// Draw to the backbuffer.
|
|
|
|
VkRect2D region = {{0, 0}, {backbuffer->GetWidth(), backbuffer->GetHeight()}};
|
2019-01-27 02:59:57 +00:00
|
|
|
StateTracker::GetInstance()->SetRenderPass(m_swap_chain->GetLoadRenderPass(),
|
|
|
|
m_swap_chain->GetClearRenderPass());
|
2017-09-09 06:09:24 +00:00
|
|
|
StateTracker::GetInstance()->SetFramebuffer(m_swap_chain->GetCurrentFramebuffer(), region);
|
|
|
|
|
2016-10-22 12:41:42 +00:00
|
|
|
// Begin render pass for rendering to the swap chain.
|
2016-08-13 12:57:50 +00:00
|
|
|
VkClearValue clear_value = {{{0.0f, 0.0f, 0.0f, 1.0f}}};
|
2017-09-09 06:09:24 +00:00
|
|
|
StateTracker::GetInstance()->BeginClearRenderPass(region, &clear_value, 1);
|
2018-11-28 04:30:47 +00:00
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2018-11-28 04:30:47 +00:00
|
|
|
void Renderer::PresentBackbuffer()
|
|
|
|
{
|
2016-08-13 12:57:50 +00:00
|
|
|
// End drawing to backbuffer
|
2017-09-09 06:09:24 +00:00
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
2018-11-28 04:30:47 +00:00
|
|
|
StateTracker::GetInstance()->OnEndFrame();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Transition the backbuffer to PRESENT_SRC to ensure all commands drawing
|
|
|
|
// to it have finished before present.
|
2019-01-27 02:59:57 +00:00
|
|
|
m_swap_chain->GetCurrentTexture()->TransitionToLayout(
|
|
|
|
g_command_buffer_mgr->GetCurrentCommandBuffer(), VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
|
2018-11-28 04:30:47 +00:00
|
|
|
|
|
|
|
// Submit the current command buffer, signaling rendering finished semaphore when it's done
|
|
|
|
// Because this final command buffer is rendering to the swap chain, we need to wait for
|
|
|
|
// the available semaphore to be signaled before executing the buffer. This final submission
|
|
|
|
// can happen off-thread in the background while we're preparing the next frame.
|
2019-01-27 02:59:57 +00:00
|
|
|
g_command_buffer_mgr->SubmitCommandBuffer(true, m_swap_chain->GetImageAvailableSemaphore(),
|
|
|
|
m_swap_chain->GetRenderingFinishedSemaphore(),
|
|
|
|
m_swap_chain->GetSwapChain(),
|
|
|
|
m_swap_chain->GetCurrentImageIndex());
|
2018-11-28 04:30:47 +00:00
|
|
|
BeginFrame();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 04:30:47 +00:00
|
|
|
void Renderer::RenderXFBToScreen(const AbstractTexture* texture, const EFBRectangle& rc)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-11-28 04:30:47 +00:00
|
|
|
const TargetRectangle target_rc = GetTargetRectangle();
|
|
|
|
|
2017-04-21 13:33:58 +00:00
|
|
|
VulkanPostProcessing* post_processor = static_cast<VulkanPostProcessing*>(m_post_processor.get());
|
2017-11-11 03:55:00 +00:00
|
|
|
if (g_ActiveConfig.stereo_mode == StereoMode::SBS ||
|
|
|
|
g_ActiveConfig.stereo_mode == StereoMode::TAB)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
TargetRectangle left_rect;
|
|
|
|
TargetRectangle right_rect;
|
2018-11-28 04:30:47 +00:00
|
|
|
std::tie(left_rect, right_rect) = ConvertStereoRectangle(target_rc);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2018-11-28 04:30:47 +00:00
|
|
|
post_processor->BlitFromTexture(left_rect, rc,
|
|
|
|
static_cast<const VKTexture*>(texture)->GetRawTexIdentifier(),
|
2019-01-27 02:59:57 +00:00
|
|
|
0, m_swap_chain->GetLoadRenderPass());
|
2018-11-28 04:30:47 +00:00
|
|
|
post_processor->BlitFromTexture(right_rect, rc,
|
|
|
|
static_cast<const VKTexture*>(texture)->GetRawTexIdentifier(),
|
2019-01-27 02:59:57 +00:00
|
|
|
1, m_swap_chain->GetLoadRenderPass());
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2017-11-11 03:55:00 +00:00
|
|
|
else if (g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer)
|
2017-06-26 19:08:21 +00:00
|
|
|
{
|
2018-11-28 04:30:47 +00:00
|
|
|
post_processor->BlitFromTexture(target_rc, rc,
|
|
|
|
static_cast<const VKTexture*>(texture)->GetRawTexIdentifier(),
|
2019-01-27 02:59:57 +00:00
|
|
|
-1, m_swap_chain->GetLoadRenderPass());
|
2017-06-26 19:08:21 +00:00
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
else
|
|
|
|
{
|
2018-11-28 04:30:47 +00:00
|
|
|
post_processor->BlitFromTexture(target_rc, rc,
|
|
|
|
static_cast<const VKTexture*>(texture)->GetRawTexIdentifier(),
|
2019-01-27 02:59:57 +00:00
|
|
|
0, m_swap_chain->GetLoadRenderPass());
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
2018-11-28 04:30:47 +00:00
|
|
|
|
|
|
|
// The post-processor uses the old-style Vulkan draws, which mess with the tracked state.
|
|
|
|
StateTracker::GetInstance()->SetPendingRebind();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::CheckForSurfaceChange()
|
|
|
|
{
|
2018-10-03 13:03:13 +00:00
|
|
|
if (!m_surface_changed.TestAndClear() || !m_swap_chain)
|
2016-08-13 12:57:50 +00:00
|
|
|
return;
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
// Submit the current draws up until rendering the XFB.
|
|
|
|
g_command_buffer_mgr->ExecuteCommandBuffer(false, false);
|
2017-09-16 06:15:20 +00:00
|
|
|
g_command_buffer_mgr->WaitForGPUIdle();
|
|
|
|
|
|
|
|
// Clear the present failed flag, since we don't want to resize after recreating.
|
|
|
|
g_command_buffer_mgr->CheckLastPresentFail();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2018-10-03 13:03:13 +00:00
|
|
|
// Recreate the surface. If this fails we're in trouble.
|
|
|
|
if (!m_swap_chain->RecreateSurface(m_new_surface_handle))
|
|
|
|
PanicAlert("Failed to recreate Vulkan surface. Cannot continue.");
|
|
|
|
m_new_surface_handle = nullptr;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2017-09-16 06:15:20 +00:00
|
|
|
// Handle case where the dimensions are now different.
|
|
|
|
OnSwapChainResized();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-26 06:23:24 +00:00
|
|
|
void Renderer::CheckForSurfaceResize()
|
|
|
|
{
|
|
|
|
if (!m_surface_resized.TestAndClear())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// If we don't have a surface, how can we resize the swap chain?
|
|
|
|
// CheckForSurfaceChange should handle this case.
|
|
|
|
if (!m_swap_chain)
|
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Surface resize event received without active surface, ignoring");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the GPU to catch up since we're going to destroy the swap chain.
|
|
|
|
g_command_buffer_mgr->ExecuteCommandBuffer(false, false);
|
|
|
|
g_command_buffer_mgr->WaitForGPUIdle();
|
|
|
|
|
|
|
|
// Clear the present failed flag, since we don't want to resize after recreating.
|
|
|
|
g_command_buffer_mgr->CheckLastPresentFail();
|
|
|
|
|
|
|
|
// Resize the swap chain.
|
|
|
|
m_swap_chain->RecreateSwapChain();
|
|
|
|
OnSwapChainResized();
|
|
|
|
}
|
|
|
|
|
2018-11-28 04:30:47 +00:00
|
|
|
void Renderer::OnConfigChanged(u32 bits)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2016-10-01 12:54:02 +00:00
|
|
|
// Update texture cache settings with any changed options.
|
2016-09-06 22:57:58 +00:00
|
|
|
TextureCache::GetInstance()->OnConfigChanged(g_ActiveConfig);
|
2016-10-01 12:54:02 +00:00
|
|
|
|
2018-01-11 06:02:31 +00:00
|
|
|
// Handle settings that can cause the EFB framebuffer to change.
|
2018-11-28 04:30:47 +00:00
|
|
|
if (bits & CONFIG_CHANGE_BIT_TARGET_SIZE)
|
2018-01-11 06:02:31 +00:00
|
|
|
RecreateEFBFramebuffer();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2016-10-07 14:10:38 +00:00
|
|
|
// MSAA samples changed, we need to recreate the EFB render pass.
|
|
|
|
// If the stereoscopy mode changed, we need to recreate the buffers as well.
|
2017-06-24 09:58:14 +00:00
|
|
|
// SSAA changed on/off, we have to recompile shaders.
|
|
|
|
// Changing stereoscopy from off<->on also requires shaders to be recompiled.
|
2018-11-28 04:30:47 +00:00
|
|
|
if (bits & (CONFIG_CHANGE_BIT_HOST_CONFIG | CONFIG_CHANGE_BIT_MULTISAMPLES))
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-01-11 06:02:31 +00:00
|
|
|
RecreateEFBFramebuffer();
|
2016-10-22 10:50:36 +00:00
|
|
|
FramebufferManager::GetInstance()->RecompileShaders();
|
2018-02-24 15:15:35 +00:00
|
|
|
g_shader_cache->ReloadPipelineCache();
|
2017-07-20 05:25:33 +00:00
|
|
|
g_shader_cache->RecompileSharedShaders();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For vsync, we need to change the present mode, which means recreating the swap chain.
|
2018-11-28 04:30:47 +00:00
|
|
|
if (m_swap_chain && bits & CONFIG_CHANGE_BIT_VSYNC)
|
2016-10-02 12:09:19 +00:00
|
|
|
{
|
|
|
|
g_command_buffer_mgr->WaitForGPUIdle();
|
2019-01-27 02:24:53 +00:00
|
|
|
m_swap_chain->SetVSync(g_ActiveConfig.bVSyncActive);
|
2016-10-02 12:09:19 +00:00
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2017-06-26 19:08:21 +00:00
|
|
|
// For quad-buffered stereo we need to change the layer count, so recreate the swap chain.
|
2018-11-28 04:30:47 +00:00
|
|
|
if (m_swap_chain && bits & CONFIG_CHANGE_BIT_STEREO_MODE)
|
2017-09-16 06:15:20 +00:00
|
|
|
{
|
|
|
|
g_command_buffer_mgr->WaitForGPUIdle();
|
|
|
|
m_swap_chain->RecreateSwapChain();
|
|
|
|
}
|
2017-06-26 19:08:21 +00:00
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// Wipe sampler cache if force texture filtering or anisotropy changes.
|
2018-11-28 04:30:47 +00:00
|
|
|
if (bits & (CONFIG_CHANGE_BIT_ANISOTROPY | CONFIG_CHANGE_BIT_FORCE_TEXTURE_FILTERING))
|
2016-08-13 12:57:50 +00:00
|
|
|
ResetSamplerStates();
|
2017-04-21 13:33:58 +00:00
|
|
|
|
|
|
|
// Check for a changed post-processing shader and recompile if needed.
|
|
|
|
static_cast<VulkanPostProcessing*>(m_post_processor.get())->UpdateConfig();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::OnSwapChainResized()
|
|
|
|
{
|
2017-03-04 06:42:21 +00:00
|
|
|
m_backbuffer_width = m_swap_chain->GetWidth();
|
|
|
|
m_backbuffer_height = m_swap_chain->GetHeight();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::BindEFBToStateTracker()
|
|
|
|
{
|
|
|
|
// Update framebuffer in state tracker
|
2016-10-22 10:50:36 +00:00
|
|
|
VkRect2D framebuffer_size = {{0, 0},
|
|
|
|
{FramebufferManager::GetInstance()->GetEFBWidth(),
|
|
|
|
FramebufferManager::GetInstance()->GetEFBHeight()}};
|
|
|
|
StateTracker::GetInstance()->SetRenderPass(
|
|
|
|
FramebufferManager::GetInstance()->GetEFBLoadRenderPass(),
|
|
|
|
FramebufferManager::GetInstance()->GetEFBClearRenderPass());
|
|
|
|
StateTracker::GetInstance()->SetFramebuffer(
|
|
|
|
FramebufferManager::GetInstance()->GetEFBFramebuffer(), framebuffer_size);
|
2018-01-21 10:22:45 +00:00
|
|
|
m_current_framebuffer = nullptr;
|
|
|
|
m_current_framebuffer_width = FramebufferManager::GetInstance()->GetEFBWidth();
|
|
|
|
m_current_framebuffer_height = FramebufferManager::GetInstance()->GetEFBHeight();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 06:02:31 +00:00
|
|
|
void Renderer::RecreateEFBFramebuffer()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Ensure the GPU is finished with the current EFB textures.
|
|
|
|
g_command_buffer_mgr->WaitForGPUIdle();
|
2018-01-11 06:02:31 +00:00
|
|
|
FramebufferManager::GetInstance()->RecreateEFBFramebuffer();
|
2016-08-13 12:57:50 +00:00
|
|
|
BindEFBToStateTracker();
|
|
|
|
|
|
|
|
// Viewport and scissor rect have to be reset since they will be scaled differently.
|
2018-01-21 12:04:15 +00:00
|
|
|
BPFunctions::SetViewport();
|
2016-08-13 12:57:50 +00:00
|
|
|
BPFunctions::SetScissor();
|
|
|
|
}
|
|
|
|
|
2016-12-28 00:37:41 +00:00
|
|
|
void Renderer::ApplyState()
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::ResetAPIState()
|
|
|
|
{
|
|
|
|
// End the EFB render pass if active
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::RestoreAPIState()
|
|
|
|
{
|
2018-02-09 11:41:31 +00:00
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
2018-01-21 10:22:45 +00:00
|
|
|
if (m_current_framebuffer)
|
|
|
|
static_cast<const VKFramebuffer*>(m_current_framebuffer)->TransitionForSample();
|
|
|
|
|
2018-02-09 11:41:31 +00:00
|
|
|
BindEFBToStateTracker();
|
2018-10-10 09:40:47 +00:00
|
|
|
BPFunctions::SetViewport();
|
|
|
|
BPFunctions::SetScissor();
|
2018-02-09 11:41:31 +00:00
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
// Instruct the state tracker to re-bind everything before the next draw
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->SetPendingRebind();
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 10:22:45 +00:00
|
|
|
void Renderer::BindFramebuffer(const VKFramebuffer* fb)
|
|
|
|
{
|
|
|
|
const VkRect2D render_area = {static_cast<int>(fb->GetWidth()),
|
|
|
|
static_cast<int>(fb->GetHeight())};
|
|
|
|
|
|
|
|
StateTracker::GetInstance()->EndRenderPass();
|
|
|
|
if (m_current_framebuffer)
|
|
|
|
static_cast<const VKFramebuffer*>(m_current_framebuffer)->TransitionForSample();
|
|
|
|
|
|
|
|
fb->TransitionForRender();
|
|
|
|
StateTracker::GetInstance()->SetFramebuffer(fb->GetFB(), render_area);
|
|
|
|
StateTracker::GetInstance()->SetRenderPass(fb->GetLoadRenderPass(), fb->GetClearRenderPass());
|
|
|
|
m_current_framebuffer = fb;
|
|
|
|
m_current_framebuffer_width = fb->GetWidth();
|
|
|
|
m_current_framebuffer_height = fb->GetHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::SetFramebuffer(const AbstractFramebuffer* framebuffer)
|
|
|
|
{
|
|
|
|
const VKFramebuffer* vkfb = static_cast<const VKFramebuffer*>(framebuffer);
|
|
|
|
BindFramebuffer(vkfb);
|
|
|
|
StateTracker::GetInstance()->BeginRenderPass();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::SetAndDiscardFramebuffer(const AbstractFramebuffer* framebuffer)
|
|
|
|
{
|
|
|
|
const VKFramebuffer* vkfb = static_cast<const VKFramebuffer*>(framebuffer);
|
|
|
|
BindFramebuffer(vkfb);
|
|
|
|
|
|
|
|
// If we're discarding, begin the discard pass, then switch to a load pass.
|
|
|
|
// This way if the command buffer is flushed, we don't start another discard pass.
|
|
|
|
StateTracker::GetInstance()->SetRenderPass(vkfb->GetDiscardRenderPass(),
|
|
|
|
vkfb->GetClearRenderPass());
|
|
|
|
StateTracker::GetInstance()->BeginRenderPass();
|
|
|
|
StateTracker::GetInstance()->SetRenderPass(vkfb->GetLoadRenderPass(), vkfb->GetClearRenderPass());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::SetAndClearFramebuffer(const AbstractFramebuffer* framebuffer,
|
|
|
|
const ClearColor& color_value, float depth_value)
|
|
|
|
{
|
|
|
|
const VKFramebuffer* vkfb = static_cast<const VKFramebuffer*>(framebuffer);
|
|
|
|
BindFramebuffer(vkfb);
|
|
|
|
|
|
|
|
const VkRect2D render_area = {static_cast<int>(vkfb->GetWidth()),
|
|
|
|
static_cast<int>(vkfb->GetHeight())};
|
|
|
|
std::array<VkClearValue, 2> clear_values;
|
|
|
|
u32 num_clear_values = 0;
|
|
|
|
if (vkfb->GetColorFormat() != AbstractTextureFormat::Undefined)
|
|
|
|
{
|
|
|
|
std::memcpy(clear_values[num_clear_values].color.float32, color_value.data(),
|
|
|
|
sizeof(clear_values[num_clear_values].color.float32));
|
|
|
|
num_clear_values++;
|
|
|
|
}
|
|
|
|
if (vkfb->GetDepthFormat() != AbstractTextureFormat::Undefined)
|
|
|
|
{
|
|
|
|
clear_values[num_clear_values].depthStencil.depth = depth_value;
|
|
|
|
clear_values[num_clear_values].depthStencil.stencil = 0;
|
|
|
|
num_clear_values++;
|
|
|
|
}
|
|
|
|
StateTracker::GetInstance()->BeginClearRenderPass(render_area, clear_values.data(),
|
|
|
|
num_clear_values);
|
|
|
|
}
|
|
|
|
|
2018-01-21 13:13:25 +00:00
|
|
|
void Renderer::SetTexture(u32 index, const AbstractTexture* texture)
|
|
|
|
{
|
|
|
|
// Texture should always be in SHADER_READ_ONLY layout prior to use.
|
|
|
|
// This is so we don't need to transition during render passes.
|
|
|
|
auto* tex = texture ? static_cast<const VKTexture*>(texture)->GetRawTexIdentifier() : nullptr;
|
2018-03-16 16:57:36 +00:00
|
|
|
DEBUG_ASSERT(!tex || tex->GetLayout() == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
2018-01-21 13:13:25 +00:00
|
|
|
StateTracker::GetInstance()->SetTexture(index, tex ? tex->GetView() : VK_NULL_HANDLE);
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:30:15 +00:00
|
|
|
void Renderer::SetSamplerState(u32 index, const SamplerState& state)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
|
|
|
// Skip lookup if the state hasn't changed.
|
2017-09-09 08:30:15 +00:00
|
|
|
if (m_sampler_states[index].hex == state.hex)
|
2016-08-13 12:57:50 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Look up new state and replace in state tracker.
|
2017-09-09 08:30:15 +00:00
|
|
|
VkSampler sampler = g_object_cache->GetSampler(state);
|
2016-08-13 12:57:50 +00:00
|
|
|
if (sampler == VK_NULL_HANDLE)
|
|
|
|
{
|
|
|
|
ERROR_LOG(VIDEO, "Failed to create sampler");
|
|
|
|
sampler = g_object_cache->GetPointSampler();
|
|
|
|
}
|
|
|
|
|
2017-09-09 08:30:15 +00:00
|
|
|
StateTracker::GetInstance()->SetSampler(index, sampler);
|
|
|
|
m_sampler_states[index].hex = state.hex;
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 13:13:25 +00:00
|
|
|
void Renderer::UnbindTexture(const AbstractTexture* texture)
|
|
|
|
{
|
|
|
|
StateTracker::GetInstance()->UnbindTexture(
|
|
|
|
static_cast<const VKTexture*>(texture)->GetRawTexIdentifier()->GetView());
|
|
|
|
}
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
void Renderer::ResetSamplerStates()
|
|
|
|
{
|
|
|
|
// Ensure none of the sampler objects are in use.
|
2016-10-02 13:53:09 +00:00
|
|
|
// This assumes that none of the samplers are in use on the command list currently being recorded.
|
2016-08-13 12:57:50 +00:00
|
|
|
g_command_buffer_mgr->WaitForGPUIdle();
|
|
|
|
|
|
|
|
// Invalidate all sampler states, next draw will re-initialize them.
|
|
|
|
for (size_t i = 0; i < m_sampler_states.size(); i++)
|
|
|
|
{
|
2017-09-09 08:30:15 +00:00
|
|
|
m_sampler_states[i].hex = RenderState::GetPointSamplerState().hex;
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->SetSampler(i, g_object_cache->GetPointSampler());
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Invalidate all sampler objects (some will be unused now).
|
|
|
|
g_object_cache->ClearSamplerCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::SetInterlacingMode()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-01-21 12:12:32 +00:00
|
|
|
void Renderer::SetScissorRect(const MathUtil::Rectangle<int>& rc)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-01-21 12:12:32 +00:00
|
|
|
VkRect2D scissor = {{rc.left, rc.top},
|
|
|
|
{static_cast<u32>(rc.GetWidth()), static_cast<u32>(rc.GetHeight())}};
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->SetScissor(scissor);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 12:04:15 +00:00
|
|
|
void Renderer::SetViewport(float x, float y, float width, float height, float near_depth,
|
|
|
|
float far_depth)
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2018-01-21 12:04:15 +00:00
|
|
|
VkViewport viewport = {x, y, std::max(width, 1.0f), std::max(height, 1.0f),
|
|
|
|
near_depth, far_depth};
|
2016-10-22 10:50:36 +00:00
|
|
|
StateTracker::GetInstance()->SetViewport(viewport);
|
2016-08-13 12:57:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-27 07:16:53 +00:00
|
|
|
void Renderer::Draw(u32 base_vertex, u32 num_vertices)
|
|
|
|
{
|
|
|
|
if (StateTracker::GetInstance()->Bind())
|
|
|
|
return;
|
|
|
|
|
|
|
|
vkCmdDraw(g_command_buffer_mgr->GetCurrentCommandBuffer(), num_vertices, 1, base_vertex, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Renderer::DrawIndexed(u32 base_index, u32 num_indices, u32 base_vertex)
|
|
|
|
{
|
|
|
|
if (!StateTracker::GetInstance()->Bind())
|
|
|
|
return;
|
|
|
|
|
|
|
|
vkCmdDrawIndexed(g_command_buffer_mgr->GetCurrentCommandBuffer(), num_indices, 1, base_index,
|
|
|
|
base_vertex, 0);
|
|
|
|
}
|
2016-08-13 12:57:50 +00:00
|
|
|
} // namespace Vulkan
|