feat(host): vendor PyroWave + minimal Granite subset as crates/pyrowave-sys
Phase 0 of design/pyrowave-codec-plan.md — the opt-in wired-LAN ultra-low- latency codec. Vendored at upstream 509e4f88 (API 0.4.0, Granite 44362775, volk + vulkan-headers pins in PUNKTFUNK-VENDOR.txt), pruned to the 6.6 MB the standalone no-renderer build needs; scripts/vendor-pyrowave.sh reproduces the tree (a pin bump is protocol-affecting, plan §4.2). build.rs drives the wrapper CMakeLists (static archives incl. a static C-API lib upstream only ships shared) + bindgen over pyrowave.h; Linux and Windows only, empty stub elsewhere (Apple gets a native Metal port, §4.7). Offline-safe by construction: no network, no system lib, vendored Vulkan headers — same model as the opus dep (flatpak builder has no network). Phase-0 validation on .21 (RTX 5070 Ti, driver 610.43.03): - upstream pyrowave-c-test + interop test (incl. dmabuf/DRM-modifier Vulkan<->Vulkan) pass, from the pristine AND the pruned tree - GPU kernel times at ~1.6 bpp noise: encode/decode 0.090/0.042 ms @800p, 0.146/0.067 @1080p, 0.226/0.103 @1440p, 0.477/0.201 @4K — order of magnitude under NVENC's 1-2 ms retrieve, CBR lands within ~100 B of target - cargo test -p pyrowave-sys green (static link + API-version pin check) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,344 @@
|
||||
/* Copyright (c) 2017-2026 Hans-Kristian Arntzen
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#define NOMINMAX
|
||||
#include "application.hpp"
|
||||
#include "asset_manager.hpp"
|
||||
#include "thread_group.hpp"
|
||||
#ifdef HAVE_GRANITE_RENDERER
|
||||
#include "material_manager.hpp"
|
||||
#include "common_renderer_data.hpp"
|
||||
#endif
|
||||
#ifdef HAVE_GRANITE_AUDIO
|
||||
#include "audio_mixer.hpp"
|
||||
#endif
|
||||
|
||||
using namespace Vulkan;
|
||||
|
||||
namespace Granite
|
||||
{
|
||||
Application::~Application()
|
||||
{
|
||||
auto *group = GRANITE_THREAD_GROUP();
|
||||
if (group)
|
||||
group->wait_idle();
|
||||
|
||||
teardown_wsi();
|
||||
}
|
||||
|
||||
bool Application::init_platform(std::unique_ptr<WSIPlatform> new_platform)
|
||||
{
|
||||
#ifdef HAVE_GRANITE_RENDERER
|
||||
if (auto *common = GRANITE_COMMON_RENDERER_DATA())
|
||||
common->initialize_static_assets(GRANITE_ASSET_MANAGER(), GRANITE_FILESYSTEM());
|
||||
#endif
|
||||
platform = std::move(new_platform);
|
||||
application_wsi.set_platform(platform.get());
|
||||
|
||||
if (auto *event = GRANITE_EVENT_MANAGER())
|
||||
event->enqueue_latched<ApplicationWSIPlatformEvent>(*platform);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::teardown_wsi()
|
||||
{
|
||||
if (auto *event = GRANITE_EVENT_MANAGER())
|
||||
{
|
||||
event->dequeue_all_latched(DevicePipelineReadyEvent::get_type_id());
|
||||
event->dequeue_all_latched(DeviceShaderModuleReadyEvent::get_type_id());
|
||||
event->dequeue_all_latched(ApplicationWSIPlatformEvent::get_type_id());
|
||||
}
|
||||
application_wsi.teardown();
|
||||
ready_modules = false;
|
||||
ready_pipelines = false;
|
||||
}
|
||||
|
||||
bool Application::init_wsi(Vulkan::ContextHandle context)
|
||||
{
|
||||
if (context)
|
||||
{
|
||||
if (!application_wsi.init_from_existing_context(std::move(context)))
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Context::SystemHandles system_handles;
|
||||
system_handles.filesystem = GRANITE_FILESYSTEM();
|
||||
system_handles.thread_group = GRANITE_THREAD_GROUP();
|
||||
system_handles.asset_manager = GRANITE_ASSET_MANAGER();
|
||||
system_handles.timeline_trace_file = system_handles.thread_group->get_timeline_trace_file();
|
||||
|
||||
if (!application_wsi.init_context_from_platform(
|
||||
system_handles.thread_group->get_num_threads() + 1,
|
||||
system_handles))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!application_wsi.init_device())
|
||||
return false;
|
||||
|
||||
application_wsi.get_device().begin_shader_caches();
|
||||
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("wsi-init-swapchain");
|
||||
if (!platform->has_external_swapchain() && !application_wsi.init_surface_swapchain())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::poll_input_tracker_async(Granite::InputTrackerHandler *override_handler)
|
||||
{
|
||||
get_platform().poll_input_async(override_handler);
|
||||
}
|
||||
|
||||
bool Application::poll()
|
||||
{
|
||||
auto &wsi = get_wsi();
|
||||
if (!get_platform().alive(wsi))
|
||||
return false;
|
||||
|
||||
if (requested_shutdown)
|
||||
return false;
|
||||
|
||||
auto *fs = GRANITE_FILESYSTEM();
|
||||
auto *em = GRANITE_EVENT_MANAGER();
|
||||
if (fs)
|
||||
fs->poll_notifications();
|
||||
if (em)
|
||||
em->dispatch();
|
||||
|
||||
#ifdef HAVE_GRANITE_AUDIO
|
||||
auto *backend = GRANITE_AUDIO_BACKEND();
|
||||
if (backend)
|
||||
backend->heartbeat();
|
||||
auto *am = GRANITE_AUDIO_MIXER();
|
||||
if (am)
|
||||
{
|
||||
// Pump through events from audio thread.
|
||||
auto &queue = am->get_message_queue();
|
||||
Util::MessageQueuePayload payload;
|
||||
while ((payload = queue.read_message()))
|
||||
{
|
||||
auto &event = payload.as<Event>();
|
||||
if (em)
|
||||
em->dispatch_inline(event);
|
||||
queue.recycle_payload(std::move(payload));
|
||||
}
|
||||
|
||||
// Recycle dead streams.
|
||||
am->dispose_dead_streams();
|
||||
}
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Application::check_initialization_progress()
|
||||
{
|
||||
auto &device = get_wsi().get_device();
|
||||
|
||||
if (!ready_modules)
|
||||
{
|
||||
if (device.query_initialization_progress(Device::InitializationStage::CacheMaintenance) >= 100 &&
|
||||
device.query_initialization_progress(Device::InitializationStage::ShaderModules) >= 100)
|
||||
{
|
||||
if (auto *manager = GRANITE_ASSET_MANAGER())
|
||||
{
|
||||
// Now is a good time to kick shader manager since it might require compute shaders for decode.
|
||||
manager->iterate(GRANITE_THREAD_GROUP());
|
||||
}
|
||||
|
||||
if (auto *event = GRANITE_EVENT_MANAGER())
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("dispatch-ready-modules");
|
||||
#ifdef HAVE_GRANITE_RENDERER
|
||||
auto *manager = &device.get_shader_manager();
|
||||
#else
|
||||
constexpr Vulkan::ShaderManager *manager = nullptr;
|
||||
#endif
|
||||
event->enqueue_latched<DeviceShaderModuleReadyEvent>(&device, manager);
|
||||
}
|
||||
ready_modules = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ready_pipelines)
|
||||
{
|
||||
if (device.query_initialization_progress(Device::InitializationStage::Pipelines) >= 100)
|
||||
{
|
||||
if (auto *event = GRANITE_EVENT_MANAGER())
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("dispatch-ready-pipelines");
|
||||
#ifdef HAVE_GRANITE_RENDERER
|
||||
auto *manager = &device.get_shader_manager();
|
||||
#else
|
||||
constexpr Vulkan::ShaderManager *manager = nullptr;
|
||||
#endif
|
||||
event->enqueue_latched<DevicePipelineReadyEvent>(&device, manager);
|
||||
}
|
||||
ready_pipelines = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::show_message_box(const std::string &str, Vulkan::WSIPlatform::MessageType type)
|
||||
{
|
||||
if (platform)
|
||||
platform->show_message_box(str, type);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case Vulkan::WSIPlatform::MessageType::Error:
|
||||
LOGE("%s\n", str.c_str());
|
||||
break;
|
||||
|
||||
case Vulkan::WSIPlatform::MessageType::Warning:
|
||||
LOGW("%s\n", str.c_str());
|
||||
break;
|
||||
|
||||
case Vulkan::WSIPlatform::MessageType::Info:
|
||||
LOGI("%s\n", str.c_str());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Application::run_frame()
|
||||
{
|
||||
check_initialization_progress();
|
||||
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("wsi-begin-frame");
|
||||
if (!application_wsi.begin_frame())
|
||||
{
|
||||
LOGE("Failed to begin frame. Fatal error. Shutting down.\n");
|
||||
request_shutdown();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
double smooth_frame_time = application_wsi.get_smooth_frame_time();
|
||||
double smooth_elapsed = application_wsi.get_smooth_elapsed_time();
|
||||
|
||||
if (!ready_modules)
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("render-early-loading");
|
||||
render_early_loading(smooth_frame_time, smooth_elapsed);
|
||||
}
|
||||
else if (!ready_pipelines)
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("render-loading");
|
||||
render_loading(smooth_frame_time, smooth_elapsed);
|
||||
}
|
||||
else
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("render-frame");
|
||||
render_frame(smooth_frame_time, smooth_elapsed);
|
||||
}
|
||||
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("wsi-end-frame");
|
||||
application_wsi.end_frame();
|
||||
}
|
||||
|
||||
{
|
||||
GRANITE_SCOPED_TIMELINE_EVENT("post-frame");
|
||||
post_frame();
|
||||
}
|
||||
}
|
||||
|
||||
void Application::render_early_loading(double, double)
|
||||
{
|
||||
auto &device = application_wsi.get_device();
|
||||
auto cmd = device.request_command_buffer();
|
||||
auto rp = device.get_swapchain_render_pass(SwapchainRenderPass::ColorOnly);
|
||||
rp.clear_color[0].float32[0] = 0.01f;
|
||||
rp.clear_color[0].float32[2] = 0.02f;
|
||||
rp.clear_color[0].float32[3] = 0.03f;
|
||||
cmd->begin_render_pass(rp);
|
||||
auto vp = cmd->get_viewport();
|
||||
|
||||
VkClearRect rect = {};
|
||||
rect.layerCount = 1;
|
||||
rect.rect.extent = {
|
||||
uint32_t(vp.width * 0.01f * float(device.query_initialization_progress(Device::InitializationStage::ShaderModules))),
|
||||
uint32_t(vp.height),
|
||||
};
|
||||
|
||||
VkClearValue value = {};
|
||||
value.color.float32[0] = 0.08f;
|
||||
value.color.float32[1] = 0.01f;
|
||||
value.color.float32[2] = 0.01f;
|
||||
|
||||
if (rect.rect.extent.width > 0)
|
||||
cmd->clear_quad(0, rect, value);
|
||||
|
||||
cmd->end_render_pass();
|
||||
device.submit(cmd);
|
||||
}
|
||||
|
||||
void Application::render_loading(double, double)
|
||||
{
|
||||
auto &device = application_wsi.get_device();
|
||||
auto cmd = device.request_command_buffer();
|
||||
auto rp = device.get_swapchain_render_pass(SwapchainRenderPass::ColorOnly);
|
||||
rp.clear_color[0].float32[0] = 0.01f;
|
||||
rp.clear_color[0].float32[2] = 0.02f;
|
||||
rp.clear_color[0].float32[3] = 0.03f;
|
||||
cmd->begin_render_pass(rp);
|
||||
auto vp = cmd->get_viewport();
|
||||
|
||||
VkClearRect rect = {};
|
||||
rect.layerCount = 1;
|
||||
rect.rect.extent = {
|
||||
uint32_t(vp.width * 0.01f * float(device.query_initialization_progress(Device::InitializationStage::Pipelines))),
|
||||
uint32_t(vp.height),
|
||||
};
|
||||
|
||||
VkClearValue value = {};
|
||||
value.color.float32[0] = 0.01f;
|
||||
value.color.float32[1] = 0.08f;
|
||||
value.color.float32[2] = 0.01f;
|
||||
if (rect.rect.extent.width > 0)
|
||||
cmd->clear_quad(0, rect, value);
|
||||
|
||||
cmd->end_render_pass();
|
||||
device.submit(cmd);
|
||||
}
|
||||
|
||||
void Application::post_frame()
|
||||
{
|
||||
// Texture manager might require shaders to be ready before we can submit work.
|
||||
if (ready_modules)
|
||||
{
|
||||
if (auto *manager = GRANITE_ASSET_MANAGER())
|
||||
manager->iterate(GRANITE_THREAD_GROUP());
|
||||
}
|
||||
|
||||
if (auto *manager = Global::material_manager())
|
||||
manager->iterate(Global::asset_manager());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user