MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "mxvk/mxvk.hpp"
3
4#include <cstdint>
5#include <exception>
6#include <iostream>
7#include <vector>
8
9namespace example {
10 class HeadlessExample final : public mxvk::VK_Window {
11 public:
12 HeadlessExample() : mxvk::VK_Window("MXVK Headless", WIDTH, HEIGHT, false, MXVK_VALIDATION, PresentModePreference::LowLatency, RuntimeMode::Headless) {
15 setClearColor(0.125F, 0.25F, 0.5F, 1.0F);
16 }
17
18 [[nodiscard]] bool run() {
19 loop();
20 return readback_verified;
21 }
22
23 protected:
24 void proc() override {
25 ++processed_frames;
26 if (processed_frames > MAX_FRAME_COUNT) {
27 std::cerr << "headless example: timed out waiting for readback\n";
28 exit();
29 }
30 }
31
32 void onFrameReadback(std::vector<std::uint8_t> &rgba_pixels, uint32_t width, uint32_t height) override {
33 if (readback_verified) {
34 return;
35 }
36 const bool dimensions_match = width == WIDTH && height == HEIGHT;
37 const bool pixels_available = rgba_pixels.size() == static_cast<size_t>(WIDTH) * HEIGHT * 4U;
38 if (!dimensions_match || !pixels_available) {
39 std::cerr << "headless example: invalid frame readback\n";
40 exit();
41 return;
42 }
43
44 const size_t center = (static_cast<size_t>(HEIGHT / 2U) * WIDTH + WIDTH / 2U) * 4U;
45 const bool color_matches = rgba_pixels[center] >= 30U && rgba_pixels[center] <= 34U && rgba_pixels[center + 1U] >= 62U && rgba_pixels[center + 1U] <= 66U && rgba_pixels[center + 2U] >= 126U && rgba_pixels[center + 2U] <= 130U;
46 if (!color_matches) {
47 std::cerr << "headless example: clear-color readback mismatch\n";
48 exit();
49 return;
50 }
51
52 readback_verified = true;
53 std::cout << "headless example: surface-free RGBA readback verified " << width << 'x' << height << '\n';
54 exit();
55 }
56
57 private:
58 static constexpr uint32_t WIDTH = 320;
59 static constexpr uint32_t HEIGHT = 180;
60 static constexpr uint32_t MAX_FRAME_COUNT = 8;
61 uint32_t processed_frames = 0;
62 bool readback_verified = false;
63 };
64} // namespace example
65
66int main() {
67 try {
69 return app.run() ? 0 : 1;
70 } catch (const mxvk::Exception &ex) {
71 std::cerr << "headless example: " << ex.text() << '\n';
72 } catch (const std::exception &ex) {
73 std::cerr << "headless example: " << ex.what() << '\n';
74 }
75 return 1;
76}
void proc() override
Execute one processing/update step.
Definition main.cpp:24
void onFrameReadback(std::vector< std::uint8_t > &rgba_pixels, uint32_t width, uint32_t height) override
Receive one tightly packed RGBA8 copy of the rendered frame.
Definition main.cpp:32
std::string text() const
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:39
void loop()
Run the main event/render loop.
Definition mxvk.cpp:651
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:637
void exit()
Request loop termination.
Definition mxvk.cpp:1378
void setFrameReadbackEnabled(bool enabled) noexcept
Enable or disable readback of each rendered swapchain frame.
Definition mxvk.hpp:186
VK_Window()=default
Construct an empty window object.
void setEnableScreenshot(bool enabled) noexcept
Enable or disable F10 screenshot capture.
Definition mxvk.cpp:644
#define MXVK_VALIDATION
Definition mxvk.hpp:28
int main()
Definition main.py:165
Utilities for loading and saving PNG images.
Definition mxvk.hpp:31