Skip to content

Incorporate skinned meshes #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add hash_combine, Pipeline::VertType (placeholder)
  • Loading branch information
karnkaul committed Nov 30, 2022
commit c4efe23aaf9e9344fa974e70c2c7aa990b9923dd
7 changes: 5 additions & 2 deletions lib/engine/src/scene_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ void SceneRenderer::render(Renderer& renderer, vk::CommandBuffer cb, Node const&
if (auto mesh_id = node.find<Mesh>()) {
auto const& mesh = resources.meshes[*mesh_id];
for (auto const& primitive : mesh.primitives) {
auto const mode = m_scene->render_mode.type == RenderMode::Type::eWireframe ? vk::PolygonMode::eLine : vk::PolygonMode::eFill;
auto const state = Pipeline::State{.mode = mode, .topology = to_primitive_topology(primitive.topology)};
auto const state = Pipeline::State{
.mode = m_scene->render_mode.type == RenderMode::Type::eWireframe ? vk::PolygonMode::eLine : vk::PolygonMode::eFill,
.topology = to_primitive_topology(primitive.topology),
.vert_type = primitive.morph_mesh ? Pipeline::VertType::eSkinned : Pipeline::VertType::eInstanced,
};
auto const& material = primitive.material ? resources.materials[primitive.material->value()] : m_material;

auto pipeline = renderer.bind_pipeline(cb, state, material.shader_id());
Expand Down
4 changes: 2 additions & 2 deletions lib/ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ if("${target_prefix}" STREQUAL "")
message(FATAL_ERROR "target_prefix unset")
endif()

file(ARCHIVE_EXTRACT INPUT "${CMAKE_CURRENT_SOURCE_DIR}/src.zip" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}")

# file(ARCHIVE_EXTRACT INPUT "${CMAKE_CURRENT_SOURCE_DIR}/src.zip" DESTINATION "${CMAKE_CURRENT_SOURCE_DIR}")
add_subdirectory(src/vk_dynamic)
add_library(${target_prefix}::vk-dynamic ALIAS vk-dynamic)

Expand Down Expand Up @@ -52,4 +51,5 @@ add_subdirectory(src/djson)
add_subdirectory(src/fmt)

set(GLTF2CPP_DYNARRAY_DEBUG_VIEW ON)
set(GLTF2CPP_BUILD_TESTS ON)
add_subdirectory(src/gltf2cpp)
Binary file modified lib/ext/src.zip
Binary file not shown.
1 change: 1 addition & 0 deletions lib/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ target_sources(${PROJECT_NAME} PRIVATE
include/${target_prefix}/util/error.hpp
include/${target_prefix}/util/fixed_string.hpp
include/${target_prefix}/util/flex_array.hpp
include/${target_prefix}/util/hash_combine.hpp
include/${target_prefix}/util/image.hpp
include/${target_prefix}/util/logger.hpp
include/${target_prefix}/util/mufo.hpp
Expand Down
20 changes: 20 additions & 0 deletions lib/util/include/facade/util/hash_combine.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
#include <functional>

namespace facade {
// boost::hash_combine
// https://www.boost.org/doc/libs/1_55_0/doc/html/hash/reference.html#boost.hash_combine
constexpr void hash_combine(std::size_t& out_seed, std::size_t hash) { out_seed ^= hash + 0x9e3779b9 + (out_seed << 6) + (out_seed >> 2); }

template <template <typename> typename Hasher = std::hash, typename... Types>
void hash_combine(std::size_t& out_seed, Types const&... t) {
(hash_combine(out_seed, Hasher<Types>{}(t)), ...);
}

template <template <typename> typename Hasher = std::hash, typename... Types>
std::size_t make_combined_hash(Types const&... t) {
auto ret = std::size_t{};
hash_combine<Hasher>(ret, t...);
return ret;
}
} // namespace facade
3 changes: 3 additions & 0 deletions lib/vk/include/facade/vk/pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
namespace facade {
class Pipeline {
public:
enum class VertType { eInstanced, eSkinned };

struct State {
vk::PolygonMode mode{vk::PolygonMode::eFill};
vk::PrimitiveTopology topology{vk::PrimitiveTopology::eTriangleList};
bool depth_test{true};
VertType vert_type{VertType::eInstanced};

bool operator==(State const&) const = default;
};
Expand Down
31 changes: 22 additions & 9 deletions lib/vk/src/pipes.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <facade/util/error.hpp>
#include <facade/util/hash_combine.hpp>
#include <facade/vk/geometry.hpp>
#include <facade/vk/pipes.hpp>
#include <facade/vk/static_mesh.hpp>
Expand Down Expand Up @@ -69,12 +70,14 @@ vk::UniqueShaderModule make_shader_module(vk::Device device, SpirV::View spir_v)
return device.createShaderModuleUnique(smci);
}

static constexpr std::size_t max_attributes_v{16};

struct VertexLayout {
FlexArray<vk::VertexInputAttributeDescription, max_sets_v> attributes{};
FlexArray<vk::VertexInputBindingDescription, max_sets_v> bindings{};
FlexArray<vk::VertexInputAttributeDescription, max_attributes_v> attributes{};
FlexArray<vk::VertexInputBindingDescription, max_attributes_v> bindings{};
};

constexpr VertexLayout vertex_layout() {
constexpr VertexLayout instanced_vertex_layout() {
auto ret = VertexLayout{};
ret.bindings.insert(vk::VertexInputBindingDescription{0, sizeof(Vertex)});
ret.attributes.insert(vk::VertexInputAttributeDescription{0, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, position)});
Expand All @@ -89,6 +92,20 @@ constexpr VertexLayout vertex_layout() {
return ret;
}

constexpr VertexLayout skinned_vertex_layout() {
auto ret = VertexLayout{};
ret.bindings.insert(vk::VertexInputBindingDescription{0, sizeof(Vertex)});
ret.attributes.insert(vk::VertexInputAttributeDescription{0, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, position)});
ret.attributes.insert(vk::VertexInputAttributeDescription{1, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, rgb)});
ret.attributes.insert(vk::VertexInputAttributeDescription{2, 0, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, normal)});
ret.attributes.insert(vk::VertexInputAttributeDescription{3, 0, vk::Format::eR32G32Sfloat, offsetof(Vertex, uv)});
ret.bindings.insert(vk::VertexInputBindingDescription{1, sizeof(glm::uvec4)});
ret.attributes.insert(vk::VertexInputAttributeDescription{4, 1, vk::Format::eR32G32B32A32Uint});
ret.bindings.insert(vk::VertexInputBindingDescription{2, sizeof(glm::vec4)});
ret.attributes.insert(vk::VertexInputAttributeDescription{5, 2, vk::Format::eR32G32B32A32Sfloat});
return ret;
}

struct PipeInfo {
Pipes::State state{};
vk::ShaderModule vert{};
Expand All @@ -105,7 +122,7 @@ vk::UniquePipeline create_pipeline(vk::Device dv, PipeInfo const& info) {
gpci.renderPass = info.render_pass;
gpci.layout = info.layout;

static constexpr auto vl = vertex_layout();
auto const vl = info.state.vert_type == Pipeline::VertType::eSkinned ? skinned_vertex_layout() : instanced_vertex_layout();
auto const vertex_bindings = vl.bindings.span();
auto const vertex_attributes = vl.attributes.span();
auto pvisci = vk::PipelineVertexInputStateCreateInfo{};
Expand Down Expand Up @@ -170,11 +187,7 @@ vk::UniquePipeline create_pipeline(vk::Device dv, PipeInfo const& info) {
} // namespace

std::size_t Pipes::Hasher::operator()(Key const& key) const {
auto ret = key.shader_hash;
ret ^= (std::hash<vk::PolygonMode>{}(key.state.mode) << 16);
ret ^= (std::hash<vk::PrimitiveTopology>{}(key.state.topology) << 24);
ret ^= (std::hash<bool>{}(key.state.depth_test) << 32);
return ret;
return make_combined_hash(key.shader_hash, key.state.mode, key.state.topology, key.state.depth_test, key.state.vert_type);
}

Pipes::Pipes(Gfx const& gfx, vk::SampleCountFlagBits samples) : m_gfx{gfx}, m_samples{samples} {
Expand Down
1 change: 0 additions & 1 deletion lib/vk/src/static_mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <facade/vk/cmd.hpp>
#include <facade/vk/geometry.hpp>
#include <facade/vk/static_mesh.hpp>
#include <span>

namespace facade {
namespace {
Expand Down