Skip to content

Commit

Permalink
Merge pull request #113 from AGH-Code-Industry/twarug/nvrhi_renderer
Browse files Browse the repository at this point in the history
Basic nvrhi renderer
  • Loading branch information
Twarug authored Dec 28, 2024
2 parents 10edc14 + ef9706f commit 9d2f86c
Show file tree
Hide file tree
Showing 84 changed files with 4,185 additions and 233 deletions.
3 changes: 2 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Checks: '-*,readability-identifier-naming'
CheckOptions:
- { key: readability-identifier-naming.NamespaceCase, value: camelBack }
- { key: readability-identifier-naming.NamespaceIgnoredRegexp, value: _* }
- { key: readability-identifier-naming.NamespaceIgnoredRegexp, value: _.* }

- { key: readability-identifier-naming.EnumCase, value: CamelCase }
- { key: readability-identifier-naming.ClassCase, value: CamelCase }
Expand All @@ -16,6 +16,7 @@ CheckOptions:
- { key: readability-identifier-naming.ParameterCase, value: camelBack }
- { key: readability-identifier-naming.ClassMemberCase, value: camelBack }
- { key: readability-identifier-naming.EnumConstantCase, value: camelBack }
- { key: readability-identifier-naming.ClassMemberPrefix, value: s_ }

- { key: readability-identifier-naming.PrivateMethodCase, value: camelBack }
- { key: readability-identifier-naming.ProtectedMethodCase, value: camelBack }
Expand Down
89 changes: 89 additions & 0 deletions archimedes_bin/examples/NvrhiRendererTestApp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#pragma once

#include <Ecs.h>
#include <Engine.h>
#include <Scene.h>

struct VelocityComponent {
arch::math::float3 velocity;
};

class NvrhiRendererTestApp: public arch::Application {
void init() override {
arch::Ref<arch::scene::Scene> testScene = arch::createRef<arch::scene::Scene>();

{
arch::ecs::Entity e = testScene->newEntity();
testScene->domain().addComponent<arch::scene::components::TransformComponent>(
e,
{
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
{ 1.0f, .5f, 0.0f },
}
);

// 2D square
// struct Vertex {
// float3 position;
// float3 color;
// float2 tex_coords;
// };

// std::vector<Vertex> vertices{
// { float3(0.5f, 0.5f, 0.0f), {}, float2(1.0f, 1.0f) },
// { float3(0.5f, -0.5f, 0.0f), {}, float2(1.0f, 0.0f) },
// { float3(-0.5f, -0.5f, 0.0f), {}, float2(0.0f, 0.0f) },
// { float3(-0.5f, 0.5f, 0.0f), {}, float2(0.0f, 1.0f) }
// };
// std::vector<u32> indices{ 0, 1, 3, 1, 2, 3 };

// Ref<Shader> vShader = Shader::load("shaders/vertex_shader.sprv");
// Ref<Shader> fShader = Shader::load("shaders/fragment_shader.sprv");
// Ref<Material> material = Material::create(vShader, fShader);
// material->setTexture("_mainTxt", TextureLoader::read_file("textures/.jpg"));
// material->SetFloat("_mixValue", 0.2f);
// material->SetFloat3("_pos", glm::vec3(0.5f, 0.5f, 0.5f));
// material->SetColor("_color", glm::vec3(1.0f, 0.0f, 0.0f));

// Ref<Mesh> mesh = Mesh::create<Vertex>(vertices, indices);
testScene->domain().addComponent<arch::scene::components::MeshComponent>(e, { /*mesh*/ });
testScene->domain().addComponent<VelocityComponent>(e, arch::float3{ 0.0f, .01f, 0.0f });
}

{
arch::ecs::Entity e = testScene->newEntity();
testScene->domain().addComponent<arch::scene::components::TransformComponent>(
e,
{
{ 0.5f, 0.5f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
arch::float3(1)
}
);
testScene->domain().addComponent<arch::scene::components::MeshComponent>(e, { /*mesh*/ });
testScene->domain().addComponent<VelocityComponent>(e, arch::float3{ 0.0f, -.01f, 0.001f });
}

arch::scene::SceneManager::get()->changeScene(testScene);
}

void update() override {
auto view = arch::scene::SceneManager::get()
->currentScene()
->domain()
.view<arch::scene::components::TransformComponent, VelocityComponent>();

for (auto [entity, transform, velocity] : view.all()) {
if (transform.position.y < -.5f || transform.position.y > .5f) {
velocity.velocity *= -1;
}
if (transform.position.x < -.5f || transform.position.x > .5f) {
velocity.velocity.x *= -1;
}

transform.position.x += velocity.velocity.x;
transform.position.y += velocity.velocity.y;
}
}
};
13 changes: 7 additions & 6 deletions archimedes_bin/main.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include "examples/NvrhiRendererTestApp.h"
#include <Engine.h>
#include <Logger.h>

struct MyApp: arch::Application {
void init() override { arch::Logger::info("Initializing user app!"); }
};

int main() {
arch::Logger::init(arch::LogLevel::trace);

arch::EngineConfig config{ 600, 480, "Archimedes Test", glm::vec4(0, 0, 0, 0) };
arch::Ref<arch::Application> myApp = arch::createRef<NvrhiRendererTestApp>();

arch::Ref<MyApp> myApp = arch::createRef<MyApp>();
arch::EngineConfig config{ .windowWidth = 600,
.windowHeight = 480,
.windowTitle = "Archimedes Test",
.backgroundColor = arch::Color(.03f, .03f, .03, 1.f),
.renderingApi = arch::gfx::RenderingAPI::Nvrhi_VK };

arch::Engine engine{ config, myApp };
engine.start();
Expand Down
14 changes: 13 additions & 1 deletion cmake/library.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ include("${PROJECT_SOURCE_DIR}/cmake/conan.cmake")
add_library(${PROJECT_NAME})

# find source files
file(GLOB_RECURSE ARCHIMEDES_SOURCE src/**.cpp)
file(GLOB_RECURSE ARCHIMEDES_SOURCE CONFIGURE_DEPENDS src/**.cpp)
target_sources(${PROJECT_NAME} PRIVATE ${ARCHIMEDES_SOURCE})
target_include_directories(${PROJECT_NAME} PUBLIC include)

# link conan libraries
target_link_libraries(${PROJECT_NAME} PUBLIC ${ARCHIMEDES_LIBRARIES})


include(FetchContent)
FetchContent_Declare(
nvrhi
GIT_REPOSITORY https://github.com/NVIDIAGameWorks/NvRhi.git
GIT_TAG main
OVERRIDE_FIND_PACKAGE
)
find_package(nvrhi REQUIRED)

target_link_libraries(${PROJECT_NAME} PRIVATE nvrhi_vk nvrhi)
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def requirements(self):
self.requires("vulkan-memory-allocator/cci.20231120")

# SPIRV (Shader compiler)
# self.requires("shaderc/2021.1") # waiting for conan repo update
self.requires("shaderc/2023.6") # waiting for conan repo update

def configure(self):
if is_msvc(self, True):
Expand Down
25 changes: 16 additions & 9 deletions include/Engine.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <memory>
#include <string>

#include "Application.h"
Expand All @@ -10,12 +9,16 @@
#include <gtest/gtest_prod.h>

namespace arch {
namespace scene {
class SceneManager;
}

struct EngineConfig {
int window_width;
int window_height;
std::string window_title;
glm::vec4 background_color;
int windowWidth;
int windowHeight;
std::string windowTitle;
Color backgroundColor;
gfx::RenderingAPI renderingApi;
};

/**
Expand All @@ -32,13 +35,14 @@ class Engine {
void start();

private:
Window _window;
Ref<Window> _mainWindow;
EngineConfig _engineConfig;
gfx::Renderer _renderer;
Ref<gfx::Renderer> _renderer;
Ref<Application> _application;

FRIEND_TEST(EngineTest, ConfigWindowTest);
Ref<scene::SceneManager> _sceneManager;

private:
/**
* Responsible for drawing game on the screen.
*/
Expand All @@ -52,7 +56,10 @@ class Engine {
/**
* Clearing all previously allocated GLFW and Engine resources.
*/
static void _terminate();
void _shutdown();

private:
FRIEND_TEST(EngineTest, ConfigWindowTest);
};

} // namespace arch
5 changes: 5 additions & 0 deletions include/Gfx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "gfx/Buffer.h"
#include "gfx/Mesh.h"
#include "gfx/Renderer.h"
10 changes: 7 additions & 3 deletions include/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum class LogLevel {
};

/// @brief Implementation details.
namespace _details { // NOLINT(*-identifier-naming)
namespace _details {

template<typename... Args>
struct UniversalLogger;
Expand Down Expand Up @@ -82,8 +82,12 @@ class Logger {
private:

template<typename... Args>
static void
_log_impl(LogLevel level, std::source_location loc, spdlog::format_string_t<Args...> fmt, Args&&... args);
static void _logImpl(
LogLevel level,
std::source_location loc,
spdlog::format_string_t<Args...> fmt,
Args&&... args
);

static std::shared_ptr<spdlog::logger> s_logger;
};
Expand Down
4 changes: 2 additions & 2 deletions include/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace arch {

template<typename... Args>
void Logger::_log_impl(
void Logger::_logImpl(
LogLevel level,
const std::source_location loc,
spdlog::format_string_t<Args...> fmt,
Expand Down Expand Up @@ -41,7 +41,7 @@ struct UniversalLogger {
std::source_location loc = std::source_location::current()
) {
// Pass all arguments to the logger
Logger::_log_impl(level, loc, fmt, std::forward<Args>(args)...);
Logger::_logImpl(level, loc, fmt, std::forward<Args>(args)...);
}
};

Expand Down
9 changes: 9 additions & 0 deletions include/Mmath.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "math/Math.h"

namespace arch {

using namespace arch::math;

} // namespace arch
10 changes: 10 additions & 0 deletions include/Ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@ namespace arch {
template<typename T>
using Ref = std::shared_ptr<T>;

template<typename T>
using WeakRef = std::weak_ptr<T>;

template<typename T, typename... Args>
requires std::constructible_from<T, Args...>
constexpr Ref<T> createRef(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}

template<typename T>
requires std::derived_from<T, std::enable_shared_from_this<T>>
constexpr Ref<T> createRef(T* ptr) {
return ptr->shared_from_this();
}

} // namespace arch
44 changes: 3 additions & 41 deletions include/Scene.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
#pragma once

#include <Ecs.h>
#include <Hier.h>

namespace arch {

class Scene {
public:

/// @brief Hierarchy node class
using Node = hier::HierarchyNode;

/// @brief Default constructor
Scene() noexcept;

/// @brief Creates new entity
/// @see arch::ecs::Domain::newEntity()
ecs::Entity newEntity() noexcept;
/// @brief Kills entity
/// @see arch::ecs::Domain::kill(const ecs::Entity)
void removeEntity(const ecs::Entity entity) noexcept;

/// @brief Returns ecs::Domain of this scene
ecs::Domain& domain() noexcept;
/// @brief Returns readonly ecs::Domain of this scene
const ecs::Domain& domain() const noexcept;

/// @brief Returns root entity
ecs::Entity root() const noexcept;
/// @brief Returns root node
Node& rootNode() noexcept;
/// @brief Returns readonly root node
const Node& rootNode() const noexcept;

private:

ecs::Domain _domain;
Node* _rootNode;
};

} // namespace arch
#include "scene/Components.h"
#include "scene/Scene.h"
#include "scene/SceneManager.h"
7 changes: 1 addition & 6 deletions include/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <string>

#include <ArchMath.h>
#include <GLFW/glfw3.h>

namespace arch {
Expand Down Expand Up @@ -33,8 +32,6 @@ class Window {
*/
GLFWwindow* get() const;

void clear(Color color) const;
void clear(float r, float g, float b, float a) const;
void swapBuffers() const;
void resize(int width, int height) const;
void setTitle(const std::string& title) const;
Expand All @@ -54,9 +51,7 @@ class Window {
* @param monitor The monitor to use for fullscreen mode.
* @param window The window whose context to share resources with, or NULL to not share resources.
*/
void initialize(int width, int height, const char* name, GLFWmonitor* monitor, GLFWwindow* window);

static void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void _initialize(int width, int height, const char* name, GLFWmonitor* monitor, GLFWwindow* window);
};

} // namespace arch
13 changes: 13 additions & 0 deletions include/gfx/Buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "buffer/Buffer.h"
#include "buffer/BufferManager.h"
#include "buffer/BufferType.h"
#include "buffer/IndexBuffer.h"
#include "buffer/VertexBuffer.h"

namespace arch::gfx {

using namespace arch::gfx::buffer;

} // namespace arch::gfx
Loading

0 comments on commit 9d2f86c

Please sign in to comment.