2016-08-13 12:57:50 +00:00
|
|
|
// Copyright 2016 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2016-10-01 03:07:50 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
2016-08-13 12:57:50 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-10-01 03:07:50 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2016-08-13 12:57:50 +00:00
|
|
|
#include "VideoBackends/Vulkan/Constants.h"
|
|
|
|
|
|
|
|
namespace Vulkan
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
class VKFramebuffer;
|
|
|
|
class VKShader;
|
2018-02-24 15:15:35 +00:00
|
|
|
class VKPipeline;
|
2019-02-15 01:59:50 +00:00
|
|
|
class VKTexture;
|
2016-08-13 12:57:50 +00:00
|
|
|
class StreamBuffer;
|
|
|
|
class VertexFormat;
|
|
|
|
|
|
|
|
class StateTracker
|
|
|
|
{
|
|
|
|
public:
|
2019-02-15 01:59:50 +00:00
|
|
|
StateTracker();
|
|
|
|
~StateTracker();
|
2016-10-22 10:50:36 +00:00
|
|
|
|
|
|
|
static StateTracker* GetInstance();
|
|
|
|
static bool CreateInstance();
|
|
|
|
static void DestroyInstance();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
VKFramebuffer* GetFramebuffer() const { return m_framebuffer; }
|
2018-02-24 15:15:35 +00:00
|
|
|
const VKPipeline* GetPipeline() const { return m_pipeline; }
|
2022-06-19 03:49:03 +00:00
|
|
|
void SetVertexBuffer(VkBuffer buffer, VkDeviceSize offset, u32 size);
|
2016-08-13 12:57:50 +00:00
|
|
|
void SetIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType type);
|
2019-02-15 01:59:50 +00:00
|
|
|
void SetFramebuffer(VKFramebuffer* framebuffer);
|
2018-02-24 15:15:35 +00:00
|
|
|
void SetPipeline(const VKPipeline* pipeline);
|
2019-02-15 01:59:50 +00:00
|
|
|
void SetComputeShader(const VKShader* shader);
|
|
|
|
void SetGXUniformBuffer(u32 index, VkBuffer buffer, u32 offset, u32 size);
|
|
|
|
void SetUtilityUniformBuffer(VkBuffer buffer, u32 offset, u32 size);
|
|
|
|
void SetTexture(u32 index, VkImageView view);
|
|
|
|
void SetSampler(u32 index, VkSampler sampler);
|
|
|
|
void SetSSBO(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize range);
|
|
|
|
void SetTexelBuffer(u32 index, VkBufferView view);
|
|
|
|
void SetImageTexture(VkImageView view);
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
void UnbindTexture(VkImageView view);
|
|
|
|
|
|
|
|
// Set dirty flags on everything to force re-bind at next draw time.
|
2019-02-15 01:59:50 +00:00
|
|
|
void InvalidateCachedState();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// Ends a render pass if we're currently in one.
|
|
|
|
// When Bind() is next called, the pass will be restarted.
|
|
|
|
// Calling this function is allowed even if a pass has not begun.
|
|
|
|
bool InRenderPass() const { return m_current_render_pass != VK_NULL_HANDLE; }
|
|
|
|
void BeginRenderPass();
|
2019-02-15 01:59:50 +00:00
|
|
|
void BeginDiscardRenderPass();
|
2016-08-13 12:57:50 +00:00
|
|
|
void EndRenderPass();
|
|
|
|
|
2016-09-11 06:37:41 +00:00
|
|
|
// Ends the current render pass if it was a clear render pass.
|
2018-01-21 10:22:45 +00:00
|
|
|
void BeginClearRenderPass(const VkRect2D& area, const VkClearValue* clear_values,
|
|
|
|
u32 num_clear_values);
|
2016-09-11 06:37:41 +00:00
|
|
|
void EndClearRenderPass();
|
|
|
|
|
2016-08-13 12:57:50 +00:00
|
|
|
void SetViewport(const VkViewport& viewport);
|
|
|
|
void SetScissor(const VkRect2D& scissor);
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// Binds all dirty state to the commmand buffer.
|
|
|
|
// If this returns false, you should not issue the draw.
|
|
|
|
bool Bind();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// Binds all dirty compute state to the command buffer.
|
|
|
|
// If this returns false, you should not dispatch the shader.
|
|
|
|
bool BindCompute();
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
// Returns true if the specified rectangle is inside the current render area (used for clears).
|
2016-09-11 06:37:41 +00:00
|
|
|
bool IsWithinRenderArea(s32 x, s32 y, u32 width, u32 height) const;
|
|
|
|
|
2016-11-13 08:39:06 +00:00
|
|
|
private:
|
2016-11-13 05:38:32 +00:00
|
|
|
// Number of descriptor sets for game draws.
|
|
|
|
enum
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
NUM_GX_DESCRIPTOR_SETS = 3,
|
|
|
|
NUM_UTILITY_DESCRIPTOR_SETS = 2,
|
|
|
|
NUM_COMPUTE_DESCRIPTOR_SETS = 1
|
2016-11-13 05:38:32 +00:00
|
|
|
};
|
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
enum DIRTY_FLAG : u32
|
2016-08-13 12:57:50 +00:00
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
DIRTY_FLAG_GX_UBOS = (1 << 0),
|
|
|
|
DIRTY_FLAG_GX_UBO_OFFSETS = (1 << 1),
|
|
|
|
DIRTY_FLAG_GX_SAMPLERS = (1 << 4),
|
|
|
|
DIRTY_FLAG_GX_SSBO = (1 << 5),
|
|
|
|
DIRTY_FLAG_UTILITY_UBO = (1 << 2),
|
|
|
|
DIRTY_FLAG_UTILITY_UBO_OFFSET = (1 << 3),
|
|
|
|
DIRTY_FLAG_UTILITY_BINDINGS = (1 << 6),
|
|
|
|
DIRTY_FLAG_COMPUTE_BINDINGS = (1 << 7),
|
|
|
|
DIRTY_FLAG_VERTEX_BUFFER = (1 << 8),
|
|
|
|
DIRTY_FLAG_INDEX_BUFFER = (1 << 9),
|
|
|
|
DIRTY_FLAG_VIEWPORT = (1 << 10),
|
|
|
|
DIRTY_FLAG_SCISSOR = (1 << 11),
|
|
|
|
DIRTY_FLAG_PIPELINE = (1 << 12),
|
|
|
|
DIRTY_FLAG_COMPUTE_SHADER = (1 << 13),
|
|
|
|
DIRTY_FLAG_DESCRIPTOR_SETS = (1 << 14),
|
|
|
|
DIRTY_FLAG_COMPUTE_DESCRIPTOR_SET = (1 << 15),
|
|
|
|
|
|
|
|
DIRTY_FLAG_ALL_DESCRIPTORS = DIRTY_FLAG_GX_UBOS | DIRTY_FLAG_UTILITY_UBO |
|
|
|
|
DIRTY_FLAG_GX_SAMPLERS | DIRTY_FLAG_GX_SSBO |
|
|
|
|
DIRTY_FLAG_UTILITY_BINDINGS | DIRTY_FLAG_COMPUTE_BINDINGS
|
2016-08-13 12:57:50 +00:00
|
|
|
};
|
2016-11-13 08:39:06 +00:00
|
|
|
|
|
|
|
bool Initialize();
|
|
|
|
|
|
|
|
// Check that the specified viewport is within the render area.
|
|
|
|
// If not, ends the render pass if it is a clear render pass.
|
|
|
|
bool IsViewportWithinRenderArea() const;
|
|
|
|
|
2022-10-05 23:35:17 +00:00
|
|
|
void UpdateDescriptorSet();
|
|
|
|
void UpdateGXDescriptorSet();
|
|
|
|
void UpdateUtilityDescriptorSet();
|
|
|
|
void UpdateComputeDescriptorSet();
|
2016-11-13 08:39:06 +00:00
|
|
|
|
|
|
|
// Which bindings/state has to be updated before the next draw.
|
2016-08-13 12:57:50 +00:00
|
|
|
u32 m_dirty_flags = 0;
|
|
|
|
|
|
|
|
// input assembly
|
|
|
|
VkBuffer m_vertex_buffer = VK_NULL_HANDLE;
|
|
|
|
VkDeviceSize m_vertex_buffer_offset = 0;
|
|
|
|
VkBuffer m_index_buffer = VK_NULL_HANDLE;
|
|
|
|
VkDeviceSize m_index_buffer_offset = 0;
|
|
|
|
VkIndexType m_index_type = VK_INDEX_TYPE_UINT16;
|
|
|
|
|
|
|
|
// pipeline state
|
2018-02-24 15:15:35 +00:00
|
|
|
const VKPipeline* m_pipeline = nullptr;
|
2019-02-15 01:59:50 +00:00
|
|
|
const VKShader* m_compute_shader = nullptr;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// shader bindings
|
|
|
|
struct
|
|
|
|
{
|
2019-02-15 01:59:50 +00:00
|
|
|
std::array<VkDescriptorBufferInfo, NUM_UBO_DESCRIPTOR_SET_BINDINGS> gx_ubo_bindings;
|
|
|
|
std::array<u32, NUM_UBO_DESCRIPTOR_SET_BINDINGS> gx_ubo_offsets;
|
|
|
|
VkDescriptorBufferInfo utility_ubo_binding;
|
|
|
|
u32 utility_ubo_offset;
|
|
|
|
std::array<VkDescriptorImageInfo, NUM_PIXEL_SHADER_SAMPLERS> samplers;
|
|
|
|
std::array<VkBufferView, NUM_COMPUTE_TEXEL_BUFFERS> texel_buffers;
|
|
|
|
VkDescriptorBufferInfo ssbo;
|
2022-06-19 03:49:03 +00:00
|
|
|
VkDescriptorBufferInfo gx_uber_vertex_ssbo;
|
2019-02-15 01:59:50 +00:00
|
|
|
VkDescriptorImageInfo image_texture;
|
|
|
|
} m_bindings = {};
|
|
|
|
std::array<VkDescriptorSet, NUM_GX_DESCRIPTOR_SETS> m_gx_descriptor_sets = {};
|
|
|
|
std::array<VkDescriptorSet, NUM_UTILITY_DESCRIPTOR_SETS> m_utility_descriptor_sets = {};
|
|
|
|
VkDescriptorSet m_compute_descriptor_set = VK_NULL_HANDLE;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
|
|
|
// rasterization
|
|
|
|
VkViewport m_viewport = {0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
|
|
|
|
VkRect2D m_scissor = {{0, 0}, {1, 1}};
|
|
|
|
|
|
|
|
// uniform buffers
|
2019-02-15 01:59:50 +00:00
|
|
|
std::unique_ptr<VKTexture> m_dummy_texture;
|
2016-08-13 12:57:50 +00:00
|
|
|
|
2019-02-15 01:59:50 +00:00
|
|
|
VKFramebuffer* m_framebuffer = nullptr;
|
2016-09-11 06:37:41 +00:00
|
|
|
VkRenderPass m_current_render_pass = VK_NULL_HANDLE;
|
2016-08-13 12:57:50 +00:00
|
|
|
VkRect2D m_framebuffer_render_area = {};
|
|
|
|
};
|
2019-01-22 14:36:46 +00:00
|
|
|
} // namespace Vulkan
|