231 lines
9.5 KiB
C++
231 lines
9.5 KiB
C++
#ifndef EMULATOR_INCLUDE_EMULATOR_GRAPHICS_GRAPHICSRENDER_H_
|
|
#define EMULATOR_INCLUDE_EMULATOR_GRAPHICS_GRAPHICSRENDER_H_
|
|
|
|
#include "common/abi.h"
|
|
#include "common/assert.h"
|
|
#include "common/common.h"
|
|
#include "graphics/host_gpu/renderer/pipeline/descriptors.h"
|
|
#include "graphics/host_gpu/renderer/pipeline/pipelineCache.h"
|
|
#include "graphics/host_gpu/renderer/renderTarget.h"
|
|
#include "graphics/host_gpu/vulkanCommon.h"
|
|
|
|
#include <array>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
namespace Libs::Graphics {
|
|
|
|
namespace HW {
|
|
class Context;
|
|
class UserConfig;
|
|
class Shader;
|
|
} // namespace HW
|
|
|
|
struct GraphicContext;
|
|
struct ShaderBufferResource;
|
|
struct ShaderComputeInputInfo;
|
|
struct RenderDepthInfo;
|
|
struct RenderColorInfo;
|
|
struct DrawCallInfo;
|
|
struct DrawEmitInfo;
|
|
struct DrawIndexBufferSource;
|
|
struct DrawRenderState;
|
|
class RenderContext;
|
|
class CommandScheduler;
|
|
struct RenderExecutorTestAccess;
|
|
|
|
enum class CommandBufferDebugOp : uint32_t {
|
|
DispatchDirect,
|
|
DrawIndex,
|
|
DrawIndexAuto,
|
|
EopWrite,
|
|
EopInterrupt,
|
|
EopWriteBack,
|
|
EopFlip,
|
|
EopWriteBackFlip,
|
|
EopOnlyFlip,
|
|
DispatchIndirect,
|
|
Unknown,
|
|
};
|
|
|
|
enum class DrawOffsetSource : uint8_t {
|
|
DrawState,
|
|
IndirectArgs,
|
|
};
|
|
|
|
struct DrawIndexArgs {
|
|
uint32_t index_count = 0;
|
|
const void* index_addr = nullptr;
|
|
uint32_t instance_count = 0;
|
|
uint32_t index_type_and_size = 0;
|
|
int32_t base_vertex = 0;
|
|
uint32_t first_instance = 0;
|
|
DrawOffsetSource offset_source = DrawOffsetSource::DrawState;
|
|
uint32_t render_target_slice_offset = 0;
|
|
};
|
|
|
|
struct DrawAutoArgs {
|
|
uint32_t vertex_count = 0;
|
|
uint32_t instance_count = 0;
|
|
uint32_t first_vertex = 0;
|
|
uint32_t first_instance = 0;
|
|
DrawOffsetSource offset_source = DrawOffsetSource::DrawState;
|
|
uint32_t render_target_slice_offset = 0;
|
|
};
|
|
|
|
struct SubmitInfo {
|
|
static constexpr uint32_t MaxSemaphores = 3;
|
|
|
|
std::array<vk::Semaphore, MaxSemaphores> wait_semaphores {};
|
|
std::array<uint64_t, MaxSemaphores> wait_ticks {};
|
|
std::array<vk::PipelineStageFlags, MaxSemaphores> wait_stages {};
|
|
std::array<vk::Semaphore, MaxSemaphores> signal_semaphores {};
|
|
std::array<uint64_t, MaxSemaphores> signal_ticks {};
|
|
uint32_t num_wait_semaphores = 0;
|
|
uint32_t num_signal_semaphores = 0;
|
|
|
|
void AddWait(vk::Semaphore semaphore, uint64_t tick = 1,
|
|
vk::PipelineStageFlags stage = vk::PipelineStageFlagBits::eAllCommands) {
|
|
EXIT_IF(semaphore == nullptr || num_wait_semaphores >= MaxSemaphores);
|
|
wait_semaphores[num_wait_semaphores] = semaphore;
|
|
wait_ticks[num_wait_semaphores] = tick;
|
|
wait_stages[num_wait_semaphores++] = stage;
|
|
}
|
|
|
|
void AddSignal(vk::Semaphore semaphore, uint64_t tick = 1) {
|
|
EXIT_IF(semaphore == nullptr || num_signal_semaphores >= MaxSemaphores);
|
|
signal_semaphores[num_signal_semaphores] = semaphore;
|
|
signal_ticks[num_signal_semaphores++] = tick;
|
|
}
|
|
};
|
|
|
|
class CommandBuffer {
|
|
public:
|
|
~CommandBuffer() = default;
|
|
|
|
KYTY_CLASS_NO_COPY(CommandBuffer);
|
|
|
|
[[nodiscard]] bool IsInvalid() const;
|
|
|
|
void SetDebugInfo(uint32_t op, uint64_t submit_id, uint32_t arg0 = 0, uint32_t arg1 = 0,
|
|
uint32_t arg2 = 0, uint32_t arg3 = 0, uint64_t arg4 = 0);
|
|
void BeginRendering(const RenderState& state) const;
|
|
void EndRendering() const;
|
|
|
|
[[nodiscard]] vk::CommandBuffer Handle() const;
|
|
[[nodiscard]] GraphicContext& GetGraphics() const noexcept { return m_graphics; }
|
|
[[nodiscard]] RenderContext& GetContext() const noexcept { return m_context; }
|
|
[[nodiscard]] HW::Context& GetRegisters() const noexcept { return *m_registers; }
|
|
[[nodiscard]] HW::UserConfig& GetUserConfig() const noexcept { return *m_user_config; }
|
|
[[nodiscard]] HW::Shader& GetShaders() const noexcept { return *m_shaders; }
|
|
|
|
private:
|
|
explicit CommandBuffer(CommandScheduler& scheduler);
|
|
void Bind(HW::Context& registers, HW::UserConfig& user_config, HW::Shader& shaders) noexcept {
|
|
m_registers = ®isters;
|
|
m_user_config = &user_config;
|
|
m_shaders = &shaders;
|
|
}
|
|
|
|
void Begin();
|
|
void End() const;
|
|
|
|
RenderContext& m_context;
|
|
GraphicContext& m_graphics;
|
|
vk::CommandBuffer m_buffer = nullptr;
|
|
uint32_t m_debug_op = 0;
|
|
uint64_t m_debug_submit_id = 0;
|
|
uint32_t m_debug_arg0 = 0;
|
|
uint32_t m_debug_arg1 = 0;
|
|
uint32_t m_debug_arg2 = 0;
|
|
uint32_t m_debug_arg3 = 0;
|
|
uint64_t m_debug_arg4 = 0;
|
|
mutable RenderState m_render_state;
|
|
mutable bool m_rendering = false;
|
|
HW::Context* m_registers = nullptr;
|
|
HW::UserConfig* m_user_config = nullptr;
|
|
HW::Shader* m_shaders = nullptr;
|
|
|
|
friend class CommandScheduler;
|
|
};
|
|
|
|
class RenderExecutor {
|
|
public:
|
|
explicit RenderExecutor(RenderContext& context): m_context(context) {}
|
|
KYTY_CLASS_NO_COPY(RenderExecutor);
|
|
|
|
void DispatchDirect(uint64_t submit_id, CommandBuffer& buffer, uint32_t thread_group_x,
|
|
uint32_t thread_group_y, uint32_t thread_group_z, uint32_t mode);
|
|
void DispatchIndirect(uint64_t submit_id, CommandBuffer& buffer, uint64_t args_addr,
|
|
uint32_t mode);
|
|
|
|
[[nodiscard]] PreparedBindings PrepareBindings(const ShaderStageRuntime& runtime);
|
|
void FindBuffers(PreparedBindings& bindings);
|
|
void RebindBuffers(PreparedBindings& bindings);
|
|
void RebindImages(PreparedBindings& bindings);
|
|
void CommitBindings(CommandBuffer& buffer, vk::PipelineBindPoint pipeline_bind_point,
|
|
const PipelineCache::Pipeline& pipeline,
|
|
std::span<PreparedBindings* const> bindings);
|
|
|
|
private:
|
|
void DrawIndex(uint64_t submit_id, CommandBuffer& buffer, const DrawIndexArgs& args);
|
|
void DrawAuto(uint64_t submit_id, CommandBuffer& buffer, const DrawAutoArgs& args);
|
|
|
|
struct GraphicsBindings {
|
|
std::array<PreparedBindings, 3> vertex;
|
|
std::optional<PreparedBindings> pixel;
|
|
};
|
|
|
|
[[nodiscard]] TextureBinding ResolveTexture(const ShaderRecompiler::IR::ImageResource& resource,
|
|
const ShaderRecompiler::IR::DescriptorValue& value);
|
|
void PrepareGraphicsBindings(std::span<PreparedBindings* const> stages,
|
|
std::span<RenderColorInfo> colors);
|
|
void ResolveRenderColorTarget(CommandBuffer& buffer, RenderColorInfo& target,
|
|
uint32_t render_target_slice_offset, uint32_t render_target_slot,
|
|
bool ignore_target_mask = false, bool exact_format = false);
|
|
void ResolveRenderDepthTarget(CommandBuffer& buffer, RenderDepthInfo& target);
|
|
[[nodiscard]] bool DepthStencilCopy(CommandBuffer& buffer);
|
|
[[nodiscard]] bool PrepareDrawRenderState(CommandBuffer& buffer,
|
|
const DrawCallInfo& draw,
|
|
uint32_t render_target_slice_offset,
|
|
DrawRenderState& state);
|
|
void ExecutePreparedDraw(uint64_t submit_id, CommandBuffer& buffer, const DrawCallInfo& draw,
|
|
DrawRenderState& state, vk::PrimitiveTopology topology,
|
|
const DrawEmitInfo& emit, const DrawIndexBufferSource& index_source,
|
|
bool primitive_restart_enable);
|
|
[[nodiscard]] RenderState AcquireRenderTargets(CommandBuffer& buffer, RenderColorInfo* colors,
|
|
uint32_t color_count, RenderDepthInfo& depth,
|
|
vk::ImageAspectFlags& feedback_aspects,
|
|
std::span<PreparedBindings* const> stages = {});
|
|
[[nodiscard]] bool ResolveColorTargets(CommandBuffer& buffer,
|
|
uint32_t render_target_slice_offset);
|
|
void BindImage(ImageId id, bool storage);
|
|
void BindRenderTarget(ImageId id);
|
|
void ResetBindings();
|
|
[[nodiscard]] bool TryConsumeComputeMetaClear(const ShaderComputeInputInfo& input,
|
|
const CommandBuffer& buffer);
|
|
[[nodiscard]] bool TryConsumeComputeImageClear(const ShaderComputeInputInfo& input,
|
|
CommandBuffer& command, uint32_t group_x,
|
|
uint32_t group_y, uint32_t group_z, uint32_t mode);
|
|
|
|
RenderContext& m_context;
|
|
std::vector<ImageId> m_bound_images;
|
|
std::vector<vk::DescriptorBufferInfo> m_descriptor_buffers;
|
|
std::vector<vk::DescriptorImageInfo> m_descriptor_images;
|
|
std::vector<vk::WriteDescriptorSet> m_descriptor_writes;
|
|
std::vector<uint32_t> m_image_occurrences;
|
|
|
|
friend class CommandProcessor;
|
|
friend struct RenderExecutorTestAccess;
|
|
};
|
|
|
|
[[nodiscard]] bool ResolveComputeBufferFill(const ShaderComputeInputInfo& input, uint32_t group_x,
|
|
uint32_t group_y, uint32_t group_z, uint32_t mode,
|
|
ShaderBufferResource& descriptor,
|
|
uint32_t& packed_clear, uint64_t& size);
|
|
|
|
} // namespace Libs::Graphics
|
|
|
|
#endif /* EMULATOR_INCLUDE_EMULATOR_GRAPHICS_GRAPHICSRENDER_H_ */
|