Initial Commit

This commit is contained in:
Nepgfurmixpro 2023-10-29 09:02:00 -04:00
commit ce5fd7e93d
23 changed files with 607 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Project exclude paths
/cmake-build-debug/
/out/build/x64-debug/
/.vs/
/.idea/

16
CMakeLists.txt Normal file
View File

@ -0,0 +1,16 @@
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
# Enable Hot Reload for MSVC compilers if supported.
if (POLICY CMP0141)
cmake_policy(SET CMP0141 NEW)
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<IF:$<AND:$<C_COMPILER_ID:MSVC>,$<CXX_COMPILER_ID:MSVC>>,$<$<CONFIG:Debug,RelWithDebInfo>:EditAndContinue>,$<$<CONFIG:Debug,RelWithDebInfo>:ProgramDatabase>>")
endif()
project ("Nitric")
# Include sub-projects.
add_subdirectory ("Nitric")
add_subdirectory ("NitricEngine")

61
CMakePresets.json Normal file
View File

@ -0,0 +1,61 @@
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}

26
Nitric/CMakeLists.txt Normal file
View File

@ -0,0 +1,26 @@
# CMakeList.txt : CMake project for Vengine, include source and define
# project specific logic here.
#
# Add source to this project's executable.
file(GLOB_RECURSE SourceFiles "${CMAKE_CURRENT_SOURCE_DIR}/Source/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Source/*.h")
find_package(spdlog CONFIG REQUIRED)
find_package(Vulkan REQUIRED)
find_package(glm CONFIG REQUIRED)
add_library (Nitric STATIC ${SourceFiles})
target_link_libraries(Nitric PRIVATE glm::glm)
target_link_libraries(Nitric PRIVATE spdlog::spdlog)
target_link_libraries(Nitric PRIVATE Vulkan::Vulkan)
target_include_directories(Nitric PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/Source")
set(NITRIC_LIBRARY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/Nitric.lib")
set(NITRIC_HEADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Source")
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET Nitric PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.

View File

@ -0,0 +1,11 @@
#include <Core/Application.h>
namespace Nitric {
Application::Application(const ApplicationSettings& settings) {
}
void Application::Run() {
}
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <cstdint>
#include <string>
#include <Render/GraphicsContext.h>
#include <Core/Window.h>
namespace Nitric {
struct ApplicationSettings {
uint32_t Width;
uint32_t Height;
std::string Title;
};
class Application {
public:
explicit Application(const ApplicationSettings& settings);
void Run();
private:
};
static Application* CreateApplication();
}

30
Nitric/Source/Core/Base.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
#include <memory>
#define NITRIC_VERSION(major, minor, patch) (major << 16) + (minor << 8) + patch
namespace Nitric {
template <typename T>
using Ref = std::shared_ptr<T>;
template <typename T, typename... Args>
Ref<T> CreateRef(Args&&... args) {
return std::make_shared<T>(std::forward<Args>(args)...);
}
template <typename T>
using Scope = std::unique_ptr<T>;
template <typename T, typename... Args>
Scope<T> CreateScope(Args&&... args) {
return std::make_unique<T>(std::forward<Args>(args)...);
}
template <typename T, typename A>
Ref<T> CastPtr(A in) {
return std::dynamic_pointer_cast<T>(in);
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <Core/Application.h>
#include <Core/LogInternal.h>
#include <spdlog/spdlog.h>
extern Nitric::Application* Nitric::CreateApplication();
#ifdef WIN32
int main(int argc, char** argv) {
Nitric::LogInternal::Init();
Nitric::LogInternal::Info("Starting Nitric Engine");
Nitric::LogInternal::Info("Creating Nitric Application");
auto application = Nitric::CreateApplication();
}
#endif

View File

@ -0,0 +1,13 @@
#include <Core/LogInternal.h>
namespace Nitric {
Ref<spdlog::logger> LogInternal::s_Logger;
void LogInternal::Init() {
s_Logger = spdlog::stdout_color_mt("Nitric Internal");
}
Ref<spdlog::logger> LogInternal::Get() {
return s_Logger;
}
}

View File

@ -0,0 +1,77 @@
#pragma once
#include <Core/Base.h>
#include <Render/GraphicsAPI.h>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/stdout_color_sinks.h>
namespace Nitric {
class LogInternal {
public:
static void Init();
template <typename T>
static void Debug(T& msg) {
Get()->debug(msg);
}
template<typename... Args>
static void Debug(spdlog::format_string_t<Args...> fmt, Args &&...args)
{
Debug(fmt::format(fmt, std::forward(args)...));
}
template <typename T>
static void Info(T& msg) {
Get()->info(msg);
}
template<typename... Args>
static void Info(spdlog::format_string_t<Args...> fmt, Args &&...args)
{
Info(fmt::format(fmt, std::forward(args)...));
}
template <typename T>
static void Critical(T& msg) {
Get()->critical(msg);
}
template<typename... Args>
static void Critical(spdlog::format_string_t<Args...> fmt, Args &&...args)
{
Critical(fmt::format(fmt, std::forward(args)...));
}
template <typename T>
static void Fatal(T& msg) {
LogInternal::Error("The next error is fatal. This will terminate the program");
Get()->log(spdlog::level::from_str("fatal"), msg);
auto api = GraphicsAPI::APIToString(GraphicsAPI::GetAPI());
Info("GraphicsAPI is set to {}", api);
throw std::runtime_error(msg);
}
template<typename... Args>
static void Fatal(spdlog::format_string_t<Args...> fmt, Args &&...args)
{
Fatal(fmt::format(fmt, std::forward(args)...));
}
template <typename T>
static void Error(T& msg) {
Get()->error(msg);
}
template<typename... Args>
static void Error(spdlog::format_string_t<Args...> fmt, Args &&...args)
{
Error(fmt::format(fmt, std::forward(args)...));
}
private:
static Ref<spdlog::logger> s_Logger;
static Ref<spdlog::logger> Get();
};
}

View File

@ -0,0 +1,20 @@
#pragma once
#include <Core/Base.h>
#include <string>
#include <cstdint>
#include <glm/glm.hpp>
namespace Nitric {
struct WindowProperties {
uint32_t Width;
uint32_t Height;
std::string Title;
};
class Window {
public:
virtual void SetSize(const glm::vec2& size) = 0;
virtual glm::vec2 GetSize() const = 0;
};
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <vulkan/vulkan.h>
#include <vulkan/vk_enum_string_helper.h>
#include <Core/LogInternal.h>
namespace Nitric {
#ifdef NDEBUG
const bool VkValidationLayersEnabled = false;
#else
const bool VkValidationLayersEnabled = true;
#endif
namespace Vk {
template <typename ...Args>
void Validate(VkResult result, bool fatal, spdlog::format_string_t<Args...> fmt, Args &&...args) {
if (result != VK_SUCCESS) {
auto error = string_VkResult(result);
if (fatal) {
LogInternal::Fatal("({}) -> {}", error, fmt::format(fmt, std::forward<Args>(args)...))
} else {
LogInternal::Error("({}) -> {}", error, fmt::format(fmt, std::forward<Args>(args)...))
}
} else {
LogInternal::Debug("Vk::Validate success.")
}
}
}
}

View File

@ -0,0 +1,75 @@
#include <Platform/Vulkan/VkGraphicsContext.h>
#include <utility>
namespace Nitric {
VkGraphicsContext::VkGraphicsContext(Ref<VkWindow> window) {
m_Window = window;
}
void VkGraphicsContext::Init() {
this->CreateInstance();
}
void VkGraphicsContext::CreateInstance() {
if (!CheckValidationLayerSupport() && VkValidationLayersEnabled) {
}
VkApplicationInfo appInfo{};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pNext = nullptr;
appInfo.pApplicationName = "Nitric";
appInfo.applicationVersion = NITRIC_VERSION(1, 0, 0);
appInfo.pEngineName = "Nitric Engine";
appInfo.engineVersion = NITRIC_VERSION(1, 0, 0);
appInfo.apiVersion = VK_API_VERSION_1_3;
VkInstanceCreateInfo createInfo{};
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;
auto windowExtensions = this->m_Window->GetExtensions();
createInfo.enabledExtensionCount = static_cast<uint32_t>(windowExtensions.size());
createInfo.ppEnabledExtensionNames = windowExtensions.data();
if (VkValidationLayersEnabled) {
createInfo.enabledLayerCount = static_cast<uint32_t>(s_ValidationLayers.size());
createInfo.ppEnabledLayerNames = s_ValidationLayers.data();
} else {
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = nullptr;
}
Vk::Validate(vkCreateInstance(&createInfo, nullptr, &m_Instance), true, "VkInstance VkGraphicsContext::m_Instance failed on creation.")
}
bool VkGraphicsContext::CheckValidationLayerSupport() {
uint32_t layerCount;
vkEnumerateInstanceLayerProperties(&layerCount, nullptr);
std::vector<VkLayerProperties> layerProperties(layerCount);
vkEnumerateInstanceLayerProperties(&layerCount, layerProperties.data());
for (auto validationLayer : s_ValidationLayers) {
bool layerFound = false;
for (auto layerProperty : layerProperties) {
if (strcmp(layerProperty.layerName, validationLayer) == 0) {
layerFound = true;
}
}
if (!layerFound) {
return false;
}
}
return true;
}
std::vector<const char*> VkGraphicsContext::s_ValidationLayers = {
"VK_LAYER_KHRONOS_validation"
};
VkGraphicsContext::~VkGraphicsContext() = default;
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <Render/GraphicsContext.h>
#include <Core/Base.h>
#include <Platform/Vulkan/VkWindow.h>
#include <Platform/Vulkan/VkBase.h>
#include <vulkan/vulkan.h>
namespace Nitric {
class VkGraphicsContext : public GraphicsContext {
public:
explicit VkGraphicsContext(Ref<VkWindow> window);
~VkGraphicsContext();
void Init() override;
private:
void CreateInstance();
static bool CheckValidationLayerSupport();
static std::vector<const char*> s_ValidationLayers;
Ref<VkWindow> m_Window;
VkInstance m_Instance{};
};
}

View File

@ -0,0 +1,18 @@
#include <Platform/Vulkan/VkWindow.h>
namespace Nitric {
VkWindow::VkWindow(const WindowProperties& properties) {
}
std::vector<const char *> VkWindow::GetExtensions() {
uint32_t extensionCount;
auto extensions = glfwGetRequiredInstanceExtensions(&extensionCount);
std::vector<const char*> returnExt(extensionCount);
for (int i = 0; i < extensionCount; i++) {
returnExt.push_back(extensions[i]);
}
return returnExt;
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include <Core/Window.h>
#include <vector>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
namespace Nitric {
class VkWindow : public Window {
public:
explicit VkWindow(const WindowProperties& properties);
virtual std::vector<const char*> GetExtensions() = 0;
};
}

View File

@ -0,0 +1,17 @@
#include "GraphicsAPI.h"
namespace Nitric {
GraphicsAPI::API GraphicsAPI::s_API = None;
void GraphicsAPI::SetAPI(API api) {
s_API = api;
}
GraphicsAPI::API GraphicsAPI::GetAPI() {
if (s_API == None) {
spdlog::critical("Graphics API requested but not set. Failing here.");
throw std::runtime_error("GraphicsAPI = None;");
}
return s_API;
}
}

View File

@ -0,0 +1,30 @@
#pragma once
#include <spdlog/spdlog.h>
namespace Nitric {
class GraphicsAPI {
public:
enum API {
Vulkan,
None
};
static inline std::string& APIToString(API api) {
switch (api) {
case Vulkan:
return "Vulkan";
default:
return "None";
}
}
static void SetAPI(API api);
static API GetAPI();
private:
static API s_API;
};
}

View File

@ -0,0 +1,11 @@
#pragma once
namespace Nitric {
class GraphicsContext {
public:
virtual void Init() = 0;
};
}

View File

@ -0,0 +1,6 @@
#include <Render/Shader.h>
namespace Nitric {
}

View File

@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
#include <map>
#include <string>
#include <Core/Base.h>
#include "GraphicsAPI.h"
namespace Nitric {
class Shader {
public:
virtual ~Shader() = 0;
virtual uint64_t GetHash() = 0;
virtual void Bind() = 0; // TODO: Bind(GraphicsPipeline);
static Ref<Shader> Create(const std::string& filepath);
};
class ShaderCache {
public:
static bool IsPresent(uint64_t hash);
static Ref<Shader> GetShader(uint64_t hash);
static void AddShader(Ref<Shader> shader);
private:
static std::map<uint64_t, Ref<Shader>> s_Shaders;
};
}

View File

@ -0,0 +1,24 @@
# CMakeList.txt : CMake project for Vengine, include source and define
# project specific logic here.
#
# Add source to this project's executable.
file(GLOB_RECURSE SourceFiles "${CMAKE_CURRENT_SOURCE_DIR}/Source/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Source/*.h")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/Source")
find_package(spdlog CONFIG REQUIRED)
find_package(Vulkan REQUIRED)
find_package(glm CONFIG REQUIRED)
add_executable (NitricEngine ${SourceFiles})
target_link_libraries(NitricEngine PRIVATE glm::glm)
target_link_libraries(NitricEngine PRIVATE spdlog::spdlog)
target_link_libraries(NitricEngine PRIVATE Vulkan::Vulkan)
target_link_libraries(NitricEngine PRIVATE Nitric)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET NitricEngine PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.

View File

@ -0,0 +1,18 @@
#include <Core/Application.h>
#include <Core/EntryPoint.h>
class NitricEngine : public Nitric::Application {
public:
NitricEngine(const Nitric::ApplicationSettings& settings) : Application(settings) {
}
};
Nitric::Application* Nitric::CreateApplication() {
ApplicationSettings settings;
settings.Width = 1280;
settings.Height = 720;
settings.Title = "Nitric Engine";
return new NitricEngine(settings);
}