Skip to content

Commit

Permalink
Merge pull request #29 from tmayoff/multiple-mesh-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmayoff authored Oct 26, 2024
2 parents 05a74c5 + a26de2d commit 3ec33f8
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 117 deletions.
23 changes: 3 additions & 20 deletions editor/src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,28 +245,11 @@ auto Editor::build_render_graph(const std::shared_ptr<wren::Context> &ctx)
pass.write_scratch_buffer(cmd, 0, 0, ubo);

render_query.each(
[cmd, &pass, ctx](
[cmd, ctx, this](
const wren::scene::components::Transform &transform,
wren::scene::components::MeshRenderer &mesh_renderer) {
struct LOCALS {
wren::math::Mat4f model;
};
LOCALS ubo{};

if (!mesh_renderer.mesh.has_value()) return;

if (!mesh_renderer.mesh->loaded()) {
mesh_renderer.mesh->load(
ctx->graphics_context->Device(),
ctx->graphics_context->allocator());
}

ubo.model = transform.matrix();

pass.write_scratch_buffer(cmd, 0, 1, ubo);

mesh_renderer.mesh->bind(cmd);
mesh_renderer.mesh->draw(cmd);
mesh_renderer.bind(ctx, mesh_shader_, cmd,
transform.matrix());
});
})
.add_pass("ui", {.shaders = {}, .target_name = "swapchain_target"},
Expand Down
2 changes: 1 addition & 1 deletion editor/src/inspector_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void draw_component(const editor::Context& ctx,
wren::scene::components::MeshRenderer& mesh_renderer) {
ImGui::Text("Mesh Renderer");

auto mesh_filename = mesh_renderer.mesh_file.filename().string();
auto mesh_filename = mesh_renderer.mesh_file().filename().string();

ImGui::InputText("file", &mesh_filename);

Expand Down
27 changes: 5 additions & 22 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 8 additions & 46 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,34 @@
description = "Wren game engine";

inputs = {
# nix-mesonlsp.url = "https://flakehub.com/f/tmayoff/nix-mesonlsp/0.1.7.tar.gz";
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
nixpkgs-unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";
nixgl.url = "github:nix-community/nixGL";
};

outputs = {
self,
nixpkgs,
nixpkgs-unstable,
nixgl,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system: let
overlays = [
nixgl.overlay
# nix-mesonlsp.overlay.default

(final: prev: {
xmake = prev.xmake.overrideAttrs (old: {
src = prev.fetchFromGitHub {
owner = "xmake-io";
repo = "xmake";
rev = "b3c6d968249e01ce2b00417c4d63ae524e883afd";
hash = "sha256-uxjxEJDdR+1QjCksjFPok3Pt6qA8tkGYA68SOdqfNfs=";
fetchSubmodules = true;
};
patches = [];
});
})
];

pkgs = import nixpkgs {
inherit system overlays;
};

unstable = import nixpkgs-unstable {
inherit system overlays;
};

boost = pkgs.boost185;

# vma = pkgs.stdenv.mkDerivation {
# name = "VulkanMemoryAllocator";
# src = pkgs.fetchFromGitHub {
# owner = "GPUOpen-LibrariesAndSDKs";
# repo = "VulkanMemoryAllocator";
# rev = "7942b798289f752dc23b0a79516fd8545febd718";
# hash = "sha256-x/5OECXCG4rxNtyHZKaMnrNbDjxiP9bQFtQiqEFjNKQ=";
# };

# nativeBuildInputs = with pkgs; [
# cmake
# ];
# };

rawNativeBuildInputs = with pkgs; [
pkg-config
meson
cmake
ninja
unstable.xmake

# doxygen
# graphviz
Expand All @@ -82,7 +46,7 @@
# vulkan / shaders
vulkan-headers
vulkan-loader
# vma
vulkan-memory-allocator
shaderc
spirv-headers

Expand Down Expand Up @@ -113,34 +77,32 @@
default = wren_editor;
};

devShell = pkgs.mkShell.override {stdenv = unstable.llvmPackages_19.stdenv;} {
devShell = pkgs.mkShell.override {stdenv = pkgs.llvmPackages_19.stdenv;} {
hardeningDisable = ["all"];
VK_LAYER_PATH = vulkan_layer_path;

nativeBuildInputs = with pkgs;
[
unstable.lua-language-server

vulkan-tools
unstable.llvmPackages_19.clang-tools
llvmPackages_19.clang-tools
sccache

just

gdb

# renderdoc
renderdoc

# unstable.tracy-x11 # for the profiler
tracy-x11 # for the profiler
wayland
]
++ rawNativeBuildInputs;

buildInputs = with pkgs;
[
unstable.lldb
lldb
muon
unstable.mesonlsp
mesonlsp
vulkan-validation-layers

pkgs.nixgl.nixVulkanIntel
Expand Down
26 changes: 13 additions & 13 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ Collapsed=0

[Window][Scene]
Pos=8,27
Size=119,1018
Size=330,710
Collapsed=0
DockId=0x00000005,0

[Window][Inspector]
Pos=1845,27
Size=373,1360
Pos=1863,27
Size=355,1360
Collapsed=0
DockId=0x00000002,0

[Window][Filesystem]
Pos=8,1047
Size=119,340
Pos=8,739
Size=330,648
Collapsed=0
DockId=0x00000006,0

[Window][Viewer]
Pos=129,27
Size=1714,1360
Pos=340,27
Size=1521,1360
Collapsed=0
DockId=0x00000004,0

Expand Down Expand Up @@ -160,10 +160,10 @@ Column 13 Width=19

[Docking][Data]
DockSpace ID=0xBB06786C Window=0xCB5DF48C Pos=8,27 Size=2210,1360 Split=X
DockNode ID=0x00000001 Parent=0xBB06786C SizeRef=1835,1395 Split=X
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=119,1395 Split=Y
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=119,1044 Selected=0xE192E354
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=119,349 Selected=0x07E2D2CA
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1714,1395 CentralNode=1 Selected=0x652D1CFE
DockNode ID=0x00000002 Parent=0xBB06786C SizeRef=373,1395 Selected=0xE7039252
DockNode ID=0x00000001 Parent=0xBB06786C SizeRef=1853,1395 Split=X
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=330,1395 Split=Y
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=119,710 Selected=0xE192E354
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=119,648 Selected=0x07E2D2CA
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1521,1395 CentralNode=1 Selected=0x652D1CFE
DockNode ID=0x00000002 Parent=0xBB06786C SizeRef=355,1395 Selected=0xE7039252

5 changes: 4 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ build:
run: build
./build/editor/wren_editor ~/Documents/wren_game

debug: build
lldb ./build/editor/wren_editor ~/Documents/wren_game

test: build
meson test -C build --print-errorlogs


clean:
ninja -C build clean

Expand Down
14 changes: 7 additions & 7 deletions wren/include/wren/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ struct Vertex {
};

const std::array kTriangleVertices = {
Vertex{{0.0f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}},
Vertex{{0.5f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
Vertex{{-0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
Vertex{.pos = {0.0f, -0.5f, 0.0f}, .normal = {1.0f, 0.0f, 0.0f}},
Vertex{.pos = {0.5f, 0.5f, 0.0f}, .normal = {0.0f, 1.0f, 0.0f}},
Vertex{.pos = {-0.5f, 0.5f, 0.0f}, .normal = {0.0f, 0.0f, 1.0f}},
};

const std::array kQuadVertices = {
Vertex{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}},
Vertex{{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
Vertex{{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
Vertex{{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}}};
Vertex{.pos = {-0.5f, -0.5f, 0.0f}, .normal = {1.0f, 0.0f, 0.0f}},
Vertex{.pos = {0.5f, -0.5f, 0.0f}, .normal = {0.0f, 1.0f, 0.0f}},
Vertex{.pos = {0.5f, 0.5f, 0.0f}, .normal = {0.0f, 0.0f, 1.0f}},
Vertex{.pos = {-0.5f, 0.5f, 0.0f}, .normal = {1.0f, 1.0f, 1.0f}}};

const std::vector<uint16_t> kQuadIndices = {0, 1, 2, 2, 3, 0};

Expand Down
55 changes: 50 additions & 5 deletions wren/include/wren/scene/components/mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,69 @@

#include <filesystem>
#include <optional>
#include <wren/math/matrix.hpp>
#include <wren/mesh.hpp>
#include <wren/mesh_loader.hpp>

#include "wren/context.hpp"

namespace wren::scene::components {

struct MeshRenderer {
std::optional<Mesh> mesh;
class MeshRenderer {
public:
auto bind(const std::shared_ptr<Context>& ctx,
const std::shared_ptr<vk::Shader>& shader,
const ::vk::CommandBuffer& cmd, const math::Mat4f& model_mat) {
if (!mesh_.has_value()) return;
if (!mesh_->loaded())
mesh_->load(ctx->graphics_context->Device(),
ctx->graphics_context->allocator());

struct LOCALS {
wren::math::Mat4f model;
};
LOCALS ubo{};

ubo.model = model_mat;

// Load UBO if not there
if (ubo_ == nullptr) {
ubo_ = vk::Buffer::create(
ctx->graphics_context->allocator(), sizeof(ubo),
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
VmaAllocationCreateFlagBits::
VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
}

ubo_->set_data_raw(&ubo, sizeof(LOCALS));

std::filesystem::path mesh_file;
::vk::DescriptorBufferInfo buffer_info(ubo_->get(), 0, sizeof(LOCALS));
std::array writes = {::vk::WriteDescriptorSet{
{}, 1, 0, ::vk::DescriptorType::eUniformBuffer, {}, buffer_info}};

cmd.pushDescriptorSetKHR(::vk::PipelineBindPoint::eGraphics,
shader->pipeline_layout(), 0, writes);

mesh_->bind(cmd);
mesh_->draw(cmd);
}

auto update_mesh(const std::filesystem::path& project_root,
const std::filesystem::path& mesh_path) -> expected<void> {
mesh_file = mesh_path;
mesh_file_ = mesh_path;

TRY_RESULT(mesh, load_mesh(project_root / mesh_path));
TRY_RESULT(mesh_, load_mesh(project_root / mesh_path));

return {};
}

[[nodiscard]] auto mesh() const { return mesh_; }
[[nodiscard]] auto mesh_file() const { return mesh_file_; }

private:
std::optional<Mesh> mesh_;
std::filesystem::path mesh_file_;
std::shared_ptr<vk::Buffer> ubo_;
};

} // namespace wren::scene::components
5 changes: 3 additions & 2 deletions wren/src/scene/serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,9 @@ void serialize_id(toml::table& table, const flecs::entity& entity,

void serialize(toml::table& table,
const components::MeshRenderer& mesh_renderer) {
table.insert("mesh_renderer",
toml::table{{"path", mesh_renderer.mesh_file.string().c_str()}});
table.insert(
"mesh_renderer",
toml::table{{"path", mesh_renderer.mesh_file().string().c_str()}});
}

void serialize(toml::table& table, const components::Transform& transform) {
Expand Down

0 comments on commit 3ec33f8

Please sign in to comment.