Add the picked glTF model to the model list, create a screenshot
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "image_writer.h"
|
||||
|
||||
#include "utils/thread_safe.h"
|
||||
#include <cassert>
|
||||
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "stb_image_write.h"
|
||||
|
||||
void write_image(
|
||||
vk::raii::Device & device,
|
||||
thread_safe<vk::raii::Queue> & queue,
|
||||
uint32_t queue_family_index,
|
||||
const std::filesystem::path & path,
|
||||
vk::Image image,
|
||||
const vk::ImageCreateInfo & info)
|
||||
{
|
||||
assert(info.usage & vk::ImageUsageFlagBits::eTransferSrc);
|
||||
assert(info.extent.depth == 1);
|
||||
assert(info.format == vk::Format::eR8G8B8A8Srgb);
|
||||
|
||||
buffer_allocation output_buffer{
|
||||
device,
|
||||
vk::BufferCreateInfo{
|
||||
.size = info.extent.height * info.extent.width * 4,
|
||||
.usage = vk::BufferUsageFlagBits::eTransferDst,
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_RANDOM_BIT,
|
||||
.usage = VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO,
|
||||
},
|
||||
"Saved image buffer"};
|
||||
|
||||
vk::raii::CommandPool cp{
|
||||
device,
|
||||
vk::CommandPoolCreateInfo{
|
||||
.queueFamilyIndex = queue_family_index,
|
||||
}};
|
||||
|
||||
vk::raii::CommandBuffer command_buffer = std::move(device.allocateCommandBuffers(
|
||||
{
|
||||
.commandPool = *cp,
|
||||
.level = vk::CommandBufferLevel::ePrimary,
|
||||
.commandBufferCount = 1,
|
||||
})[0]);
|
||||
|
||||
vk::raii::Fence fence = device.createFence({});
|
||||
|
||||
command_buffer.begin(vk::CommandBufferBeginInfo{});
|
||||
|
||||
command_buffer.pipelineBarrier(
|
||||
vk::PipelineStageFlagBits::eColorAttachmentOutput,
|
||||
vk::PipelineStageFlagBits::eTransfer,
|
||||
vk::DependencyFlags{},
|
||||
{},
|
||||
{},
|
||||
vk::ImageMemoryBarrier{
|
||||
.srcAccessMask = vk::AccessFlagBits::eColorAttachmentWrite,
|
||||
.dstAccessMask = vk::AccessFlagBits::eTransferRead,
|
||||
.oldLayout = vk::ImageLayout::eColorAttachmentOptimal,
|
||||
.newLayout = vk::ImageLayout::eTransferSrcOptimal,
|
||||
.image = image,
|
||||
.subresourceRange = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.baseMipLevel = 0,
|
||||
.levelCount = 1,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
});
|
||||
|
||||
command_buffer.copyImageToBuffer(
|
||||
image,
|
||||
vk::ImageLayout::eTransferSrcOptimal,
|
||||
output_buffer,
|
||||
vk::BufferImageCopy{
|
||||
.bufferOffset = 0,
|
||||
.bufferRowLength = 0,
|
||||
.bufferImageHeight = 0,
|
||||
.imageSubresource = {
|
||||
.aspectMask = vk::ImageAspectFlagBits::eColor,
|
||||
.mipLevel = 0,
|
||||
.baseArrayLayer = 0,
|
||||
.layerCount = 1,
|
||||
},
|
||||
.imageOffset = {0, 0, 0},
|
||||
.imageExtent = info.extent,
|
||||
});
|
||||
|
||||
command_buffer.end();
|
||||
queue.lock()->submit(vk::SubmitInfo{
|
||||
.commandBufferCount = 1,
|
||||
.pCommandBuffers = &*command_buffer,
|
||||
},
|
||||
*fence);
|
||||
|
||||
if (auto result = device.waitForFences(*fence, true, 1'000'000'000); result == vk::Result::eSuccess)
|
||||
stbi_write_png(path.c_str(), info.extent.width, info.extent.height, 4, output_buffer.data(), 0);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* WiVRn VR streaming
|
||||
* Copyright (C) 2025 Guillaume Meunier <guillaume.meunier@centraliens.net>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "utils/thread_safe.h"
|
||||
#include "vk/allocation.h"
|
||||
#include <filesystem>
|
||||
#include <vulkan/vulkan.hpp>
|
||||
|
||||
void write_image(
|
||||
vk::raii::Device & device,
|
||||
thread_safe<vk::raii::Queue> & queue,
|
||||
uint32_t queue_family_index,
|
||||
const std::filesystem::path & path,
|
||||
vk::Image image,
|
||||
const vk::ImageCreateInfo & info);
|
||||
|
||||
inline void write_image(
|
||||
vk::raii::Device & device,
|
||||
thread_safe<vk::raii::Queue> & queue,
|
||||
uint32_t queue_family_index,
|
||||
const std::filesystem::path & path,
|
||||
const image_allocation & image)
|
||||
{
|
||||
write_image(device, queue, queue_family_index, path, (vk::Image)image, image.info());
|
||||
}
|
||||
@@ -26,12 +26,18 @@
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
#include "imspinner.h"
|
||||
#include "render/image_writer.h"
|
||||
#include "render/scene_renderer.h"
|
||||
#include "utils/files.h"
|
||||
#include "utils/i18n.h"
|
||||
#include "utils/json_string.h"
|
||||
#include "vk/allocation.h"
|
||||
#include <algorithm>
|
||||
#include <boost/url/url.hpp>
|
||||
#include <boost/url/url_view.hpp>
|
||||
#include <chrono>
|
||||
#include <spdlog/fmt/chrono.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include "IconsFontAwesome6.h"
|
||||
|
||||
@@ -53,8 +59,12 @@ void scenes::lobby::save_environment_json()
|
||||
<< ",\"description\":" << json_string(model.description)
|
||||
<< ",\"screenshot\":" << json_string(model.screenshot_url)
|
||||
<< ",\"url\":" << json_string(model.gltf_url)
|
||||
<< ",\"size\":" << model.size
|
||||
<< "},";
|
||||
<< ",\"size\":" << model.size;
|
||||
|
||||
// TODO only if not the default value
|
||||
ss << ",\"local_screenshot\":" << json_string(model.local_screenshot_path);
|
||||
ss << ",\"local_path\":" << json_string(model.local_gltf_path);
|
||||
ss << "},";
|
||||
}
|
||||
if (not empty)
|
||||
ss.seekp(-1, ss.cur);
|
||||
@@ -112,8 +122,16 @@ std::vector<scenes::lobby::environment_model> scenes::lobby::load_environment_js
|
||||
model.size = val.get_int64();
|
||||
|
||||
model.builtin = false;
|
||||
model.local_screenshot_path = application::get_config_path() / "environments" / (model.name + ".png");
|
||||
model.local_gltf_path = application::get_config_path() / "environments" / (model.name + ".glb");
|
||||
|
||||
if (auto val = i["local_screenshot"]; val.is_string())
|
||||
model.local_screenshot_path = val.get_string().value();
|
||||
else
|
||||
model.local_screenshot_path = application::get_config_path() / "environments" / (model.name + ".png");
|
||||
|
||||
if (auto val = i["local_path"]; val.is_string())
|
||||
model.local_gltf_path = val.get_string().value();
|
||||
else
|
||||
model.local_gltf_path = application::get_config_path() / "environments" / (model.name + ".glb");
|
||||
|
||||
models.push_back(std::move(model));
|
||||
}
|
||||
@@ -180,18 +198,95 @@ void scenes::lobby::update_file_picker()
|
||||
load_environment_status = "";
|
||||
future_environment = utils::async<std::pair<std::string, std::shared_ptr<entt::registry>>, float>(
|
||||
[this](auto token, file_picker_result environment) {
|
||||
auto local_path = application::get_cache_path() / "local.glb";
|
||||
auto t = std::chrono::system_clock::now();
|
||||
auto local_path = application::get_cache_path() / fmt::format("local-{:%F-%T}.glb", t);
|
||||
auto local_screenshot_path = application::get_cache_path() / fmt::format("local-{:%F-%T}.png", t);
|
||||
|
||||
utils::write_whole_file(local_path, (std::span<const std::byte>)environment.file);
|
||||
|
||||
// TODO remove only local.glb from cache
|
||||
clear_gltf_cache();
|
||||
environment_model model{
|
||||
.name = _S("Locally loaded environment"),
|
||||
.author = "",
|
||||
.description = "",
|
||||
.screenshot_url = "",
|
||||
.gltf_url = fmt::format("local-{:%F-%T}", t), // Used as key
|
||||
.size = environment.file.size(),
|
||||
.local_screenshot_path = local_screenshot_path,
|
||||
.local_gltf_path = local_path,
|
||||
};
|
||||
|
||||
return std::make_pair(
|
||||
local_path,
|
||||
load_gltf(local_path, [&](float progress) {
|
||||
token.set_progress(progress);
|
||||
}));
|
||||
int n = 2;
|
||||
while (std::ranges::find(local_environments, model.name, &environment_model::name) != local_environments.end())
|
||||
{
|
||||
model.name = fmt::format(_F("Locally loaded environment ({})"), n++);
|
||||
}
|
||||
|
||||
local_environments.push_back(model);
|
||||
std::ranges::sort(local_environments, std::less{});
|
||||
save_environment_json();
|
||||
|
||||
std::shared_ptr<entt::registry> env = load_gltf(local_path, [&](float progress) { token.set_progress(progress); });
|
||||
|
||||
// Create a screenshot of the loaded environment
|
||||
scene_renderer::frame_info frame{
|
||||
.projection = projection_matrix(
|
||||
XrFovf{
|
||||
.angleLeft = -0.7,
|
||||
.angleRight = 0.7,
|
||||
.angleUp = 0.7,
|
||||
.angleDown = -0.7,
|
||||
},
|
||||
constants::lobby::near_plane),
|
||||
.view = view_matrix(XrPosef{
|
||||
.orientation = {0, 0, 0, 1},
|
||||
.position = {0, 1.6, 0},
|
||||
})};
|
||||
|
||||
image_allocation output{
|
||||
device,
|
||||
vk::ImageCreateInfo{
|
||||
.imageType = vk::ImageType::e2D,
|
||||
.format = vk::Format::eR8G8B8A8Srgb,
|
||||
.extent = {512, 512, 1},
|
||||
.mipLevels = 1,
|
||||
.arrayLayers = 1,
|
||||
.usage = vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eTransferSrc,
|
||||
},
|
||||
VmaAllocationCreateInfo{
|
||||
.usage = VmaMemoryUsage::VMA_MEMORY_USAGE_AUTO,
|
||||
},
|
||||
"Screenshot image",
|
||||
};
|
||||
|
||||
scene_renderer local_renderer{device, physical_device, queue, queue_family_index};
|
||||
local_renderer.start_frame();
|
||||
local_renderer.render(
|
||||
*env,
|
||||
{
|
||||
constants::lobby::sky_color.r,
|
||||
constants::lobby::sky_color.g,
|
||||
constants::lobby::sky_color.b,
|
||||
constants::lobby::sky_color.a,
|
||||
},
|
||||
layer_lobby,
|
||||
vk::Extent2D{
|
||||
output.info().extent.width,
|
||||
output.info().extent.height,
|
||||
|
||||
}, // Output size
|
||||
output.info().format, // Output format
|
||||
vk::Format::eD32Sfloat, // Depth format
|
||||
output, // Output image
|
||||
vk::Image{}, // Depth image
|
||||
vk::Image{}, // Foveation image
|
||||
{&frame, 1}, // View info
|
||||
false);
|
||||
local_renderer.end_frame();
|
||||
local_renderer.wait_idle(); // TODO get a semaphore from end_frame instead
|
||||
|
||||
write_image(device, queue, queue_family_index, local_screenshot_path, output);
|
||||
|
||||
return std::make_pair(local_path, env);
|
||||
},
|
||||
lobby_file_picker_future.get());
|
||||
}
|
||||
|
||||
@@ -93,11 +93,21 @@ public:
|
||||
return *resource;
|
||||
}
|
||||
|
||||
operator const T() const
|
||||
{
|
||||
return *resource;
|
||||
}
|
||||
|
||||
operator CType()
|
||||
{
|
||||
return CType(*resource);
|
||||
}
|
||||
|
||||
operator const CType() const
|
||||
{
|
||||
return CType(*resource);
|
||||
}
|
||||
|
||||
operator bool() const
|
||||
{
|
||||
return *resource;
|
||||
|
||||
Reference in New Issue
Block a user