-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathApp.cpp
More file actions
218 lines (180 loc) · 7.84 KB
/
App.cpp
File metadata and controls
218 lines (180 loc) · 7.84 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "App.h"
#include <Babylon/AppRuntime.h>
#include <Babylon/ScriptLoader.h>
#include <Babylon/Plugins/NativeEngine.h>
#include <Babylon/Plugins/ShaderCache.h>
#include <Babylon/Polyfills/Console.h>
#include <Babylon/Polyfills/Window.h>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <future>
#include <iostream>
namespace
{
void CatchAndLogError(Napi::Promise jsPromise)
{
auto jsOnRejected = Napi::Function::New(jsPromise.Env(), [](const Napi::CallbackInfo& info) {
auto console = info.Env().Global().Get("console").As<Napi::Object>();
console.Get("error").As<Napi::Function>().Call(console, {info[0]});
std::exit(1);
});
jsPromise.Get("catch").As<Napi::Function>().Call(jsPromise, {jsOnRejected});
}
// Compare two images pixel-by-pixel.
// Returns true when the images match within the given tolerance.
bool CompareImages(
const ImageData& rendered,
const ImageData& reference,
uint32_t perChannelThreshold = 25,
double maxErrorPercent = 2.5)
{
if (rendered.Width != reference.Width || rendered.Height != reference.Height)
{
std::cerr << "Image dimensions do not match: "
<< rendered.Width << "x" << rendered.Height << " vs "
<< reference.Width << "x" << reference.Height << std::endl;
return false;
}
const size_t pixelCount = static_cast<size_t>(rendered.Width) * rendered.Height;
const size_t byteCount = pixelCount * 4;
if (rendered.Pixels.size() != byteCount || reference.Pixels.size() != byteCount)
{
std::cerr << "Unexpected pixel buffer size." << std::endl;
return false;
}
size_t diffCount = 0;
for (size_t i = 0; i < byteCount; i += 4)
{
// Compare R, G, B channels (skip alpha).
if (std::abs(static_cast<int>(rendered.Pixels[i]) - static_cast<int>(reference.Pixels[i])) >= static_cast<int>(perChannelThreshold) ||
std::abs(static_cast<int>(rendered.Pixels[i + 1]) - static_cast<int>(reference.Pixels[i + 1])) >= static_cast<int>(perChannelThreshold) ||
std::abs(static_cast<int>(rendered.Pixels[i + 2]) - static_cast<int>(reference.Pixels[i + 2])) >= static_cast<int>(perChannelThreshold))
{
++diffCount;
}
}
const double errorPercent = (static_cast<double>(diffCount) * 100.0) / static_cast<double>(pixelCount);
std::cout << "Image comparison: " << diffCount << "/" << pixelCount
<< " pixels differ (" << errorPercent << "%)" << std::endl;
return errorPercent <= maxErrorPercent;
}
bool LoadShaderCache(const std::filesystem::path& cachePath)
{
std::ifstream cacheFile(cachePath, std::ios::binary);
if (!cacheFile.is_open())
{
std::cerr << "Failed to open shader cache file: " << cachePath.string() << std::endl;
return false;
}
const uint32_t count = Babylon::Plugins::ShaderCache::Load(cacheFile);
std::cout << "Loaded " << count << " shader(s) from cache: " << cachePath.string() << std::endl;
return count > 0;
}
}
int RunApp(
const std::filesystem::path& executablePath,
Babylon::Graphics::Configuration config,
Babylon::Plugins::ExternalTexture externalTexture,
std::function<void(const std::filesystem::path&)> saveTexture,
std::function<ImageData(const std::filesystem::path&)> loadImage)
{
// Enable and load the pre-compiled shader cache.
Babylon::Plugins::ShaderCache::Enable();
auto cachePath = executablePath / "shaders.bin";
if (!LoadShaderCache(cachePath))
{
std::cerr << "No shaders loaded from cache. Aborting." << std::endl;
return EXIT_FAILURE;
}
// Create the Babylon Native graphics device and update.
auto device = Babylon::Graphics::Device(config);
auto deviceUpdate = device.GetUpdate("update");
// Start rendering a frame to unblock the JavaScript from queuing graphics
// commands.
device.StartRenderingCurrentFrame();
deviceUpdate.Start();
// Create a Babylon Native application runtime which hosts a JavaScript
// engine on a new thread.
Babylon::AppRuntime runtime{{.UnhandledExceptionHandler = [](const Napi::Error& e) {
Babylon::AppRuntime::DefaultUnhandledExceptionHandler(e);
std::quick_exit(1);
}}};
runtime.Dispatch([&device](Napi::Env env) {
env.Global().Set("globalThis", env.Global());
// Add the Babylon Native graphics device to the JavaScript environment.
device.AddToJavaScript(env);
// Initialize polyfills.
Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) {
std::cout << message << std::endl;
});
Babylon::Polyfills::Window::Initialize(env);
Babylon::Plugins::NativeEngine::Initialize(env);
});
// Load scripts.
Babylon::ScriptLoader loader{runtime};
loader.LoadScript("app:///index.js");
std::promise<void> startup{};
// Create an external texture for the render target texture and pass it to
// the `startup` JavaScript function.
loader.Dispatch([externalTexture = std::move(externalTexture), &startup](Napi::Env env) {
auto nativeTexture = externalTexture.CreateForJavaScript(env);
env.Global().Get("startup").As<Napi::Function>().Call(
{
nativeTexture,
Napi::Value::From(env, WIDTH),
Napi::Value::From(env, HEIGHT),
});
startup.set_value();
});
deviceUpdate.Finish();
device.FinishRenderingCurrentFrame();
startup.get_future().wait();
// Pump an extra frame so overrideInternal applies the native texture.
device.StartRenderingCurrentFrame();
deviceUpdate.Start();
deviceUpdate.Finish();
device.FinishRenderingCurrentFrame();
// Start a new frame for rendering the scene.
device.StartRenderingCurrentFrame();
deviceUpdate.Start();
std::promise<void> renderScene{};
// Call `renderSceneAsync` in JavaScript.
loader.Dispatch([&renderScene](Napi::Env env) {
std::cout << "Rendering scene" << std::endl;
auto jsPromise = env.Global().Get("renderSceneAsync").As<Napi::Function>().Call({}).As<Napi::Promise>();
auto jsOnFulfilled = Napi::Function::New(env, [&renderScene](const Napi::CallbackInfo&) {
renderScene.set_value();
});
jsPromise = jsPromise.Get("then").As<Napi::Function>().Call(jsPromise, {jsOnFulfilled}).As<Napi::Promise>();
CatchAndLogError(jsPromise);
});
// Wait for the scene to render.
renderScene.get_future().wait();
// Finish the frame.
deviceUpdate.Finish();
device.FinishRenderingCurrentFrame();
// Save the rendered output as a PNG.
auto filePath = executablePath / "output.png";
std::cout << "Writing " << filePath.filename().string() << std::endl;
saveTexture(filePath);
// Compare the rendered output with the reference image.
auto referencePath = executablePath / "reference.png";
if (!std::filesystem::exists(referencePath))
{
std::cerr << "ERROR: Reference image not found: " << referencePath.string() << std::endl;
Babylon::Plugins::ShaderCache::Disable();
return EXIT_FAILURE;
}
std::cout << "Comparing with " << referencePath.filename().string() << std::endl;
auto renderedImage = loadImage(filePath);
auto referenceImage = loadImage(referencePath);
if (!CompareImages(renderedImage, referenceImage))
{
std::cerr << "ERROR: Rendered image does not match reference image." << std::endl;
Babylon::Plugins::ShaderCache::Disable();
return EXIT_FAILURE;
}
Babylon::Plugins::ShaderCache::Disable();
return EXIT_SUCCESS;
}