mirror of
https://github.com/slendidev/lunar.git
synced 2026-01-30 16:28:58 +02:00
Fix initializers, more stuff
Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
@@ -418,6 +418,9 @@ Application::Application()
|
||||
}
|
||||
|
||||
m_renderer = std::make_unique<VulkanRenderer>(m_window, m_logger);
|
||||
|
||||
m_window_focused
|
||||
= (SDL_GetWindowFlags(m_window) & SDL_WINDOW_INPUT_FOCUS) != 0;
|
||||
m_renderer->set_antialiasing_immediate(
|
||||
VulkanRenderer::AntiAliasingKind::MSAA_4X);
|
||||
|
||||
@@ -480,13 +483,13 @@ auto Application::asset_directory() -> std::filesystem::path
|
||||
if (auto const *xdg_data_dirs = getenv("XDG_DATA_DIRS");
|
||||
xdg_data_dirs && *xdg_data_dirs) {
|
||||
std::string_view dirs_view { xdg_data_dirs };
|
||||
size_t start = 0;
|
||||
size_t start { 0 };
|
||||
while (start <= dirs_view.size()) {
|
||||
size_t end = dirs_view.find(':', start);
|
||||
size_t end { dirs_view.find(':', start) };
|
||||
if (end == std::string_view::npos) {
|
||||
end = dirs_view.size();
|
||||
}
|
||||
auto segment = dirs_view.substr(start, end - start);
|
||||
auto segment { dirs_view.substr(start, end - start) };
|
||||
if (!segment.empty()) {
|
||||
add_xdg_path(std::filesystem::path { segment });
|
||||
}
|
||||
@@ -497,7 +500,7 @@ auto Application::asset_directory() -> std::filesystem::path
|
||||
add_xdg_path("/usr/share");
|
||||
}
|
||||
|
||||
auto base_dir = binary_directory();
|
||||
auto base_dir { binary_directory() };
|
||||
candidates.emplace_back(base_dir / "assets");
|
||||
candidates.emplace_back(base_dir / "../assets");
|
||||
|
||||
@@ -515,9 +518,9 @@ auto Application::asset_directory() -> std::filesystem::path
|
||||
|
||||
auto Application::init_test_meshes() -> void
|
||||
{
|
||||
auto assets_dir = asset_directory();
|
||||
auto mesh_path = assets_dir / "basicmesh.glb";
|
||||
auto meshes = Mesh::load_gltf_meshes(*m_renderer, mesh_path);
|
||||
auto assets_dir { asset_directory() };
|
||||
auto mesh_path { assets_dir / "basicmesh.glb" };
|
||||
auto meshes { Mesh::load_gltf_meshes(*m_renderer, mesh_path) };
|
||||
if (!meshes) {
|
||||
m_logger.err("Failed to load test mesh: {}", mesh_path.string());
|
||||
return;
|
||||
@@ -548,6 +551,7 @@ auto Application::run() -> void
|
||||
|
||||
{
|
||||
GZoneScopedN("Input");
|
||||
m_key_state_previous = m_key_state;
|
||||
process_libinput_events();
|
||||
|
||||
while (SDL_PollEvent(&e)) {
|
||||
@@ -561,6 +565,18 @@ auto Application::run() -> void
|
||||
static_cast<uint32_t>(height));
|
||||
clamp_mouse_to_window(width, height);
|
||||
forward_to_imgui = true;
|
||||
} else if (e.type == SDL_EVENT_WINDOW_FOCUS_GAINED) {
|
||||
m_window_focused = true;
|
||||
forward_to_imgui = true;
|
||||
} else if (e.type == SDL_EVENT_WINDOW_FOCUS_LOST) {
|
||||
m_window_focused = false;
|
||||
m_ctrl_pressed_count = 0;
|
||||
m_key_state.fill(false);
|
||||
m_key_state_previous.fill(false);
|
||||
m_mouse_dx = 0.0;
|
||||
|
||||
m_mouse_dy = 0.0;
|
||||
forward_to_imgui = true;
|
||||
} else if (e.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
m_mouse_x = e.motion.x;
|
||||
m_mouse_y = e.motion.y;
|
||||
@@ -583,6 +599,15 @@ auto Application::run() -> void
|
||||
}
|
||||
}
|
||||
|
||||
bool const ctrl_down { is_key_down(KEY_LEFTCTRL)
|
||||
|| is_key_down(KEY_RIGHTCTRL) };
|
||||
{
|
||||
bool const shift_down { is_key_down(KEY_LEFTSHIFT)
|
||||
|| is_key_down(KEY_RIGHTSHIFT) };
|
||||
if (ctrl_down && shift_down && is_key_pressed(KEY_Q))
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
{
|
||||
GZoneScopedN("CameraUpdate");
|
||||
|
||||
@@ -623,19 +648,26 @@ auto Application::run() -> void
|
||||
if (camera_up.magnitude() == 0.0f)
|
||||
camera_up = world_up;
|
||||
|
||||
auto forward_dir { smath::Vec3 {
|
||||
look_dir.x(), 0.0f, look_dir.z() } };
|
||||
if (forward_dir.magnitude() > 0.0f)
|
||||
forward_dir = forward_dir.normalized_safe();
|
||||
smath::Vec3 move_dir {};
|
||||
if (is_key_pressed(KEY_W))
|
||||
move_dir += look_dir;
|
||||
if (is_key_pressed(KEY_S))
|
||||
move_dir -= look_dir;
|
||||
if (is_key_pressed(KEY_D))
|
||||
move_dir += right;
|
||||
if (is_key_pressed(KEY_A))
|
||||
move_dir -= right;
|
||||
if (is_key_pressed(KEY_SPACE))
|
||||
move_dir += world_up;
|
||||
if (is_key_pressed(KEY_LEFTSHIFT))
|
||||
move_dir -= world_up;
|
||||
|
||||
if (!ctrl_down) {
|
||||
if (is_key_down(KEY_W))
|
||||
move_dir += forward_dir;
|
||||
if (is_key_down(KEY_S))
|
||||
move_dir -= forward_dir;
|
||||
if (is_key_down(KEY_D))
|
||||
move_dir += right;
|
||||
if (is_key_down(KEY_A))
|
||||
move_dir -= right;
|
||||
if (is_key_down(KEY_SPACE))
|
||||
move_dir += world_up;
|
||||
if (is_key_down(KEY_LEFTSHIFT))
|
||||
move_dir -= world_up;
|
||||
}
|
||||
|
||||
if (move_dir.magnitude() > 0.0f) {
|
||||
constexpr float move_speed { 10.0f };
|
||||
@@ -795,11 +827,11 @@ auto Application::run() -> void
|
||||
projection[1][1] *= -1;
|
||||
auto view_projection { projection * view };
|
||||
|
||||
auto skybox_view = view;
|
||||
auto skybox_view { view };
|
||||
skybox_view[3][0] = 0.0f;
|
||||
skybox_view[3][1] = 0.0f;
|
||||
skybox_view[3][2] = 0.0f;
|
||||
m_skybox.draw(gl, projection * skybox_view);
|
||||
m_skybox.draw(gl, *m_renderer, projection * skybox_view);
|
||||
|
||||
gl.set_transform(view_projection);
|
||||
|
||||
@@ -930,6 +962,9 @@ auto Application::handle_keyboard_event(libinput_event_keyboard *event) -> void
|
||||
auto const state { libinput_event_keyboard_get_key_state(event) };
|
||||
bool const pressed { state == LIBINPUT_KEY_STATE_PRESSED };
|
||||
|
||||
if (!m_window_focused)
|
||||
return;
|
||||
|
||||
if (key == KEY_LEFTCTRL || key == KEY_RIGHTCTRL) {
|
||||
if (pressed) {
|
||||
++m_ctrl_pressed_count;
|
||||
@@ -978,9 +1013,8 @@ auto Application::handle_keyboard_event(libinput_event_keyboard *event) -> void
|
||||
}
|
||||
|
||||
if (m_show_imgui && pressed) {
|
||||
bool const shift_pressed { is_key_pressed(KEY_LEFTSHIFT)
|
||||
|| is_key_pressed(KEY_RIGHTSHIFT)
|
||||
|| (key == KEY_LEFTSHIFT && pressed)
|
||||
bool const shift_pressed { is_key_down(KEY_LEFTSHIFT)
|
||||
|| is_key_down(KEY_RIGHTSHIFT) || (key == KEY_LEFTSHIFT && pressed)
|
||||
|| (key == KEY_RIGHTSHIFT && pressed) };
|
||||
|
||||
if (auto ch { linux_key_to_char(key, shift_pressed) })
|
||||
@@ -1013,11 +1047,32 @@ auto Application::mouse_captured(bool new_state) -> void
|
||||
m_mouse_captured = new_state && !m_show_imgui;
|
||||
}
|
||||
|
||||
auto Application::is_key_pressed(uint32_t key) const -> bool
|
||||
auto Application::is_key_down(uint32_t key) const -> bool
|
||||
{
|
||||
if (key >= m_key_state.size())
|
||||
return false;
|
||||
return m_key_state[key];
|
||||
}
|
||||
|
||||
auto Application::is_key_up(uint32_t key) const -> bool
|
||||
{
|
||||
if (key >= m_key_state.size())
|
||||
return true;
|
||||
return !m_key_state[key];
|
||||
}
|
||||
|
||||
auto Application::is_key_pressed(uint32_t key) const -> bool
|
||||
{
|
||||
if (key >= m_key_state.size())
|
||||
return false;
|
||||
return m_key_state[key] && !m_key_state_previous[key];
|
||||
}
|
||||
|
||||
auto Application::is_key_released(uint32_t key) const -> bool
|
||||
{
|
||||
if (key >= m_key_state.size())
|
||||
return false;
|
||||
return !m_key_state[key] && m_key_state_previous[key];
|
||||
}
|
||||
|
||||
} // namespace Lunar
|
||||
|
||||
@@ -33,7 +33,10 @@ struct Application {
|
||||
auto mouse_captured(bool new_state) -> void;
|
||||
auto mouse_captured() const -> bool { return m_mouse_captured; }
|
||||
auto toggle_mouse_captured() -> void { mouse_captured(!m_mouse_captured); }
|
||||
auto is_key_down(uint32_t key) const -> bool;
|
||||
auto is_key_up(uint32_t key) const -> bool;
|
||||
auto is_key_pressed(uint32_t key) const -> bool;
|
||||
auto is_key_released(uint32_t key) const -> bool;
|
||||
|
||||
private:
|
||||
auto init_input() -> void;
|
||||
@@ -56,6 +59,7 @@ private:
|
||||
bool m_running { true };
|
||||
bool m_mouse_captured { false };
|
||||
bool m_show_imgui { false };
|
||||
bool m_window_focused { true };
|
||||
int m_ctrl_pressed_count { 0 };
|
||||
std::uint32_t m_screenshot_index { 0 };
|
||||
|
||||
@@ -66,6 +70,7 @@ private:
|
||||
float m_mouse_sensitivity { 0.001f };
|
||||
|
||||
std::array<bool, KEY_MAX + 1> m_key_state {};
|
||||
std::array<bool, KEY_MAX + 1> m_key_state_previous {};
|
||||
|
||||
Camera m_camera;
|
||||
PolarCoordinate m_cursor;
|
||||
|
||||
@@ -59,11 +59,11 @@ namespace Lunar {
|
||||
|
||||
CPUTexture::CPUTexture(std::filesystem::path const &path)
|
||||
{
|
||||
int width_out = 0;
|
||||
int height_out = 0;
|
||||
int channels_out = 0;
|
||||
stbi_uc *data = stbi_load(path.string().c_str(), &width_out, &height_out,
|
||||
&channels_out, STBI_rgb_alpha);
|
||||
int width_out { 0 };
|
||||
int height_out { 0 };
|
||||
int channels_out { 0 };
|
||||
stbi_uc *data { stbi_load(path.string().c_str(), &width_out, &height_out,
|
||||
&channels_out, STBI_rgb_alpha) };
|
||||
if (!data) {
|
||||
throw std::runtime_error(
|
||||
std::format("Failed to load texture: {}", path.string()));
|
||||
|
||||
@@ -78,7 +78,7 @@ auto DescriptorAllocatorGrowable::destroy_pools(VkDevice dev) -> void
|
||||
auto DescriptorAllocatorGrowable::allocate(Logger &logger, VkDevice dev,
|
||||
VkDescriptorSetLayout layout, void *p_next) -> VkDescriptorSet
|
||||
{
|
||||
auto pool_to_use = get_pool(dev);
|
||||
auto pool_to_use { get_pool(dev) };
|
||||
|
||||
VkDescriptorSetAllocateInfo alloci {};
|
||||
alloci.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
|
||||
@@ -61,7 +61,7 @@ auto Mesh::load_gltf_meshes(
|
||||
{
|
||||
renderer.logger().debug("Loading GLTF from file: {}", path);
|
||||
|
||||
auto data = fastgltf::GltfDataBuffer::FromPath(path);
|
||||
auto data { fastgltf::GltfDataBuffer::FromPath(path) };
|
||||
if (data.error() != fastgltf::Error::None) {
|
||||
renderer.logger().err("Failed to open glTF file: {} (error {})", path,
|
||||
fastgltf::to_underlying(data.error()));
|
||||
@@ -98,7 +98,7 @@ auto Mesh::load_gltf_meshes(
|
||||
new_surface.count = static_cast<uint32_t>(
|
||||
gltf.accessors[p.indicesAccessor.value()].count);
|
||||
|
||||
size_t initial_vertex = vertices.size();
|
||||
size_t initial_vertex { vertices.size() };
|
||||
|
||||
{ // Indices
|
||||
auto &accessor = gltf.accessors[p.indicesAccessor.value()];
|
||||
@@ -128,7 +128,7 @@ auto Mesh::load_gltf_meshes(
|
||||
|
||||
if (auto attr = p.findAttribute("NORMAL")) { // Normals
|
||||
auto &accessor = gltf.accessors[attr->accessorIndex];
|
||||
size_t local_index = 0;
|
||||
size_t local_index { 0 };
|
||||
for (auto normal :
|
||||
fastgltf::iterateAccessor<smath::Vec3>(gltf, accessor)) {
|
||||
vertices[initial_vertex + local_index].normal = normal;
|
||||
@@ -138,7 +138,7 @@ auto Mesh::load_gltf_meshes(
|
||||
|
||||
if (auto attr = p.findAttribute("TEXCOORD_0")) { // UVs
|
||||
auto &accessor = gltf.accessors[attr->accessorIndex];
|
||||
size_t local_index = 0;
|
||||
size_t local_index { 0 };
|
||||
for (auto uv :
|
||||
fastgltf::iterateAccessor<smath::Vec2>(gltf, accessor)) {
|
||||
uv.unpack(vertices[initial_vertex + local_index].u,
|
||||
@@ -149,7 +149,7 @@ auto Mesh::load_gltf_meshes(
|
||||
|
||||
if (auto attr = p.findAttribute("COLOR_0")) { // Colors
|
||||
auto &accessor = gltf.accessors[attr->accessorIndex];
|
||||
size_t local_index = 0;
|
||||
size_t local_index { 0 };
|
||||
|
||||
switch (accessor.type) {
|
||||
case fastgltf::AccessorType::Vec3: {
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
static std::filesystem::path get_log_path(std::string_view app_name)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
PWSTR path = nullptr;
|
||||
PWSTR path { nullptr };
|
||||
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &path);
|
||||
std::wstring wpath(path);
|
||||
CoTaskMemFree(path);
|
||||
@@ -70,7 +70,7 @@ static int compress_file(std::filesystem::path const &input_path,
|
||||
std::vector<char> buffer(chunk_size);
|
||||
while (in) {
|
||||
in.read(buffer.data(), static_cast<std::streamsize>(buffer.size()));
|
||||
std::streamsize bytes = in.gcount();
|
||||
std::streamsize bytes { in.gcount() };
|
||||
if (bytes > 0)
|
||||
gzwrite(out, buffer.data(), static_cast<unsigned int>(bytes));
|
||||
}
|
||||
@@ -99,20 +99,20 @@ Logger::Logger(std::string_view app_name)
|
||||
if (!file.is_regular_file())
|
||||
continue;
|
||||
|
||||
auto name = file.path().filename().stem().string();
|
||||
auto name { file.path().filename().stem().string() };
|
||||
constexpr std::string_view prefix = "log_";
|
||||
|
||||
if (name.rfind(prefix, 0) != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int v = std::stoi(name.substr(prefix.size()));
|
||||
int v { std::stoi(name.substr(prefix.size())) };
|
||||
if (v > max)
|
||||
max = v;
|
||||
|
||||
auto ext = file.path().filename().extension().string();
|
||||
auto ext { file.path().filename().extension().string() };
|
||||
if (ext == ".txt") {
|
||||
auto np = file.path();
|
||||
auto np { file.path() };
|
||||
np.replace_extension(ext + ".gz");
|
||||
compress_file(file.path(), np);
|
||||
}
|
||||
@@ -153,7 +153,7 @@ static std::string get_current_time_string()
|
||||
|
||||
void Logger::log(Level level, std::string_view msg)
|
||||
{
|
||||
auto time_str = get_current_time_string();
|
||||
auto time_str { get_current_time_string() };
|
||||
std::string level_str;
|
||||
switch (level) {
|
||||
case Logger::Level::Debug:
|
||||
|
||||
@@ -27,13 +27,13 @@ auto Pipeline::Builder::set_push_constant_ranges(
|
||||
auto Pipeline::Builder::build_compute(
|
||||
vk::PipelineShaderStageCreateInfo const &stage) -> Pipeline
|
||||
{
|
||||
auto pipeline_layout = build_layout();
|
||||
auto pipeline_layout { build_layout() };
|
||||
|
||||
vk::ComputePipelineCreateInfo pipeline_ci {};
|
||||
pipeline_ci.layout = pipeline_layout.get();
|
||||
pipeline_ci.stage = stage;
|
||||
|
||||
auto pipeline_ret = m_device.createComputePipelineUnique({}, pipeline_ci);
|
||||
auto pipeline_ret { m_device.createComputePipelineUnique({}, pipeline_ci) };
|
||||
VK_CHECK(m_logger, pipeline_ret.result);
|
||||
|
||||
return Pipeline {
|
||||
@@ -46,14 +46,14 @@ auto Pipeline::Builder::build_graphics(
|
||||
std::function<GraphicsPipelineBuilder &(GraphicsPipelineBuilder &)> const
|
||||
&configure) -> Pipeline
|
||||
{
|
||||
auto pipeline_layout = build_layout();
|
||||
auto pipeline_layout { build_layout() };
|
||||
|
||||
auto builder = GraphicsPipelineBuilder { m_logger };
|
||||
auto builder { GraphicsPipelineBuilder { m_logger } };
|
||||
builder.set_pipeline_layout(
|
||||
static_cast<VkPipelineLayout>(pipeline_layout.get()));
|
||||
configure(builder);
|
||||
|
||||
auto pipeline_handle = builder.build(static_cast<VkDevice>(m_device));
|
||||
auto pipeline_handle { builder.build(static_cast<VkDevice>(m_device)) };
|
||||
vk::UniquePipeline pipeline_unique(pipeline_handle,
|
||||
vk::detail::ObjectDestroy<vk::Device,
|
||||
VULKAN_HPP_DEFAULT_DISPATCHER_TYPE>(m_device));
|
||||
|
||||
171
src/Skybox.cpp
171
src/Skybox.cpp
@@ -23,7 +23,7 @@ struct FaceOffset {
|
||||
uint32_t y;
|
||||
};
|
||||
|
||||
constexpr std::array<FaceOffset, 6> kCrossOffsets {
|
||||
constexpr std::array<FaceOffset, 6> CROSS_OFFSETS {
|
||||
FaceOffset { 2, 1 }, // +X
|
||||
FaceOffset { 0, 1 }, // -X
|
||||
FaceOffset { 1, 0 }, // +Y
|
||||
@@ -34,6 +34,83 @@ constexpr std::array<FaceOffset, 6> kCrossOffsets {
|
||||
|
||||
} // namespace
|
||||
|
||||
auto Skybox::rebuild_pipeline(VulkanRenderer &renderer) -> bool
|
||||
{
|
||||
Pipeline::Builder pipeline_builder { renderer.device(), renderer.logger() };
|
||||
|
||||
uint8_t skybox_vert_shader_data[] {
|
||||
#embed "skybox_vert.spv"
|
||||
};
|
||||
auto skybox_vert_shader
|
||||
= vkutil::load_shader_module(std::span<uint8_t>(skybox_vert_shader_data,
|
||||
sizeof(skybox_vert_shader_data)),
|
||||
renderer.device());
|
||||
if (!skybox_vert_shader) {
|
||||
renderer.logger().err("Failed to load skybox vert shader");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t skybox_frag_shader_data[] {
|
||||
#embed "skybox_frag.spv"
|
||||
};
|
||||
auto skybox_frag_shader
|
||||
= vkutil::load_shader_module(std::span<uint8_t>(skybox_frag_shader_data,
|
||||
sizeof(skybox_frag_shader_data)),
|
||||
renderer.device());
|
||||
if (!skybox_frag_shader) {
|
||||
renderer.logger().err("Failed to load skybox frag shader");
|
||||
return false;
|
||||
}
|
||||
|
||||
vk::PushConstantRange push_constant_range {};
|
||||
push_constant_range.stageFlags = vk::ShaderStageFlagBits::eVertex;
|
||||
push_constant_range.offset = 0;
|
||||
push_constant_range.size = sizeof(SkyboxPushConstants);
|
||||
|
||||
std::array push_constant_ranges { push_constant_range };
|
||||
pipeline_builder.set_push_constant_ranges(push_constant_ranges);
|
||||
std::array descriptor_set_layouts {
|
||||
renderer.single_image_descriptor_layout()
|
||||
};
|
||||
pipeline_builder.set_descriptor_set_layouts(descriptor_set_layouts);
|
||||
|
||||
VkVertexInputBindingDescription binding {};
|
||||
binding.binding = 0;
|
||||
binding.stride = sizeof(Vertex);
|
||||
binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||
|
||||
VkVertexInputAttributeDescription attribute {};
|
||||
attribute.location = 0;
|
||||
attribute.binding = 0;
|
||||
attribute.format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
attribute.offset = offsetof(Vertex, position);
|
||||
|
||||
std::array bindings { binding };
|
||||
std::array attributes { attribute };
|
||||
|
||||
m_pipeline = pipeline_builder.build_graphics(
|
||||
[&](GraphicsPipelineBuilder &builder) -> GraphicsPipelineBuilder & {
|
||||
builder.set_vertex_input(bindings, attributes);
|
||||
return builder
|
||||
.set_shaders(skybox_vert_shader.get(), skybox_frag_shader.get())
|
||||
.set_input_topology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST)
|
||||
.set_polygon_mode(VK_POLYGON_MODE_FILL)
|
||||
.set_cull_mode(
|
||||
VK_CULL_MODE_FRONT_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE)
|
||||
.set_multisampling(
|
||||
static_cast<VkSampleCountFlagBits>(renderer.msaa_samples()))
|
||||
.disable_blending()
|
||||
.enable_depth_testing(false, VK_COMPARE_OP_LESS_OR_EQUAL)
|
||||
.set_color_attachment_format(
|
||||
static_cast<VkFormat>(renderer.draw_image_format()))
|
||||
.set_depth_format(
|
||||
static_cast<VkFormat>(renderer.depth_image_format()));
|
||||
});
|
||||
|
||||
m_pipeline_samples = renderer.msaa_samples();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto Skybox::init(VulkanRenderer &renderer, std::filesystem::path const &path)
|
||||
-> void
|
||||
{
|
||||
@@ -61,10 +138,10 @@ auto Skybox::init(VulkanRenderer &renderer, std::filesystem::path const &path)
|
||||
uint32_t const face_size = texture.width / 4;
|
||||
size_t const face_bytes = static_cast<size_t>(face_size) * face_size * 4;
|
||||
|
||||
std::vector<uint8_t> cubemap_pixels(face_bytes * kCrossOffsets.size());
|
||||
std::vector<uint8_t> cubemap_pixels(face_bytes * CROSS_OFFSETS.size());
|
||||
|
||||
for (size_t face = 0; face < kCrossOffsets.size(); ++face) {
|
||||
auto const offset = kCrossOffsets[face];
|
||||
for (size_t face = 0; face < CROSS_OFFSETS.size(); ++face) {
|
||||
auto const offset = CROSS_OFFSETS[face];
|
||||
for (uint32_t y = 0; y < face_size; ++y) {
|
||||
for (uint32_t x = 0; x < face_size; ++x) {
|
||||
uint32_t const src_x = offset.x * face_size + x;
|
||||
@@ -108,7 +185,9 @@ auto Skybox::init(VulkanRenderer &renderer, std::filesystem::path const &path)
|
||||
vk::DescriptorSetAllocateInfo alloc_info {};
|
||||
alloc_info.descriptorPool = m_descriptor_pool.get();
|
||||
alloc_info.descriptorSetCount = 1;
|
||||
vk::DescriptorSetLayout layout = renderer.single_image_descriptor_layout();
|
||||
vk::DescriptorSetLayout layout {
|
||||
renderer.single_image_descriptor_layout()
|
||||
};
|
||||
alloc_info.pSetLayouts = &layout;
|
||||
m_descriptor_set
|
||||
= renderer.device().allocateDescriptorSets(alloc_info).front();
|
||||
@@ -183,79 +262,11 @@ auto Skybox::init(VulkanRenderer &renderer, std::filesystem::path const &path)
|
||||
m_index_count = static_cast<uint32_t>(indices.size());
|
||||
m_cube_mesh = renderer.upload_mesh(indices, vertices);
|
||||
|
||||
Pipeline::Builder pipeline_builder { renderer.device(), renderer.logger() };
|
||||
|
||||
uint8_t skybox_vert_shader_data[] {
|
||||
#embed "skybox_vert.spv"
|
||||
};
|
||||
auto skybox_vert_shader
|
||||
= vkutil::load_shader_module(std::span<uint8_t>(skybox_vert_shader_data,
|
||||
sizeof(skybox_vert_shader_data)),
|
||||
renderer.device());
|
||||
if (!skybox_vert_shader) {
|
||||
renderer.logger().err("Failed to load skybox vert shader");
|
||||
if (!rebuild_pipeline(renderer)) {
|
||||
ok = false;
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t skybox_frag_shader_data[] {
|
||||
#embed "skybox_frag.spv"
|
||||
};
|
||||
auto skybox_frag_shader
|
||||
= vkutil::load_shader_module(std::span<uint8_t>(skybox_frag_shader_data,
|
||||
sizeof(skybox_frag_shader_data)),
|
||||
renderer.device());
|
||||
if (!skybox_frag_shader) {
|
||||
renderer.logger().err("Failed to load skybox frag shader");
|
||||
ok = false;
|
||||
return;
|
||||
}
|
||||
|
||||
vk::PushConstantRange push_constant_range {};
|
||||
push_constant_range.stageFlags = vk::ShaderStageFlagBits::eVertex;
|
||||
push_constant_range.offset = 0;
|
||||
push_constant_range.size = sizeof(SkyboxPushConstants);
|
||||
|
||||
std::array push_constant_ranges { push_constant_range };
|
||||
pipeline_builder.set_push_constant_ranges(push_constant_ranges);
|
||||
std::array descriptor_set_layouts {
|
||||
renderer.single_image_descriptor_layout()
|
||||
};
|
||||
pipeline_builder.set_descriptor_set_layouts(descriptor_set_layouts);
|
||||
|
||||
VkVertexInputBindingDescription binding {};
|
||||
binding.binding = 0;
|
||||
binding.stride = sizeof(Vertex);
|
||||
binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
||||
|
||||
VkVertexInputAttributeDescription attribute {};
|
||||
attribute.location = 0;
|
||||
attribute.binding = 0;
|
||||
attribute.format = VK_FORMAT_R32G32B32_SFLOAT;
|
||||
attribute.offset = offsetof(Vertex, position);
|
||||
|
||||
std::array bindings { binding };
|
||||
std::array attributes { attribute };
|
||||
|
||||
m_pipeline = pipeline_builder.build_graphics(
|
||||
[&](GraphicsPipelineBuilder &builder) -> GraphicsPipelineBuilder & {
|
||||
builder.set_vertex_input(bindings, attributes);
|
||||
return builder
|
||||
.set_shaders(skybox_vert_shader.get(), skybox_frag_shader.get())
|
||||
.set_input_topology(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST)
|
||||
.set_polygon_mode(VK_POLYGON_MODE_FILL)
|
||||
.set_cull_mode(
|
||||
VK_CULL_MODE_FRONT_BIT, VK_FRONT_FACE_COUNTER_CLOCKWISE)
|
||||
.set_multisampling(
|
||||
static_cast<VkSampleCountFlagBits>(renderer.msaa_samples()))
|
||||
.disable_blending()
|
||||
.enable_depth_testing(false, VK_COMPARE_OP_LESS_OR_EQUAL)
|
||||
.set_color_attachment_format(
|
||||
static_cast<VkFormat>(renderer.draw_image_format()))
|
||||
.set_depth_format(
|
||||
static_cast<VkFormat>(renderer.depth_image_format()));
|
||||
});
|
||||
|
||||
ok = true;
|
||||
}
|
||||
|
||||
@@ -273,6 +284,7 @@ auto Skybox::destroy(VulkanRenderer &renderer) -> void
|
||||
m_sampler.reset();
|
||||
m_descriptor_pool.reset();
|
||||
m_pipeline.reset();
|
||||
m_pipeline_samples = vk::SampleCountFlagBits::e1;
|
||||
m_descriptor_set = vk::DescriptorSet {};
|
||||
m_cube_mesh = {};
|
||||
m_cubemap = {};
|
||||
@@ -280,14 +292,21 @@ auto Skybox::destroy(VulkanRenderer &renderer) -> void
|
||||
ok = false;
|
||||
}
|
||||
|
||||
auto Skybox::draw(VulkanRenderer::GL &gl, smath::Mat4 const &mvp) -> void
|
||||
auto Skybox::draw(VulkanRenderer::GL &gl, VulkanRenderer &renderer,
|
||||
smath::Mat4 const &mvp) -> void
|
||||
{
|
||||
if (!ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_pipeline_samples != renderer.msaa_samples()) {
|
||||
if (!rebuild_pipeline(renderer)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
SkyboxPushConstants push_constants { mvp };
|
||||
auto bytes = std::as_bytes(std::span { &push_constants, 1 });
|
||||
auto bytes { std::as_bytes(std::span { &push_constants, 1 }) };
|
||||
gl.draw_indexed(m_pipeline, m_descriptor_set, m_cube_mesh.vertex_buffer,
|
||||
m_cube_mesh.index_buffer, m_index_count, bytes);
|
||||
}
|
||||
|
||||
@@ -17,15 +17,19 @@ struct Skybox {
|
||||
auto init(VulkanRenderer &renderer, std::filesystem::path const &path)
|
||||
-> void;
|
||||
auto destroy(VulkanRenderer &renderer) -> void;
|
||||
auto draw(VulkanRenderer::GL &gl, smath::Mat4 const &mvp) -> void;
|
||||
auto draw(VulkanRenderer::GL &gl, VulkanRenderer &renderer,
|
||||
smath::Mat4 const &mvp) -> void;
|
||||
|
||||
private:
|
||||
auto rebuild_pipeline(VulkanRenderer &renderer) -> bool;
|
||||
|
||||
Pipeline m_pipeline {};
|
||||
GPUMeshBuffers m_cube_mesh {};
|
||||
AllocatedImage m_cubemap {};
|
||||
vk::UniqueSampler m_sampler {};
|
||||
vk::UniqueDescriptorPool m_descriptor_pool {};
|
||||
vk::DescriptorSet m_descriptor_set {};
|
||||
vk::SampleCountFlagBits m_pipeline_samples { vk::SampleCountFlagBits::e1 };
|
||||
uint32_t m_index_count { 0 };
|
||||
};
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ struct PolarCoordinate {
|
||||
|
||||
smath::Vec3 to_vec3() const
|
||||
{
|
||||
float sin_phi = std::sin(phi);
|
||||
float sin_phi { std::sin(phi) };
|
||||
|
||||
return smath::Vec3 { r * sin_phi * std::cos(theta), r * std::cos(phi),
|
||||
r * sin_phi * std::sin(theta) };
|
||||
|
||||
@@ -29,7 +29,7 @@ template<typename F> privDefer<F> defer_func(F f) { return privDefer<F>(f); }
|
||||
#define VK_CHECK(logger, x) \
|
||||
do { \
|
||||
auto err { x }; \
|
||||
auto result = vk::Result(err); \
|
||||
auto result { vk::Result(err) }; \
|
||||
if (result != vk::Result::eSuccess) { \
|
||||
(logger).err("Detected Vulkan error: {}", vk::to_string(result)); \
|
||||
throw std::runtime_error("Vulkan error"); \
|
||||
|
||||
@@ -290,7 +290,7 @@ auto VulkanRenderer::GL::flush() -> void
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
|
||||
.update_set(m_renderer.m_vkb.dev.device, image_set);
|
||||
|
||||
auto vk_image_set = vk::DescriptorSet { image_set };
|
||||
auto vk_image_set { vk::DescriptorSet { image_set } };
|
||||
cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
|
||||
m_active_pipeline->get_layout(), 0, vk_image_set, {});
|
||||
|
||||
@@ -423,22 +423,16 @@ auto VulkanRenderer::GL::draw_sphere(smath::Vec3 center, float radius,
|
||||
|
||||
float const pi = 3.14159265358979323846f;
|
||||
|
||||
// Use caller color if provided, otherwise keep current GL color state.
|
||||
if (sphere_color.has_value())
|
||||
color(*sphere_color);
|
||||
|
||||
// Build as latitude strips
|
||||
for (int y = 0; y < rings; y++) {
|
||||
float const v0 = static_cast<float>(y) / static_cast<float>(rings);
|
||||
float const v1 = static_cast<float>(y + 1) / static_cast<float>(rings);
|
||||
float const v = static_cast<float>(y + 1) / static_cast<float>(rings);
|
||||
|
||||
float const theta0 = v0 * pi;
|
||||
float const theta1 = v1 * pi;
|
||||
float const theta = v * pi;
|
||||
|
||||
float const sin0 = std::sin(theta0);
|
||||
float const cos0 = std::cos(theta0);
|
||||
float const sin1 = std::sin(theta1);
|
||||
float const cos1 = std::cos(theta1);
|
||||
float const s = std::sin(theta);
|
||||
float const c = std::cos(theta);
|
||||
|
||||
begin(GeometryKind::TriangleStrip);
|
||||
|
||||
@@ -452,21 +446,11 @@ auto VulkanRenderer::GL::draw_sphere(smath::Vec3 center, float radius,
|
||||
|
||||
// Vertex on ring y+1
|
||||
{
|
||||
smath::Vec3 n { sin1 * cp, cos1, sin1 * sp };
|
||||
smath::Vec3 n { s * cp, c, s * sp };
|
||||
normal(n);
|
||||
uv(smath::Vec2 { u, 1.0f - v1 });
|
||||
uv(smath::Vec2 { u, 1.0f - v });
|
||||
|
||||
smath::Vec3 p = center + n * radius;
|
||||
vert(p);
|
||||
}
|
||||
|
||||
// Vertex on ring y
|
||||
{
|
||||
smath::Vec3 n { sin0 * cp, cos0, sin0 * sp };
|
||||
normal(n);
|
||||
uv(smath::Vec2 { u, 1.0f - v0 });
|
||||
|
||||
smath::Vec3 p = center + n * radius;
|
||||
smath::Vec3 p { center + n * radius };
|
||||
vert(p);
|
||||
}
|
||||
}
|
||||
@@ -501,7 +485,7 @@ auto VulkanRenderer::GL::draw_mesh(GPUMeshBuffers const &mesh,
|
||||
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER)
|
||||
.update_set(m_renderer.m_vkb.dev.device, image_set);
|
||||
|
||||
auto vk_image_set = vk::DescriptorSet { image_set };
|
||||
auto vk_image_set { vk::DescriptorSet { image_set } };
|
||||
m_cmd.bindDescriptorSets(vk::PipelineBindPoint::eGraphics,
|
||||
mesh_pipeline.get_layout(), 0, vk_image_set, {});
|
||||
|
||||
@@ -541,7 +525,7 @@ auto VulkanRenderer::GL::draw_indexed(Pipeline &pipeline,
|
||||
push_constants.data());
|
||||
}
|
||||
|
||||
vk::DeviceSize offset = 0;
|
||||
vk::DeviceSize offset { 0 };
|
||||
cmd.bindVertexBuffers(0, vertex_buffer.buffer, offset);
|
||||
cmd.bindIndexBuffer(index_buffer.buffer, 0, vk::IndexType::eUint32);
|
||||
cmd.drawIndexed(index_count, 1, 0, 0, 0);
|
||||
@@ -881,7 +865,7 @@ auto VulkanRenderer::vk_init() -> void
|
||||
void *user_data) {
|
||||
auto renderer { reinterpret_cast<VulkanRenderer *>(user_data) };
|
||||
|
||||
auto level = Logger::Level::Debug;
|
||||
auto level { Logger::Level::Debug };
|
||||
if (message_severity
|
||||
& VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
|
||||
level = Logger::Level::Error;
|
||||
@@ -1254,7 +1238,7 @@ auto VulkanRenderer::imgui_init() -> void
|
||||
{ VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT, 1000 },
|
||||
};
|
||||
|
||||
VkDescriptorPoolCreateInfo pool_info = {};
|
||||
VkDescriptorPoolCreateInfo pool_info {};
|
||||
pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
pool_info.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
||||
pool_info.maxSets = 1000;
|
||||
@@ -1267,7 +1251,7 @@ auto VulkanRenderer::imgui_init() -> void
|
||||
|
||||
ImGui_ImplSDL3_InitForVulkan(m_window);
|
||||
|
||||
ImGui_ImplVulkan_InitInfo init_info = {};
|
||||
ImGui_ImplVulkan_InitInfo init_info {};
|
||||
init_info.Instance = m_vkb.instance;
|
||||
init_info.PhysicalDevice = m_vkb.phys_dev.physical_device;
|
||||
init_info.Device = m_vkb.dev.device;
|
||||
@@ -1281,7 +1265,8 @@ auto VulkanRenderer::imgui_init() -> void
|
||||
= VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
|
||||
init_info.PipelineInfoMain.PipelineRenderingCreateInfo.colorAttachmentCount
|
||||
= 1;
|
||||
auto swapchain_format = static_cast<VkFormat>(m_vk.swapchain_image_format);
|
||||
auto swapchain_format { static_cast<VkFormat>(
|
||||
m_vk.swapchain_image_format) };
|
||||
init_info.PipelineInfoMain.PipelineRenderingCreateInfo
|
||||
.pColorAttachmentFormats
|
||||
= &swapchain_format;
|
||||
@@ -1412,7 +1397,7 @@ auto VulkanRenderer::render(std::function<void(GL &)> const &record) -> void
|
||||
emit_tracy_frame_image(frame);
|
||||
#endif
|
||||
|
||||
auto raw_fence = static_cast<VkFence>(frame.render_fence.get());
|
||||
auto raw_fence { static_cast<VkFence>(frame.render_fence.get()) };
|
||||
VK_CHECK(m_logger, vkResetFences(m_vkb.dev.device, 1, &raw_fence));
|
||||
|
||||
auto const acquire_result = m_device.acquireNextImageKHR(
|
||||
@@ -1642,7 +1627,7 @@ auto VulkanRenderer::create_swapchain(uint32_t width, uint32_t height) -> void
|
||||
m_vk.swapchain = m_vkb.swapchain.swapchain;
|
||||
m_vk.swapchain_extent = vk::Extent2D { m_vkb.swapchain.extent.width,
|
||||
m_vkb.swapchain.extent.height };
|
||||
auto images = m_vkb.swapchain.get_images().value();
|
||||
auto images { m_vkb.swapchain.get_images().value() };
|
||||
m_vk.swapchain_images.assign(images.begin(), images.end());
|
||||
|
||||
m_vk.swapchain_image_views.clear();
|
||||
@@ -2394,7 +2379,8 @@ auto VulkanRenderer::upload_mesh(
|
||||
void *data = info.pMappedData;
|
||||
bool mapped_here { false };
|
||||
if (!data) {
|
||||
VkResult res = vmaMapMemory(m_vk.allocator, staging.allocation, &data);
|
||||
VkResult res { vmaMapMemory(
|
||||
m_vk.allocator, staging.allocation, &data) };
|
||||
assert(res == VK_SUCCESS);
|
||||
mapped_here = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user