-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffline_sdf_renderer.h
More file actions
105 lines (92 loc) · 3.42 KB
/
offline_sdf_renderer.h
File metadata and controls
105 lines (92 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef OFFLINE_SDF_RENDERER_H
#define OFFLINE_SDF_RENDERER_H
#include "sdf_renderer.h"
#include "ffmpeg_encode_settings.h"
#include "ffmpeg_encoder.h"
#include "vkutils.h"
#include <condition_variable>
#include <deque>
#include <filesystem>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <thread>
inline constexpr uint32_t OFFSCREEN_DEFAULT_WIDTH = 1280;
inline constexpr uint32_t OFFSCREEN_DEFAULT_HEIGHT = 720;
inline constexpr uint32_t OFFSCREEN_DEFAULT_RING_SIZE = 2;
struct OfflineRenderOptions {
uint32_t maxFrames = 1;
std::optional<std::filesystem::path> debugDumpPPMDir = std::nullopt;
uint32_t width = OFFSCREEN_DEFAULT_WIDTH;
uint32_t height = OFFSCREEN_DEFAULT_HEIGHT;
uint32_t ringSize = OFFSCREEN_DEFAULT_RING_SIZE;
ffmpeg_utils::EncodeSettings encodeSettings = {};
};
// Offline SDF Renderer
// This basis will be used for FFMPEG integration
class OfflineSDFRenderer : public SDFRenderer {
private:
struct RingSlot {
VkImage image = VK_NULL_HANDLE;
VkDeviceMemory imageMemory = VK_NULL_HANDLE;
VkImageView imageView = VK_NULL_HANDLE;
VkFramebuffer framebuffer = VK_NULL_HANDLE;
vkutils::ReadbackBuffer stagingBuffer{};
void *mappedData = nullptr;
uint32_t rowStride = 0;
bool pendingReadback = false;
bool pendingEncode = false;
};
// Render Context
VkExtent2D imageSize{};
VkFormat imageFormat = VK_FORMAT_B8G8R8A8_UNORM;
vkutils::ReadbackFormatInfo readbackFormatInfo{};
// Ring buffer timing intuition:
// - 1 slot: total ≈ N * (render + readback) (no overlap).
// - K >= 2: total ≈ (render + readback) + (N - 1) * max(render, readback).
const uint32_t ringSize = OFFSCREEN_DEFAULT_RING_SIZE;
std::array<RingSlot, MAX_FRAME_SLOTS> ringSlots;
const uint32_t maxFrames;
static uint32_t validateRingSize(uint32_t value);
void vulkanSetup();
void setupRenderContext();
void createPipeline();
void createCommandBuffers();
void destroyDeviceObjects() noexcept;
void destroyRenderContextObjects() noexcept;
void destroyPipelineObjects() noexcept;
void destroy() noexcept;
void recordCommandBuffer(uint32_t slotIndex, uint32_t currentFrame);
[[nodiscard]] PPMDebugFrame debugReadbackOffscreenImage(const RingSlot &slot);
[[nodiscard]] vkutils::PushConstants
getPushConstants(uint32_t currentFrame) noexcept;
struct EncodeItem {
uint32_t slotIndex = 0;
uint32_t frameIndex = 0;
};
ffmpeg_utils::EncodeSettings encodeSettings;
std::unique_ptr<ffmpeg_utils::FfmpegEncoder> encoder;
std::thread encoderThread;
std::mutex encodeMutex;
std::condition_variable encodeCv;
std::deque<EncodeItem> encodeQueue;
bool encodeStop = false;
bool encodeFailed = false;
void startEncoding();
void stopEncoding();
void stopEncodingNoexcept() noexcept;
void enqueueEncode(uint32_t slotIndex, uint32_t frameIndex);
void waitForSlotEncode(uint32_t slotIndex);
void runEncoderLoop();
public:
OfflineSDFRenderer(const OfflineSDFRenderer &) = delete;
OfflineSDFRenderer &operator=(const OfflineSDFRenderer &) = delete;
OfflineSDFRenderer(
const std::string &fragShaderPath, bool useToyTemplate = false,
OfflineRenderOptions options = {});
~OfflineSDFRenderer() noexcept;
void setup();
void renderFrames();
};
#endif // OFFLINE_SDF_RENDERER_H