-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathApp.cpp
More file actions
231 lines (183 loc) · 8.45 KB
/
App.cpp
File metadata and controls
231 lines (183 loc) · 8.45 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
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <Babylon/AppRuntime.h>
#include <Babylon/Graphics/Device.h>
#include <Babylon/ScriptLoader.h>
#include <Babylon/Plugins/ExternalTexture.h>
#include <Babylon/Plugins/NativeEngine.h>
#include <Babylon/Polyfills/Console.h>
#include <Babylon/Polyfills/Window.h>
#include <Babylon/Polyfills/XMLHttpRequest.h>
#include <winrt/base.h>
#include <WICTextureLoader.h>
#include <ScreenGrab.h>
#include <wincodec.h>
#include <filesystem>
#include <iostream>
#include "RenderDoc.h"
namespace
{
constexpr const uint32_t WIDTH = 1024;
constexpr const uint32_t HEIGHT = 1024;
std::filesystem::path GetModulePath()
{
WCHAR modulePath[4096];
DWORD result = GetModuleFileNameW(nullptr, modulePath, ARRAYSIZE(modulePath));
winrt::check_bool(result != 0 && result != std::size(modulePath));
return std::filesystem::path{modulePath}.parent_path();
}
winrt::com_ptr<ID3D11Device> CreateD3DDevice()
{
winrt::com_ptr<ID3D11Device> d3dDevice{};
uint32_t flags = D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_BGRA_SUPPORT;
winrt::check_hresult(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, nullptr, 0, D3D11_SDK_VERSION, d3dDevice.put(), nullptr, nullptr));
return d3dDevice;
}
winrt::com_ptr<ID3D11Texture2D> CreateD3DRenderTargetTexture(ID3D11Device* d3dDevice)
{
D3D11_TEXTURE2D_DESC desc{};
desc.Width = WIDTH;
desc.Height = HEIGHT;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc = {4, 0};
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = 0;
desc.MiscFlags = 0;
winrt::com_ptr<ID3D11Texture2D> texture;
winrt::check_hresult(d3dDevice->CreateTexture2D(&desc, nullptr, texture.put()));
return texture;
}
Babylon::Graphics::Device CreateGraphicsDevice(ID3D11Device* d3dDevice)
{
Babylon::Graphics::Configuration config{};
config.Device = d3dDevice;
config.Width = WIDTH;
config.Height = HEIGHT;
return Babylon::Graphics::Device(config);
}
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});
}
}
int main()
{
// Initialize RenderDoc.
RenderDoc::Init();
// Create a DirectX device.
auto d3dDevice = CreateD3DDevice();
// Get the immediate context for DirectXTK when saving the texture.
winrt::com_ptr<ID3D11DeviceContext> d3dDeviceContext;
d3dDevice->GetImmediateContext(d3dDeviceContext.put());
// Create the Babylon Native graphics device and update.
auto device = CreateGraphicsDevice(d3dDevice.get());
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{};
runtime.Dispatch([&device](Napi::Env env) {
// Add the Babylon Native graphics device to the JavaScript environment.
device.AddToJavaScript(env);
// Initialize the console polyfill.
Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) {
std::cout << message << std::endl;
});
// Initialize the window, XMLHttpRequest, and NativeEngine polyfills.
Babylon::Polyfills::Window::Initialize(env);
Babylon::Polyfills::XMLHttpRequest::Initialize(env);
Babylon::Plugins::NativeEngine::Initialize(env);
});
// Load the scripts for Babylon.js core and loaders plus this app's index.js.
Babylon::ScriptLoader loader{runtime};
loader.LoadScript("app:///Scripts/babylon.max.js");
loader.LoadScript("app:///Scripts/babylonjs.loaders.js");
loader.LoadScript("app:///Scripts/index.js");
// Create a render target texture for the output.
winrt::com_ptr<ID3D11Texture2D> outputTexture = CreateD3DRenderTargetTexture(d3dDevice.get());
std::promise<void> addToContext{};
std::promise<void> startup{};
// Create an external texture for the render target texture and pass it to
// the `startup` JavaScript function.
loader.Dispatch([externalTexture = Babylon::Plugins::ExternalTexture{outputTexture.get()}, &addToContext, &startup](Napi::Env env) {
auto jsPromise = externalTexture.AddToContextAsync(env);
addToContext.set_value();
auto jsOnFulfilled = Napi::Function::New(env, [&startup](const Napi::CallbackInfo& info) {
auto nativeTexture = info[0];
info.Env().Global().Get("startup").As<Napi::Function>().Call(
{
nativeTexture,
Napi::Value::From(info.Env(), WIDTH),
Napi::Value::From(info.Env(), HEIGHT),
});
startup.set_value();
});
jsPromise = jsPromise.Get("then").As<Napi::Function>().Call(jsPromise, {jsOnFulfilled}).As<Napi::Promise>();
CatchAndLogError(jsPromise);
});
// Wait for `AddToContextAsync` to be called.
addToContext.get_future().wait();
// Render a frame so that `AddToContextAsync` will complete.
deviceUpdate.Finish();
device.FinishRenderingCurrentFrame();
// Reopen the gate so JS can continue running (startup may issue bgfx commands).
device.StartRenderingCurrentFrame();
deviceUpdate.Start();
// Wait for `startup` to finish.
startup.get_future().wait();
// Close the frame opened above.
deviceUpdate.Finish();
device.FinishRenderingCurrentFrame();
struct Asset
{
const char* Name;
const char* Url;
};
std::array<Asset, 3> assets = {
Asset{"BoomBox", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/BoomBox/glTF/BoomBox.gltf"},
Asset{"GlamVelvetSofa", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/GlamVelvetSofa/glTF/GlamVelvetSofa.gltf"},
Asset{"MaterialsVariantsShoe", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/MaterialsVariantsShoe/glTF/MaterialsVariantsShoe.gltf"},
};
for (const auto& asset : assets)
{
// Tell RenderDoc to start capturing.
RenderDoc::StartFrameCapture(d3dDevice.get());
// Start rendering a frame to unblock the JavaScript again.
device.StartRenderingCurrentFrame();
deviceUpdate.Start();
std::promise<void> loadAndRenderAsset{};
// Call `loadAndRenderAssetAsync` with the asset URL.
loader.Dispatch([&loadAndRenderAsset, &asset](Napi::Env env) {
std::cout << "Loading " << asset.Name << std::endl;
auto jsPromise = env.Global().Get("loadAndRenderAssetAsync").As<Napi::Function>().Call({Napi::String::From(env, asset.Url)}).As<Napi::Promise>();
auto jsOnFulfilled = Napi::Function::New(env, [&loadAndRenderAsset](const Napi::CallbackInfo&) {
loadAndRenderAsset.set_value();
});
jsPromise = jsPromise.Get("then").As<Napi::Function>().Call(jsPromise, {jsOnFulfilled}).As<Napi::Promise>();
CatchAndLogError(jsPromise);
});
// Wait for the function to complete.
loadAndRenderAsset.get_future().wait();
// Finish rendering the frame.
deviceUpdate.Finish();
device.FinishRenderingCurrentFrame();
// Tell RenderDoc to stop capturing.
RenderDoc::StopFrameCapture(d3dDevice.get());
// Save the texture into an PNG next to the executable.
auto filePath = GetModulePath() / asset.Name;
filePath.concat(".png");
std::cout << "Writing " << filePath.string() << std::endl;
// See https://github.com/Microsoft/DirectXTK/wiki/ScreenGrab#srgb-vs-linear-color-space
winrt::check_hresult(DirectX::SaveWICTextureToFile(d3dDeviceContext.get(), outputTexture.get(), GUID_ContainerFormatPng, filePath.c_str(), nullptr, nullptr, true));
}
return 0;
}