Debian Codegraph @

Source/Core/VideoBackends/Vulkan/StateTracker.h

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <cstddef>
8 #include <memory>
9
10 #include "Common/CommonTypes.h"
11 #include "VideoBackends/Vulkan/Constants.h"
12 #include "VideoCommon/Constants.h"
13
14 namespace Vulkan
15 {
16 class VKFramebuffer;
17 class VKShader;
18 class VKPipeline;
19 class VKTexture;
20 class StreamBuffer;
21 class VertexFormat;
22
23 class StateTracker
24 {
25 public:
26 StateTracker();
27 ~StateTracker();
28
29 static StateTracker* GetInstance();
30 static bool CreateInstance();
31 static void DestroyInstance();
32
33 VKFramebuffer* GetFramebuffer() const { return m_framebuffer; }
34 const VKPipeline* GetPipeline() const { return m_pipeline; }
35 void SetVertexBuffer(VkBuffer buffer, VkDeviceSize offset, u32 size);
36 void SetIndexBuffer(VkBuffer buffer, VkDeviceSize offset, VkIndexType type);
37 void SetFramebuffer(VKFramebuffer* framebuffer);
38 void SetPipeline(const VKPipeline* pipeline);
39 void SetComputeShader(const VKShader* shader);
40 void SetGXUniformBuffer(u32 index, VkBuffer buffer, u32 offset, u32 size);
41 void SetUtilityUniformBuffer(VkBuffer buffer, u32 offset, u32 size);
42 void SetTexture(u32 index, VkImageView view);
43 void SetSampler(u32 index, VkSampler sampler);
44 void SetSSBO(VkBuffer buffer, VkDeviceSize offset, VkDeviceSize range);
45 void SetTexelBuffer(u32 index, VkBufferView view);
46 void SetImageTexture(u32 index, VkImageView view);
47
48 void UnbindTexture(VkImageView view);
49
50 // Set dirty flags on everything to force re-bind at next draw time.
51 void InvalidateCachedState();
52
53 // Ends a render pass if we're currently in one.
54 // When Bind() is next called, the pass will be restarted.
55 // Calling this function is allowed even if a pass has not begun.
56 bool InRenderPass() const { return m_current_render_pass != VK_NULL_HANDLE; }
57 void BeginRenderPass();
58 void BeginDiscardRenderPass();
59 void EndRenderPass();
60
61 // Ends the current render pass if it was a clear render pass.
62 void BeginClearRenderPass(const VkRect2D& area, const VkClearValue* clear_values,
63 u32 num_clear_values);
64 void EndClearRenderPass();
65
66 void SetViewport(const VkViewport& viewport);
67 void SetScissor(const VkRect2D& scissor);
68
69 // Binds all dirty state to the command buffer.
70 // If this returns false, you should not issue the draw.
71 bool Bind();
72
73 // Binds all dirty compute state to the command buffer.
74 // If this returns false, you should not dispatch the shader.
75 bool BindCompute();
76
77 // Returns true if the specified rectangle is inside the current render area (used for clears).
78 bool IsWithinRenderArea(s32 x, s32 y, u32 width, u32 height) const;
79
80 private:
81 // Number of descriptor sets for game draws.
82 enum
83 {
84 NUM_GX_DESCRIPTOR_SETS = 3,
85 NUM_UTILITY_DESCRIPTOR_SETS = 2,
86 NUM_COMPUTE_DESCRIPTOR_SETS = 1
87 };
88
89 enum DIRTY_FLAG : u32
90 {
91 DIRTY_FLAG_GX_UBOS = (1 << 0),
92 DIRTY_FLAG_GX_UBO_OFFSETS = (1 << 1),
93 DIRTY_FLAG_GX_SAMPLERS = (1 << 4),
94 DIRTY_FLAG_GX_SSBO = (1 << 5),
95 DIRTY_FLAG_UTILITY_UBO = (1 << 2),
96 DIRTY_FLAG_UTILITY_UBO_OFFSET = (1 << 3),
97 DIRTY_FLAG_UTILITY_BINDINGS = (1 << 6),
98 DIRTY_FLAG_COMPUTE_BINDINGS = (1 << 7),
99 DIRTY_FLAG_VERTEX_BUFFER = (1 << 8),
100 DIRTY_FLAG_INDEX_BUFFER = (1 << 9),
101 DIRTY_FLAG_VIEWPORT = (1 << 10),
102 DIRTY_FLAG_SCISSOR = (1 << 11),
103 DIRTY_FLAG_PIPELINE = (1 << 12),
104 DIRTY_FLAG_COMPUTE_SHADER = (1 << 13),
105 DIRTY_FLAG_DESCRIPTOR_SETS = (1 << 14),
106 DIRTY_FLAG_COMPUTE_DESCRIPTOR_SET = (1 << 15),
107
108 DIRTY_FLAG_ALL_DESCRIPTORS = DIRTY_FLAG_GX_UBOS | DIRTY_FLAG_UTILITY_UBO |
109 DIRTY_FLAG_GX_SAMPLERS | DIRTY_FLAG_GX_SSBO |
110 DIRTY_FLAG_UTILITY_BINDINGS | DIRTY_FLAG_COMPUTE_BINDINGS
111 };
112
113 bool Initialize();
114
115 // Check that the specified viewport is within the render area.
116 // If not, ends the render pass if it is a clear render pass.
117 bool IsViewportWithinRenderArea() const;
118
119 void UpdateDescriptorSet();
120 void UpdateGXDescriptorSet();
121 void UpdateUtilityDescriptorSet();
122 void UpdateComputeDescriptorSet();
123
124 // Which bindings/state has to be updated before the next draw.
125 u32 m_dirty_flags = 0;
126
127 // input assembly
128 VkBuffer m_vertex_buffer = VK_NULL_HANDLE;
129 VkDeviceSize m_vertex_buffer_offset = 0;
130 VkBuffer m_index_buffer = VK_NULL_HANDLE;
131 VkDeviceSize m_index_buffer_offset = 0;
132 VkIndexType m_index_type = VK_INDEX_TYPE_UINT16;
133
134 // pipeline state
135 const VKPipeline* m_pipeline = nullptr;
136 const VKShader* m_compute_shader = nullptr;
137
138 // shader bindings
139 struct
140 {
141 std::array<VkDescriptorBufferInfo, NUM_UBO_DESCRIPTOR_SET_BINDINGS> gx_ubo_bindings;
142 std::array<u32, NUM_UBO_DESCRIPTOR_SET_BINDINGS> gx_ubo_offsets;
143 VkDescriptorBufferInfo utility_ubo_binding;
144 u32 utility_ubo_offset;
145 std::array<VkDescriptorImageInfo, VideoCommon::MAX_PIXEL_SHADER_SAMPLERS> samplers;
146 std::array<VkBufferView, NUM_COMPUTE_TEXEL_BUFFERS> texel_buffers;
147 VkDescriptorBufferInfo ssbo;
148 VkDescriptorBufferInfo gx_uber_vertex_ssbo;
149 std::array<VkDescriptorImageInfo, VideoCommon::MAX_COMPUTE_SHADER_SAMPLERS> image_textures;
150 } m_bindings = {};
151 std::array<VkDescriptorSet, NUM_GX_DESCRIPTOR_SETS> m_gx_descriptor_sets = {};
152 std::array<VkDescriptorSet, NUM_UTILITY_DESCRIPTOR_SETS> m_utility_descriptor_sets = {};
153 VkDescriptorSet m_compute_descriptor_set = VK_NULL_HANDLE;
154
155 // rasterization
156 VkViewport m_viewport = {0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};
157 VkRect2D m_scissor = {{0, 0}, {1, 1}};
158
159 // uniform buffers
160 std::unique_ptr<VKTexture> m_dummy_texture;
161 std::unique_ptr<VKTexture> m_dummy_compute_texture;
162
163 VKFramebuffer* m_framebuffer = nullptr;
164 VkRenderPass m_current_render_pass = VK_NULL_HANDLE;
165 VkRect2D m_framebuffer_render_area = {};
166 };
167 } // namespace Vulkan