|
| 1 | +#include <Babylon/AppRuntime.h> |
| 2 | +#include <Babylon/Graphics/Device.h> |
| 3 | +#include <Babylon/ScriptLoader.h> |
| 4 | +#include <Babylon/Plugins/ExternalTexture.h> |
| 5 | +#include <Babylon/Plugins/NativeEngine.h> |
| 6 | +#include <Babylon/Polyfills/Console.h> |
| 7 | +#include <Babylon/Polyfills/Window.h> |
| 8 | +#include <Babylon/Polyfills/XMLHttpRequest.h> |
| 9 | + |
| 10 | +#include <winrt/base.h> |
| 11 | + |
| 12 | +#include <WICTextureLoader.h> |
| 13 | +#include <ScreenGrab.h> |
| 14 | +#include <wincodec.h> |
| 15 | + |
| 16 | +#include <filesystem> |
| 17 | +#include <iostream> |
| 18 | + |
| 19 | +#include "RenderDoc.h" |
| 20 | + |
| 21 | +namespace |
| 22 | +{ |
| 23 | + constexpr const uint32_t WIDTH = 1024; |
| 24 | + constexpr const uint32_t HEIGHT = 1024; |
| 25 | + |
| 26 | + std::filesystem::path GetModulePath() |
| 27 | + { |
| 28 | + WCHAR modulePath[4096]; |
| 29 | + DWORD result = GetModuleFileNameW(nullptr, modulePath, ARRAYSIZE(modulePath)); |
| 30 | + winrt::check_bool(result != 0 && result != std::size(modulePath)); |
| 31 | + return std::filesystem::path{modulePath}.parent_path(); |
| 32 | + } |
| 33 | + |
| 34 | + winrt::com_ptr<ID3D11Device> CreateD3DDevice() |
| 35 | + { |
| 36 | + winrt::com_ptr<ID3D11Device> d3dDevice{}; |
| 37 | + uint32_t flags = D3D11_CREATE_DEVICE_SINGLETHREADED | D3D11_CREATE_DEVICE_BGRA_SUPPORT; |
| 38 | + winrt::check_hresult(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags, nullptr, 0, D3D11_SDK_VERSION, d3dDevice.put(), nullptr, nullptr)); |
| 39 | + return d3dDevice; |
| 40 | + } |
| 41 | + |
| 42 | + winrt::com_ptr<ID3D11Texture2D> CreateD3DRenderTargetTexture(ID3D11Device* d3dDevice) |
| 43 | + { |
| 44 | + D3D11_TEXTURE2D_DESC desc{}; |
| 45 | + desc.Width = WIDTH; |
| 46 | + desc.Height = HEIGHT; |
| 47 | + desc.MipLevels = 1; |
| 48 | + desc.ArraySize = 1; |
| 49 | + desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| 50 | + desc.SampleDesc = {4, 0}; |
| 51 | + desc.Usage = D3D11_USAGE_DEFAULT; |
| 52 | + desc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE; |
| 53 | + desc.CPUAccessFlags = 0; |
| 54 | + desc.MiscFlags = 0; |
| 55 | + |
| 56 | + winrt::com_ptr<ID3D11Texture2D> texture; |
| 57 | + winrt::check_hresult(d3dDevice->CreateTexture2D(&desc, nullptr, texture.put())); |
| 58 | + return texture; |
| 59 | + } |
| 60 | + |
| 61 | + Babylon::Graphics::Device CreateGraphicsDevice(ID3D11Device* d3dDevice) |
| 62 | + { |
| 63 | + Babylon::Graphics::Configuration config{}; |
| 64 | + config.Device = d3dDevice; |
| 65 | + config.Width = WIDTH; |
| 66 | + config.Height = HEIGHT; |
| 67 | + return Babylon::Graphics::Device(config); |
| 68 | + } |
| 69 | + |
| 70 | + void CatchAndLogError(Napi::Promise jsPromise) |
| 71 | + { |
| 72 | + auto jsOnRejected = Napi::Function::New(jsPromise.Env(), [](const Napi::CallbackInfo& info) { |
| 73 | + auto console = info.Env().Global().Get("console").As<Napi::Object>(); |
| 74 | + console.Get("error").As<Napi::Function>().Call(console, {info[0]}); |
| 75 | + std::exit(1); |
| 76 | + }); |
| 77 | + |
| 78 | + jsPromise.Get("catch").As<Napi::Function>().Call(jsPromise, {jsOnRejected}); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +int main() |
| 83 | +{ |
| 84 | + // Initialize RenderDoc. |
| 85 | + RenderDoc::Init(); |
| 86 | + |
| 87 | + // Create a DirectX device. |
| 88 | + auto d3dDevice = CreateD3DDevice(); |
| 89 | + |
| 90 | + // Get the immediate context for DirectXTK when saving the texture. |
| 91 | + winrt::com_ptr<ID3D11DeviceContext> d3dDeviceContext; |
| 92 | + d3dDevice->GetImmediateContext(d3dDeviceContext.put()); |
| 93 | + |
| 94 | + // Create the Babylon Native graphics device and update. |
| 95 | + auto device = CreateGraphicsDevice(d3dDevice.get()); |
| 96 | + auto deviceUpdate = device.GetUpdate("update"); |
| 97 | + |
| 98 | + // Start rendering a frame to unblock the JavaScript from queuing graphics |
| 99 | + // commands. |
| 100 | + device.StartRenderingCurrentFrame(); |
| 101 | + deviceUpdate.Start(); |
| 102 | + |
| 103 | + // Create a Babylon Native application runtime which hosts a JavaScript |
| 104 | + // engine on a new thread. |
| 105 | + Babylon::AppRuntime runtime{}; |
| 106 | + runtime.Dispatch([&device](Napi::Env env) { |
| 107 | + // Add the Babylon Native graphics device to the JavaScript environment. |
| 108 | + device.AddToJavaScript(env); |
| 109 | + |
| 110 | + // Initialize the console polyfill. |
| 111 | + Babylon::Polyfills::Console::Initialize(env, [](const char* message, auto) { |
| 112 | + std::cout << message; |
| 113 | + }); |
| 114 | + |
| 115 | + // Initialize the window, XMLHttpRequest, and NativeEngine polyfills. |
| 116 | + Babylon::Polyfills::Window::Initialize(env); |
| 117 | + Babylon::Polyfills::XMLHttpRequest::Initialize(env); |
| 118 | + Babylon::Plugins::NativeEngine::Initialize(env); |
| 119 | + }); |
| 120 | + |
| 121 | + // Load the scripts for Babylon.js core and loaders plus this app's index.js. |
| 122 | + Babylon::ScriptLoader loader{runtime}; |
| 123 | + loader.LoadScript("app:///Scripts/babylon.max.js"); |
| 124 | + loader.LoadScript("app:///Scripts/babylonjs.loaders.js"); |
| 125 | + loader.LoadScript("app:///Scripts/index.js"); |
| 126 | + |
| 127 | + // Create a render target texture for the output. |
| 128 | + winrt::com_ptr<ID3D11Texture2D> outputTexture = CreateD3DRenderTargetTexture(d3dDevice.get()); |
| 129 | + |
| 130 | + std::promise<void> addToContext{}; |
| 131 | + std::promise<void> startup{}; |
| 132 | + |
| 133 | + // Create an external texture for the render target texture and pass it to |
| 134 | + // the `startup` JavaScript function. |
| 135 | + loader.Dispatch([externalTexture = Babylon::Plugins::ExternalTexture{outputTexture.get()}, &addToContext, &startup](Napi::Env env) { |
| 136 | + auto jsPromise = externalTexture.AddToContextAsync(env); |
| 137 | + addToContext.set_value(); |
| 138 | + |
| 139 | + auto jsOnFulfilled = Napi::Function::New(env, [&startup](const Napi::CallbackInfo& info) { |
| 140 | + auto nativeTexture = info[0]; |
| 141 | + info.Env().Global().Get("startup").As<Napi::Function>().Call( |
| 142 | + { |
| 143 | + nativeTexture, |
| 144 | + Napi::Value::From(info.Env(), WIDTH), |
| 145 | + Napi::Value::From(info.Env(), HEIGHT), |
| 146 | + }); |
| 147 | + startup.set_value(); |
| 148 | + }); |
| 149 | + |
| 150 | + jsPromise = jsPromise.Get("then").As<Napi::Function>().Call(jsPromise, {jsOnFulfilled}).As<Napi::Promise>(); |
| 151 | + |
| 152 | + CatchAndLogError(jsPromise); |
| 153 | + }); |
| 154 | + |
| 155 | + // Wait for `AddToContextAsync` to be called. |
| 156 | + addToContext.get_future().wait(); |
| 157 | + |
| 158 | + // Render a frame so that `AddToContextAsync` will complete. |
| 159 | + deviceUpdate.Finish(); |
| 160 | + device.FinishRenderingCurrentFrame(); |
| 161 | + |
| 162 | + // Wait for `startup` to finish. |
| 163 | + startup.get_future().wait(); |
| 164 | + |
| 165 | + struct Asset |
| 166 | + { |
| 167 | + const char* Name; |
| 168 | + const char* Url; |
| 169 | + }; |
| 170 | + |
| 171 | + std::array<Asset, 3> assets = { |
| 172 | + Asset{"BoomBox", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/BoomBox/glTF/BoomBox.gltf"}, |
| 173 | + Asset{"GlamVelvetSofa", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/GlamVelvetSofa/glTF/GlamVelvetSofa.gltf"}, |
| 174 | + Asset{"MaterialsVariantsShoe", "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/MaterialsVariantsShoe/glTF/MaterialsVariantsShoe.gltf"}, |
| 175 | + }; |
| 176 | + |
| 177 | + for (const auto& asset : assets) |
| 178 | + { |
| 179 | + // Tell RenderDoc to start capturing. |
| 180 | + RenderDoc::StartFrameCapture(d3dDevice.get()); |
| 181 | + |
| 182 | + // Start rendering a frame to unblock the JavaScript again. |
| 183 | + device.StartRenderingCurrentFrame(); |
| 184 | + deviceUpdate.Start(); |
| 185 | + |
| 186 | + std::promise<void> loadAndRenderAsset{}; |
| 187 | + |
| 188 | + // Call `loadAndRenderAssetAsync` with the asset URL. |
| 189 | + loader.Dispatch([&loadAndRenderAsset, &asset](Napi::Env env) { |
| 190 | + std::cout << "Loading " << asset.Name << std::endl; |
| 191 | + |
| 192 | + auto jsPromise = env.Global().Get("loadAndRenderAssetAsync").As<Napi::Function>().Call({Napi::String::From(env, asset.Url)}).As<Napi::Promise>(); |
| 193 | + |
| 194 | + auto jsOnFulfilled = Napi::Function::New(env, [&loadAndRenderAsset](const Napi::CallbackInfo&) { |
| 195 | + loadAndRenderAsset.set_value(); |
| 196 | + }); |
| 197 | + |
| 198 | + jsPromise = jsPromise.Get("then").As<Napi::Function>().Call(jsPromise, {jsOnFulfilled}).As<Napi::Promise>(); |
| 199 | + |
| 200 | + CatchAndLogError(jsPromise); |
| 201 | + }); |
| 202 | + |
| 203 | + // Wait for the function to complete. |
| 204 | + loadAndRenderAsset.get_future().wait(); |
| 205 | + |
| 206 | + // Finish rendering the frame. |
| 207 | + deviceUpdate.Finish(); |
| 208 | + device.FinishRenderingCurrentFrame(); |
| 209 | + |
| 210 | + // Tell RenderDoc to stop capturing. |
| 211 | + RenderDoc::StopFrameCapture(d3dDevice.get()); |
| 212 | + |
| 213 | + // Save the texture into an PNG next to the executable. |
| 214 | + auto filePath = GetModulePath() / asset.Name; |
| 215 | + filePath.concat(".png"); |
| 216 | + std::cout << "Writing " << filePath.string() << std::endl; |
| 217 | + |
| 218 | + // See https://github.com/Microsoft/DirectXTK/wiki/ScreenGrab#srgb-vs-linear-color-space |
| 219 | + winrt::check_hresult(DirectX::SaveWICTextureToFile(d3dDeviceContext.get(), outputTexture.get(), GUID_ContainerFormatPng, filePath.c_str(), nullptr, nullptr, true)); |
| 220 | + } |
| 221 | + |
| 222 | + return 0; |
| 223 | +} |
0 commit comments