mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-10 22:23:34 +00:00
big refactoring ! ci skip
This commit is contained in:
19
Makefile
19
Makefile
@@ -1,21 +1,12 @@
|
|||||||
# **************************************************************************** #
|
|
||||||
# #
|
|
||||||
# ::: :::::::: #
|
|
||||||
# Makefile :+: :+: :+: #
|
|
||||||
# +:+ +:+ +:+ #
|
|
||||||
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
|
|
||||||
# +#+#+#+#+#+ +#+ #
|
|
||||||
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
|
|
||||||
# Updated: 2024/07/05 13:34:03 by maldavid ### ########.fr #
|
|
||||||
# #
|
|
||||||
# **************************************************************************** #
|
|
||||||
|
|
||||||
NAME = libmlx.so
|
NAME = libmlx.so
|
||||||
|
|
||||||
SRCS = $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Core))
|
SRCS = $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Core))
|
||||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Platform))
|
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Platform))
|
||||||
|
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Graphics))
|
||||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer))
|
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer))
|
||||||
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer/**))
|
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer/Vulkan))
|
||||||
|
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer/Pipelines))
|
||||||
|
SRCS += $(wildcard $(addsuffix /*.cpp, ./runtime/Sources/Renderer/RenderPasses))
|
||||||
|
|
||||||
OBJ_DIR = objs/makefile
|
OBJ_DIR = objs/makefile
|
||||||
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
|
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
|
||||||
@@ -37,7 +28,7 @@ MODE = "release"
|
|||||||
CXX = clang++
|
CXX = clang++
|
||||||
|
|
||||||
CXXFLAGS = -std=c++17 -O3 -fPIC -Wall -Wextra -Wno-deprecated -DSDL_MAIN_HANDLED
|
CXXFLAGS = -std=c++17 -O3 -fPIC -Wall -Wextra -Wno-deprecated -DSDL_MAIN_HANDLED
|
||||||
INCLUDES = -I./includes -I./runtime/Includes -I./third_party
|
INCLUDES = -I./includes -I./runtime/Includes -I./runtime/Sources -I./third_party
|
||||||
|
|
||||||
LDLIBS =
|
LDLIBS =
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Application.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 14:04:19 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_APPLICATION__
|
#ifndef __MLX_APPLICATION__
|
||||||
#define __MLX_APPLICATION__
|
#define __MLX_APPLICATION__
|
||||||
|
|
||||||
@@ -26,32 +14,32 @@ namespace mlx
|
|||||||
Application();
|
Application();
|
||||||
|
|
||||||
inline void GetMousePos(int* x, int* y) noexcept;
|
inline void GetMousePos(int* x, int* y) noexcept;
|
||||||
inline void MouseMove(void* win, int x, int y) noexcept;
|
inline void MouseMove(Handle win, int x, int y) noexcept;
|
||||||
|
|
||||||
inline void OnEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
|
inline void OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
|
||||||
|
|
||||||
inline void GetScreenSize(void* win, int* w, int* h) noexcept;
|
inline void GetScreenSize(Handle win, int* w, int* h) noexcept;
|
||||||
|
|
||||||
inline void SetFPSCap(std::uint32_t fps) noexcept;
|
inline void SetFPSCap(std::uint32_t fps) noexcept;
|
||||||
|
|
||||||
inline void* NewGraphicsSuport(std::size_t w, std::size_t h, const char* title);
|
inline Handle NewGraphicsSuport(std::size_t w, std::size_t h, const char* title);
|
||||||
inline void ClearGraphicsSupport(void* win);
|
inline void ClearGraphicsSupport(Handle win);
|
||||||
inline void DestroyGraphicsSupport(void* win);
|
inline void DestroyGraphicsSupport(Handle win);
|
||||||
|
|
||||||
inline void PixelPut(void* win, int x, int y, std::uint32_t color) const noexcept;
|
inline void PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept;
|
||||||
inline void StringPut(void* win, int x, int y, std::uint32_t color, char* str);
|
inline void StringPut(Handle win, int x, int y, std::uint32_t color, char* str);
|
||||||
|
|
||||||
void* NewTexture(int w, int h);
|
Handle NewTexture(int w, int h);
|
||||||
void* NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...)
|
Handle NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...)
|
||||||
inline void TexturePut(void* win, void* img, int x, int y);
|
inline void TexturePut(Handle win, Handle img, int x, int y);
|
||||||
inline int GetTexturePixel(void* img, int x, int y);
|
inline int GetTexturePixel(Handle img, int x, int y);
|
||||||
inline void SetTexturePixel(void* img, int x, int y, std::uint32_t color);
|
inline void SetTexturePixel(Handle img, int x, int y, std::uint32_t color);
|
||||||
void DestroyTexture(void* ptr);
|
void DestroyTexture(Handle ptr);
|
||||||
|
|
||||||
inline void LoopHook(int (*f)(void*), void* param);
|
inline void LoopHook(int (*f)(void*), void* param);
|
||||||
inline void LoopEnd() noexcept;
|
inline void LoopEnd() noexcept;
|
||||||
|
|
||||||
inline void LoadFont(void* win, const std::filesystem::path& filepath, float scale);
|
inline void LoadFont(Handle win, const std::filesystem::path& filepath, float scale);
|
||||||
|
|
||||||
void Run() noexcept;
|
void Run() noexcept;
|
||||||
|
|
||||||
@@ -62,8 +50,8 @@ namespace mlx
|
|||||||
Inputs m_in;
|
Inputs m_in;
|
||||||
ImageRegistry m_image_registry;
|
ImageRegistry m_image_registry;
|
||||||
std::vector<std::unique_ptr<GraphicsSupport>> m_graphics;
|
std::vector<std::unique_ptr<GraphicsSupport>> m_graphics;
|
||||||
std::function<int(void*)> f_loop_hook;
|
std::function<int(Handle)> f_loop_hook;
|
||||||
void* p_param = nullptr;
|
Handle p_param = nullptr;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,4 @@
|
|||||||
/* ************************************************************************** */
|
#pragma once
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Application.inl :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 14:45:07 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <Core/Application.h>
|
#include <Core/Application.h>
|
||||||
|
|
||||||
#define CHECK_WINDOW_PTR(win) \
|
#define CHECK_WINDOW_PTR(win) \
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Enums.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:15:24 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:23:10 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_CORE_ENUMS__
|
#ifndef __MLX_CORE_ENUMS__
|
||||||
#define __MLX_CORE_ENUMS__
|
#define __MLX_CORE_ENUMS__
|
||||||
|
|
||||||
@@ -27,8 +15,18 @@ namespace mlx
|
|||||||
|
|
||||||
EndEnum
|
EndEnum
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr std::size_t LogTypeCount = static_cast<std::size_t>(LogType::EndEnum);
|
constexpr std::size_t LogTypeCount = static_cast<std::size_t>(LogType::EndEnum);
|
||||||
|
|
||||||
|
enum class Event
|
||||||
|
{
|
||||||
|
ResizeEventCode = 56,
|
||||||
|
FrameBeginEventCode = 57,
|
||||||
|
FatalErrorEventCode = 168,
|
||||||
|
QuitEventCode = 168,
|
||||||
|
|
||||||
|
EndEnum
|
||||||
|
};
|
||||||
|
constexpr std::size_t EventCount = static_cast<std::size_t>(Event::EndEnum);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* EventBase.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:27:22 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:31:16 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_BASE_EVENT__
|
#ifndef __MLX_BASE_EVENT__
|
||||||
#define __MLX_BASE_EVENT__
|
#define __MLX_BASE_EVENT__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* EventBus.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:30:36 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:31:41 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_EVENT_BUS__
|
#ifndef __MLX_EVENT_BUS__
|
||||||
#define __MLX_EVENT_BUS__
|
#define __MLX_EVENT_BUS__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* EventListener.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:28:17 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:37:53 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_EVENT_LISTENER__
|
#ifndef __MLX_EVENT_LISTENER__
|
||||||
#define __MLX_EVENT_LISTENER__
|
#define __MLX_EVENT_LISTENER__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Format.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:11:09 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:12:03 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_FORMAT__
|
#ifndef __MLX_FORMAT__
|
||||||
#define __MLX_FORMAT__
|
#define __MLX_FORMAT__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,4 @@
|
|||||||
/* ************************************************************************** */
|
#pragma once
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Format.inl :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:11:09 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:12:03 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <Core/Format.h>
|
#include <Core/Format.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Fps.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/18 14:53:30 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 20:52:06 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_FPS__
|
#ifndef __MLX_FPS__
|
||||||
#define __MLX_FPS__
|
#define __MLX_FPS__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Graphics.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:46:58 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_GRAPHICS__
|
#ifndef __MLX_GRAPHICS__
|
||||||
#define __MLX_GRAPHICS__
|
#define __MLX_GRAPHICS__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,4 @@
|
|||||||
/* ************************************************************************** */
|
#pragma once
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* graphics.inl :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2023/04/02 15:26:16 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <Core/Graphics.h>
|
#include <Core/Graphics.h>
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ImagesRegistry.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/04/03 15:11:47 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/21 20:31:00 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_CORE_IMAGES_REGISTRY__
|
#ifndef __MLX_CORE_IMAGES_REGISTRY__
|
||||||
#define __MLX_CORE_IMAGES_REGISTRY__
|
#define __MLX_CORE_IMAGES_REGISTRY__
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,3 @@
|
|||||||
// This file is a part of Akel
|
|
||||||
// Authors : @kbz_8
|
|
||||||
// Created : 21/04/2024
|
|
||||||
// Updated : 21/04/2024
|
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <Core/ImagesRegistry.h>
|
#include <Core/ImagesRegistry.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Logs.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:14:10 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:19:23 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_LOGS__
|
#ifndef __MLX_LOGS__
|
||||||
#define __MLX_LOGS__
|
#define __MLX_LOGS__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,4 @@
|
|||||||
/* **************************************************************************** */
|
#pragma once
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Logs.inl :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 17:19:47 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 17:19:47 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* **************************************************************************** */
|
|
||||||
|
|
||||||
#include <Core/Logs.h>
|
#include <Core/Logs.h>
|
||||||
#include <Core/Format.h>
|
#include <Core/Format.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Memory.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 21:16:44 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_MEMORY__
|
#ifndef __MLX_MEMORY__
|
||||||
#define __MLX_MEMORY__
|
#define __MLX_MEMORY__
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Profiler.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/10 13:35:45 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:24:17 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_PROFILER__
|
#ifndef __MLX_PROFILER__
|
||||||
#define __MLX_PROFILER__
|
#define __MLX_PROFILER__
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,7 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* SDLManager.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/05/25 15:28:59 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 22:15:22 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_SDL_MANAGER__
|
#ifndef __MLX_SDL_MANAGER__
|
||||||
#define __MLX_SDL_MANAGER__
|
#define __MLX_SDL_MANAGER__
|
||||||
|
|
||||||
#include <Utils/Singleton.h>
|
#include <Maths/Vec2.h>
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
@@ -25,17 +13,27 @@ namespace mlx
|
|||||||
void Init() noexcept;
|
void Init() noexcept;
|
||||||
void Shutdown() noexcept;
|
void Shutdown() noexcept;
|
||||||
|
|
||||||
void* CreateWindow(const std::string& title, std::size_t w, std::size_t h);
|
Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden);
|
||||||
void DestroyWindow(void* window) noexcept;
|
void DestroyWindow(Handle window) noexcept;
|
||||||
|
|
||||||
void SetEventCallback();
|
VkSurfaceKHR CreateVulkanSurface(Handle window, VkInstance instance) const noexcept;
|
||||||
|
std::vector<const char*> GetRequiredVulkanInstanceExtentions(Handle window) const noexcept;
|
||||||
|
Vec2ui GetVulkanDrawableSize(Handle window) const noexcept;
|
||||||
|
|
||||||
|
inline void SetEventCallback(func::function<void(mlx_event_type, int, void*)> functor, void* userdata) { f_callback = std::move(functor); p_callback_data = userdata; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDLManager() = default;
|
SDLManager() = default;
|
||||||
~SDLManager() = default;
|
~SDLManager() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_set<void*> m_windows_registry;
|
std::unordered_set<Handle> m_windows_registry;
|
||||||
|
func::function<void(mlx_event_type, int, void*)> f_callback;
|
||||||
|
void* p_callback_data = nullptr;
|
||||||
|
std::int32_t m_x;
|
||||||
|
std::int32_t m_y;
|
||||||
|
std::int32_t m_rel_x;
|
||||||
|
std::int32_t m_rel_y;
|
||||||
bool m_drop_sdl_responsability = false;
|
bool m_drop_sdl_responsability = false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* UUID.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/06 11:13:23 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 21:19:18 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_UUID__
|
#ifndef __MLX_UUID__
|
||||||
#define __MLX_UUID__
|
#define __MLX_UUID__
|
||||||
|
|
||||||
|
|||||||
28
runtime/Includes/Embedded/2DFragment.nzsl
git.filemode.normal_file
28
runtime/Includes/Embedded/2DFragment.nzsl
git.filemode.normal_file
@@ -0,0 +1,28 @@
|
|||||||
|
[nzsl_version("1.0")]
|
||||||
|
module;
|
||||||
|
|
||||||
|
struct VertOut
|
||||||
|
{
|
||||||
|
[location(0)] color: vec4[f32],
|
||||||
|
[location(1)] uv: vec2[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FragOut
|
||||||
|
{
|
||||||
|
[location(0)] color: vec4[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
external
|
||||||
|
{
|
||||||
|
[set(1), binding(0)] u_texture: sampler2D[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(frag)]
|
||||||
|
fn main(input: VertOut) -> FragOut
|
||||||
|
{
|
||||||
|
let output: FragOut;
|
||||||
|
output.color = input.color * u_texture.Sample(input.uv);
|
||||||
|
if(output.color.w == 0.0)
|
||||||
|
discard;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
44
runtime/Includes/Embedded/2DFragment.spv.h
git.filemode.normal_file
44
runtime/Includes/Embedded/2DFragment.spv.h
git.filemode.normal_file
@@ -0,0 +1,44 @@
|
|||||||
|
3,2,35,7,0,0,1,0,39,0,0,0,51,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
|
||||||
|
3,0,0,0,0,0,1,0,0,0,15,0,8,0,4,0,0,0,28,0,0,0,109,97,105,110,0,0,0,0,
|
||||||
|
10,0,0,0,16,0,0,0,22,0,0,0,16,0,3,0,28,0,0,0,7,0,0,0,3,0,3,0,0,0,
|
||||||
|
0,0,100,0,0,0,5,0,4,0,19,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,19,0,0,0,
|
||||||
|
0,0,0,0,99,111,108,111,114,0,0,0,6,0,4,0,19,0,0,0,1,0,0,0,117,118,0,0,5,0,
|
||||||
|
4,0,23,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,23,0,0,0,0,0,0,0,99,111,108,111,
|
||||||
|
114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0,5,0,4,0,10,0,
|
||||||
|
0,0,99,111,108,111,114,0,0,0,5,0,3,0,16,0,0,0,117,118,0,0,5,0,4,0,22,0,0,0,
|
||||||
|
99,111,108,111,114,0,0,0,5,0,4,0,28,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,5,0,
|
||||||
|
0,0,33,0,0,0,0,0,0,0,71,0,4,0,5,0,0,0,34,0,0,0,1,0,0,0,71,0,4,0,
|
||||||
|
10,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,16,0,0,0,30,0,0,0,1,0,0,0,71,0,
|
||||||
|
4,0,22,0,0,0,30,0,0,0,0,0,0,0,72,0,5,0,19,0,0,0,0,0,0,0,35,0,0,0,
|
||||||
|
0,0,0,0,72,0,5,0,19,0,0,0,1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,23,0,
|
||||||
|
0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0,3,0,1,0,0,0,32,0,0,0,25,0,9,0,
|
||||||
|
2,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,
|
||||||
|
0,0,27,0,3,0,3,0,0,0,2,0,0,0,32,0,4,0,4,0,0,0,0,0,0,0,3,0,0,0,
|
||||||
|
19,0,2,0,6,0,0,0,33,0,3,0,7,0,0,0,6,0,0,0,23,0,4,0,8,0,0,0,1,0,
|
||||||
|
0,0,4,0,0,0,32,0,4,0,9,0,0,0,1,0,0,0,8,0,0,0,21,0,4,0,11,0,0,0,
|
||||||
|
32,0,0,0,1,0,0,0,43,0,4,0,11,0,0,0,12,0,0,0,0,0,0,0,32,0,4,0,13,0,
|
||||||
|
0,0,7,0,0,0,8,0,0,0,23,0,4,0,14,0,0,0,1,0,0,0,2,0,0,0,32,0,4,0,
|
||||||
|
15,0,0,0,1,0,0,0,14,0,0,0,43,0,4,0,11,0,0,0,17,0,0,0,1,0,0,0,32,0,
|
||||||
|
4,0,18,0,0,0,7,0,0,0,14,0,0,0,30,0,4,0,19,0,0,0,8,0,0,0,14,0,0,0,
|
||||||
|
32,0,4,0,20,0,0,0,7,0,0,0,19,0,0,0,32,0,4,0,21,0,0,0,3,0,0,0,8,0,
|
||||||
|
0,0,30,0,3,0,23,0,0,0,8,0,0,0,32,0,4,0,24,0,0,0,7,0,0,0,23,0,0,0,
|
||||||
|
43,0,4,0,11,0,0,0,25,0,0,0,3,0,0,0,43,0,4,0,1,0,0,0,26,0,0,0,0,0,
|
||||||
|
0,0,20,0,2,0,27,0,0,0,59,0,4,0,4,0,0,0,5,0,0,0,0,0,0,0,59,0,4,0,
|
||||||
|
9,0,0,0,10,0,0,0,1,0,0,0,59,0,4,0,15,0,0,0,16,0,0,0,1,0,0,0,59,0,
|
||||||
|
4,0,21,0,0,0,22,0,0,0,3,0,0,0,54,0,5,0,6,0,0,0,28,0,0,0,0,0,0,0,
|
||||||
|
7,0,0,0,248,0,2,0,29,0,0,0,59,0,4,0,24,0,0,0,30,0,0,0,7,0,0,0,59,0,
|
||||||
|
4,0,20,0,0,0,31,0,0,0,7,0,0,0,65,0,5,0,13,0,0,0,32,0,0,0,31,0,0,0,
|
||||||
|
12,0,0,0,63,0,3,0,32,0,0,0,10,0,0,0,65,0,5,0,18,0,0,0,33,0,0,0,31,0,
|
||||||
|
0,0,17,0,0,0,63,0,3,0,33,0,0,0,16,0,0,0,65,0,5,0,13,0,0,0,34,0,0,0,
|
||||||
|
31,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,35,0,0,0,34,0,0,0,61,0,4,0,3,0,
|
||||||
|
0,0,36,0,0,0,5,0,0,0,65,0,5,0,18,0,0,0,37,0,0,0,31,0,0,0,17,0,0,0,
|
||||||
|
61,0,4,0,14,0,0,0,38,0,0,0,37,0,0,0,87,0,5,0,8,0,0,0,39,0,0,0,36,0,
|
||||||
|
0,0,38,0,0,0,133,0,5,0,8,0,0,0,40,0,0,0,35,0,0,0,39,0,0,0,65,0,5,0,
|
||||||
|
13,0,0,0,41,0,0,0,30,0,0,0,12,0,0,0,62,0,3,0,41,0,0,0,40,0,0,0,65,0,
|
||||||
|
5,0,13,0,0,0,45,0,0,0,30,0,0,0,12,0,0,0,61,0,4,0,8,0,0,0,46,0,0,0,
|
||||||
|
45,0,0,0,81,0,5,0,1,0,0,0,47,0,0,0,46,0,0,0,3,0,0,0,180,0,5,0,27,0,
|
||||||
|
0,0,48,0,0,0,47,0,0,0,26,0,0,0,247,0,3,0,42,0,0,0,0,0,0,0,250,0,4,0,
|
||||||
|
48,0,0,0,43,0,0,0,44,0,0,0,248,0,2,0,43,0,0,0,252,0,1,0,248,0,2,0,44,0,
|
||||||
|
0,0,249,0,2,0,42,0,0,0,248,0,2,0,42,0,0,0,61,0,4,0,23,0,0,0,49,0,0,0,
|
||||||
|
30,0,0,0,81,0,5,0,8,0,0,0,50,0,0,0,49,0,0,0,0,0,0,0,62,0,3,0,22,0,
|
||||||
|
0,0,50,0,0,0,253,0,1,0,56,0,1,0
|
||||||
45
runtime/Includes/Embedded/2DVertex.nzsl
git.filemode.normal_file
45
runtime/Includes/Embedded/2DVertex.nzsl
git.filemode.normal_file
@@ -0,0 +1,45 @@
|
|||||||
|
[nzsl_version("1.0")]
|
||||||
|
module;
|
||||||
|
|
||||||
|
struct VertIn
|
||||||
|
{
|
||||||
|
[location(0)] pos: vec4[f32],
|
||||||
|
[location(1)] color: vec4[f32], // unused
|
||||||
|
[location(2)] normal: vec4[f32], // unused
|
||||||
|
[location(3)] uv: vec2[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VertOut
|
||||||
|
{
|
||||||
|
[location(0)] color: vec4[f32],
|
||||||
|
[location(1)] uv: vec2[f32],
|
||||||
|
[builtin(position)] pos: vec4[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ViewerData
|
||||||
|
{
|
||||||
|
projection_matrix: mat4[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpriteData
|
||||||
|
{
|
||||||
|
color: vec4[f32],
|
||||||
|
position: vec2[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
external
|
||||||
|
{
|
||||||
|
[set(0), binding(0)] viewer_data: uniform[ViewerData],
|
||||||
|
model : push_constant[SpriteData]
|
||||||
|
}
|
||||||
|
|
||||||
|
[entry(vert)]
|
||||||
|
fn main(input: VertIn) -> VertOut
|
||||||
|
{
|
||||||
|
input.uv.x *= -1.0;
|
||||||
|
let output: VertOut;
|
||||||
|
output.uv = input.uv;
|
||||||
|
output.color = model.color;
|
||||||
|
output.pos = viewer_data.projection_matrix * vec4[f32](input.pos.xy + model.position, 0.0, 1.0);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
80
runtime/Includes/Embedded/2DVertex.spv.h
git.filemode.normal_file
80
runtime/Includes/Embedded/2DVertex.spv.h
git.filemode.normal_file
@@ -0,0 +1,80 @@
|
|||||||
|
3,2,35,7,0,0,1,0,39,0,0,0,77,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
|
||||||
|
3,0,0,0,0,0,1,0,0,0,15,0,12,0,0,0,0,0,37,0,0,0,109,97,105,110,0,0,0,0,
|
||||||
|
14,0,0,0,18,0,0,0,20,0,0,0,23,0,0,0,29,0,0,0,31,0,0,0,32,0,0,0,3,0,
|
||||||
|
3,0,0,0,0,0,100,0,0,0,5,0,5,0,4,0,0,0,86,105,101,119,101,114,68,97,116,97,0,0,
|
||||||
|
6,0,8,0,4,0,0,0,0,0,0,0,112,114,111,106,101,99,116,105,111,110,95,109,97,116,114,105,120,0,
|
||||||
|
0,0,5,0,5,0,8,0,0,0,83,112,114,105,116,101,68,97,116,97,0,0,6,0,5,0,8,0,0,0,
|
||||||
|
0,0,0,0,99,111,108,111,114,0,0,0,6,0,6,0,8,0,0,0,1,0,0,0,112,111,115,105,116,105,
|
||||||
|
111,110,0,0,0,0,5,0,4,0,26,0,0,0,86,101,114,116,73,110,0,0,6,0,4,0,26,0,0,0,
|
||||||
|
0,0,0,0,112,111,115,0,6,0,5,0,26,0,0,0,1,0,0,0,99,111,108,111,114,0,0,0,6,0,
|
||||||
|
5,0,26,0,0,0,2,0,0,0,110,111,114,109,97,108,0,0,6,0,4,0,26,0,0,0,3,0,0,0,
|
||||||
|
117,118,0,0,5,0,4,0,33,0,0,0,86,101,114,116,79,117,116,0,6,0,5,0,33,0,0,0,0,0,
|
||||||
|
0,0,99,111,108,111,114,0,0,0,6,0,4,0,33,0,0,0,1,0,0,0,117,118,0,0,6,0,4,0,
|
||||||
|
33,0,0,0,2,0,0,0,112,111,115,0,5,0,5,0,6,0,0,0,118,105,101,119,101,114,95,100,97,116,
|
||||||
|
97,0,5,0,4,0,10,0,0,0,109,111,100,101,108,0,0,0,5,0,3,0,14,0,0,0,112,111,115,0,
|
||||||
|
5,0,4,0,18,0,0,0,99,111,108,111,114,0,0,0,5,0,4,0,20,0,0,0,110,111,114,109,97,108,
|
||||||
|
0,0,5,0,3,0,23,0,0,0,117,118,0,0,5,0,4,0,29,0,0,0,99,111,108,111,114,0,0,0,
|
||||||
|
5,0,3,0,31,0,0,0,117,118,0,0,5,0,5,0,32,0,0,0,112,111,115,105,116,105,111,110,0,0,
|
||||||
|
0,0,5,0,4,0,37,0,0,0,109,97,105,110,0,0,0,0,71,0,4,0,6,0,0,0,33,0,0,0,
|
||||||
|
0,0,0,0,71,0,4,0,6,0,0,0,34,0,0,0,0,0,0,0,71,0,4,0,32,0,0,0,11,0,
|
||||||
|
0,0,0,0,0,0,71,0,4,0,14,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,18,0,0,0,
|
||||||
|
30,0,0,0,1,0,0,0,71,0,4,0,20,0,0,0,30,0,0,0,2,0,0,0,71,0,4,0,23,0,
|
||||||
|
0,0,30,0,0,0,3,0,0,0,71,0,4,0,29,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,
|
||||||
|
31,0,0,0,30,0,0,0,1,0,0,0,71,0,3,0,4,0,0,0,2,0,0,0,72,0,4,0,4,0,
|
||||||
|
0,0,0,0,0,0,5,0,0,0,72,0,5,0,4,0,0,0,0,0,0,0,7,0,0,0,16,0,0,0,
|
||||||
|
72,0,5,0,4,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,71,0,3,0,8,0,0,0,2,0,
|
||||||
|
0,0,72,0,5,0,8,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0,8,0,0,0,
|
||||||
|
1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,26,0,0,0,0,0,0,0,35,0,0,0,0,0,
|
||||||
|
0,0,72,0,5,0,26,0,0,0,1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,26,0,0,0,
|
||||||
|
2,0,0,0,35,0,0,0,32,0,0,0,72,0,5,0,26,0,0,0,3,0,0,0,35,0,0,0,48,0,
|
||||||
|
0,0,72,0,5,0,33,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0,33,0,0,0,
|
||||||
|
1,0,0,0,35,0,0,0,16,0,0,0,72,0,5,0,33,0,0,0,2,0,0,0,35,0,0,0,32,0,
|
||||||
|
0,0,22,0,3,0,1,0,0,0,32,0,0,0,23,0,4,0,2,0,0,0,1,0,0,0,4,0,0,0,
|
||||||
|
24,0,4,0,3,0,0,0,2,0,0,0,4,0,0,0,30,0,3,0,4,0,0,0,3,0,0,0,32,0,
|
||||||
|
4,0,5,0,0,0,2,0,0,0,4,0,0,0,23,0,4,0,7,0,0,0,1,0,0,0,2,0,0,0,
|
||||||
|
30,0,4,0,8,0,0,0,2,0,0,0,7,0,0,0,32,0,4,0,9,0,0,0,9,0,0,0,8,0,
|
||||||
|
0,0,19,0,2,0,11,0,0,0,33,0,3,0,12,0,0,0,11,0,0,0,32,0,4,0,13,0,0,0,
|
||||||
|
1,0,0,0,2,0,0,0,21,0,4,0,15,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,15,0,
|
||||||
|
0,0,16,0,0,0,0,0,0,0,32,0,4,0,17,0,0,0,7,0,0,0,2,0,0,0,43,0,4,0,
|
||||||
|
15,0,0,0,19,0,0,0,1,0,0,0,43,0,4,0,15,0,0,0,21,0,0,0,2,0,0,0,32,0,
|
||||||
|
4,0,22,0,0,0,1,0,0,0,7,0,0,0,43,0,4,0,15,0,0,0,24,0,0,0,3,0,0,0,
|
||||||
|
32,0,4,0,25,0,0,0,7,0,0,0,7,0,0,0,30,0,6,0,26,0,0,0,2,0,0,0,2,0,
|
||||||
|
0,0,2,0,0,0,7,0,0,0,32,0,4,0,27,0,0,0,7,0,0,0,26,0,0,0,32,0,4,0,
|
||||||
|
28,0,0,0,3,0,0,0,2,0,0,0,32,0,4,0,30,0,0,0,3,0,0,0,7,0,0,0,30,0,
|
||||||
|
5,0,33,0,0,0,2,0,0,0,7,0,0,0,2,0,0,0,43,0,4,0,1,0,0,0,34,0,0,0,
|
||||||
|
0,0,128,63,32,0,4,0,35,0,0,0,7,0,0,0,33,0,0,0,43,0,4,0,1,0,0,0,36,0,
|
||||||
|
0,0,0,0,0,0,32,0,4,0,51,0,0,0,7,0,0,0,1,0,0,0,32,0,4,0,56,0,0,0,
|
||||||
|
9,0,0,0,2,0,0,0,32,0,4,0,60,0,0,0,2,0,0,0,3,0,0,0,32,0,4,0,66,0,
|
||||||
|
0,0,9,0,0,0,7,0,0,0,59,0,4,0,5,0,0,0,6,0,0,0,2,0,0,0,59,0,4,0,
|
||||||
|
9,0,0,0,10,0,0,0,9,0,0,0,59,0,4,0,13,0,0,0,14,0,0,0,1,0,0,0,59,0,
|
||||||
|
4,0,13,0,0,0,18,0,0,0,1,0,0,0,59,0,4,0,13,0,0,0,20,0,0,0,1,0,0,0,
|
||||||
|
59,0,4,0,22,0,0,0,23,0,0,0,1,0,0,0,59,0,4,0,28,0,0,0,29,0,0,0,3,0,
|
||||||
|
0,0,59,0,4,0,30,0,0,0,31,0,0,0,3,0,0,0,59,0,4,0,28,0,0,0,32,0,0,0,
|
||||||
|
3,0,0,0,54,0,5,0,11,0,0,0,37,0,0,0,0,0,0,0,12,0,0,0,248,0,2,0,38,0,
|
||||||
|
0,0,59,0,4,0,35,0,0,0,39,0,0,0,7,0,0,0,59,0,4,0,27,0,0,0,40,0,0,0,
|
||||||
|
7,0,0,0,65,0,5,0,17,0,0,0,41,0,0,0,40,0,0,0,16,0,0,0,63,0,3,0,41,0,
|
||||||
|
0,0,14,0,0,0,65,0,5,0,17,0,0,0,42,0,0,0,40,0,0,0,19,0,0,0,63,0,3,0,
|
||||||
|
42,0,0,0,18,0,0,0,65,0,5,0,17,0,0,0,43,0,0,0,40,0,0,0,21,0,0,0,63,0,
|
||||||
|
3,0,43,0,0,0,20,0,0,0,65,0,5,0,25,0,0,0,44,0,0,0,40,0,0,0,24,0,0,0,
|
||||||
|
63,0,3,0,44,0,0,0,23,0,0,0,65,0,5,0,25,0,0,0,45,0,0,0,40,0,0,0,24,0,
|
||||||
|
0,0,61,0,4,0,7,0,0,0,46,0,0,0,45,0,0,0,81,0,5,0,1,0,0,0,47,0,0,0,
|
||||||
|
46,0,0,0,0,0,0,0,127,0,4,0,1,0,0,0,48,0,0,0,34,0,0,0,133,0,5,0,1,0,
|
||||||
|
0,0,49,0,0,0,47,0,0,0,48,0,0,0,65,0,5,0,25,0,0,0,50,0,0,0,40,0,0,0,
|
||||||
|
24,0,0,0,65,0,5,0,51,0,0,0,52,0,0,0,50,0,0,0,16,0,0,0,62,0,3,0,52,0,
|
||||||
|
0,0,49,0,0,0,65,0,5,0,25,0,0,0,53,0,0,0,40,0,0,0,24,0,0,0,61,0,4,0,
|
||||||
|
7,0,0,0,54,0,0,0,53,0,0,0,65,0,5,0,25,0,0,0,55,0,0,0,39,0,0,0,19,0,
|
||||||
|
0,0,62,0,3,0,55,0,0,0,54,0,0,0,65,0,5,0,56,0,0,0,57,0,0,0,10,0,0,0,
|
||||||
|
16,0,0,0,61,0,4,0,2,0,0,0,58,0,0,0,57,0,0,0,65,0,5,0,17,0,0,0,59,0,
|
||||||
|
0,0,39,0,0,0,16,0,0,0,62,0,3,0,59,0,0,0,58,0,0,0,65,0,5,0,60,0,0,0,
|
||||||
|
61,0,0,0,6,0,0,0,16,0,0,0,61,0,4,0,3,0,0,0,62,0,0,0,61,0,0,0,65,0,
|
||||||
|
5,0,17,0,0,0,63,0,0,0,40,0,0,0,16,0,0,0,61,0,4,0,2,0,0,0,64,0,0,0,
|
||||||
|
63,0,0,0,79,0,7,0,7,0,0,0,65,0,0,0,64,0,0,0,64,0,0,0,0,0,0,0,1,0,
|
||||||
|
0,0,65,0,5,0,66,0,0,0,67,0,0,0,10,0,0,0,19,0,0,0,61,0,4,0,7,0,0,0,
|
||||||
|
68,0,0,0,67,0,0,0,129,0,5,0,7,0,0,0,69,0,0,0,65,0,0,0,68,0,0,0,80,0,
|
||||||
|
6,0,2,0,0,0,70,0,0,0,69,0,0,0,36,0,0,0,34,0,0,0,145,0,5,0,2,0,0,0,
|
||||||
|
71,0,0,0,62,0,0,0,70,0,0,0,65,0,5,0,17,0,0,0,72,0,0,0,39,0,0,0,21,0,
|
||||||
|
0,0,62,0,3,0,72,0,0,0,71,0,0,0,61,0,4,0,33,0,0,0,73,0,0,0,39,0,0,0,
|
||||||
|
81,0,5,0,2,0,0,0,74,0,0,0,73,0,0,0,0,0,0,0,62,0,3,0,29,0,0,0,74,0,
|
||||||
|
0,0,81,0,5,0,7,0,0,0,75,0,0,0,73,0,0,0,1,0,0,0,62,0,3,0,31,0,0,0,
|
||||||
|
75,0,0,0,81,0,5,0,2,0,0,0,76,0,0,0,73,0,0,0,2,0,0,0,62,0,3,0,32,0,
|
||||||
|
0,0,76,0,0,0,253,0,1,0,56,0,1,0
|
||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* DogicaTTF.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/04/11 16:20:25 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 21:59:40 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_DOGICA_TTF__
|
#ifndef __MLX_DOGICA_TTF__
|
||||||
#define __MLX_DOGICA_TTF__
|
#define __MLX_DOGICA_TTF__
|
||||||
|
|
||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* IconMlx.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/11/25 11:23:16 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 21:59:45 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __ICON_MLX__
|
#ifndef __ICON_MLX__
|
||||||
#define __ICON_MLX__
|
#define __ICON_MLX__
|
||||||
|
|
||||||
46
runtime/Includes/Embedded/ScreenFragment.nzsl
git.filemode.normal_file
46
runtime/Includes/Embedded/ScreenFragment.nzsl
git.filemode.normal_file
@@ -0,0 +1,46 @@
|
|||||||
|
[nzsl_version("1.0")]
|
||||||
|
module;
|
||||||
|
|
||||||
|
struct VertOut
|
||||||
|
{
|
||||||
|
[location(0)] uv : vec2[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
struct FragOut
|
||||||
|
{
|
||||||
|
[location(0)] color: vec4[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
external
|
||||||
|
{
|
||||||
|
[set(0), binding(0)] u_texture: sampler2D[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
option approximates_rgb: bool = false;
|
||||||
|
|
||||||
|
fn LinearTosRGB(color: vec3[f32]) -> vec3[f32]
|
||||||
|
{
|
||||||
|
const if(!approximates_rgb)
|
||||||
|
{
|
||||||
|
return select(
|
||||||
|
color > (0.0031308).rrr,
|
||||||
|
1.055 * pow(color, (1.0 / 2.4).rrr) - (0.055).rrr,
|
||||||
|
12.92 * color
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return pow(color, (1.0 / 2.2).rrr);
|
||||||
|
}
|
||||||
|
|
||||||
|
option gamma_correction: bool = false;
|
||||||
|
|
||||||
|
[entry(frag)]
|
||||||
|
fn main(input: VertOut) -> FragOut
|
||||||
|
{
|
||||||
|
let output: FragOut;
|
||||||
|
const if(gamma_correction)
|
||||||
|
output.color = vec4[f32](LinearTosRGB(u_texture.Sample(input.uv).xyz), 1.0);
|
||||||
|
else
|
||||||
|
output.color = u_texture.Sample(input.uv);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
49
runtime/Includes/Embedded/ScreenFragment.spv.h
git.filemode.normal_file
49
runtime/Includes/Embedded/ScreenFragment.spv.h
git.filemode.normal_file
@@ -0,0 +1,49 @@
|
|||||||
|
3,2,35,7,0,0,1,0,39,0,0,0,62,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,11,0,
|
||||||
|
6,0,32,0,0,0,71,76,83,76,46,115,116,100,46,52,53,48,0,0,0,0,14,0,3,0,0,0,0,0,
|
||||||
|
1,0,0,0,15,0,7,0,4,0,0,0,34,0,0,0,109,97,105,110,0,0,0,0,23,0,0,0,29,0,
|
||||||
|
0,0,16,0,3,0,34,0,0,0,7,0,0,0,3,0,3,0,0,0,0,0,100,0,0,0,5,0,4,0,
|
||||||
|
25,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,25,0,0,0,0,0,0,0,117,118,0,0,5,0,
|
||||||
|
4,0,30,0,0,0,70,114,97,103,79,117,116,0,6,0,5,0,30,0,0,0,0,0,0,0,99,111,108,111,
|
||||||
|
114,0,0,0,5,0,5,0,5,0,0,0,117,95,116,101,120,116,117,114,101,0,0,0,5,0,3,0,23,0,
|
||||||
|
0,0,117,118,0,0,5,0,4,0,29,0,0,0,99,111,108,111,114,0,0,0,5,0,6,0,33,0,0,0,
|
||||||
|
76,105,110,101,97,114,84,111,115,82,71,66,0,0,0,0,5,0,4,0,34,0,0,0,109,97,105,110,0,0,
|
||||||
|
0,0,71,0,4,0,5,0,0,0,33,0,0,0,0,0,0,0,71,0,4,0,5,0,0,0,34,0,0,0,
|
||||||
|
0,0,0,0,71,0,4,0,23,0,0,0,30,0,0,0,0,0,0,0,71,0,4,0,29,0,0,0,30,0,
|
||||||
|
0,0,0,0,0,0,72,0,5,0,25,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,72,0,5,0,
|
||||||
|
30,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,22,0,3,0,1,0,0,0,32,0,0,0,25,0,
|
||||||
|
9,0,2,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
|
||||||
|
0,0,0,0,27,0,3,0,3,0,0,0,2,0,0,0,32,0,4,0,4,0,0,0,0,0,0,0,3,0,
|
||||||
|
0,0,23,0,4,0,6,0,0,0,1,0,0,0,3,0,0,0,32,0,4,0,7,0,0,0,7,0,0,0,
|
||||||
|
6,0,0,0,33,0,4,0,8,0,0,0,6,0,0,0,7,0,0,0,43,0,4,0,1,0,0,0,9,0,
|
||||||
|
0,0,28,46,77,59,21,0,4,0,10,0,0,0,32,0,0,0,1,0,0,0,43,0,4,0,10,0,0,0,
|
||||||
|
11,0,0,0,0,0,0,0,20,0,2,0,12,0,0,0,23,0,4,0,13,0,0,0,12,0,0,0,3,0,
|
||||||
|
0,0,43,0,4,0,1,0,0,0,14,0,0,0,61,10,135,63,43,0,4,0,1,0,0,0,15,0,0,0,
|
||||||
|
0,0,128,63,43,0,4,0,1,0,0,0,16,0,0,0,154,153,25,64,43,0,4,0,1,0,0,0,17,0,
|
||||||
|
0,0,174,71,97,61,43,0,4,0,1,0,0,0,18,0,0,0,82,184,78,65,19,0,2,0,19,0,0,0,
|
||||||
|
33,0,3,0,20,0,0,0,19,0,0,0,23,0,4,0,21,0,0,0,1,0,0,0,2,0,0,0,32,0,
|
||||||
|
4,0,22,0,0,0,1,0,0,0,21,0,0,0,32,0,4,0,24,0,0,0,7,0,0,0,21,0,0,0,
|
||||||
|
30,0,3,0,25,0,0,0,21,0,0,0,32,0,4,0,26,0,0,0,7,0,0,0,25,0,0,0,23,0,
|
||||||
|
4,0,27,0,0,0,1,0,0,0,4,0,0,0,32,0,4,0,28,0,0,0,3,0,0,0,27,0,0,0,
|
||||||
|
30,0,3,0,30,0,0,0,27,0,0,0,32,0,4,0,31,0,0,0,7,0,0,0,30,0,0,0,32,0,
|
||||||
|
4,0,59,0,0,0,7,0,0,0,27,0,0,0,59,0,4,0,4,0,0,0,5,0,0,0,0,0,0,0,
|
||||||
|
59,0,4,0,22,0,0,0,23,0,0,0,1,0,0,0,59,0,4,0,28,0,0,0,29,0,0,0,3,0,
|
||||||
|
0,0,54,0,5,0,6,0,0,0,33,0,0,0,0,0,0,0,8,0,0,0,55,0,3,0,7,0,0,0,
|
||||||
|
35,0,0,0,248,0,2,0,36,0,0,0,61,0,4,0,6,0,0,0,37,0,0,0,35,0,0,0,80,0,
|
||||||
|
6,0,6,0,0,0,38,0,0,0,9,0,0,0,9,0,0,0,9,0,0,0,186,0,5,0,13,0,0,0,
|
||||||
|
39,0,0,0,37,0,0,0,38,0,0,0,61,0,4,0,6,0,0,0,40,0,0,0,35,0,0,0,136,0,
|
||||||
|
5,0,1,0,0,0,41,0,0,0,15,0,0,0,16,0,0,0,80,0,6,0,6,0,0,0,42,0,0,0,
|
||||||
|
41,0,0,0,41,0,0,0,41,0,0,0,12,0,7,0,6,0,0,0,43,0,0,0,32,0,0,0,26,0,
|
||||||
|
0,0,40,0,0,0,42,0,0,0,142,0,5,0,6,0,0,0,44,0,0,0,43,0,0,0,14,0,0,0,
|
||||||
|
80,0,6,0,6,0,0,0,45,0,0,0,17,0,0,0,17,0,0,0,17,0,0,0,131,0,5,0,6,0,
|
||||||
|
0,0,46,0,0,0,44,0,0,0,45,0,0,0,61,0,4,0,6,0,0,0,47,0,0,0,35,0,0,0,
|
||||||
|
142,0,5,0,6,0,0,0,48,0,0,0,47,0,0,0,18,0,0,0,169,0,6,0,6,0,0,0,49,0,
|
||||||
|
0,0,39,0,0,0,46,0,0,0,48,0,0,0,254,0,2,0,49,0,0,0,56,0,1,0,54,0,5,0,
|
||||||
|
19,0,0,0,34,0,0,0,0,0,0,0,20,0,0,0,248,0,2,0,50,0,0,0,59,0,4,0,31,0,
|
||||||
|
0,0,51,0,0,0,7,0,0,0,59,0,4,0,26,0,0,0,52,0,0,0,7,0,0,0,65,0,5,0,
|
||||||
|
24,0,0,0,53,0,0,0,52,0,0,0,11,0,0,0,63,0,3,0,53,0,0,0,23,0,0,0,61,0,
|
||||||
|
4,0,3,0,0,0,54,0,0,0,5,0,0,0,65,0,5,0,24,0,0,0,55,0,0,0,52,0,0,0,
|
||||||
|
11,0,0,0,61,0,4,0,21,0,0,0,56,0,0,0,55,0,0,0,87,0,5,0,27,0,0,0,57,0,
|
||||||
|
0,0,54,0,0,0,56,0,0,0,65,0,5,0,59,0,0,0,58,0,0,0,51,0,0,0,11,0,0,0,
|
||||||
|
62,0,3,0,58,0,0,0,57,0,0,0,61,0,4,0,30,0,0,0,60,0,0,0,51,0,0,0,81,0,
|
||||||
|
5,0,27,0,0,0,61,0,0,0,60,0,0,0,0,0,0,0,62,0,3,0,29,0,0,0,61,0,0,0,
|
||||||
|
253,0,1,0,56,0,1,0
|
||||||
31
runtime/Includes/Embedded/ScreenVertex.nzsl
git.filemode.normal_file
31
runtime/Includes/Embedded/ScreenVertex.nzsl
git.filemode.normal_file
@@ -0,0 +1,31 @@
|
|||||||
|
[nzsl_version("1.0")]
|
||||||
|
module;
|
||||||
|
|
||||||
|
struct VertIn
|
||||||
|
{
|
||||||
|
[builtin(vertex_index)] vert_index: i32
|
||||||
|
}
|
||||||
|
|
||||||
|
struct VertOut
|
||||||
|
{
|
||||||
|
[location(0)] uv: vec2[f32],
|
||||||
|
[builtin(position)] position: vec4[f32]
|
||||||
|
}
|
||||||
|
|
||||||
|
const vertices = array[vec2[f32]](
|
||||||
|
vec2[f32](-1.0, -3.0),
|
||||||
|
vec2[f32](-1.0, 1.0),
|
||||||
|
vec2[f32]( 3.0, 1.0)
|
||||||
|
);
|
||||||
|
|
||||||
|
[entry(vert)]
|
||||||
|
fn main(input: VertIn) -> VertOut
|
||||||
|
{
|
||||||
|
let position = vertices[input.vert_index];
|
||||||
|
|
||||||
|
let output: VertOut;
|
||||||
|
output.position = vec4[f32](position, 0.0, 1.0);
|
||||||
|
output.uv = position * 0.5 + vec2[f32](0.5, 0.5);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
48
runtime/Includes/Embedded/ScreenVertex.spv.h
git.filemode.normal_file
48
runtime/Includes/Embedded/ScreenVertex.spv.h
git.filemode.normal_file
@@ -0,0 +1,48 @@
|
|||||||
|
3,2,35,7,0,0,1,0,39,0,0,0,59,0,0,0,0,0,0,0,17,0,2,0,1,0,0,0,14,0,
|
||||||
|
3,0,0,0,0,0,1,0,0,0,15,0,8,0,0,0,0,0,36,0,0,0,109,97,105,110,0,0,0,0,
|
||||||
|
20,0,0,0,26,0,0,0,29,0,0,0,3,0,3,0,0,0,0,0,100,0,0,0,5,0,4,0,23,0,
|
||||||
|
0,0,86,101,114,116,73,110,0,0,6,0,6,0,23,0,0,0,0,0,0,0,118,101,114,116,95,105,110,100,
|
||||||
|
101,120,0,0,5,0,4,0,30,0,0,0,86,101,114,116,79,117,116,0,6,0,4,0,30,0,0,0,0,0,
|
||||||
|
0,0,117,118,0,0,6,0,6,0,30,0,0,0,1,0,0,0,112,111,115,105,116,105,111,110,0,0,0,0,
|
||||||
|
5,0,5,0,15,0,0,0,118,101,114,116,105,99,101,115,0,0,0,0,5,0,6,0,20,0,0,0,118,101,
|
||||||
|
114,116,101,120,95,105,110,100,101,120,0,0,0,0,5,0,3,0,26,0,0,0,117,118,0,0,5,0,5,0,
|
||||||
|
29,0,0,0,112,111,115,105,116,105,111,110,0,0,0,0,5,0,4,0,36,0,0,0,109,97,105,110,0,0,
|
||||||
|
0,0,71,0,4,0,20,0,0,0,11,0,0,0,42,0,0,0,71,0,4,0,29,0,0,0,11,0,0,0,
|
||||||
|
0,0,0,0,71,0,4,0,26,0,0,0,30,0,0,0,0,0,0,0,72,0,5,0,23,0,0,0,0,0,
|
||||||
|
0,0,35,0,0,0,0,0,0,0,72,0,5,0,30,0,0,0,0,0,0,0,35,0,0,0,0,0,0,0,
|
||||||
|
72,0,5,0,30,0,0,0,1,0,0,0,35,0,0,0,16,0,0,0,22,0,3,0,1,0,0,0,32,0,
|
||||||
|
0,0,23,0,4,0,2,0,0,0,1,0,0,0,2,0,0,0,21,0,4,0,3,0,0,0,32,0,0,0,
|
||||||
|
0,0,0,0,43,0,4,0,3,0,0,0,4,0,0,0,3,0,0,0,28,0,4,0,5,0,0,0,2,0,
|
||||||
|
0,0,4,0,0,0,32,0,4,0,6,0,0,0,6,0,0,0,5,0,0,0,43,0,4,0,1,0,0,0,
|
||||||
|
7,0,0,0,0,0,128,191,43,0,4,0,1,0,0,0,8,0,0,0,0,0,64,192,44,0,5,0,2,0,
|
||||||
|
0,0,9,0,0,0,7,0,0,0,8,0,0,0,43,0,4,0,1,0,0,0,10,0,0,0,0,0,128,63,
|
||||||
|
44,0,5,0,2,0,0,0,11,0,0,0,7,0,0,0,10,0,0,0,43,0,4,0,1,0,0,0,12,0,
|
||||||
|
0,0,0,0,64,64,44,0,5,0,2,0,0,0,13,0,0,0,12,0,0,0,10,0,0,0,44,0,6,0,
|
||||||
|
5,0,0,0,14,0,0,0,9,0,0,0,11,0,0,0,13,0,0,0,19,0,2,0,16,0,0,0,33,0,
|
||||||
|
3,0,17,0,0,0,16,0,0,0,21,0,4,0,18,0,0,0,32,0,0,0,1,0,0,0,32,0,4,0,
|
||||||
|
19,0,0,0,1,0,0,0,18,0,0,0,43,0,4,0,18,0,0,0,21,0,0,0,0,0,0,0,32,0,
|
||||||
|
4,0,22,0,0,0,7,0,0,0,18,0,0,0,30,0,3,0,23,0,0,0,18,0,0,0,32,0,4,0,
|
||||||
|
24,0,0,0,7,0,0,0,23,0,0,0,32,0,4,0,25,0,0,0,3,0,0,0,2,0,0,0,23,0,
|
||||||
|
4,0,27,0,0,0,1,0,0,0,4,0,0,0,32,0,4,0,28,0,0,0,3,0,0,0,27,0,0,0,
|
||||||
|
30,0,4,0,30,0,0,0,2,0,0,0,27,0,0,0,32,0,4,0,31,0,0,0,7,0,0,0,2,0,
|
||||||
|
0,0,32,0,4,0,32,0,0,0,7,0,0,0,30,0,0,0,43,0,4,0,18,0,0,0,33,0,0,0,
|
||||||
|
1,0,0,0,43,0,4,0,1,0,0,0,34,0,0,0,0,0,0,0,43,0,4,0,1,0,0,0,35,0,
|
||||||
|
0,0,0,0,0,63,32,0,4,0,44,0,0,0,6,0,0,0,2,0,0,0,32,0,4,0,50,0,0,0,
|
||||||
|
7,0,0,0,27,0,0,0,59,0,5,0,6,0,0,0,15,0,0,0,6,0,0,0,14,0,0,0,59,0,
|
||||||
|
4,0,19,0,0,0,20,0,0,0,1,0,0,0,59,0,4,0,25,0,0,0,26,0,0,0,3,0,0,0,
|
||||||
|
59,0,4,0,28,0,0,0,29,0,0,0,3,0,0,0,54,0,5,0,16,0,0,0,36,0,0,0,0,0,
|
||||||
|
0,0,17,0,0,0,248,0,2,0,37,0,0,0,59,0,4,0,31,0,0,0,38,0,0,0,7,0,0,0,
|
||||||
|
59,0,4,0,32,0,0,0,39,0,0,0,7,0,0,0,59,0,4,0,24,0,0,0,40,0,0,0,7,0,
|
||||||
|
0,0,65,0,5,0,22,0,0,0,41,0,0,0,40,0,0,0,21,0,0,0,63,0,3,0,41,0,0,0,
|
||||||
|
20,0,0,0,65,0,5,0,22,0,0,0,42,0,0,0,40,0,0,0,21,0,0,0,61,0,4,0,18,0,
|
||||||
|
0,0,43,0,0,0,42,0,0,0,65,0,5,0,44,0,0,0,45,0,0,0,15,0,0,0,43,0,0,0,
|
||||||
|
61,0,4,0,2,0,0,0,46,0,0,0,45,0,0,0,62,0,3,0,38,0,0,0,46,0,0,0,61,0,
|
||||||
|
4,0,2,0,0,0,47,0,0,0,38,0,0,0,80,0,6,0,27,0,0,0,48,0,0,0,47,0,0,0,
|
||||||
|
34,0,0,0,10,0,0,0,65,0,5,0,50,0,0,0,49,0,0,0,39,0,0,0,33,0,0,0,62,0,
|
||||||
|
3,0,49,0,0,0,48,0,0,0,61,0,4,0,2,0,0,0,51,0,0,0,38,0,0,0,142,0,5,0,
|
||||||
|
2,0,0,0,52,0,0,0,51,0,0,0,35,0,0,0,80,0,5,0,2,0,0,0,53,0,0,0,35,0,
|
||||||
|
0,0,35,0,0,0,129,0,5,0,2,0,0,0,54,0,0,0,52,0,0,0,53,0,0,0,65,0,5,0,
|
||||||
|
31,0,0,0,55,0,0,0,39,0,0,0,21,0,0,0,62,0,3,0,55,0,0,0,54,0,0,0,61,0,
|
||||||
|
4,0,30,0,0,0,56,0,0,0,39,0,0,0,81,0,5,0,2,0,0,0,57,0,0,0,56,0,0,0,
|
||||||
|
0,0,0,0,62,0,3,0,26,0,0,0,57,0,0,0,81,0,5,0,27,0,0,0,58,0,0,0,56,0,
|
||||||
|
0,0,1,0,0,0,62,0,3,0,29,0,0,0,58,0,0,0,253,0,1,0,56,0,1,0
|
||||||
53
runtime/Includes/Graphics/Mesh.h
git.filemode.normal_file
53
runtime/Includes/Graphics/Mesh.h
git.filemode.normal_file
@@ -0,0 +1,53 @@
|
|||||||
|
#ifndef __MLX_RENDERER_MESH__
|
||||||
|
#define __MLX_RENDERER_MESH__
|
||||||
|
|
||||||
|
#include <Renderer/Vertex.h>
|
||||||
|
#include <Renderer/Buffer.h>
|
||||||
|
#include <Utils/Buffer.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class Mesh
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
struct SubMesh
|
||||||
|
{
|
||||||
|
VertexBuffer vbo;
|
||||||
|
IndexBuffer ibo;
|
||||||
|
std::size_t triangle_count = 0;
|
||||||
|
|
||||||
|
inline SubMesh(const std::vector<Vertex>& vertices, const std::vector<std::uint32_t>& indices)
|
||||||
|
{
|
||||||
|
CPUBuffer vb(vertices.size() * sizeof(Vertex));
|
||||||
|
std::memcpy(vb.GetData(), vertices.data(), vb.GetSize());
|
||||||
|
vbo.Init(vb.GetSize());
|
||||||
|
vbo.SetData(std::move(vb));
|
||||||
|
|
||||||
|
CPUBuffer ib(indices.size() * sizeof(std::uint32_t));
|
||||||
|
std::memcpy(ib.GetData(), indices.data(), ib.GetSize());
|
||||||
|
ibo.Init(ib.GetSize());
|
||||||
|
ibo.SetData(std::move(ib));
|
||||||
|
|
||||||
|
triangle_count = vertices.size() / 3;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
Mesh() = default;
|
||||||
|
|
||||||
|
void Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn) const noexcept;
|
||||||
|
void Draw(VkCommandBuffer cmd, std::size_t& drawcalls, std::size_t& polygondrawn, std::size_t submesh_index) const noexcept;
|
||||||
|
|
||||||
|
inline std::size_t GetSubMeshCount() const { return m_sub_meshes.size(); }
|
||||||
|
|
||||||
|
inline void AddSubMesh(SubMesh mesh) { m_sub_meshes.emplace_back(std::move(mesh)); }
|
||||||
|
[[nodiscard]] inline SubMesh& GetSubMesh(std::size_t index) { return m_sub_meshes.at(index); }
|
||||||
|
|
||||||
|
~Mesh();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<SubMesh> m_sub_meshes;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
32
runtime/Includes/Graphics/Scene.h
git.filemode.normal_file
32
runtime/Includes/Graphics/Scene.h
git.filemode.normal_file
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef __MLX_SCENE__
|
||||||
|
#define __MLX_SCENE__
|
||||||
|
|
||||||
|
#include <Graphics/Sprite.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
struct SceneDescriptor
|
||||||
|
{
|
||||||
|
NonOwningPtr<Renderer> renderer;
|
||||||
|
// More description may come in future
|
||||||
|
};
|
||||||
|
|
||||||
|
class Scene
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Scene(SceneDescriptor desc);
|
||||||
|
|
||||||
|
Sprite& CreateSprite(std::shared_ptr<class Texture> texture) noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] inline const std::vector<std::shared_ptr<Sprite>>& GetSprites() const noexcept { return m_sprites; }
|
||||||
|
[[nodiscard]] inline const SceneDescriptor& GetDescription() const noexcept { return m_descriptor; }
|
||||||
|
|
||||||
|
~Scene() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
SceneDescriptor m_descriptor;
|
||||||
|
std::vector<std::shared_ptr<Sprite>> m_sprites;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
53
runtime/Includes/Graphics/Sprite.h
git.filemode.normal_file
53
runtime/Includes/Graphics/Sprite.h
git.filemode.normal_file
@@ -0,0 +1,53 @@
|
|||||||
|
#ifndef __MLX_SPRITE__
|
||||||
|
#define __MLX_SPRITE__
|
||||||
|
|
||||||
|
#include <Maths/Vec2.h>
|
||||||
|
#include <Maths/Vec4.h>
|
||||||
|
#include <Graphics/Mesh.h>
|
||||||
|
#include <Renderer/Descriptor.h>
|
||||||
|
#include <Renderer/Image.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class Sprite
|
||||||
|
{
|
||||||
|
friend class Render2DPass;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Sprite(std::shared_ptr<Texture> texture);
|
||||||
|
|
||||||
|
inline void SetColor(Vec4f color) noexcept { m_color = color; }
|
||||||
|
inline void SetPosition(Vec2ui position) noexcept { m_position = position; }
|
||||||
|
|
||||||
|
[[nodiscard]] inline const Vec4f& GetColor() const noexcept { return m_color; }
|
||||||
|
[[nodiscard]] inline const Vec2ui& GetPosition() const noexcept { return m_position; }
|
||||||
|
[[nodiscard]] inline std::shared_ptr<Mesh> GetMesh() const { return p_mesh; }
|
||||||
|
[[nodiscard]] inline std::shared_ptr<Texture> GetTexture() const { return p_texture; }
|
||||||
|
|
||||||
|
~Sprite() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
[[nodiscard]] inline bool IsSetInit() const noexcept { return m_set.IsInit(); }
|
||||||
|
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t frame_index) const noexcept { return m_set.GetSet(frame_index); }
|
||||||
|
|
||||||
|
inline void UpdateDescriptorSet(const DescriptorSet& set)
|
||||||
|
{
|
||||||
|
m_set = set.Duplicate();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void Bind(std::size_t frame_index, VkCommandBuffer cmd)
|
||||||
|
{
|
||||||
|
m_set.SetImage(frame_index, 0, *p_texture);
|
||||||
|
m_set.Update(frame_index, cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
DescriptorSet m_set;
|
||||||
|
std::shared_ptr<Texture> p_texture;
|
||||||
|
std::shared_ptr<Mesh> p_mesh;
|
||||||
|
Vec4f m_color = Vec4f{ 1.0f, 1.0f, 1.0f, 1.0f };
|
||||||
|
Vec2ui m_position = Vec2ui{ 0, 0 };
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
108
runtime/Includes/Maths/Angles.h
git.filemode.normal_file
108
runtime/Includes/Maths/Angles.h
git.filemode.normal_file
@@ -0,0 +1,108 @@
|
|||||||
|
#ifndef __SCOP_ANGLES__
|
||||||
|
#define __SCOP_ANGLES__
|
||||||
|
|
||||||
|
#include <Maths/Enums.h>
|
||||||
|
#include <utility>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T> struct EulerAngles;
|
||||||
|
template<typename T> struct Quat;
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
struct Angle
|
||||||
|
{
|
||||||
|
T value;
|
||||||
|
|
||||||
|
constexpr Angle() = default;
|
||||||
|
constexpr Angle(T angle);
|
||||||
|
template<typename U> constexpr explicit Angle(const Angle<Unit, U>& Angle);
|
||||||
|
template<AngleUnit FromUnit> constexpr Angle(const Angle<FromUnit, T>& angle);
|
||||||
|
constexpr Angle(const Angle&) = default;
|
||||||
|
constexpr Angle(Angle&&) noexcept = default;
|
||||||
|
~Angle() = default;
|
||||||
|
|
||||||
|
constexpr bool ApproxEqual(const Angle& angle) const;
|
||||||
|
constexpr bool ApproxEqual(const Angle& angle, T max_difference) const;
|
||||||
|
|
||||||
|
T GetCos() const;
|
||||||
|
T GetSin() const;
|
||||||
|
std::pair<T, T> GetSinCos() const;
|
||||||
|
T GetTan() const;
|
||||||
|
|
||||||
|
constexpr Angle& Normalize();
|
||||||
|
|
||||||
|
template<AngleUnit ToUnit> T To() const;
|
||||||
|
template<AngleUnit ToUnit> Angle<ToUnit, T> ToAngle() const;
|
||||||
|
constexpr T ToDegrees() const;
|
||||||
|
constexpr Angle<AngleUnit::Degree, T> ToDegreeAngle() const;
|
||||||
|
EulerAngles<T> ToEulerAngles() const;
|
||||||
|
Quat<T> ToQuat() const;
|
||||||
|
constexpr T ToRadians() const;
|
||||||
|
constexpr Angle<AngleUnit::Radian, T> ToRadianAngle() const;
|
||||||
|
std::string ToString() const;
|
||||||
|
constexpr T ToTurns() const;
|
||||||
|
constexpr Angle<AngleUnit::Turn, T> ToTurnAngle() const;
|
||||||
|
|
||||||
|
constexpr Angle& operator=(const Angle&) = default;
|
||||||
|
constexpr Angle& operator=(Angle&&) noexcept = default;
|
||||||
|
|
||||||
|
constexpr Angle operator+() const;
|
||||||
|
constexpr Angle operator-() const;
|
||||||
|
|
||||||
|
constexpr Angle operator+(Angle other) const;
|
||||||
|
constexpr Angle operator-(Angle other) const;
|
||||||
|
constexpr Angle operator*(T scalar) const;
|
||||||
|
constexpr Angle operator/(T divider) const;
|
||||||
|
|
||||||
|
constexpr Angle& operator+=(Angle other);
|
||||||
|
constexpr Angle& operator-=(Angle other);
|
||||||
|
constexpr Angle& operator*=(T scalar);
|
||||||
|
constexpr Angle& operator/=(T divider);
|
||||||
|
|
||||||
|
constexpr bool operator==(Angle other) const;
|
||||||
|
constexpr bool operator!=(Angle other) const;
|
||||||
|
constexpr bool operator<(Angle other) const;
|
||||||
|
constexpr bool operator<=(Angle other) const;
|
||||||
|
constexpr bool operator>(Angle other) const;
|
||||||
|
constexpr bool operator>=(Angle other) const;
|
||||||
|
|
||||||
|
static constexpr bool ApproxEqual(const Angle& lhs, const Angle& rhs);
|
||||||
|
static constexpr bool ApproxEqual(const Angle& lhs, const Angle& rhs, T max_difference);
|
||||||
|
static constexpr Angle Clamp(Angle angle, Angle min, Angle max);
|
||||||
|
template<AngleUnit FromUnit> static constexpr Angle From(T value);
|
||||||
|
static constexpr Angle FromDegrees(T degrees);
|
||||||
|
static constexpr Angle FromRadians(T radians);
|
||||||
|
static constexpr Angle FromTurns(T turn);
|
||||||
|
static constexpr Angle Zero();
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using DegreeAngle = Angle<AngleUnit::Degree, T>;
|
||||||
|
|
||||||
|
using DegreeAngled = DegreeAngle<double>;
|
||||||
|
using DegreeAnglef = DegreeAngle<float>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using RadianAngle = Angle<AngleUnit::Radian, T>;
|
||||||
|
|
||||||
|
using RadianAngled = RadianAngle<double>;
|
||||||
|
using RadianAnglef = RadianAngle<float>;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using TurnAngle = Angle<AngleUnit::Turn, T>;
|
||||||
|
|
||||||
|
using TurnAngled = TurnAngle<double>;
|
||||||
|
using TurnAnglef = TurnAngle<float>;
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T> Angle<Unit, T> operator*(T scale, Angle<Unit, T> angle);
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T> Angle<Unit, T> operator/(T divider, Angle<Unit, T> angle);
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T> std::ostream& operator<<(std::ostream& out, Angle<Unit, T> angle);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/Angles.inl>
|
||||||
|
|
||||||
|
#endif
|
||||||
488
runtime/Includes/Maths/Angles.inl
git.filemode.normal_file
488
runtime/Includes/Maths/Angles.inl
git.filemode.normal_file
@@ -0,0 +1,488 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Maths/Angles.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include <Maths/Constants.h>
|
||||||
|
#include <Maths/MathsUtils.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
namespace Internal
|
||||||
|
{
|
||||||
|
template<AngleUnit From, AngleUnit To> struct AngleConversion;
|
||||||
|
|
||||||
|
template<AngleUnit Unit>
|
||||||
|
struct AngleConversion<Unit, Unit>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return angle;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleConversion<AngleUnit::Degree, AngleUnit::Radian>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return DegreeToRadian(angle);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleConversion<AngleUnit::Degree, AngleUnit::Turn>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return angle / T(360);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleConversion<AngleUnit::Radian, AngleUnit::Degree>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return RadianToDegree(angle);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleConversion<AngleUnit::Radian, AngleUnit::Turn>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return angle / Tau<T>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleConversion<AngleUnit::Turn, AngleUnit::Degree>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return angle * T(360);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleConversion<AngleUnit::Turn, AngleUnit::Radian>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T Convert(T angle)
|
||||||
|
{
|
||||||
|
return angle * Tau<T>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<AngleUnit Unit> struct AngleUtils;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleUtils<AngleUnit::Degree>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T GetEpsilon()
|
||||||
|
{
|
||||||
|
return T(1e-4);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T GetLimit()
|
||||||
|
{
|
||||||
|
return 360;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T> static std::ostream& ToString(std::ostream& out, T value)
|
||||||
|
{
|
||||||
|
return out << "Angle(" << value << "deg)";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleUtils<AngleUnit::Radian>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T GetEpsilon()
|
||||||
|
{
|
||||||
|
return T(1e-5);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T GetLimit()
|
||||||
|
{
|
||||||
|
return Tau<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::ostream& ToString(std::ostream& out, T value)
|
||||||
|
{
|
||||||
|
return out << "Angle(" << value << "rad)";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct AngleUtils<AngleUnit::Turn>
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T GetEpsilon()
|
||||||
|
{
|
||||||
|
return T(1e-5);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static constexpr T GetLimit()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::ostream& ToString(std::ostream& out, T value)
|
||||||
|
{
|
||||||
|
return out << "Angle(" << value << "turn)";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
void SinCos(T x, T* sin, T* cos)
|
||||||
|
{
|
||||||
|
double s, c;
|
||||||
|
::sincos(x, &s, &c);
|
||||||
|
|
||||||
|
*sin = static_cast<T>(s);
|
||||||
|
*cos = static_cast<T>(c);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline void SinCos(float x, float* s, float* c)
|
||||||
|
{
|
||||||
|
::sincosf(x, s, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline void SinCos(long double x, long double* s, long double* c)
|
||||||
|
{
|
||||||
|
::sincosl(x, s, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T>::Angle(T angle) :
|
||||||
|
value(angle)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
template<typename U>
|
||||||
|
constexpr Angle<Unit, T>::Angle(const Angle<Unit, U>& angle) :
|
||||||
|
value(static_cast<T>(angle.value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
template<AngleUnit FromUnit>
|
||||||
|
constexpr Angle<Unit, T>::Angle(const Angle<FromUnit, T>& angle) :
|
||||||
|
value(Internal::AngleConversion<FromUnit, Unit>::Convert(angle.value))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::ApproxEqual(const Angle& angle) const
|
||||||
|
{
|
||||||
|
return ApproxEqual(angle, Internal::AngleUtils<Unit>::template GetEpsilon<T>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::ApproxEqual(const Angle& angle, T maxDifference) const
|
||||||
|
{
|
||||||
|
return NumberEquals(value, angle.value, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
T Angle<Unit, T>::GetCos() const
|
||||||
|
{
|
||||||
|
return std::cos(ToRadians());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
T Angle<Unit, T>::GetSin() const
|
||||||
|
{
|
||||||
|
return std::sin(ToRadians());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
std::pair<T, T> Angle<Unit, T>::GetSinCos() const
|
||||||
|
{
|
||||||
|
T sin, cos;
|
||||||
|
Internal::SinCos<T>(ToRadians(), &sin, &cos);
|
||||||
|
|
||||||
|
return std::make_pair(sin, cos);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
T Angle<Unit, T>::GetTan() const
|
||||||
|
{
|
||||||
|
return std::tan(ToRadians());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T>& Angle<Unit, T>::Normalize()
|
||||||
|
{
|
||||||
|
constexpr T limit = Internal::AngleUtils<Unit>::template GetLimit<T>();
|
||||||
|
constexpr T halfLimit = limit / T(2);
|
||||||
|
|
||||||
|
value = Mod(value + halfLimit, limit);
|
||||||
|
if (value < T(0))
|
||||||
|
value += limit;
|
||||||
|
|
||||||
|
value -= halfLimit;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
template<AngleUnit ToUnit>
|
||||||
|
T Angle<Unit, T>::To() const
|
||||||
|
{
|
||||||
|
return Internal::AngleConversion<Unit, ToUnit>::Convert(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
template<AngleUnit ToUnit>
|
||||||
|
Angle<ToUnit, T> Angle<Unit, T>::ToAngle() const
|
||||||
|
{
|
||||||
|
return Angle<ToUnit, T>(To<ToUnit>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr T Angle<Unit, T>::ToDegrees() const
|
||||||
|
{
|
||||||
|
return To<AngleUnit::Degree>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<AngleUnit::Degree, T> Angle<Unit, T>::ToDegreeAngle() const
|
||||||
|
{
|
||||||
|
return ToAngle<AngleUnit::Degree>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
EulerAngles<T> Angle<Unit, T>::ToEulerAngles() const
|
||||||
|
{
|
||||||
|
return EulerAngles<T>(0, 0, ToDegrees());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
Quat<T> Angle<Unit, T>::ToQuat() const
|
||||||
|
{
|
||||||
|
auto halfAngle = Angle(*this) / 2.f;
|
||||||
|
auto sincos = halfAngle.GetSinCos();
|
||||||
|
return Quat<T>(sincos.second, 0, 0, sincos.first);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr T Angle<Unit, T>::ToRadians() const
|
||||||
|
{
|
||||||
|
return To<AngleUnit::Radian>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<AngleUnit::Radian, T> Angle<Unit, T>::ToRadianAngle() const
|
||||||
|
{
|
||||||
|
return ToAngle<AngleUnit::Radian>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
std::string Angle<Unit, T>::ToString() const
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
Internal::AngleUtils<Unit>::ToString(oss, value);
|
||||||
|
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr T Angle<Unit, T>::ToTurns() const
|
||||||
|
{
|
||||||
|
return To<AngleUnit::Turn>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<AngleUnit::Turn, T> Angle<Unit, T>::ToTurnAngle() const
|
||||||
|
{
|
||||||
|
return ToAngle<AngleUnit::Turn>();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::operator+() const
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::operator-() const
|
||||||
|
{
|
||||||
|
return Angle(-value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::operator+(Angle other) const
|
||||||
|
{
|
||||||
|
return Angle(value + other.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::operator-(Angle other) const
|
||||||
|
{
|
||||||
|
return Angle(value - other.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::operator*(T scalar) const
|
||||||
|
{
|
||||||
|
return Angle(value * scalar);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::operator/(T divider) const
|
||||||
|
{
|
||||||
|
return Angle(value / divider);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T>& Angle<Unit, T>::operator+=(Angle other)
|
||||||
|
{
|
||||||
|
value += other.value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T>& Angle<Unit, T>::operator-=(Angle other)
|
||||||
|
{
|
||||||
|
value -= other.value;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T>& Angle<Unit, T>::operator*=(T scalar)
|
||||||
|
{
|
||||||
|
value *= scalar;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T>& Angle<Unit, T>::operator/=(T divider)
|
||||||
|
{
|
||||||
|
value /= divider;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::operator==(Angle other) const
|
||||||
|
{
|
||||||
|
return value == other.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::operator!=(Angle other) const
|
||||||
|
{
|
||||||
|
return value != other.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::operator<(Angle other) const
|
||||||
|
{
|
||||||
|
return value < other.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::operator<=(Angle other) const
|
||||||
|
{
|
||||||
|
return value <= other.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::operator>(Angle other) const
|
||||||
|
{
|
||||||
|
return value > other.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::operator>=(Angle other) const
|
||||||
|
{
|
||||||
|
return value >= other.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::ApproxEqual(const Angle& lhs, const Angle& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr bool Angle<Unit, T>::ApproxEqual(const Angle& lhs, const Angle& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::Clamp(Angle angle, Angle min, Angle max)
|
||||||
|
{
|
||||||
|
return Angle(std::clamp(angle.value, min.value, max.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
template<AngleUnit FromUnit>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::From(T value)
|
||||||
|
{
|
||||||
|
return Angle(Internal::AngleConversion<FromUnit, Unit>::Convert(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::FromDegrees(T degrees)
|
||||||
|
{
|
||||||
|
return From<AngleUnit::Degree>(degrees);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::FromRadians(T radians)
|
||||||
|
{
|
||||||
|
return From<AngleUnit::Radian>(radians);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::FromTurns(T turns)
|
||||||
|
{
|
||||||
|
return From<AngleUnit::Turn>(turns);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
constexpr Angle<Unit, T> Angle<Unit, T>::Zero()
|
||||||
|
{
|
||||||
|
return Angle(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
Angle<Unit, T> operator/(T scale, Angle<Unit, T> angle)
|
||||||
|
{
|
||||||
|
return Angle<Unit, T>(scale / angle.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<AngleUnit Unit, typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, Angle<Unit, T> angle)
|
||||||
|
{
|
||||||
|
return Internal::AngleUtils<Unit>::ToString(out, angle.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, AngleUnit Unit>
|
||||||
|
constexpr Angle<Unit, T> Clamp(Angle<Unit, T> value, T min, T max)
|
||||||
|
{
|
||||||
|
return std::max(std::min(value.value, max), min);
|
||||||
|
}
|
||||||
|
}
|
||||||
87
runtime/Includes/Maths/Constants.h
git.filemode.normal_file
87
runtime/Includes/Maths/Constants.h
git.filemode.normal_file
@@ -0,0 +1,87 @@
|
|||||||
|
#ifndef __SCOP_MATHS_CONSTANTS__
|
||||||
|
#define __SCOP_MATHS_CONSTANTS__
|
||||||
|
|
||||||
|
#include <climits>
|
||||||
|
#include <limits>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T> constexpr std::size_t BitCount = CHAR_BIT * sizeof(T);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct MathConstants
|
||||||
|
{
|
||||||
|
static constexpr T Infinity()
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<T>::has_infinity);
|
||||||
|
return std::numeric_limits<T>::infinity();
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Max()
|
||||||
|
{
|
||||||
|
return std::numeric_limits<T>::max();
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Min()
|
||||||
|
{
|
||||||
|
return std::numeric_limits<T>::min();
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T NaN()
|
||||||
|
{
|
||||||
|
static_assert(std::numeric_limits<T>::has_signaling_NaN);
|
||||||
|
return std::numeric_limits<T>::quiet_NaN();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Math constants
|
||||||
|
static constexpr T HalfPi()
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>);
|
||||||
|
return T(1.5707963267948966192313216916398);
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Pi()
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>);
|
||||||
|
return T(3.1415926535897932384626433832795);
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Sqrt2()
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>);
|
||||||
|
return T(1.4142135623730950488016887242097);
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Sqrt3()
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>);
|
||||||
|
return T(1.7320508075688772935274463415059);
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Sqrt5()
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>);
|
||||||
|
return T(2.2360679774997896964091736687313);
|
||||||
|
}
|
||||||
|
|
||||||
|
static constexpr T Tau()
|
||||||
|
{
|
||||||
|
static_assert(std::is_floating_point_v<T>);
|
||||||
|
return T(6.2831853071795864769252867665590);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T = void> constexpr auto Infinity() { return MathConstants<T>::Infinity(); }
|
||||||
|
template<typename T = void> constexpr auto MaxValue() { return MathConstants<T>::Max(); }
|
||||||
|
template<typename T = void> constexpr auto MinValue() { return MathConstants<T>::Min(); }
|
||||||
|
template<typename T = void> constexpr auto NaN() { return MathConstants<T>::NaN(); }
|
||||||
|
template<typename T = void> constexpr auto HalfPi() { return MathConstants<T>::HalfPi(); }
|
||||||
|
template<typename T = void> constexpr auto Pi() { return MathConstants<T>::Pi(); }
|
||||||
|
template<typename T = void> constexpr auto Sqrt2() { return MathConstants<T>::Sqrt2(); }
|
||||||
|
template<typename T = void> constexpr auto Sqrt3() { return MathConstants<T>::Sqrt3(); }
|
||||||
|
template<typename T = void> constexpr auto Sqrt5() { return MathConstants<T>::Sqrt5(); }
|
||||||
|
template<typename T = void> constexpr auto Tau() { return MathConstants<T>::Tau(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
20
runtime/Includes/Maths/Enums.h
git.filemode.normal_file
20
runtime/Includes/Maths/Enums.h
git.filemode.normal_file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef __SCOPE_MATHS_ENUMS__
|
||||||
|
#define __SCOPE_MATHS_ENUMS__
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
enum class AngleUnit
|
||||||
|
{
|
||||||
|
Degree = 0,
|
||||||
|
Radian,
|
||||||
|
Turn,
|
||||||
|
|
||||||
|
EndEnum
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr std::size_t AngleUnitCount = static_cast<std::size_t>(AngleUnit::EndEnum);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
57
runtime/Includes/Maths/EulerAngles.h
git.filemode.normal_file
57
runtime/Includes/Maths/EulerAngles.h
git.filemode.normal_file
@@ -0,0 +1,57 @@
|
|||||||
|
#ifndef __SCOP_EULER_ANGLES__
|
||||||
|
#define __SCOP_EULER_ANGLES__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <Maths/Angles.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
struct EulerAngles
|
||||||
|
{
|
||||||
|
constexpr EulerAngles() = default;
|
||||||
|
constexpr EulerAngles(DegreeAngle<T> P, DegreeAngle<T> Y, DegreeAngle<T> R);
|
||||||
|
constexpr EulerAngles(const DegreeAngle<T> angles[3]);
|
||||||
|
template<AngleUnit Unit> constexpr EulerAngles(const Angle<Unit, T>& angle);
|
||||||
|
constexpr EulerAngles(const Quat<T>& quat);
|
||||||
|
template<typename U> constexpr explicit EulerAngles(const EulerAngles<U>& angles);
|
||||||
|
constexpr EulerAngles(const EulerAngles&) = default;
|
||||||
|
constexpr EulerAngles(EulerAngles&&) = default;
|
||||||
|
~EulerAngles() = default;
|
||||||
|
|
||||||
|
constexpr bool ApproxEqual(const EulerAngles& angles, T maxDifference = std::numeric_limits<T>::epsilon()) const;
|
||||||
|
|
||||||
|
constexpr EulerAngles& Normalize();
|
||||||
|
|
||||||
|
Quat<T> ToQuat() const;
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
constexpr EulerAngles operator+(const EulerAngles& angles) const;
|
||||||
|
constexpr EulerAngles operator-(const EulerAngles& angles) const;
|
||||||
|
|
||||||
|
constexpr EulerAngles& operator=(const EulerAngles&) = default;
|
||||||
|
constexpr EulerAngles& operator=(EulerAngles&&) = default;
|
||||||
|
|
||||||
|
constexpr EulerAngles& operator+=(const EulerAngles& angles);
|
||||||
|
constexpr EulerAngles& operator-=(const EulerAngles& angles);
|
||||||
|
|
||||||
|
constexpr bool operator==(const EulerAngles& angles) const;
|
||||||
|
constexpr bool operator!=(const EulerAngles& angles) const;
|
||||||
|
constexpr bool operator<(const EulerAngles& angles) const;
|
||||||
|
constexpr bool operator<=(const EulerAngles& angles) const;
|
||||||
|
constexpr bool operator>(const EulerAngles& angles) const;
|
||||||
|
constexpr bool operator>=(const EulerAngles& angles) const;
|
||||||
|
|
||||||
|
static constexpr bool ApproxEqual(const EulerAngles& lhs, const EulerAngles& rhs, T maxDifference = std::numeric_limits<T>::epsilon());
|
||||||
|
static constexpr EulerAngles Zero();
|
||||||
|
|
||||||
|
DegreeAngle<T> pitch, yaw, roll;
|
||||||
|
};
|
||||||
|
|
||||||
|
using EulerAnglesf = EulerAngles<float>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/EulerAngles.inl>
|
||||||
|
|
||||||
|
#endif
|
||||||
169
runtime/Includes/Maths/EulerAngles.inl
git.filemode.normal_file
169
runtime/Includes/Maths/EulerAngles.inl
git.filemode.normal_file
@@ -0,0 +1,169 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Maths/EulerAngles.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T>::EulerAngles(DegreeAngle<T> P, DegreeAngle<T> Y, DegreeAngle<T> R) :
|
||||||
|
pitch(P), yaw(Y), roll(R)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T>::EulerAngles(const DegreeAngle<T> angles[3]) :
|
||||||
|
EulerAngles(angles[0], angles[1], angles[2])
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<AngleUnit Unit>
|
||||||
|
constexpr EulerAngles<T>::EulerAngles(const Angle<Unit, T>& angle) :
|
||||||
|
EulerAngles(angle.ToEulerAngles())
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T>::EulerAngles(const Quat<T>& quat) :
|
||||||
|
EulerAngles(quat.ToEulerAngles())
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
constexpr EulerAngles<T>::EulerAngles(const EulerAngles<U>& angles) :
|
||||||
|
pitch(DegreeAngle<T>(angles.pitch)), yaw(DegreeAngle<T>(angles.yaw)), roll(DegreeAngle<T>(angles.roll))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::ApproxEqual(const EulerAngles& angles, T maxDifference) const
|
||||||
|
{
|
||||||
|
return pitch.ApproxEqual(angles.pitch, maxDifference) && yaw.ApproxEqual(angles.yaw, maxDifference) && roll.ApproxEqual(angles.roll, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T>& EulerAngles<T>::Normalize()
|
||||||
|
{
|
||||||
|
pitch.Normalize();
|
||||||
|
yaw.Normalize();
|
||||||
|
roll.Normalize();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> EulerAngles<T>::ToQuat() const
|
||||||
|
{
|
||||||
|
// XYZ
|
||||||
|
auto [s1, c1] = (yaw / T(2.0)).GetSinCos();
|
||||||
|
auto [s2, c2] = (roll / T(2.0)).GetSinCos();
|
||||||
|
auto [s3, c3] = (pitch / T(2.0)).GetSinCos();
|
||||||
|
|
||||||
|
return Quat<T>(c1 * c2 * c3 - s1 * s2 * s3,
|
||||||
|
s1 * s2 * c3 + c1 * c2 * s3,
|
||||||
|
s1 * c2 * c3 + c1 * s2 * s3,
|
||||||
|
c1 * s2 * c3 - s1 * c2 * s3);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string EulerAngles<T>::ToString() const
|
||||||
|
{
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *this;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T> EulerAngles<T>::operator+(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
return EulerAngles(pitch + angles.pitch, yaw + angles.yaw, roll + angles.roll);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T> EulerAngles<T>::operator-(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
return EulerAngles(pitch - angles.pitch, yaw - angles.yaw, roll - angles.roll);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T>& EulerAngles<T>::operator+=(const EulerAngles& angles)
|
||||||
|
{
|
||||||
|
pitch += angles.pitch;
|
||||||
|
yaw += angles.yaw;
|
||||||
|
roll += angles.roll;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T>& EulerAngles<T>::operator-=(const EulerAngles& angles)
|
||||||
|
{
|
||||||
|
pitch -= angles.pitch;
|
||||||
|
yaw -= angles.yaw;
|
||||||
|
roll -= angles.roll;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::operator==(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
return pitch == angles.pitch && yaw == angles.yaw && roll == angles.roll;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::operator!=(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
return !operator==(angles);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::operator<(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
if (pitch != angles.pitch)
|
||||||
|
return pitch < angles.pitch;
|
||||||
|
if (yaw != angles.yaw)
|
||||||
|
return yaw < angles.yaw;
|
||||||
|
return roll < angles.roll;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::operator<=(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
if (pitch != angles.pitch)
|
||||||
|
return pitch < angles.pitch;
|
||||||
|
if (yaw != angles.yaw)
|
||||||
|
return yaw < angles.yaw;
|
||||||
|
return roll <= angles.roll;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::operator>(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
if (pitch != angles.pitch)
|
||||||
|
return pitch > angles.pitch;
|
||||||
|
if (yaw != angles.yaw)
|
||||||
|
return yaw > angles.yaw;
|
||||||
|
return roll > angles.roll;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::operator>=(const EulerAngles& angles) const
|
||||||
|
{
|
||||||
|
if (pitch != angles.pitch)
|
||||||
|
return pitch > angles.pitch;
|
||||||
|
if (yaw != angles.yaw)
|
||||||
|
return yaw > angles.yaw;
|
||||||
|
return roll >= angles.roll;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool EulerAngles<T>::ApproxEqual(const EulerAngles& lhs, const EulerAngles& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr EulerAngles<T> EulerAngles<T>::Zero()
|
||||||
|
{
|
||||||
|
return EulerAngles(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, const EulerAngles<T>& angles)
|
||||||
|
{
|
||||||
|
return out << "EulerAngles(" << angles.pitch << ", " << angles.yaw << ", " << angles.roll << ')';
|
||||||
|
}
|
||||||
|
}
|
||||||
122
runtime/Includes/Maths/Mat4.h
git.filemode.normal_file
122
runtime/Includes/Maths/Mat4.h
git.filemode.normal_file
@@ -0,0 +1,122 @@
|
|||||||
|
#ifndef __SCOP_MAT4__
|
||||||
|
#define __SCOP_MAT4__
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <limits>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <Maths/Angles.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T> struct Vec2;
|
||||||
|
template<typename T> struct Vec3;
|
||||||
|
template<typename T> struct Vec4;
|
||||||
|
template<typename T> struct Quat;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Mat4
|
||||||
|
{
|
||||||
|
T m11, m12, m13, m14;
|
||||||
|
T m21, m22, m23, m24;
|
||||||
|
T m31, m32, m33, m34;
|
||||||
|
T m41, m42, m43, m44;
|
||||||
|
|
||||||
|
constexpr Mat4() = default;
|
||||||
|
constexpr Mat4(T r11, T r12, T r13, T r14,
|
||||||
|
T r21, T r22, T r23, T r24,
|
||||||
|
T r31, T r32, T r33, T r34,
|
||||||
|
T r41, T r42, T r43, T r44);
|
||||||
|
constexpr Mat4(const T matrix[16]);
|
||||||
|
constexpr Mat4(const Mat4&) = default;
|
||||||
|
constexpr Mat4(Mat4&&) = default;
|
||||||
|
|
||||||
|
constexpr Mat4& ApplyRotation(const Quat<T>& rotation);
|
||||||
|
constexpr Mat4& ApplyScale(const Vec3<T>& scale);
|
||||||
|
constexpr Mat4& ApplyTranslation(const Vec3<T>& translation);
|
||||||
|
|
||||||
|
constexpr bool ApproxEqual(const Mat4& vec, T max_difference = std::numeric_limits<T>::epsilon()) const;
|
||||||
|
|
||||||
|
constexpr Mat4& Concatenate(const Mat4& matrix);
|
||||||
|
constexpr Mat4& ConcatenateTransform(const Mat4& matrix);
|
||||||
|
|
||||||
|
constexpr Vec4<T> GetColumn(std::size_t column) const;
|
||||||
|
constexpr T GetDeterminant() const;
|
||||||
|
constexpr T GetDeterminantTransform() const;
|
||||||
|
constexpr bool GetInverse(Mat4* dest) const;
|
||||||
|
constexpr bool GetInverseTransform(Mat4* dest) const;
|
||||||
|
Quat<T> GetRotation() const;
|
||||||
|
constexpr Vec4<T> GetRow(std::size_t row) const;
|
||||||
|
constexpr Vec3<T> GetScale() const;
|
||||||
|
constexpr Vec3<T> GetSquaredScale() const;
|
||||||
|
constexpr Vec3<T> GetTranslation() const;
|
||||||
|
constexpr void GetTransposed(Mat4* dest) const;
|
||||||
|
|
||||||
|
constexpr bool HasNegativeScale() const;
|
||||||
|
constexpr bool HasScale() const;
|
||||||
|
|
||||||
|
constexpr Mat4& Inverse(bool* succeeded = nullptr);
|
||||||
|
constexpr Mat4& InverseTransform(bool* succeeded = nullptr);
|
||||||
|
|
||||||
|
constexpr bool IsTransformMatrix() const;
|
||||||
|
constexpr bool IsIdentity() const;
|
||||||
|
|
||||||
|
constexpr Mat4& SetRotation(const Quat<T>& rotation);
|
||||||
|
constexpr Mat4& SetScale(const Vec3<T>& scale);
|
||||||
|
constexpr Mat4& SetTranslation(const Vec3<T>& translation);
|
||||||
|
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
constexpr Vec2<T> Transform(const Vec2<T>& vector, T z = 0.0, T w = 1.0) const;
|
||||||
|
constexpr Vec3<T> Transform(const Vec3<T>& vector, T w = 1.0) const;
|
||||||
|
constexpr Vec4<T> Transform(const Vec4<T>& vector) const;
|
||||||
|
|
||||||
|
constexpr Mat4& Transpose();
|
||||||
|
|
||||||
|
constexpr T& operator()(std::size_t x, std::size_t y);
|
||||||
|
constexpr const T& operator()(std::size_t x, std::size_t y) const;
|
||||||
|
|
||||||
|
constexpr T& operator[](std::size_t i);
|
||||||
|
constexpr const T& operator[](std::size_t i) const;
|
||||||
|
|
||||||
|
constexpr Mat4& operator=(const Mat4&) = default;
|
||||||
|
constexpr Mat4& operator=(Mat4&&) = default;
|
||||||
|
|
||||||
|
constexpr Mat4 operator*(const Mat4& matrix) const;
|
||||||
|
constexpr Vec2<T> operator*(const Vec2<T>& vector) const;
|
||||||
|
constexpr Vec3<T> operator*(const Vec3<T>& vector) const;
|
||||||
|
constexpr Vec4<T> operator*(const Vec4<T>& vector) const;
|
||||||
|
constexpr Mat4 operator*(T scalar) const;
|
||||||
|
|
||||||
|
constexpr Mat4& operator*=(const Mat4& matrix);
|
||||||
|
constexpr Mat4& operator*=(T scalar);
|
||||||
|
|
||||||
|
constexpr bool operator==(const Mat4& mat) const;
|
||||||
|
constexpr bool operator!=(const Mat4& mat) const;
|
||||||
|
|
||||||
|
static constexpr bool ApproxEqual(const Mat4& lhs, const Mat4& rhs, T max_difference = std::numeric_limits<T>::epsilon());
|
||||||
|
static constexpr Mat4 Concatenate(const Mat4& left, const Mat4& right);
|
||||||
|
static constexpr Mat4 ConcatenateTransform(const Mat4& left, const Mat4& right);
|
||||||
|
static constexpr Mat4 Identity();
|
||||||
|
static constexpr Mat4 LookAt(const Vec3<T>& eye, const Vec3<T>& target, const Vec3<T>& up = Vec3<T>::Up());
|
||||||
|
static constexpr Mat4 Ortho(T left, T right, T top, T bottom, T zNear = -1.0, T zFar = 1.0);
|
||||||
|
static Mat4 Perspective(RadianAngle<T> angle, T ratio, T zNear, T zFar);
|
||||||
|
static constexpr Mat4 Rotate(const Quat<T>& rotation);
|
||||||
|
static constexpr Mat4 Scale(const Vec3<T>& scale);
|
||||||
|
static constexpr Mat4 Translate(const Vec3<T>& translation);
|
||||||
|
static constexpr Mat4 Transform(const Vec3<T>& translation, const Quat<T>& rotation);
|
||||||
|
static constexpr Mat4 Transform(const Vec3<T>& translation, const Quat<T>& rotation, const Vec3<T>& scale);
|
||||||
|
static constexpr Mat4 TransformInverse(const Vec3<T>& translation, const Quat<T>& rotation);
|
||||||
|
static constexpr Mat4 TransformInverse(const Vec3<T>& translation, const Quat<T>& rotation, const Vec3<T>& scale);
|
||||||
|
static constexpr Mat4 Zero();
|
||||||
|
|
||||||
|
~Mat4() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Mat4d = Mat4<double>;
|
||||||
|
using Mat4f = Mat4<float>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/Mat4.inl>
|
||||||
|
|
||||||
|
#endif
|
||||||
879
runtime/Includes/Maths/Mat4.inl
git.filemode.normal_file
879
runtime/Includes/Maths/Mat4.inl
git.filemode.normal_file
@@ -0,0 +1,879 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Maths/Mat4.h>
|
||||||
|
|
||||||
|
#include <Core/Logs.h>
|
||||||
|
#include <Maths/EulerAngles.h>
|
||||||
|
#include <Maths/Quaternions.h>
|
||||||
|
#include <Maths/Vec2.h>
|
||||||
|
#include <Maths/Vec3.h>
|
||||||
|
#include <Maths/Vec4.h>
|
||||||
|
#include <Maths/MathsUtils.h>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>::Mat4(T r11, T r12, T r13, T r14,
|
||||||
|
T r21, T r22, T r23, T r24,
|
||||||
|
T r31, T r32, T r33, T r34,
|
||||||
|
T r41, T r42, T r43, T r44) :
|
||||||
|
m11(r11), m12(r12), m13(r13), m14(r14),
|
||||||
|
m21(r21), m22(r22), m23(r23), m24(r24),
|
||||||
|
m31(r31), m32(r32), m33(r33), m34(r34),
|
||||||
|
m41(r41), m42(r42), m43(r43), m44(r44)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>::Mat4(const T matrix[16]) :
|
||||||
|
Mat4(matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
|
||||||
|
matrix[ 4], matrix[ 5], matrix[ 6], matrix[ 7],
|
||||||
|
matrix[ 8], matrix[ 9], matrix[10], matrix[11],
|
||||||
|
matrix[12], matrix[13], matrix[14], matrix[15])
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::ApplyRotation(const Quat<T>& rotation)
|
||||||
|
{
|
||||||
|
return Concatenate(Mat4<T>::Rotate(rotation));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::ApplyScale(const Vec3<T>& scale)
|
||||||
|
{
|
||||||
|
m11 *= scale.x;
|
||||||
|
m12 *= scale.x;
|
||||||
|
m13 *= scale.x;
|
||||||
|
|
||||||
|
m21 *= scale.y;
|
||||||
|
m22 *= scale.y;
|
||||||
|
m23 *= scale.y;
|
||||||
|
|
||||||
|
m31 *= scale.z;
|
||||||
|
m32 *= scale.z;
|
||||||
|
m33 *= scale.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::ApplyTranslation(const Vec3<T>& translation)
|
||||||
|
{
|
||||||
|
m41 += translation.x;
|
||||||
|
m42 += translation.y;
|
||||||
|
m43 += translation.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::ApproxEqual(const Mat4& mat, T maxDifference) const
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < 16; ++i)
|
||||||
|
if(!NumberEquals((&m11)[i], (&mat.m11)[i], maxDifference))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::Concatenate(const Mat4& matrix)
|
||||||
|
{
|
||||||
|
return operator=(Mat4(
|
||||||
|
m11 * matrix.m11 + m12 * matrix.m21 + m13 * matrix.m31 + m14 * matrix.m41,
|
||||||
|
m11 * matrix.m12 + m12 * matrix.m22 + m13 * matrix.m32 + m14 * matrix.m42,
|
||||||
|
m11 * matrix.m13 + m12 * matrix.m23 + m13 * matrix.m33 + m14 * matrix.m43,
|
||||||
|
m11 * matrix.m14 + m12 * matrix.m24 + m13 * matrix.m34 + m14 * matrix.m44,
|
||||||
|
|
||||||
|
m21 * matrix.m11 + m22 * matrix.m21 + m23 * matrix.m31 + m24 * matrix.m41,
|
||||||
|
m21 * matrix.m12 + m22 * matrix.m22 + m23 * matrix.m32 + m24 * matrix.m42,
|
||||||
|
m21 * matrix.m13 + m22 * matrix.m23 + m23 * matrix.m33 + m24 * matrix.m43,
|
||||||
|
m21 * matrix.m14 + m22 * matrix.m24 + m23 * matrix.m34 + m24 * matrix.m44,
|
||||||
|
|
||||||
|
m31 * matrix.m11 + m32 * matrix.m21 + m33 * matrix.m31 + m34 * matrix.m41,
|
||||||
|
m31 * matrix.m12 + m32 * matrix.m22 + m33 * matrix.m32 + m34 * matrix.m42,
|
||||||
|
m31 * matrix.m13 + m32 * matrix.m23 + m33 * matrix.m33 + m34 * matrix.m43,
|
||||||
|
m31 * matrix.m14 + m32 * matrix.m24 + m33 * matrix.m34 + m34 * matrix.m44,
|
||||||
|
|
||||||
|
m41 * matrix.m11 + m42 * matrix.m21 + m43 * matrix.m31 + m44 * matrix.m41,
|
||||||
|
m41 * matrix.m12 + m42 * matrix.m22 + m43 * matrix.m32 + m44 * matrix.m42,
|
||||||
|
m41 * matrix.m13 + m42 * matrix.m23 + m43 * matrix.m33 + m44 * matrix.m43,
|
||||||
|
m41 * matrix.m14 + m42 * matrix.m24 + m43 * matrix.m34 + m44 * matrix.m44
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::ConcatenateTransform(const Mat4& matrix)
|
||||||
|
{
|
||||||
|
return operator=(Mat4(
|
||||||
|
m11*matrix.m11 + m12*matrix.m21 + m13*matrix.m31,
|
||||||
|
m11*matrix.m12 + m12*matrix.m22 + m13*matrix.m32,
|
||||||
|
m11*matrix.m13 + m12*matrix.m23 + m13*matrix.m33,
|
||||||
|
T(0.0),
|
||||||
|
|
||||||
|
m21*matrix.m11 + m22*matrix.m21 + m23*matrix.m31,
|
||||||
|
m21*matrix.m12 + m22*matrix.m22 + m23*matrix.m32,
|
||||||
|
m21*matrix.m13 + m22*matrix.m23 + m23*matrix.m33,
|
||||||
|
T(0.0),
|
||||||
|
|
||||||
|
m31*matrix.m11 + m32*matrix.m21 + m33*matrix.m31,
|
||||||
|
m31*matrix.m12 + m32*matrix.m22 + m33*matrix.m32,
|
||||||
|
m31*matrix.m13 + m32*matrix.m23 + m33*matrix.m33,
|
||||||
|
T(0.0),
|
||||||
|
|
||||||
|
m41*matrix.m11 + m42*matrix.m21 + m43*matrix.m31 + matrix.m41,
|
||||||
|
m41*matrix.m12 + m42*matrix.m22 + m43*matrix.m32 + matrix.m42,
|
||||||
|
m41*matrix.m13 + m42*matrix.m23 + m43*matrix.m33 + matrix.m43,
|
||||||
|
T(1.0)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Mat4<T>::GetColumn(std::size_t column) const
|
||||||
|
{
|
||||||
|
Assert(column < 4, "column index out of range");
|
||||||
|
const T* ptr = &m11 + column * 4;
|
||||||
|
return Vec4<T>(ptr[0], ptr[1], ptr[2], ptr[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Mat4<T>::GetDeterminant() const
|
||||||
|
{
|
||||||
|
T A = m22*(m33*m44 - m43*m34) - m32*(m23*m44 - m43*m24) + m42*(m23*m34 - m33*m24);
|
||||||
|
T B = m12*(m33*m44 - m43*m34) - m32*(m13*m44 - m43*m14) + m42*(m13*m34 - m33*m14);
|
||||||
|
T C = m12*(m23*m44 - m43*m24) - m22*(m13*m44 - m43*m14) + m42*(m13*m24 - m23*m14);
|
||||||
|
T D = m12*(m23*m34 - m33*m24) - m22*(m13*m34 - m33*m14) + m32*(m13*m24 - m23*m14);
|
||||||
|
|
||||||
|
return m11*A - m21*B + m31*C - m41*D;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Mat4<T>::GetDeterminantTransform() const
|
||||||
|
{
|
||||||
|
T A = m22*m33 - m32*m23;
|
||||||
|
T B = m12*m33 - m32*m13;
|
||||||
|
T C = m12*m23 - m22*m13;
|
||||||
|
|
||||||
|
return m11*A - m21*B + m31*C;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::GetInverse(Mat4* dest) const
|
||||||
|
{
|
||||||
|
Assert(dest, "destination matrix must be valid");
|
||||||
|
|
||||||
|
T det = GetDeterminant();
|
||||||
|
if(det == T(0.0))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
|
||||||
|
T inv[16];
|
||||||
|
inv[0] = m22 * m33 * m44 -
|
||||||
|
m22 * m34 * m43 -
|
||||||
|
m32 * m23 * m44 +
|
||||||
|
m32 * m24 * m43 +
|
||||||
|
m42 * m23 * m34 -
|
||||||
|
m42 * m24 * m33;
|
||||||
|
|
||||||
|
inv[1] = -m12 * m33 * m44 +
|
||||||
|
m12 * m34 * m43 +
|
||||||
|
m32 * m13 * m44 -
|
||||||
|
m32 * m14 * m43 -
|
||||||
|
m42 * m13 * m34 +
|
||||||
|
m42 * m14 * m33;
|
||||||
|
|
||||||
|
inv[2] = m12 * m23 * m44 -
|
||||||
|
m12 * m24 * m43 -
|
||||||
|
m22 * m13 * m44 +
|
||||||
|
m22 * m14 * m43 +
|
||||||
|
m42 * m13 * m24 -
|
||||||
|
m42 * m14 * m23;
|
||||||
|
|
||||||
|
inv[3] = -m12 * m23 * m34 +
|
||||||
|
m12 * m24 * m33 +
|
||||||
|
m22 * m13 * m34 -
|
||||||
|
m22 * m14 * m33 -
|
||||||
|
m32 * m13 * m24 +
|
||||||
|
m32 * m14 * m23;
|
||||||
|
|
||||||
|
inv[4] = -m21 * m33 * m44 +
|
||||||
|
m21 * m34 * m43 +
|
||||||
|
m31 * m23 * m44 -
|
||||||
|
m31 * m24 * m43 -
|
||||||
|
m41 * m23 * m34 +
|
||||||
|
m41 * m24 * m33;
|
||||||
|
|
||||||
|
inv[5] = m11 * m33 * m44 -
|
||||||
|
m11 * m34 * m43 -
|
||||||
|
m31 * m13 * m44 +
|
||||||
|
m31 * m14 * m43 +
|
||||||
|
m41 * m13 * m34 -
|
||||||
|
m41 * m14 * m33;
|
||||||
|
|
||||||
|
inv[6] = -m11 * m23 * m44 +
|
||||||
|
m11 * m24 * m43 +
|
||||||
|
m21 * m13 * m44 -
|
||||||
|
m21 * m14 * m43 -
|
||||||
|
m41 * m13 * m24 +
|
||||||
|
m41 * m14 * m23;
|
||||||
|
|
||||||
|
inv[7] = m11 * m23 * m34 -
|
||||||
|
m11 * m24 * m33 -
|
||||||
|
m21 * m13 * m34 +
|
||||||
|
m21 * m14 * m33 +
|
||||||
|
m31 * m13 * m24 -
|
||||||
|
m31 * m14 * m23;
|
||||||
|
|
||||||
|
inv[8] = m21 * m32 * m44 -
|
||||||
|
m21 * m34 * m42 -
|
||||||
|
m31 * m22 * m44 +
|
||||||
|
m31 * m24 * m42 +
|
||||||
|
m41 * m22 * m34 -
|
||||||
|
m41 * m24 * m32;
|
||||||
|
|
||||||
|
inv[9] = -m11 * m32 * m44 +
|
||||||
|
m11 * m34 * m42 +
|
||||||
|
m31 * m12 * m44 -
|
||||||
|
m31 * m14 * m42 -
|
||||||
|
m41 * m12 * m34 +
|
||||||
|
m41 * m14 * m32;
|
||||||
|
|
||||||
|
inv[10] = m11 * m22 * m44 -
|
||||||
|
m11 * m24 * m42 -
|
||||||
|
m21 * m12 * m44 +
|
||||||
|
m21 * m14 * m42 +
|
||||||
|
m41 * m12 * m24 -
|
||||||
|
m41 * m14 * m22;
|
||||||
|
|
||||||
|
inv[11] = -m11 * m22 * m34 +
|
||||||
|
m11 * m24 * m32 +
|
||||||
|
m21 * m12 * m34 -
|
||||||
|
m21 * m14 * m32 -
|
||||||
|
m31 * m12 * m24 +
|
||||||
|
m31 * m14 * m22;
|
||||||
|
|
||||||
|
inv[12] = -m21 * m32 * m43 +
|
||||||
|
m21 * m33 * m42 +
|
||||||
|
m31 * m22 * m43 -
|
||||||
|
m31 * m23 * m42 -
|
||||||
|
m41 * m22 * m33 +
|
||||||
|
m41 * m23 * m32;
|
||||||
|
|
||||||
|
inv[13] = m11 * m32 * m43 -
|
||||||
|
m11 * m33 * m42 -
|
||||||
|
m31 * m12 * m43 +
|
||||||
|
m31 * m13 * m42 +
|
||||||
|
m41 * m12 * m33 -
|
||||||
|
m41 * m13 * m32;
|
||||||
|
|
||||||
|
inv[14] = -m11 * m22 * m43 +
|
||||||
|
m11 * m23 * m42 +
|
||||||
|
m21 * m12 * m43 -
|
||||||
|
m21 * m13 * m42 -
|
||||||
|
m41 * m12 * m23 +
|
||||||
|
m41 * m13 * m22;
|
||||||
|
|
||||||
|
inv[15] = m11 * m22 * m33 -
|
||||||
|
m11 * m23 * m32 -
|
||||||
|
m21 * m12 * m33 +
|
||||||
|
m21 * m13 * m32 +
|
||||||
|
m31 * m12 * m23 -
|
||||||
|
m31 * m13 * m22;
|
||||||
|
|
||||||
|
T invDet = T(1.0) / det;
|
||||||
|
for(unsigned int i = 0; i < 16; ++i)
|
||||||
|
inv[i] *= invDet;
|
||||||
|
|
||||||
|
*dest = inv;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::GetInverseTransform(Mat4* dest) const
|
||||||
|
{
|
||||||
|
Assert(dest, "destination matrix must be valid");
|
||||||
|
|
||||||
|
T det = GetDeterminantTransform();
|
||||||
|
if(det == T(0.0))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
|
||||||
|
// http://stackoverflow.com/questions/1148309/inverting-a-4x4-matrix
|
||||||
|
T inv[16];
|
||||||
|
inv[0] = m22 * m33 -
|
||||||
|
m32 * m23;
|
||||||
|
|
||||||
|
inv[1] = -m12 * m33 +
|
||||||
|
m32 * m13;
|
||||||
|
|
||||||
|
inv[2] = m12 * m23 -
|
||||||
|
m22 * m13;
|
||||||
|
|
||||||
|
inv[3] = T(0.0);
|
||||||
|
|
||||||
|
inv[4] = -m21 * m33 +
|
||||||
|
m31 * m23;
|
||||||
|
|
||||||
|
inv[5] = m11 * m33 -
|
||||||
|
m31 * m13;
|
||||||
|
|
||||||
|
inv[6] = -m11 * m23 +
|
||||||
|
m21 * m13;
|
||||||
|
|
||||||
|
inv[7] = T(0.0);
|
||||||
|
|
||||||
|
inv[8] = m21 * m32 -
|
||||||
|
m31 * m22;
|
||||||
|
|
||||||
|
inv[9] = -m11 * m32 +
|
||||||
|
m31 * m12;
|
||||||
|
|
||||||
|
inv[10] = m11 * m22 -
|
||||||
|
m21 * m12;
|
||||||
|
|
||||||
|
inv[11] = T(0.0);
|
||||||
|
|
||||||
|
inv[12] = -m21 * m32 * m43 +
|
||||||
|
m21 * m33 * m42 +
|
||||||
|
m31 * m22 * m43 -
|
||||||
|
m31 * m23 * m42 -
|
||||||
|
m41 * m22 * m33 +
|
||||||
|
m41 * m23 * m32;
|
||||||
|
|
||||||
|
inv[13] = m11 * m32 * m43 -
|
||||||
|
m11 * m33 * m42 -
|
||||||
|
m31 * m12 * m43 +
|
||||||
|
m31 * m13 * m42 +
|
||||||
|
m41 * m12 * m33 -
|
||||||
|
m41 * m13 * m32;
|
||||||
|
|
||||||
|
inv[14] = -m11 * m22 * m43 +
|
||||||
|
m11 * m23 * m42 +
|
||||||
|
m21 * m12 * m43 -
|
||||||
|
m21 * m13 * m42 -
|
||||||
|
m41 * m12 * m23 +
|
||||||
|
m41 * m13 * m22;
|
||||||
|
|
||||||
|
T invDet = T(1.0) / det;
|
||||||
|
for(unsigned int i = 0; i < 16; ++i)
|
||||||
|
inv[i] *= invDet;
|
||||||
|
|
||||||
|
inv[15] = T(1.0);
|
||||||
|
|
||||||
|
*dest = inv;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Mat4<T>::GetRotation() const
|
||||||
|
{
|
||||||
|
// http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuat/
|
||||||
|
Quat<T> quat;
|
||||||
|
|
||||||
|
T trace = m11 + m22 + m33;
|
||||||
|
if(trace > T(0.0))
|
||||||
|
{
|
||||||
|
T s = T(0.5) / std::sqrt(trace + T(1.0));
|
||||||
|
quat.w = T(0.25) / s;
|
||||||
|
quat.x = (m23 - m32) * s;
|
||||||
|
quat.y = (m31 - m13) * s;
|
||||||
|
quat.z = (m12 - m21) * s;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(m11 > m22 && m11 > m33)
|
||||||
|
{
|
||||||
|
T s = T(2.0) * std::sqrt(T(1.0) + m11 - m22 - m33);
|
||||||
|
|
||||||
|
quat.w = (m23 - m32) / s;
|
||||||
|
quat.x = T(0.25) * s;
|
||||||
|
quat.y = (m21 + m12) / s;
|
||||||
|
quat.z = (m31 + m13) / s;
|
||||||
|
}
|
||||||
|
else if(m22 > m33)
|
||||||
|
{
|
||||||
|
T s = T(2.0) * std::sqrt(T(1.0) + m22 - m11 - m33);
|
||||||
|
|
||||||
|
quat.w = (m31 - m13) / s;
|
||||||
|
quat.x = (m21 + m12) / s;
|
||||||
|
quat.y = T(0.25) * s;
|
||||||
|
quat.z = (m32 + m23) / s;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
T s = T(2.0) * std::sqrt(T(1.0) + m33 - m11 - m22);
|
||||||
|
|
||||||
|
quat.w = (m12 - m21) / s;
|
||||||
|
quat.x = (m31 + m13) / s;
|
||||||
|
quat.y = (m32 + m23) / s;
|
||||||
|
quat.z = T(0.25) * s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return quat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Mat4<T>::GetRow(std::size_t row) const
|
||||||
|
{
|
||||||
|
Assert(row < 4, "row index out of range");
|
||||||
|
|
||||||
|
const T* ptr = &m11;
|
||||||
|
return Vec4<T>(ptr[row], ptr[row+4], ptr[row+8], ptr[row+12]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Mat4<T>::GetScale() const
|
||||||
|
{
|
||||||
|
Vec3<T> squaredScale = GetSquaredScale();
|
||||||
|
return Vec3<T>(std::sqrt(squaredScale.x), std::sqrt(squaredScale.y), std::sqrt(squaredScale.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Mat4<T>::GetSquaredScale() const
|
||||||
|
{
|
||||||
|
return Vec3<T>(m11 * m11 + m12 * m12 + m13 * m13,
|
||||||
|
m21 * m21 + m22 * m22 + m23 * m23,
|
||||||
|
m31 * m31 + m32 * m32 + m33 * m33);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Mat4<T>::GetTranslation() const
|
||||||
|
{
|
||||||
|
return Vec3<T>(m41, m42, m43);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr void Mat4<T>::GetTransposed(Mat4* dest) const
|
||||||
|
{
|
||||||
|
(*dest) = Mat4f(
|
||||||
|
m11, m21, m31, m41,
|
||||||
|
m12, m22, m32, m42,
|
||||||
|
m13, m23, m33, m43,
|
||||||
|
m14, m24, m34, m44
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::HasNegativeScale() const
|
||||||
|
{
|
||||||
|
return GetDeterminant() < T(0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::HasScale() const
|
||||||
|
{
|
||||||
|
T t = m11*m11 + m21*m21 + m31*m31;
|
||||||
|
if(!NumberEquals(t, T(1.0)))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
t = m12*m12 + m22*m22 + m32*m32;
|
||||||
|
if(!NumberEquals(t, T(1.0)))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
t = m13*m13 + m23*m23 + m33*m33;
|
||||||
|
if(!NumberEquals(t, T(1.0)))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::Inverse(bool* succeeded)
|
||||||
|
{
|
||||||
|
bool result = GetInverse(this);
|
||||||
|
if(succeeded)
|
||||||
|
*succeeded = result;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::InverseTransform(bool* succeeded)
|
||||||
|
{
|
||||||
|
bool result = GetInverseTransform(this);
|
||||||
|
if(succeeded)
|
||||||
|
*succeeded = result;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::IsTransformMatrix() const
|
||||||
|
{
|
||||||
|
return NumberEquals(m14, T(0.0)) && NumberEquals(m24, T(0.0)) && NumberEquals(m34, T(0.0)) && NumberEquals(m44, T(1.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::IsIdentity() const
|
||||||
|
{
|
||||||
|
return (NumberEquals(m11, T(1.0)) && NumberEquals(m12, T(0.0)) && NumberEquals(m13, T(0.0)) && NumberEquals(m14, T(0.0)) &&
|
||||||
|
NumberEquals(m21, T(0.0)) && NumberEquals(m22, T(1.0)) && NumberEquals(m23, T(0.0)) && NumberEquals(m24, T(0.0)) &&
|
||||||
|
NumberEquals(m31, T(0.0)) && NumberEquals(m32, T(0.0)) && NumberEquals(m33, T(1.0)) && NumberEquals(m34, T(0.0)) &&
|
||||||
|
NumberEquals(m41, T(0.0)) && NumberEquals(m42, T(0.0)) && NumberEquals(m43, T(0.0)) && NumberEquals(m44, T(1.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::SetRotation(const Quat<T>& rotation)
|
||||||
|
{
|
||||||
|
T qw = rotation.w;
|
||||||
|
T qx = rotation.x;
|
||||||
|
T qy = rotation.y;
|
||||||
|
T qz = rotation.z;
|
||||||
|
|
||||||
|
T qx2 = qx * qx;
|
||||||
|
T qy2 = qy * qy;
|
||||||
|
T qz2 = qz * qz;
|
||||||
|
|
||||||
|
m11 = T(1.0) - T(2.0) * qy2 - T(2.0) * qz2;
|
||||||
|
m21 = T(2.0) * qx * qy - T(2.0) * qz * qw;
|
||||||
|
m31 = T(2.0) * qx * qz + T(2.0) * qy * qw;
|
||||||
|
|
||||||
|
m12 = T(2.0) * qx * qy + T(2.0) * qz * qw;
|
||||||
|
m22 = T(1.0) - T(2.0) * qx2 - T(2.0) * qz2;
|
||||||
|
m32 = T(2.0) * qy * qz - T(2.0) * qx * qw;
|
||||||
|
|
||||||
|
m13 = T(2.0) * qx * qz - T(2.0) * qy * qw;
|
||||||
|
m23 = T(2.0) * qy * qz + T(2.0) * qx * qw;
|
||||||
|
m33 = T(1.0) - T(2.0) * qx2 - T(2.0) * qy2;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::SetScale(const Vec3<T>& scale)
|
||||||
|
{
|
||||||
|
m11 = scale.x;
|
||||||
|
m22 = scale.y;
|
||||||
|
m33 = scale.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::SetTranslation(const Vec3<T>& translation)
|
||||||
|
{
|
||||||
|
m41 = translation.x;
|
||||||
|
m42 = translation.y;
|
||||||
|
m43 = translation.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string Mat4<T>::ToString() const
|
||||||
|
{
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *this;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Mat4<T>::Transform(const Vec2<T>& vector, T z, T w) const
|
||||||
|
{
|
||||||
|
return Vec2<T>(m11 * vector.x + m21 * vector.y + m31 * z + m41 * w,
|
||||||
|
m12 * vector.x + m22 * vector.y + m32 * z + m42 * w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Mat4<T>::Transform(const Vec3<T>& vector, T w) const
|
||||||
|
{
|
||||||
|
return Vec3<T>(m11 * vector.x + m21 * vector.y + m31 * vector.z + m41 * w,
|
||||||
|
m12 * vector.x + m22 * vector.y + m32 * vector.z + m42 * w,
|
||||||
|
m13 * vector.x + m23 * vector.y + m33 * vector.z + m43 * w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Mat4<T>::Transform(const Vec4<T>& vector) const
|
||||||
|
{
|
||||||
|
return Vec4<T>(m11 * vector.x + m21 * vector.y + m31 * vector.z + m41 * vector.w,
|
||||||
|
m12 * vector.x + m22 * vector.y + m32 * vector.z + m42 * vector.w,
|
||||||
|
m13 * vector.x + m23 * vector.y + m33 * vector.z + m43 * vector.w,
|
||||||
|
m14 * vector.x + m24 * vector.y + m34 * vector.z + m44 * vector.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::Transpose()
|
||||||
|
{
|
||||||
|
std::swap(m12, m21);
|
||||||
|
std::swap(m13, m31);
|
||||||
|
std::swap(m14, m41);
|
||||||
|
std::swap(m23, m32);
|
||||||
|
std::swap(m24, m42);
|
||||||
|
std::swap(m34, m43);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T& Mat4<T>::operator()(std::size_t x, std::size_t y)
|
||||||
|
{
|
||||||
|
Assert(x <= 3, "index out of range");
|
||||||
|
Assert(y <= 3, "index out of range");
|
||||||
|
|
||||||
|
return (&m11)[y*4 + x];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const T& Mat4<T>::operator()(std::size_t x, std::size_t y) const
|
||||||
|
{
|
||||||
|
Assert(x <= 3, "index out of range");
|
||||||
|
Assert(y <= 3, "index out of range");
|
||||||
|
|
||||||
|
return (&m11)[y*4+x];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T& Mat4<T>::operator[](std::size_t i)
|
||||||
|
{
|
||||||
|
Assert(i <= 16, "index out of range");
|
||||||
|
|
||||||
|
return (&m11)[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const T& Mat4<T>::operator[](std::size_t i) const
|
||||||
|
{
|
||||||
|
Assert(i <= 16, "index out of range");
|
||||||
|
|
||||||
|
return (&m11)[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::operator*(const Mat4& matrix) const
|
||||||
|
{
|
||||||
|
Mat4 result(*this);
|
||||||
|
return result.Concatenate(matrix);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Mat4<T>::operator*(const Vec2<T>& vector) const
|
||||||
|
{
|
||||||
|
return Transform(vector);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Mat4<T>::operator*(const Vec3<T>& vector) const
|
||||||
|
{
|
||||||
|
return Transform(vector);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Mat4<T>::operator*(const Vec4<T>& vector) const
|
||||||
|
{
|
||||||
|
return Transform(vector);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::operator*(T scalar) const
|
||||||
|
{
|
||||||
|
Mat4 mat;
|
||||||
|
for(unsigned int i = 0; i < 16; ++i)
|
||||||
|
mat[i] = (&m11)[i] * scalar;
|
||||||
|
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::operator*=(const Mat4& matrix)
|
||||||
|
{
|
||||||
|
Concatenate(matrix);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T>& Mat4<T>::operator*=(T scalar)
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < 16; ++i)
|
||||||
|
(&m11)[i] *= scalar;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::operator==(const Mat4& mat) const
|
||||||
|
{
|
||||||
|
for(unsigned int i = 0; i < 16; ++i)
|
||||||
|
if((&m11)[i] != (&mat.m11)[i])
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::operator!=(const Mat4& mat) const
|
||||||
|
{
|
||||||
|
return !operator==(mat);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Mat4<T>::ApproxEqual(const Mat4& lhs, const Mat4& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Concatenate(const Mat4& left, const Mat4& right)
|
||||||
|
{
|
||||||
|
Mat4 matrix(left); // Copy of left-hand side matrix
|
||||||
|
matrix.Concatenate(right); // Concatenation with right-hand side
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::ConcatenateTransform(const Mat4& left, const Mat4& right)
|
||||||
|
{
|
||||||
|
Mat4 matrix(left); // Copy of left-hand side matrix
|
||||||
|
matrix.ConcatenateTransform(right); // Affine concatenation with right-hand side
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Identity()
|
||||||
|
{
|
||||||
|
return Mat4(
|
||||||
|
T(1.0), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(1.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(1.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(0.0), T(1.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::LookAt(const Vec3<T>& eye, const Vec3<T>& target, const Vec3<T>& up)
|
||||||
|
{
|
||||||
|
Vec3<T> f = Vec3<T>::Normalize(target - eye);
|
||||||
|
Vec3<T> s = Vec3<T>::Normalize(f.CrossProduct(up));
|
||||||
|
Vec3<T> u = s.CrossProduct(f);
|
||||||
|
|
||||||
|
return Mat4(
|
||||||
|
s.x, u.x, -f.x, T(0.0),
|
||||||
|
s.y, u.y, -f.y, T(0.0),
|
||||||
|
s.z, u.z, -f.z, T(0.0),
|
||||||
|
-s.DotProduct(eye), -u.DotProduct(eye), f.DotProduct(eye), T(1.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Ortho(T left, T right, T top, T bottom, T zNear, T zFar)
|
||||||
|
{
|
||||||
|
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb204942(v=vs.85).aspx
|
||||||
|
return Mat4(
|
||||||
|
T(2.0) / (right - left), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(2.0) / (top - bottom), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(1.0) / (zNear - zFar), T(0.0),
|
||||||
|
(left + right) / (left - right), (top + bottom) / (bottom - top), zNear / (zNear - zFar), T(1.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Mat4<T> Mat4<T>::Perspective(RadianAngle<T> angle, T ratio, T zNear, T zFar)
|
||||||
|
{
|
||||||
|
angle /= T(2.0);
|
||||||
|
|
||||||
|
T yScale = angle.GetTan();
|
||||||
|
|
||||||
|
return Mat4(
|
||||||
|
T(1.0) / (ratio * yScale), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(-1.0) / (yScale), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), zFar / (zNear - zFar), T(-1.0),
|
||||||
|
T(0.0), T(0.0), -(zNear * zFar) / (zFar - zNear), T(0.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Rotate(const Quat<T>& rotation)
|
||||||
|
{
|
||||||
|
Mat4 matrix = Mat4::Identity();
|
||||||
|
matrix.SetRotation(rotation);
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Scale(const Vec3<T>& scale)
|
||||||
|
{
|
||||||
|
return Mat4(
|
||||||
|
scale.x, T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), scale.y, T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), scale.z, T(0.0),
|
||||||
|
T(0.0), T(0.0), T(0.0), T(1.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Translate(const Vec3<T>& translation)
|
||||||
|
{
|
||||||
|
return Mat4(
|
||||||
|
T(1.0), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(1.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(1.0), T(0.0),
|
||||||
|
translation.x, translation.y, translation.z, T(1.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Transform(const Vec3<T>& translation, const Quat<T>& rotation)
|
||||||
|
{
|
||||||
|
Mat4 mat = Mat4f::Identity();
|
||||||
|
mat.SetRotation(rotation);
|
||||||
|
mat.SetTranslation(translation);
|
||||||
|
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Transform(const Vec3<T>& translation, const Quat<T>& rotation, const Vec3<T>& scale)
|
||||||
|
{
|
||||||
|
Mat4 mat = Transform(translation, rotation);
|
||||||
|
mat.ApplyScale(scale);
|
||||||
|
|
||||||
|
return mat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::TransformInverse(const Vec3<T>& translation, const Quat<T>& rotation)
|
||||||
|
{
|
||||||
|
// A view matrix must apply an inverse transformation of the 'world' matrix
|
||||||
|
Quat<T> invRot = rotation.GetConjugate(); // Inverse of the rotation
|
||||||
|
|
||||||
|
return Transform(-(invRot * translation), invRot);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::TransformInverse(const Vec3<T>& translation, const Quat<T>& rotation, const Vec3<T>& scale)
|
||||||
|
{
|
||||||
|
return TransformInverse(translation, rotation).ApplyScale(T(1.0) / scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> Mat4<T>::Zero()
|
||||||
|
{
|
||||||
|
return Mat4(
|
||||||
|
T(0.0), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(0.0), T(0.0),
|
||||||
|
T(0.0), T(0.0), T(0.0), T(0.0)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, const Mat4<T>& matrix)
|
||||||
|
{
|
||||||
|
return out << "Mat4(" << matrix.m11 << ", " << matrix.m12 << ", " << matrix.m13 << ", " << matrix.m14 << ",\n"
|
||||||
|
<< " " << matrix.m21 << ", " << matrix.m22 << ", " << matrix.m23 << ", " << matrix.m24 << ",\n"
|
||||||
|
<< " " << matrix.m31 << ", " << matrix.m32 << ", " << matrix.m33 << ", " << matrix.m34 << ",\n"
|
||||||
|
<< " " << matrix.m41 << ", " << matrix.m42 << ", " << matrix.m43 << ", " << matrix.m44 << ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Mat4<T> operator*(T scale, const Mat4<T>& matrix)
|
||||||
|
{
|
||||||
|
return matrix * scale;
|
||||||
|
}
|
||||||
|
}
|
||||||
26
runtime/Includes/Maths/MathsUtils.h
git.filemode.normal_file
26
runtime/Includes/Maths/MathsUtils.h
git.filemode.normal_file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef __SCOP_MATHS_UTILS__
|
||||||
|
#define __SCOP_MATHS_UTILS__
|
||||||
|
|
||||||
|
#include <concepts>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]] constexpr T Mod(T x, T y) noexcept;
|
||||||
|
|
||||||
|
template<std::floating_point T>
|
||||||
|
[[nodiscard]] constexpr T DegreeToRadian(T degrees) noexcept;
|
||||||
|
|
||||||
|
template<std::floating_point T>
|
||||||
|
[[nodiscard]] constexpr T RadianToDegree(T radians) noexcept;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]] constexpr T Clamp(T value, T min, T max) noexcept;
|
||||||
|
|
||||||
|
template<typename T, typename T2>
|
||||||
|
[[nodiscard]] constexpr T Lerp(const T& from, const T& to, const T2& interpolation) noexcept;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/MathsUtils.inl>
|
||||||
|
|
||||||
|
#endif
|
||||||
47
runtime/Includes/Maths/MathsUtils.inl
git.filemode.normal_file
47
runtime/Includes/Maths/MathsUtils.inl
git.filemode.normal_file
@@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Maths/MathsUtils.h>
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include <Maths/Constants.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]] constexpr T Mod(T x, T y) noexcept
|
||||||
|
{
|
||||||
|
if constexpr(std::is_floating_point_v<T>)
|
||||||
|
{
|
||||||
|
if(!std::is_constant_evaluated())
|
||||||
|
return x - static_cast<long long>(x / y) * y;
|
||||||
|
else
|
||||||
|
return std::fmod(x, y);
|
||||||
|
}
|
||||||
|
return x % y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::floating_point T>
|
||||||
|
[[nodiscard]] constexpr T DegreeToRadian(T degrees) noexcept
|
||||||
|
{
|
||||||
|
return degrees * (Pi<T>() / T(180.0));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<std::floating_point T>
|
||||||
|
[[nodiscard]] constexpr T RadianToDegree(T radians) noexcept
|
||||||
|
{
|
||||||
|
return radians * (T(180.0) / Pi<T>());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]] constexpr T Clamp(T value, T min, T max) noexcept
|
||||||
|
{
|
||||||
|
return std::max(std::min(value, max), min);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename T2>
|
||||||
|
[[nodiscard]] constexpr T Lerp(const T& from, const T& to, const T2& interpolation) noexcept
|
||||||
|
{
|
||||||
|
return static_cast<T>(from + interpolation * (to - from));
|
||||||
|
}
|
||||||
|
}
|
||||||
91
runtime/Includes/Maths/Quaternions.h
git.filemode.normal_file
91
runtime/Includes/Maths/Quaternions.h
git.filemode.normal_file
@@ -0,0 +1,91 @@
|
|||||||
|
#ifndef __SCOP_QUATERNIONS__
|
||||||
|
#define __SCOP_QUATERNIONS__
|
||||||
|
|
||||||
|
#include <Maths/Angles.h>
|
||||||
|
#include <Maths/Vec3.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
struct Quat
|
||||||
|
{
|
||||||
|
T w, x, y, z;
|
||||||
|
|
||||||
|
constexpr Quat() = default;
|
||||||
|
constexpr Quat(T W, T X, T Y, T Z);
|
||||||
|
template<AngleUnit Unit> Quat(const Angle<Unit, T>& angle);
|
||||||
|
Quat(const EulerAngles<T>& angles);
|
||||||
|
constexpr Quat(RadianAngle<T> angle, const Vec3<T>& axis);
|
||||||
|
constexpr Quat(const T quat[4]);
|
||||||
|
template<typename U> constexpr explicit Quat(const Quat<U>& quat);
|
||||||
|
constexpr Quat(const Quat&) = default;
|
||||||
|
constexpr Quat(Quat&&) = default;
|
||||||
|
~Quat() = default;
|
||||||
|
|
||||||
|
RadianAngle<T> AngleBetween(const Quat& vec) const;
|
||||||
|
constexpr bool ApproxEqual(const Quat& quat, T maxDifference = std::numeric_limits<T>::epsilon()) const;
|
||||||
|
|
||||||
|
Quat& ComputeW();
|
||||||
|
constexpr Quat& Conjugate();
|
||||||
|
|
||||||
|
constexpr T DotProduct(const Quat& vec) const;
|
||||||
|
|
||||||
|
constexpr Quat GetConjugate() const;
|
||||||
|
Quat GetInverse() const;
|
||||||
|
Quat GetNormal(T* length = nullptr) const;
|
||||||
|
|
||||||
|
Quat& Inverse();
|
||||||
|
|
||||||
|
T Magnitude() const;
|
||||||
|
|
||||||
|
Quat& Normalize(T* length = nullptr);
|
||||||
|
|
||||||
|
constexpr T SquaredMagnitude() const;
|
||||||
|
|
||||||
|
RadianAngle<T> To2DAngle() const;
|
||||||
|
EulerAngles<T> ToEulerAngles() const;
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
constexpr Quat& operator=(const Quat& quat) = default;
|
||||||
|
constexpr Quat& operator=(Quat&&) = default;
|
||||||
|
|
||||||
|
constexpr Quat operator+(const Quat& quat) const;
|
||||||
|
constexpr Quat operator*(const Quat& quat) const;
|
||||||
|
constexpr Vec3<T> operator*(const Vec3<T>& vec) const;
|
||||||
|
constexpr Quat operator*(T scale) const;
|
||||||
|
constexpr Quat operator/(const Quat& quat) const;
|
||||||
|
|
||||||
|
constexpr Quat& operator+=(const Quat& quat);
|
||||||
|
constexpr Quat& operator*=(const Quat& quat);
|
||||||
|
constexpr Quat& operator*=(T scale);
|
||||||
|
constexpr Quat& operator/=(const Quat& quat);
|
||||||
|
|
||||||
|
constexpr bool operator==(const Quat& quat) const;
|
||||||
|
constexpr bool operator!=(const Quat& quat) const;
|
||||||
|
constexpr bool operator<(const Quat& quat) const;
|
||||||
|
constexpr bool operator<=(const Quat& quat) const;
|
||||||
|
constexpr bool operator>(const Quat& quat) const;
|
||||||
|
constexpr bool operator>=(const Quat& quat) const;
|
||||||
|
|
||||||
|
static RadianAngle<T> AngleBetween(const Quat& lhs, const Quat& rhs);
|
||||||
|
static constexpr bool ApproxEqual(const Quat& lhs, const Quat& rhs, T maxDifference = std::numeric_limits<T>::epsilon());
|
||||||
|
static constexpr Quat Identity();
|
||||||
|
static constexpr Quat Lerp(const Quat& from, const Quat& to, T interpolation);
|
||||||
|
static Quat LookAt(const Vec3<T>& forward, const Vec3<T>& up);
|
||||||
|
static Quat Normalize(const Quat& quat, T* length = nullptr);
|
||||||
|
static Quat RotationBetween(const Vec3<T>& from, const Vec3<T>& to);
|
||||||
|
static Quat RotateTowards(const Quat& from, const Quat& to, RadianAngle<T> maxRotation);
|
||||||
|
static Quat Mirror(Quat quat, const Vec3<T>& axis);
|
||||||
|
static Quat Slerp(const Quat& from, const Quat& to, T interpolation);
|
||||||
|
static constexpr Quat Zero();
|
||||||
|
};
|
||||||
|
|
||||||
|
using Quatd = Quat<double>;
|
||||||
|
using Quatf = Quat<float>;
|
||||||
|
|
||||||
|
template<typename T> std::ostream& operator<<(std::ostream& out, const Quat<T>& quat);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/Quaternions.inl>
|
||||||
|
|
||||||
|
#endif
|
||||||
508
runtime/Includes/Maths/Quaternions.inl
git.filemode.normal_file
508
runtime/Includes/Maths/Quaternions.inl
git.filemode.normal_file
@@ -0,0 +1,508 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Maths/Quaternions.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>::Quat(T W, T X, T Y, T Z) : w(W), x(X), y(Y), z(Z)
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<AngleUnit Unit>
|
||||||
|
Quat<T>::Quat(const Angle<Unit, T>& angle) : Quat(angle.ToQuat())
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T>::Quat(const EulerAngles<T>& angles) : Quat(angles.ToQuat())
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>::Quat(RadianAngle<T> angle, const Vec3<T>& axis)
|
||||||
|
{
|
||||||
|
angle /= T(2.0);
|
||||||
|
|
||||||
|
Vec3<T> normalizedAxis = axis.GetNormal();
|
||||||
|
|
||||||
|
auto sincos = angle.GetSinCos();
|
||||||
|
|
||||||
|
w = sincos.second;
|
||||||
|
x = normalizedAxis.x * sincos.first;
|
||||||
|
y = normalizedAxis.y * sincos.first;
|
||||||
|
z = normalizedAxis.z * sincos.first;
|
||||||
|
|
||||||
|
Normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>::Quat(const T quat[4]) : w(quat[0]), x(quat[1]), y(quat[2]), z(quat[3])
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
constexpr Quat<T>::Quat(const Quat<U>& quat) : w(static_cast<T>(quat.w)), x(static_cast<T>(quat.x)), y(static_cast<T>(quat.y)), z(static_cast<T>(quat.z))
|
||||||
|
{}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
RadianAngle<T> Quat<T>::AngleBetween(const Quat& quat) const
|
||||||
|
{
|
||||||
|
T alpha = Vec3<T>::DotProduct(Vec3<T>(x, y, z), Vec3<T>(quat.x, quat.y, quat.z));
|
||||||
|
return std::acos(Scop::Clamp(alpha, T(-1.0), T(1.0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::ApproxEqual(const Quat& quat, T maxDifference) const
|
||||||
|
{
|
||||||
|
return NumberEquals(w, quat.w, maxDifference) &&
|
||||||
|
NumberEquals(x, quat.x, maxDifference) &&
|
||||||
|
NumberEquals(y, quat.y, maxDifference) &&
|
||||||
|
NumberEquals(z, quat.z, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T>& Quat<T>::ComputeW()
|
||||||
|
{
|
||||||
|
T t = T(1.0) - SquaredMagnitude();
|
||||||
|
|
||||||
|
if(t < T(0.0))
|
||||||
|
w = T(0.0);
|
||||||
|
else
|
||||||
|
w = -std::sqrt(t);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>& Quat<T>::Conjugate()
|
||||||
|
{
|
||||||
|
x = -x;
|
||||||
|
y = -y;
|
||||||
|
z = -z;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Quat<T>::DotProduct(const Quat& quat) const
|
||||||
|
{
|
||||||
|
return w * quat.w + x * quat.x + y * quat.y + z * quat.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::GetConjugate() const
|
||||||
|
{
|
||||||
|
Quat<T> quat(*this);
|
||||||
|
quat.Conjugate();
|
||||||
|
return quat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::GetInverse() const
|
||||||
|
{
|
||||||
|
Quat<T> quat(*this);
|
||||||
|
quat.Inverse();
|
||||||
|
return quat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::GetNormal(T* length) const
|
||||||
|
{
|
||||||
|
Quat<T> quat(*this);
|
||||||
|
quat.Normalize(length);
|
||||||
|
return quat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T>& Quat<T>::Inverse()
|
||||||
|
{
|
||||||
|
T norm = SquaredMagnitude();
|
||||||
|
if(norm > T(0.0))
|
||||||
|
{
|
||||||
|
T invNorm = T(1.0) / std::sqrt(norm);
|
||||||
|
|
||||||
|
w *= invNorm;
|
||||||
|
x *= -invNorm;
|
||||||
|
y *= -invNorm;
|
||||||
|
z *= -invNorm;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T Quat<T>::Magnitude() const
|
||||||
|
{
|
||||||
|
return std::sqrt(SquaredMagnitude());
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T>& Quat<T>::Normalize(T* length)
|
||||||
|
{
|
||||||
|
T norm = std::sqrt(SquaredMagnitude());
|
||||||
|
if(norm > T(0.0))
|
||||||
|
{
|
||||||
|
T invNorm = T(1.0) / norm;
|
||||||
|
w *= invNorm;
|
||||||
|
x *= invNorm;
|
||||||
|
y *= invNorm;
|
||||||
|
z *= invNorm;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(length)
|
||||||
|
*length = norm;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Quat<T>::SquaredMagnitude() const
|
||||||
|
{
|
||||||
|
return w * w + x * x + y * y + z * z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
RadianAngle<T> Quat<T>::To2DAngle() const
|
||||||
|
{
|
||||||
|
T siny_cosp = T(2.0) * (w * z + x * y);
|
||||||
|
T cosy_cosp = T(1.0) - T(2.0) * (y * y + z * z);
|
||||||
|
|
||||||
|
return std::atan2(siny_cosp, cosy_cosp);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
EulerAngles<T> Quat<T>::ToEulerAngles() const
|
||||||
|
{
|
||||||
|
T test = x * y + z * w;
|
||||||
|
if(test > T(0.499))
|
||||||
|
// singularity at north pole
|
||||||
|
return EulerAngles<T>(DegreeAngle<T>(T(0.0)), RadianAngle<T>(T(2.0) * std::atan2(x, w)), DegreeAngle<T>(T(90.0)));
|
||||||
|
|
||||||
|
if(test < T(-0.499))
|
||||||
|
// singularity at south pole
|
||||||
|
return EulerAngles<T>(DegreeAngle<T>(T(0.0)), RadianAngle<T>(T(-2.0) * std::atan2(x, w)), DegreeAngle<T>(T(-90.0)));
|
||||||
|
|
||||||
|
return EulerAngles<T>(RadianAngle<T>(std::atan2(T(2.0) * x * w - T(2.0) * y * z, T(1.0) - T(2.0) * x * x - T(2.0) * z * z)),
|
||||||
|
RadianAngle<T>(std::atan2(T(2.0) * y * w - T(2.0) * x * z, T(1.0) - T(2.0) * y * y - T(2.0) * z * z)),
|
||||||
|
RadianAngle<T>(std::asin(T(2.0) * test)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string Quat<T>::ToString() const
|
||||||
|
{
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *this;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::operator+(const Quat& quat) const
|
||||||
|
{
|
||||||
|
Quat result;
|
||||||
|
result.w = w + quat.w;
|
||||||
|
result.x = x + quat.x;
|
||||||
|
result.y = y + quat.y;
|
||||||
|
result.z = z + quat.z;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::operator*(const Quat& quat) const
|
||||||
|
{
|
||||||
|
Quat result;
|
||||||
|
result.w = w * quat.w - x * quat.x - y * quat.y - z * quat.z;
|
||||||
|
result.x = w * quat.x + x * quat.w + y * quat.z - z * quat.y;
|
||||||
|
result.y = w * quat.y + y * quat.w + z * quat.x - x * quat.z;
|
||||||
|
result.z = w * quat.z + z * quat.w + x * quat.y - y * quat.x;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Quat<T>::operator*(const Vec3<T>& vec) const
|
||||||
|
{
|
||||||
|
Vec3<T> quatVec(x, y, z);
|
||||||
|
Vec3<T> uv = quatVec.CrossProduct(vec);
|
||||||
|
Vec3<T> uuv = quatVec.CrossProduct(uv);
|
||||||
|
uv *= T(2.0) * w;
|
||||||
|
uuv *= T(2.0);
|
||||||
|
|
||||||
|
return vec + uv + uuv;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::operator*(T scale) const
|
||||||
|
{
|
||||||
|
return Quat(w * scale,
|
||||||
|
x * scale,
|
||||||
|
y * scale,
|
||||||
|
z * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::operator/(const Quat& quat) const
|
||||||
|
{
|
||||||
|
return quat.GetConjugate() * (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>& Quat<T>::operator+=(const Quat& quat)
|
||||||
|
{
|
||||||
|
return operator=(operator+(quat));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>& Quat<T>::operator*=(const Quat& quat)
|
||||||
|
{
|
||||||
|
return operator=(operator*(quat));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>& Quat<T>::operator*=(T scale)
|
||||||
|
{
|
||||||
|
return operator=(operator*(scale));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T>& Quat<T>::operator/=(const Quat& quat)
|
||||||
|
{
|
||||||
|
return operator=(operator/(quat));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::operator==(const Quat& quat) const
|
||||||
|
{
|
||||||
|
return w == quat.w && x == quat.x && y == quat.y && z == quat.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::operator!=(const Quat& quat) const
|
||||||
|
{
|
||||||
|
return !operator==(quat);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::operator<(const Quat& quat) const
|
||||||
|
{
|
||||||
|
if(w != quat.w)
|
||||||
|
return w < quat.w;
|
||||||
|
|
||||||
|
if(x != quat.x)
|
||||||
|
return x < quat.x;
|
||||||
|
|
||||||
|
if(y != quat.y)
|
||||||
|
return y < quat.y;
|
||||||
|
|
||||||
|
if(z != quat.z)
|
||||||
|
return z < quat.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::operator<=(const Quat& quat) const
|
||||||
|
{
|
||||||
|
if(w != quat.w)
|
||||||
|
return w < quat.w;
|
||||||
|
|
||||||
|
if(x != quat.x)
|
||||||
|
return x < quat.x;
|
||||||
|
|
||||||
|
if(y != quat.y)
|
||||||
|
return y < quat.y;
|
||||||
|
|
||||||
|
if(z != quat.z)
|
||||||
|
return z <= quat.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::operator>(const Quat& quat) const
|
||||||
|
{
|
||||||
|
if(w != quat.w)
|
||||||
|
return w > quat.w;
|
||||||
|
|
||||||
|
if(x != quat.x)
|
||||||
|
return x > quat.x;
|
||||||
|
|
||||||
|
if(y != quat.y)
|
||||||
|
return y > quat.y;
|
||||||
|
|
||||||
|
if(z != quat.z)
|
||||||
|
return z > quat.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::operator>=(const Quat& quat) const
|
||||||
|
{
|
||||||
|
if(w != quat.w)
|
||||||
|
return w > quat.w;
|
||||||
|
|
||||||
|
if(x != quat.x)
|
||||||
|
return x > quat.x;
|
||||||
|
|
||||||
|
if(y != quat.y)
|
||||||
|
return y > quat.y;
|
||||||
|
|
||||||
|
if(z != quat.z)
|
||||||
|
return z >= quat.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
RadianAngle<T> Quat<T>::AngleBetween(const Quat& lhs, const Quat& rhs)
|
||||||
|
{
|
||||||
|
return lhs.AngleBetween(rhs);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Quat<T>::ApproxEqual(const Quat& lhs, const Quat& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::Identity()
|
||||||
|
{
|
||||||
|
return Quat(1, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::Lerp(const Quat& from, const Quat& to, T interpolation)
|
||||||
|
{
|
||||||
|
Quat interpolated;
|
||||||
|
interpolated.w = Scop::Lerp(from.w, to.w, interpolation);
|
||||||
|
interpolated.x = Scop::Lerp(from.x, to.x, interpolation);
|
||||||
|
interpolated.y = Scop::Lerp(from.y, to.y, interpolation);
|
||||||
|
interpolated.z = Scop::Lerp(from.z, to.z, interpolation);
|
||||||
|
|
||||||
|
return interpolated;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::LookAt(const Vec3<T>& forward, const Vec3<T>& up)
|
||||||
|
{
|
||||||
|
// From https://gamedev.stackexchange.com/questions/53129/quaternion-look-at-with-up-vector
|
||||||
|
Vec3<T> forward_w = Vec3<T>::Forward();
|
||||||
|
Vec3<T> axis = Vec3<T>::CrossProduct(forward, forward_w);
|
||||||
|
RadianAngle<T> angle = std::acos(Vec3<T>::DotProduct(forward, forward_w));
|
||||||
|
|
||||||
|
Vec3<T> third = Vec3<T>::CrossProduct(axis, forward_w);
|
||||||
|
if(Vec3<T>::DotProduct(third, forward) < 0)
|
||||||
|
angle = -angle;
|
||||||
|
|
||||||
|
Quat<T> q1 = Quat(angle, axis);
|
||||||
|
|
||||||
|
Vec3<T> up_l = q1 * up;
|
||||||
|
Vec3<T> right = Vec3<T>::Normalize(Vec3<T>::CrossProduct(forward, up));
|
||||||
|
Vec3<T> up_w = Vec3<T>::Normalize(Vec3<T>::CrossProduct(right, forward));
|
||||||
|
|
||||||
|
Vec3<T> axis2 = Vec3<T>::CrossProduct(up_l, up_w);
|
||||||
|
RadianAngle<T> angle2 = std::acos(Vec3<T>::DotProduct(forward, forward_w));
|
||||||
|
|
||||||
|
Quat<T> q2 = Quat(angle2, axis2);
|
||||||
|
|
||||||
|
return q2 * q1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::Normalize(const Quat& quat, T* length)
|
||||||
|
{
|
||||||
|
return quat.GetNormal(length);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::RotationBetween(const Vec3<T>& from, const Vec3<T>& to)
|
||||||
|
{
|
||||||
|
T dot = from.DotProduct(to);
|
||||||
|
if(dot < T(-0.999999))
|
||||||
|
{
|
||||||
|
Vec3<T> crossProduct;
|
||||||
|
if(from.DotProduct(Vec3<T>::UnitX()) < T(0.999999))
|
||||||
|
crossProduct = Vec3<T>::UnitX().CrossProduct(from);
|
||||||
|
else
|
||||||
|
crossProduct = Vec3<T>::UnitY().CrossProduct(from);
|
||||||
|
|
||||||
|
crossProduct.Normalize();
|
||||||
|
return Quat(Pi<T>(), crossProduct);
|
||||||
|
}
|
||||||
|
else if(dot > T(0.999999))
|
||||||
|
return Quat::Identity();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
T norm = std::sqrt(from.GetSquaredLength() * to.GetSquaredLength());
|
||||||
|
Vec3<T> crossProduct = from.CrossProduct(to);
|
||||||
|
|
||||||
|
return Quat(norm + dot, crossProduct.x, crossProduct.y, crossProduct.z).GetNormal();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::RotateTowards(const Quat& from, const Quat& to, RadianAngle<T> maxRotation)
|
||||||
|
{
|
||||||
|
RadianAngle<T> rotationBetween = AngleBetween(from, to);
|
||||||
|
if(rotationBetween < maxRotation)
|
||||||
|
return to;
|
||||||
|
|
||||||
|
return Slerp(from, to, std::min(maxRotation.value / rotationBetween.value), 1.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::Mirror(Quat quat, const Vec3<T>& axis)
|
||||||
|
{
|
||||||
|
T x = std::copysign(T(1.0), axis.x);
|
||||||
|
T y = std::copysign(T(1.0), axis.y);
|
||||||
|
T z = std::copysign(T(1.0), axis.z);
|
||||||
|
|
||||||
|
quat.x = y * z * quat.x;
|
||||||
|
quat.y = x * z * quat.y;
|
||||||
|
quat.z = x * y * quat.z;
|
||||||
|
|
||||||
|
return quat;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Quat<T> Quat<T>::Slerp(const Quat& from, const Quat& to, T interpolation)
|
||||||
|
{
|
||||||
|
Quat q;
|
||||||
|
|
||||||
|
T cosOmega = from.DotProduct(to);
|
||||||
|
if(cosOmega < T(0.0))
|
||||||
|
{
|
||||||
|
// We invert everything
|
||||||
|
q = Quat(-to.w, -to.x, -to.y, -to.z);
|
||||||
|
cosOmega = -cosOmega;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
q = Quat(to);
|
||||||
|
|
||||||
|
T k0, k1;
|
||||||
|
if(cosOmega > T(0.9999))
|
||||||
|
{
|
||||||
|
// Linear interpolation to avoid division by zero
|
||||||
|
k0 = T(1.0) - interpolation;
|
||||||
|
k1 = interpolation;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
T sinOmega = std::sqrt(T(1.0) - cosOmega*cosOmega);
|
||||||
|
T omega = std::atan2(sinOmega, cosOmega);
|
||||||
|
|
||||||
|
// To avoid two divisions
|
||||||
|
sinOmega = T(1.0)/sinOmega;
|
||||||
|
|
||||||
|
k0 = std::sin((T(1.0) - interpolation) * omega) * sinOmega;
|
||||||
|
k1 = std::sin(interpolation*omega) * sinOmega;
|
||||||
|
}
|
||||||
|
|
||||||
|
Quat result(k0 * from.w, k0 * from.x, k0 * from.y, k0 * from.z);
|
||||||
|
return result += q * k1;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Quat<T> Quat<T>::Zero()
|
||||||
|
{
|
||||||
|
return Quat(0, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, const Quat<T>& quat)
|
||||||
|
{
|
||||||
|
return out << "Quat(" << quat.w << " | " << quat.x << ", " << quat.y << ", " << quat.z << ')';
|
||||||
|
}
|
||||||
|
}
|
||||||
116
runtime/Includes/Maths/Vec2.h
git.filemode.executable_file
116
runtime/Includes/Maths/Vec2.h
git.filemode.executable_file
@@ -0,0 +1,116 @@
|
|||||||
|
#ifndef __SCOP_VEC2__
|
||||||
|
#define __SCOP_VEC2__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <limits>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include <Core/Logs.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template <typename T> class Vec3;
|
||||||
|
template <typename T> class Vec4;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct Vec2
|
||||||
|
{
|
||||||
|
union { T x, r, s; };
|
||||||
|
union { T y, g, t; };
|
||||||
|
|
||||||
|
constexpr Vec2() = default;
|
||||||
|
constexpr Vec2(T X, T Y);
|
||||||
|
constexpr explicit Vec2(T scale);
|
||||||
|
template<typename U> constexpr explicit Vec2(const Vec2<U>& vec);
|
||||||
|
constexpr Vec2(const Vec2&) = default;
|
||||||
|
constexpr Vec2(Vec2&&) = default;
|
||||||
|
constexpr explicit Vec2(const Vec3<T>& vec);
|
||||||
|
constexpr explicit Vec2(const Vec4<T>& vec);
|
||||||
|
|
||||||
|
T AbsDotProduct(const Vec2& vec) const;
|
||||||
|
constexpr bool ApproxEqual(const Vec2& vec, T max_difference = std::numeric_limits<T>::epsilon()) const;
|
||||||
|
|
||||||
|
template<typename U = T> U Distance(const Vec2& vec) const;
|
||||||
|
constexpr T DotProduct(const Vec2& vec) const;
|
||||||
|
|
||||||
|
template<typename U = T> T GetLength() const;
|
||||||
|
Vec2 GetNormal(T* length = nullptr) const;
|
||||||
|
constexpr T GetSquaredLength() const;
|
||||||
|
|
||||||
|
constexpr Vec2& Maximize(const Vec2& vec);
|
||||||
|
constexpr Vec2& Minimize(const Vec2& vec);
|
||||||
|
|
||||||
|
Vec2& Normalize(T* length = nullptr);
|
||||||
|
|
||||||
|
constexpr T SquaredDistance(const Vec2& vec) const;
|
||||||
|
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
constexpr T& operator[](std::size_t i);
|
||||||
|
constexpr T operator[](std::size_t i) const;
|
||||||
|
|
||||||
|
constexpr const Vec2& operator+() const;
|
||||||
|
constexpr Vec2 operator-() const;
|
||||||
|
|
||||||
|
constexpr Vec2 operator+(const Vec2& vec) const;
|
||||||
|
constexpr Vec2 operator-(const Vec2& vec) const;
|
||||||
|
constexpr Vec2 operator*(const Vec2& vec) const;
|
||||||
|
constexpr Vec2 operator*(T scale) const;
|
||||||
|
constexpr Vec2 operator/(const Vec2& vec) const;
|
||||||
|
constexpr Vec2 operator/(T scale) const;
|
||||||
|
constexpr Vec2 operator%(const Vec2& vec) const;
|
||||||
|
constexpr Vec2 operator%(T mod) const;
|
||||||
|
|
||||||
|
constexpr Vec2& operator=(const Vec2&) = default;
|
||||||
|
constexpr Vec2& operator=(Vec2&&) = default;
|
||||||
|
|
||||||
|
constexpr Vec2& operator+=(const Vec2& vec);
|
||||||
|
constexpr Vec2& operator-=(const Vec2& vec);
|
||||||
|
constexpr Vec2& operator*=(const Vec2& vec);
|
||||||
|
constexpr Vec2& operator*=(T scale);
|
||||||
|
constexpr Vec2& operator/=(const Vec2& vec);
|
||||||
|
constexpr Vec2& operator/=(T scale);
|
||||||
|
constexpr Vec2& operator%=(const Vec2& vec);
|
||||||
|
constexpr Vec2& operator%=(T mod);
|
||||||
|
|
||||||
|
constexpr bool operator==(const Vec2& vec) const;
|
||||||
|
constexpr bool operator!=(const Vec2& vec) const;
|
||||||
|
constexpr bool operator<(const Vec2& vec) const;
|
||||||
|
constexpr bool operator<=(const Vec2& vec) const;
|
||||||
|
constexpr bool operator>(const Vec2& vec) const;
|
||||||
|
constexpr bool operator>=(const Vec2& vec) const;
|
||||||
|
|
||||||
|
static constexpr Vec2 Apply(T(*func)(T), const Vec2& vec);
|
||||||
|
static constexpr bool ApproxEqual(const Vec2& lhs, const Vec2& rhs, T max_difference = std::numeric_limits<T>::epsilon());
|
||||||
|
template<typename U = T> static U Distance(const Vec2& vec1, const Vec2& vec2);
|
||||||
|
static constexpr T DotProduct(const Vec2& vec1, const Vec2& vec2);
|
||||||
|
static constexpr Vec2 Lerp(const Vec2& from, const Vec2& to, T interpolation);
|
||||||
|
static Vec2 Normalize(const Vec2& vec);
|
||||||
|
static constexpr Vec2 Unit();
|
||||||
|
static constexpr Vec2 UnitX();
|
||||||
|
static constexpr Vec2 UnitY();
|
||||||
|
static constexpr Vec2 Zero();
|
||||||
|
|
||||||
|
~Vec2() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Vec2d = Vec2<double>;
|
||||||
|
using Vec2f = Vec2<float>;
|
||||||
|
using Vec2i = Vec2<int>;
|
||||||
|
using Vec2ui = Vec2<unsigned int>;
|
||||||
|
using Vec2i32 = Vec2<std::int32_t>;
|
||||||
|
using Vec2i64 = Vec2<std::int64_t>;
|
||||||
|
using Vec2ui32 = Vec2<std::uint32_t>;
|
||||||
|
using Vec2ui64 = Vec2<std::uint64_t>;
|
||||||
|
|
||||||
|
template<typename T> std::ostream& operator<<(std::ostream& out, const Vec2<T>& vec);
|
||||||
|
|
||||||
|
template<typename T> constexpr Vec2<T> operator*(T scale, const Vec2<T>& vec);
|
||||||
|
template<typename T> constexpr Vec2<T> operator/(T scale, const Vec2<T>& vec);
|
||||||
|
template<typename T> constexpr Vec2<T> operator%(T mod, const Vec2<T>& vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/Vec2.inl>
|
||||||
|
|
||||||
|
#endif // __AK_VEC2__
|
||||||
388
runtime/Includes/Maths/Vec2.inl
git.filemode.executable_file
388
runtime/Includes/Maths/Vec2.inl
git.filemode.executable_file
@@ -0,0 +1,388 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Maths/Vec2.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>::Vec2(T X, T Y) : x(X), y(Y) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>::Vec2(T scale) : x(scale), y(scale) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
constexpr Vec2<T>::Vec2(const Vec2<U>& vec) : x(static_cast<T>(vec.x)), y(static_cast<T>(vec.y)) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>::Vec2(const Vec3<T>& vec) : x(vec.x), y(vec.y) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>::Vec2(const Vec4<T>& vec) : x(vec.x), y(vec.y) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T Vec2<T>::AbsDotProduct(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return std::abs(x * vec.x) + std::abs(y * vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::ApproxEqual(const Vec2& vec, T maxDifference) const
|
||||||
|
{
|
||||||
|
return NumberEquals(x, vec.x, maxDifference) && NumberEquals(y, vec.y, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
U Vec2<T>::Distance(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return static_cast<U>(std::sqrt(SquaredDistance(vec)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec2<T>::DotProduct(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return x * vec.x + y * vec.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
T Vec2<T>::GetLength() const
|
||||||
|
{
|
||||||
|
return static_cast<U>(std::sqrt(static_cast<U>(GetSquaredLength())));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec2<T> Vec2<T>::GetNormal(T* length) const
|
||||||
|
{
|
||||||
|
Vec2 vec(*this);
|
||||||
|
vec.Normalize(length);
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec2<T>::GetSquaredLength() const
|
||||||
|
{
|
||||||
|
return x * x + y * y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::Maximize(const Vec2& vec)
|
||||||
|
{
|
||||||
|
if(vec.x > x)
|
||||||
|
x = vec.x;
|
||||||
|
if(vec.y > y)
|
||||||
|
y = vec.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::Minimize(const Vec2& vec)
|
||||||
|
{
|
||||||
|
if(vec.x < x)
|
||||||
|
x = vec.x;
|
||||||
|
if(vec.y < y)
|
||||||
|
y = vec.y;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec2<T>& Vec2<T>::Normalize(T* length)
|
||||||
|
{
|
||||||
|
T norm = GetLength();
|
||||||
|
if(norm > T(0.0))
|
||||||
|
{
|
||||||
|
T invNorm = T(1.0) / norm;
|
||||||
|
x *= invNorm;
|
||||||
|
y *= invNorm;
|
||||||
|
}
|
||||||
|
if(length)
|
||||||
|
*length = norm;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec2<T>::SquaredDistance(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return (*this - vec).GetSquaredLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string Vec2<T>::ToString() const
|
||||||
|
{
|
||||||
|
return "Vec2(" + std::to_string(x) + ", " + std::to_string(y) + ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T& Vec2<T>::operator[](std::size_t i)
|
||||||
|
{
|
||||||
|
Scop::Assert(i < 2, "index out of range");
|
||||||
|
return *(&x + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec2<T>::operator[](std::size_t i) const
|
||||||
|
{
|
||||||
|
Scop::Assert(i < 2, "index out of range");
|
||||||
|
return *(&x + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const Vec2<T>& Vec2<T>::operator+() const
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator-() const
|
||||||
|
{
|
||||||
|
return Vec2(-x, -y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator+(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return Vec2(x + vec.x, y + vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator-(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return Vec2(x - vec.x, y - vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator*(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return Vec2(x * vec.x, y * vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator*(T scale) const
|
||||||
|
{
|
||||||
|
return Vec2(x * scale, y * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator/(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return Vec2(x / vec.x, y / vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator/(T scale) const
|
||||||
|
{
|
||||||
|
return Vec2(x / scale, y / scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator%(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return Vec2(Mod(x, vec.x), Mod(y, vec.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::operator%(T mod) const
|
||||||
|
{
|
||||||
|
return Vec2(Mod(x, mod), Mod(y, mod));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator+=(const Vec2& vec)
|
||||||
|
{
|
||||||
|
x += vec.x;
|
||||||
|
y += vec.y;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator-=(const Vec2& vec)
|
||||||
|
{
|
||||||
|
x -= vec.x;
|
||||||
|
y -= vec.y;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator*=(const Vec2& vec)
|
||||||
|
{
|
||||||
|
x *= vec.x;
|
||||||
|
y *= vec.y;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator*=(T scale)
|
||||||
|
{
|
||||||
|
x *= scale;
|
||||||
|
y *= scale;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator/=(const Vec2& vec)
|
||||||
|
{
|
||||||
|
x /= vec.x;
|
||||||
|
y /= vec.y;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator/=(T scale)
|
||||||
|
{
|
||||||
|
x /= scale;
|
||||||
|
y /= scale;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator%=(const Vec2& vec)
|
||||||
|
{
|
||||||
|
x = Mod(x, vec.x);
|
||||||
|
y = Mod(y, vec.y);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T>& Vec2<T>::operator%=(T value)
|
||||||
|
{
|
||||||
|
x = Mod(x, value);
|
||||||
|
y = Mod(y, value);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::operator==(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return x == vec.x && y == vec.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::operator!=(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
return !operator==(vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::operator<(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x < vec.x;
|
||||||
|
|
||||||
|
return y < vec.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::operator<=(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x < vec.x;
|
||||||
|
|
||||||
|
return y <= vec.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::operator>(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x > vec.x;
|
||||||
|
|
||||||
|
return y > vec.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::operator>=(const Vec2& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x > vec.x;
|
||||||
|
|
||||||
|
return y >= vec.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::Apply(T(*func)(T), const Vec2& vec)
|
||||||
|
{
|
||||||
|
return Vec2(func(vec.x), func(vec.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec2<T>::ApproxEqual(const Vec2& lhs, const Vec2& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
U Vec2<T>::Distance(const Vec2& vec1, const Vec2& vec2)
|
||||||
|
{
|
||||||
|
return vec1.Distance<U>(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec2<T>::DotProduct(const Vec2& vec1, const Vec2& vec2)
|
||||||
|
{
|
||||||
|
return vec1.DotProduct(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec2<T> Vec2<T>::Normalize(const Vec2& vec)
|
||||||
|
{
|
||||||
|
return vec.GetNormal();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::Unit()
|
||||||
|
{
|
||||||
|
return Vec2(1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::UnitX()
|
||||||
|
{
|
||||||
|
return Vec2(1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::UnitY()
|
||||||
|
{
|
||||||
|
return Vec2(0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> Vec2<T>::Zero()
|
||||||
|
{
|
||||||
|
return Vec2(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, const Vec2<T>& vec)
|
||||||
|
{
|
||||||
|
return out << "Vec2(" << vec.x << ", " << vec.y << ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> operator*(T scale, const Vec2<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec2<T>(scale * vec.x, scale * vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> operator/(T scale, const Vec2<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec2<T>(scale / vec.x, scale / vec.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec2<T> operator%(T mod, const Vec2<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec2<T>(Mod(mod, vec.x), Mod(mod, vec.y));
|
||||||
|
}
|
||||||
|
}
|
||||||
133
runtime/Includes/Maths/Vec3.h
git.filemode.executable_file
133
runtime/Includes/Maths/Vec3.h
git.filemode.executable_file
@@ -0,0 +1,133 @@
|
|||||||
|
#ifndef __SCOP_VEC3__
|
||||||
|
#define __SCOP_VEC3__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <limits>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include <Core/Logs.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T> class Vec2;
|
||||||
|
template<typename T> class Vec4;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Vec3
|
||||||
|
{
|
||||||
|
union { T x, r, s; };
|
||||||
|
union { T y, g, t; };
|
||||||
|
union { T z, b, p; };
|
||||||
|
|
||||||
|
constexpr Vec3() = default;
|
||||||
|
constexpr Vec3(T X, T Y, T Z);
|
||||||
|
constexpr Vec3(T X, const Vec2<T>& vec);
|
||||||
|
constexpr explicit Vec3(T scale);
|
||||||
|
constexpr Vec3(const Vec2<T>& vec, T Z = 0.0);
|
||||||
|
template<typename U> constexpr explicit Vec3(const Vec3<U>& vec);
|
||||||
|
constexpr Vec3(const Vec3&) = default;
|
||||||
|
constexpr Vec3(Vec3&&) = default;
|
||||||
|
constexpr explicit Vec3(const Vec4<T>& vec);
|
||||||
|
|
||||||
|
T AbsDotProduct(const Vec3& vec) const;
|
||||||
|
constexpr bool ApproxEqual(const Vec3& vec, T max_difference = std::numeric_limits<T>::epsilon()) const;
|
||||||
|
|
||||||
|
constexpr Vec3 CrossProduct(const Vec3& vec) const;
|
||||||
|
|
||||||
|
template<typename U = T> U Distance(const Vec3& vec) const;
|
||||||
|
constexpr T DotProduct(const Vec3& vec) const;
|
||||||
|
|
||||||
|
Vec3 GetAbs() const;
|
||||||
|
template<typename U = T> U GetLength() const;
|
||||||
|
Vec3 GetNormal(T* length = nullptr) const;
|
||||||
|
constexpr T GetSquaredLength() const;
|
||||||
|
|
||||||
|
constexpr Vec3& Maximize(const Vec3& vec);
|
||||||
|
constexpr Vec3& Minimize(const Vec3& vec);
|
||||||
|
|
||||||
|
Vec3& Normalize(T* length = nullptr);
|
||||||
|
|
||||||
|
constexpr T SquaredDistance(const Vec3& vec) const;
|
||||||
|
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
constexpr T& operator[](std::size_t i);
|
||||||
|
constexpr const T& operator[](std::size_t i) const;
|
||||||
|
|
||||||
|
constexpr const Vec3& operator+() const;
|
||||||
|
constexpr Vec3 operator-() const;
|
||||||
|
|
||||||
|
constexpr Vec3 operator+(const Vec3& vec) const;
|
||||||
|
constexpr Vec3 operator-(const Vec3& vec) const;
|
||||||
|
constexpr Vec3 operator*(const Vec3& vec) const;
|
||||||
|
constexpr Vec3 operator*(T scale) const;
|
||||||
|
constexpr Vec3 operator/(const Vec3& vec) const;
|
||||||
|
constexpr Vec3 operator/(T scale) const;
|
||||||
|
constexpr Vec3 operator%(const Vec3& vec) const;
|
||||||
|
constexpr Vec3 operator%(T mod) const;
|
||||||
|
|
||||||
|
constexpr Vec3& operator=(const Vec3&) = default;
|
||||||
|
constexpr Vec3& operator=(Vec3&&) = default;
|
||||||
|
|
||||||
|
constexpr Vec3& operator+=(const Vec3& vec);
|
||||||
|
constexpr Vec3& operator-=(const Vec3& vec);
|
||||||
|
constexpr Vec3& operator*=(const Vec3& vec);
|
||||||
|
constexpr Vec3& operator*=(T scale);
|
||||||
|
constexpr Vec3& operator/=(const Vec3& vec);
|
||||||
|
constexpr Vec3& operator/=(T scale);
|
||||||
|
constexpr Vec3& operator%=(const Vec3& vec);
|
||||||
|
constexpr Vec3& operator%=(T mod);
|
||||||
|
|
||||||
|
constexpr bool operator==(const Vec3& vec) const;
|
||||||
|
constexpr bool operator!=(const Vec3& vec) const;
|
||||||
|
constexpr bool operator<(const Vec3& vec) const;
|
||||||
|
constexpr bool operator<=(const Vec3& vec) const;
|
||||||
|
constexpr bool operator>(const Vec3& vec) const;
|
||||||
|
constexpr bool operator>=(const Vec3& vec) const;
|
||||||
|
|
||||||
|
static constexpr Vec3 Apply(T(*func)(T), const Vec3& vec);
|
||||||
|
static constexpr bool ApproxEqual(const Vec3& lhs, const Vec3& rhs, T max_difference = std::numeric_limits<T>::epsilon());
|
||||||
|
static constexpr Vec3 Backward();
|
||||||
|
static constexpr Vec3 Clamp(const Vec3& vec, const Vec3& min, const Vec3& max);
|
||||||
|
static constexpr Vec3 CrossProduct(const Vec3& vec1, const Vec3& vec2);
|
||||||
|
template<typename U = T> static U Distance(const Vec3& vec1, const Vec3& vec2);
|
||||||
|
static constexpr T DotProduct(const Vec3& vec1, const Vec3& vec2);
|
||||||
|
static constexpr Vec3 Down();
|
||||||
|
static constexpr Vec3 Forward();
|
||||||
|
static constexpr Vec3 Left();
|
||||||
|
static constexpr Vec3 Max(const Vec3& lhs, const Vec3& rhs);
|
||||||
|
static constexpr Vec3 Min(const Vec3& lhs, const Vec3& rhs);
|
||||||
|
static Vec3 Normalize(const Vec3& vec);
|
||||||
|
static constexpr Vec3 Right();
|
||||||
|
static constexpr T SquaredDistance(const Vec3& vec1, const Vec3& vec2);
|
||||||
|
static constexpr Vec3 Unit();
|
||||||
|
static constexpr Vec3 UnitX();
|
||||||
|
static constexpr Vec3 UnitY();
|
||||||
|
static constexpr Vec3 UnitZ();
|
||||||
|
static constexpr Vec3 Up();
|
||||||
|
static constexpr Vec3 Zero();
|
||||||
|
|
||||||
|
~Vec3() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Vec3b = Vec3<std::uint8_t>;
|
||||||
|
using Vec3d = Vec3<double>;
|
||||||
|
using Vec3f = Vec3<float>;
|
||||||
|
using Vec3i = Vec3<int>;
|
||||||
|
using Vec3ui = Vec3<unsigned int>;
|
||||||
|
using Vec3i32 = Vec3<std::int32_t>;
|
||||||
|
using Vec3i64 = Vec3<std::int64_t>;
|
||||||
|
using Vec3ui32 = Vec3<std::uint32_t>;
|
||||||
|
using Vec3ui64 = Vec3<std::uint64_t>;
|
||||||
|
|
||||||
|
template<typename T> std::ostream& operator<<(std::ostream& out, const Vec3<T>& vec);
|
||||||
|
|
||||||
|
template<typename T> constexpr Vec3<T> operator*(T scale, const Vec3<T>& vec);
|
||||||
|
template<typename T> constexpr Vec3<T> operator/(T scale, const Vec3<T>& vec);
|
||||||
|
template<typename T> constexpr Vec3<T> operator%(T scale, const Vec3<T>& vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/Vec3.inl>
|
||||||
|
|
||||||
|
#endif // __AK_VEC3__
|
||||||
509
runtime/Includes/Maths/Vec3.inl
git.filemode.executable_file
509
runtime/Includes/Maths/Vec3.inl
git.filemode.executable_file
@@ -0,0 +1,509 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Maths/Vec3.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>::Vec3(T X, T Y, T Z) : x(X), y(Y), z(Z) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>::Vec3(T X, const Vec2<T>& vec) : x(X), y(vec.x), z(vec.y) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>::Vec3(T scale) : x(scale), y(scale), z(scale) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>::Vec3(const Vec2<T>& vec, T Z) : x(vec.x), y(vec.y), z(Z) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
constexpr Vec3<T>::Vec3(const Vec3<U>& vec) : x(static_cast<T>(vec.x)), y(static_cast<T>(vec.y)), z(static_cast<T>(vec.z)) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>::Vec3(const Vec4<T>& vec) : x(vec.x), y(vec.y), z(vec.z) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T Vec3<T>::AbsDotProduct(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return std::abs(x * vec.x) + std::abs(y * vec.y) + std::abs(z * vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::ApproxEqual(const Vec3& vec, T maxDifference) const
|
||||||
|
{
|
||||||
|
return NumberEquals(x, vec.x, maxDifference) && NumberEquals(y, vec.y, maxDifference) && NumberEquals(z, vec.z, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::CrossProduct(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return Vec3(y * vec.z - z * vec.y, z * vec.x - x * vec.z, x * vec.y - y * vec.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
U Vec3<T>::Distance(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return static_cast<U>(std::sqrt(static_cast<U>(SquaredDistance(vec))));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec3<T>::DotProduct(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return x * vec.x + y * vec.y + z * vec.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec3<T> Vec3<T>::GetAbs() const
|
||||||
|
{
|
||||||
|
return Vec3(std::abs(x), std::abs(y), std::abs(z));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
U Vec3<T>::GetLength() const
|
||||||
|
{
|
||||||
|
return static_cast<U>(std::sqrt(static_cast<U>(GetSquaredLength())));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec3<T> Vec3<T>::GetNormal(T* length) const
|
||||||
|
{
|
||||||
|
Vec3 vec(*this);
|
||||||
|
vec.Normalize(length);
|
||||||
|
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec3<T>::GetSquaredLength() const
|
||||||
|
{
|
||||||
|
return x*x + y*y + z*z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::Maximize(const Vec3& vec)
|
||||||
|
{
|
||||||
|
if (vec.x > x)
|
||||||
|
x = vec.x;
|
||||||
|
|
||||||
|
if (vec.y > y)
|
||||||
|
y = vec.y;
|
||||||
|
|
||||||
|
if (vec.z > z)
|
||||||
|
z = vec.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::Minimize(const Vec3& vec)
|
||||||
|
{
|
||||||
|
if (vec.x < x)
|
||||||
|
x = vec.x;
|
||||||
|
|
||||||
|
if (vec.y < y)
|
||||||
|
y = vec.y;
|
||||||
|
|
||||||
|
if (vec.z < z)
|
||||||
|
z = vec.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec3<T>& Vec3<T>::Normalize(T* length)
|
||||||
|
{
|
||||||
|
T norm = GetLength();
|
||||||
|
if (norm > T(0.0))
|
||||||
|
{
|
||||||
|
T invNorm = T(1.0) / norm;
|
||||||
|
x *= invNorm;
|
||||||
|
y *= invNorm;
|
||||||
|
z *= invNorm;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length)
|
||||||
|
*length = norm;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec3<T>::SquaredDistance(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return (*this - vec).GetSquaredLength();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string Vec3<T>::ToString() const
|
||||||
|
{
|
||||||
|
return "Vec3(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T& Vec3<T>::operator[](std::size_t i)
|
||||||
|
{
|
||||||
|
Scop::Assert(i < 3, "index out of range");
|
||||||
|
return *(&x + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const T& Vec3<T>::operator[](std::size_t i) const
|
||||||
|
{
|
||||||
|
Scop::Assert(i < 3, "index out of range");
|
||||||
|
return *(&x + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const Vec3<T>& Vec3<T>::operator+() const
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator-() const
|
||||||
|
{
|
||||||
|
return Vec3(-x, -y, -z);
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator+(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return Vec3(x + vec.x, y + vec.y, z + vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator-(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return Vec3(x - vec.x, y - vec.y, z - vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator*(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return Vec3(x * vec.x, y * vec.y, z * vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator*(T scale) const
|
||||||
|
{
|
||||||
|
return Vec3(x * scale, y * scale, z * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator/(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return Vec3(x / vec.x, y / vec.y, z / vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator/(T scale) const
|
||||||
|
{
|
||||||
|
return Vec3(x / scale, y / scale, z / scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator%(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return Vec3(Mod(x, vec.x), Mod(y, vec.y), Mod(z, vec.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::operator%(T mod) const
|
||||||
|
{
|
||||||
|
return Vec3(Mod(x, mod), Mod(y, mod), Mod(z, mod));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator+=(const Vec3& vec)
|
||||||
|
{
|
||||||
|
x += vec.x;
|
||||||
|
y += vec.y;
|
||||||
|
z += vec.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator-=(const Vec3& vec)
|
||||||
|
{
|
||||||
|
x -= vec.x;
|
||||||
|
y -= vec.y;
|
||||||
|
z -= vec.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator*=(const Vec3& vec)
|
||||||
|
{
|
||||||
|
x *= vec.x;
|
||||||
|
y *= vec.y;
|
||||||
|
z *= vec.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator*=(T scale)
|
||||||
|
{
|
||||||
|
x *= scale;
|
||||||
|
y *= scale;
|
||||||
|
z *= scale;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator/=(const Vec3& vec)
|
||||||
|
{
|
||||||
|
x /= vec.x;
|
||||||
|
y /= vec.y;
|
||||||
|
z /= vec.z;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator/=(T scale)
|
||||||
|
{
|
||||||
|
x /= scale;
|
||||||
|
y /= scale;
|
||||||
|
z /= scale;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator%=(const Vec3& vec)
|
||||||
|
{
|
||||||
|
x = Mod(x, vec.x);
|
||||||
|
y = Mod(y, vec.y);
|
||||||
|
z = Mod(z, vec.z);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T>& Vec3<T>::operator%=(T mod)
|
||||||
|
{
|
||||||
|
x = Mod(x, mod);
|
||||||
|
y = Mod(y, mod);
|
||||||
|
z = Mod(z, mod);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::operator==(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return x == vec.x && y == vec.y && z == vec.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::operator!=(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
return !operator==(vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::operator<(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x < vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y < vec.y;
|
||||||
|
|
||||||
|
return z < vec.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::operator<=(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x < vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y < vec.y;
|
||||||
|
|
||||||
|
return z <= vec.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::operator>(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x > vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y > vec.y;
|
||||||
|
|
||||||
|
return z > vec.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::operator>=(const Vec3& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x > vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y > vec.y;
|
||||||
|
|
||||||
|
return z >= vec.z;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Apply(T(*func)(T), const Vec3& vec)
|
||||||
|
{
|
||||||
|
return Vec3(func(vec.x), func(vec.y), func(vec.z));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec3<T>::ApproxEqual(const Vec3& lhs, const Vec3& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::CrossProduct(const Vec3& vec1, const Vec3& vec2)
|
||||||
|
{
|
||||||
|
return vec1.CrossProduct(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec3<T>::DotProduct(const Vec3& vec1, const Vec3& vec2)
|
||||||
|
{
|
||||||
|
return vec1.DotProduct(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Backward()
|
||||||
|
{
|
||||||
|
return Vec3(0, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
U Vec3<T>::Distance(const Vec3& vec1, const Vec3& vec2)
|
||||||
|
{
|
||||||
|
return vec1.Distance<U>(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Down()
|
||||||
|
{
|
||||||
|
return Vec3(0, -1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Forward()
|
||||||
|
{
|
||||||
|
return Vec3(0, 0, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Left()
|
||||||
|
{
|
||||||
|
return Vec3(-1, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Max(const Vec3& lhs, const Vec3& rhs)
|
||||||
|
{
|
||||||
|
Vec3 max = lhs;
|
||||||
|
max.Maximize(rhs);
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Min(const Vec3& lhs, const Vec3& rhs)
|
||||||
|
{
|
||||||
|
Vec3 min = lhs;
|
||||||
|
min.Minimize(rhs);
|
||||||
|
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec3<T> Vec3<T>::Normalize(const Vec3& vec)
|
||||||
|
{
|
||||||
|
return vec.GetNormal();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Right()
|
||||||
|
{
|
||||||
|
return Vec3(1, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec3<T>::SquaredDistance(const Vec3& vec1, const Vec3& vec2)
|
||||||
|
{
|
||||||
|
return vec1.SquaredDistance(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Unit()
|
||||||
|
{
|
||||||
|
return Vec3(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::UnitX()
|
||||||
|
{
|
||||||
|
return Vec3(1, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::UnitY()
|
||||||
|
{
|
||||||
|
return Vec3(0, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::UnitZ()
|
||||||
|
{
|
||||||
|
return Vec3(0, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Up()
|
||||||
|
{
|
||||||
|
return Vec3(0, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> Vec3<T>::Zero()
|
||||||
|
{
|
||||||
|
return Vec3(0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, const Vec3<T>& vec)
|
||||||
|
{
|
||||||
|
return out << "Vec3(" << vec.x << ", " << vec.y << ", " << vec.z << ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> operator*(T scale, const Vec3<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec3<T>(scale * vec.x, scale * vec.y, scale * vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> operator/(T scale, const Vec3<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec3<T>(scale / vec.x, scale / vec.y, scale / vec.z);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec3<T> operator%(T mod, const Vec3<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec3<T>(Mod(mod, vec.x), Mod(mod, vec.y), Mod(mod, vec.z));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
115
runtime/Includes/Maths/Vec4.h
git.filemode.executable_file
115
runtime/Includes/Maths/Vec4.h
git.filemode.executable_file
@@ -0,0 +1,115 @@
|
|||||||
|
#ifndef __SCOP_VEC4__
|
||||||
|
#define __SCOP_VEC4__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <limits>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include <Core/Logs.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T> class Vec2;
|
||||||
|
template<typename T> class Vec3;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Vec4
|
||||||
|
{
|
||||||
|
union { T x, r, s; };
|
||||||
|
union { T y, g, t; };
|
||||||
|
union { T z, b, p; };
|
||||||
|
union { T w, a, q; };
|
||||||
|
|
||||||
|
constexpr Vec4() = default;
|
||||||
|
constexpr Vec4(T X, T Y, T Z, T W = 1.0);
|
||||||
|
constexpr Vec4(T X, T Y, const Vec2<T>& vec);
|
||||||
|
constexpr Vec4(T X, const Vec2<T>& vec, T W);
|
||||||
|
constexpr Vec4(T X, const Vec3<T>& vec);
|
||||||
|
constexpr explicit Vec4(T scale);
|
||||||
|
constexpr Vec4(const Vec2<T>& vec, T Z = 0.0, T W = 1.0);
|
||||||
|
constexpr Vec4(const Vec3<T>& vec, T W = 1.0);
|
||||||
|
template<typename U> constexpr explicit Vec4(const Vec4<U>& vec);
|
||||||
|
constexpr Vec4(const Vec4&) = default;
|
||||||
|
constexpr Vec4(Vec4&&) = default;
|
||||||
|
|
||||||
|
T AbsDotProduct(const Vec4& vec) const;
|
||||||
|
constexpr bool ApproxEqual(const Vec4& vec, T max_difference = std::numeric_limits<T>::epsilon()) const;
|
||||||
|
|
||||||
|
constexpr T DotProduct(const Vec4& vec) const;
|
||||||
|
|
||||||
|
Vec4 GetNormal(T* length = nullptr) const;
|
||||||
|
|
||||||
|
constexpr Vec4& Maximize(const Vec4& vec);
|
||||||
|
constexpr Vec4& Minimize(const Vec4& vec);
|
||||||
|
|
||||||
|
Vec4& Normalize(T* length = nullptr);
|
||||||
|
|
||||||
|
std::string ToString() const;
|
||||||
|
|
||||||
|
constexpr Vec4& operator=(const Vec4&) = default;
|
||||||
|
constexpr Vec4& operator=(Vec4&&) = default;
|
||||||
|
|
||||||
|
constexpr T& operator[](std::size_t i);
|
||||||
|
constexpr const T& operator[](std::size_t i) const;
|
||||||
|
|
||||||
|
constexpr const Vec4& operator+() const;
|
||||||
|
constexpr Vec4 operator-() const;
|
||||||
|
|
||||||
|
constexpr Vec4 operator+(const Vec4& vec) const;
|
||||||
|
constexpr Vec4 operator-(const Vec4& vec) const;
|
||||||
|
constexpr Vec4 operator*(const Vec4& vec) const;
|
||||||
|
constexpr Vec4 operator*(T scale) const;
|
||||||
|
constexpr Vec4 operator/(const Vec4& vec) const;
|
||||||
|
constexpr Vec4 operator/(T scale) const;
|
||||||
|
constexpr Vec4 operator%(const Vec4& vec) const;
|
||||||
|
constexpr Vec4 operator%(T mod) const;
|
||||||
|
|
||||||
|
constexpr Vec4& operator+=(const Vec4& vec);
|
||||||
|
constexpr Vec4& operator-=(const Vec4& vec);
|
||||||
|
constexpr Vec4& operator*=(const Vec4& vec);
|
||||||
|
constexpr Vec4& operator*=(T scale);
|
||||||
|
constexpr Vec4& operator/=(const Vec4& vec);
|
||||||
|
constexpr Vec4& operator/=(T scale);
|
||||||
|
constexpr Vec4& operator%=(const Vec4& vec);
|
||||||
|
constexpr Vec4& operator%=(T mod);
|
||||||
|
|
||||||
|
constexpr bool operator==(const Vec4& vec) const;
|
||||||
|
constexpr bool operator!=(const Vec4& vec) const;
|
||||||
|
constexpr bool operator<(const Vec4& vec) const;
|
||||||
|
constexpr bool operator<=(const Vec4& vec) const;
|
||||||
|
constexpr bool operator>(const Vec4& vec) const;
|
||||||
|
constexpr bool operator>=(const Vec4& vec) const;
|
||||||
|
|
||||||
|
static constexpr Vec4 Apply(T(*func)(T), const Vec4& vec);
|
||||||
|
static constexpr bool ApproxEqual(const Vec4& lhs, const Vec4& rhs, T max_difference = std::numeric_limits<T>::epsilon());
|
||||||
|
static constexpr T DotProduct(const Vec4& vec1, const Vec4& vec2);
|
||||||
|
static Vec4 Normalize(const Vec4& vec);
|
||||||
|
static constexpr Vec4 UnitX();
|
||||||
|
static constexpr Vec4 UnitY();
|
||||||
|
static constexpr Vec4 UnitZ();
|
||||||
|
static constexpr Vec4 Zero();
|
||||||
|
|
||||||
|
~Vec4() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
using Vec4d = Vec4<double>;
|
||||||
|
using Vec4f = Vec4<float>;
|
||||||
|
using Vec4i = Vec4<int>;
|
||||||
|
using Vec4ui = Vec4<unsigned int>;
|
||||||
|
using Vec4i32 = Vec4<std::int32_t>;
|
||||||
|
using Vec4i64 = Vec4<std::int64_t>;
|
||||||
|
using Vec4ui32 = Vec4<std::uint32_t>;
|
||||||
|
using Vec4ui64 = Vec4<std::uint64_t>;
|
||||||
|
|
||||||
|
template<typename T> std::ostream& operator<<(std::ostream& out, const Vec4<T>& vec);
|
||||||
|
|
||||||
|
template<typename T> constexpr Vec4<T> operator*(T scale, const Vec4<T>& vec);
|
||||||
|
template<typename T> constexpr Vec4<T> operator/(T scale, const Vec4<T>& vec);
|
||||||
|
template<typename T> constexpr Vec4<T> operator%(T mod, const Vec4<T>& vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Maths/Vec4.inl>
|
||||||
|
|
||||||
|
#endif // __AK_VEC4__
|
||||||
|
|
||||||
424
runtime/Includes/Maths/Vec4.inl
git.filemode.executable_file
424
runtime/Includes/Maths/Vec4.inl
git.filemode.executable_file
@@ -0,0 +1,424 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Maths/Vec4.h>
|
||||||
|
|
||||||
|
namespace Scop
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(T X, T Y, T Z, T W) : x(X), y(Y), z(Z), w(W) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(T X, T Y, const Vec2<T>& vec) : x(X), y(Y), z(vec.x), w(vec.y) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(T X, const Vec2<T>& vec, T W) : x(X), y(vec.x), z(vec.y), w(W) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(T X, const Vec3<T>& vec) : x(X), y(vec.x), z(vec.y), w(vec.z) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(T scale) : x(scale), y(scale), z(scale), w(scale) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(const Vec2<T>& vec, T Z, T W) : x(vec.x), y(vec.y), z(Z), w(W) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>::Vec4(const Vec3<T>& vec, T W) : x(vec.x), y(vec.y), z(vec.z), w(W) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
template<typename U>
|
||||||
|
constexpr Vec4<T>::Vec4(const Vec4<U>& vec) : x(static_cast<T>(vec.x)), y(static_cast<T>(vec.y)), z(static_cast<T>(vec.z)), w(static_cast<T>(vec.w)) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T Vec4<T>::AbsDotProduct(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return std::abs(x * vec.x) + std::abs(y * vec.y) + std::abs(z * vec.z) + std::abs(w * vec.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::ApproxEqual(const Vec4& vec, T maxDifference) const
|
||||||
|
{
|
||||||
|
return NumberEquals(x, vec.x, maxDifference) && NumberEquals(y, vec.y, maxDifference) && NumberEquals(z, vec.z, maxDifference) && NumberEquals(w, vec.w, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec4<T>::DotProduct(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return x*vec.x + y*vec.y + z*vec.z + w*vec.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec4<T> Vec4<T>::GetNormal(T* length) const
|
||||||
|
{
|
||||||
|
Vec4<T> vec(*this);
|
||||||
|
vec.Normalize(length);
|
||||||
|
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::Maximize(const Vec4& vec)
|
||||||
|
{
|
||||||
|
if (vec.x > x)
|
||||||
|
x = vec.x;
|
||||||
|
|
||||||
|
if (vec.y > y)
|
||||||
|
y = vec.y;
|
||||||
|
|
||||||
|
if (vec.z > z)
|
||||||
|
z = vec.z;
|
||||||
|
|
||||||
|
if (vec.w > w)
|
||||||
|
w = vec.w;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::Minimize(const Vec4& vec)
|
||||||
|
{
|
||||||
|
if (vec.x < x)
|
||||||
|
x = vec.x;
|
||||||
|
|
||||||
|
if (vec.y < y)
|
||||||
|
y = vec.y;
|
||||||
|
|
||||||
|
if (vec.z < z)
|
||||||
|
z = vec.z;
|
||||||
|
|
||||||
|
if (vec.w < w)
|
||||||
|
w = vec.w;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec4<T>& Vec4<T>::Normalize(T* length)
|
||||||
|
{
|
||||||
|
T invLength = T(1.0) / w;
|
||||||
|
x *= invLength;
|
||||||
|
y *= invLength;
|
||||||
|
z *= invLength;
|
||||||
|
|
||||||
|
if (length)
|
||||||
|
*length = w;
|
||||||
|
|
||||||
|
w = T(1.0);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string Vec4<T>::ToString() const
|
||||||
|
{
|
||||||
|
std::ostringstream ss;
|
||||||
|
ss << *this;
|
||||||
|
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T& Vec4<T>::operator[](std::size_t i)
|
||||||
|
{
|
||||||
|
Scop::Assert(i < 4, "index out of range");
|
||||||
|
return *(&x + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const T& Vec4<T>::operator[](std::size_t i) const
|
||||||
|
{
|
||||||
|
Scop::Assert(i < 4, "index out of range");
|
||||||
|
return *(&x + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr const Vec4<T>& Vec4<T>::operator+() const
|
||||||
|
{
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator-() const
|
||||||
|
{
|
||||||
|
return Vec4(-x, -y, -z, -w);
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator+(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return Vec4(x + vec.x, y + vec.y, z + vec.z, w + vec.w);
|
||||||
|
}
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator-(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return Vec4(x - vec.x, y - vec.y, z - vec.z, w - vec.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator*(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return Vec4(x * vec.x, y * vec.y, z * vec.z, w * vec.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator*(T scale) const
|
||||||
|
{
|
||||||
|
return Vec4(x * scale, y * scale, z * scale, w * scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator/(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return Vec4(x / vec.x, y / vec.y, z / vec.z, w / vec.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator/(T scale) const
|
||||||
|
{
|
||||||
|
return Vec4(x / scale, y / scale, z / scale, w / scale);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator%(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return Vec4(Mod(x, vec.x), Mod(y, vec.y), Mod(z, vec.z), Mod(w, vec.w));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::operator%(T mod) const
|
||||||
|
{
|
||||||
|
return Vec4(Mod(x, mod), Mod(y, mod), Mod(z, mod), Mod(z, mod));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator+=(const Vec4& vec)
|
||||||
|
{
|
||||||
|
x += vec.x;
|
||||||
|
y += vec.y;
|
||||||
|
z += vec.z;
|
||||||
|
w += vec.w;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator-=(const Vec4& vec)
|
||||||
|
{
|
||||||
|
x -= vec.x;
|
||||||
|
y -= vec.y;
|
||||||
|
z -= vec.z;
|
||||||
|
w -= vec.w;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator*=(const Vec4& vec)
|
||||||
|
{
|
||||||
|
x *= vec.x;
|
||||||
|
y *= vec.y;
|
||||||
|
z *= vec.z;
|
||||||
|
w *= vec.w;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator*=(T scale)
|
||||||
|
{
|
||||||
|
x *= scale;
|
||||||
|
y *= scale;
|
||||||
|
z *= scale;
|
||||||
|
w *= scale;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator/=(const Vec4& vec)
|
||||||
|
{
|
||||||
|
x /= vec.x;
|
||||||
|
y /= vec.y;
|
||||||
|
z /= vec.z;
|
||||||
|
w /= vec.w;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator/=(T scale)
|
||||||
|
{
|
||||||
|
x /= scale;
|
||||||
|
y /= scale;
|
||||||
|
z /= scale;
|
||||||
|
w /= scale;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator%=(const Vec4& vec)
|
||||||
|
{
|
||||||
|
x = Mod(x, vec.x);
|
||||||
|
y = Mod(y, vec.y);
|
||||||
|
z = Mod(z, vec.z);
|
||||||
|
w = Mod(w, vec.w);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T>& Vec4<T>::operator%=(T mod)
|
||||||
|
{
|
||||||
|
x = Mod(x, mod);
|
||||||
|
y = Mod(y, mod);
|
||||||
|
z = Mod(z, mod);
|
||||||
|
w = Mod(w, mod);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::operator==(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return x == vec.x && y == vec.y && z == vec.z && w == vec.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::operator!=(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
return !operator==(vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::operator<(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x < vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y < vec.y;
|
||||||
|
|
||||||
|
if (z != vec.z)
|
||||||
|
return z < vec.z;
|
||||||
|
|
||||||
|
return w < vec.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::operator<=(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x < vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y < vec.y;
|
||||||
|
|
||||||
|
if (z != vec.z)
|
||||||
|
return z < vec.z;
|
||||||
|
|
||||||
|
return w <= vec.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::operator>(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x > vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y > vec.y;
|
||||||
|
|
||||||
|
if (z != vec.z)
|
||||||
|
return z > vec.z;
|
||||||
|
|
||||||
|
return w > vec.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::operator>=(const Vec4& vec) const
|
||||||
|
{
|
||||||
|
if (x != vec.x)
|
||||||
|
return x > vec.x;
|
||||||
|
|
||||||
|
if (y != vec.y)
|
||||||
|
return y > vec.y;
|
||||||
|
|
||||||
|
if (z != vec.z)
|
||||||
|
return z > vec.z;
|
||||||
|
|
||||||
|
return w >= vec.w;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::Apply(T(*func)(T), const Vec4& vec)
|
||||||
|
{
|
||||||
|
return Vec4(func(vec.x), func(vec.y), func(vec.z), func(vec.w));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr bool Vec4<T>::ApproxEqual(const Vec4& lhs, const Vec4& rhs, T maxDifference)
|
||||||
|
{
|
||||||
|
return lhs.ApproxEqual(rhs, maxDifference);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T Vec4<T>::DotProduct(const Vec4& vec1, const Vec4& vec2)
|
||||||
|
{
|
||||||
|
return vec1.DotProduct(vec2);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
Vec4<T> Vec4<T>::Normalize(const Vec4& vec)
|
||||||
|
{
|
||||||
|
return vec.GetNormal();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::UnitX()
|
||||||
|
{
|
||||||
|
return Vec4(1, 0, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::UnitY()
|
||||||
|
{
|
||||||
|
return Vec4(0, 1, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::UnitZ()
|
||||||
|
{
|
||||||
|
return Vec4(0, 0, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> Vec4<T>::Zero()
|
||||||
|
{
|
||||||
|
return Vec4(0, 0, 0, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::ostream& operator<<(std::ostream& out, const Vec4<T>& vec)
|
||||||
|
{
|
||||||
|
return out << "Vec4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ')';
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> operator*(T scale, const Vec4<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec4<T>(scale * vec.x, scale * vec.y, scale * vec.z, scale * vec.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> operator/(T scale, const Vec4<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec4<T>(scale / vec.x, scale / vec.y, scale / vec.z, scale / vec.w);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr Vec4<T> operator%(T mod, const Vec4<T>& vec)
|
||||||
|
{
|
||||||
|
return Vec4<T>(Mod(mod, vec.x), Mod(mod, vec.y), Mod(mod, vec.z), Mod(mod, vec.w));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,18 +1,7 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Inputs.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 20:35:09 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_INPUTS__
|
#ifndef __MLX_INPUTS__
|
||||||
#define __MLX_INPUTS__
|
#define __MLX_INPUTS__
|
||||||
|
|
||||||
|
#include <mlx.h>
|
||||||
#include <mlx_profile.h>
|
#include <mlx_profile.h>
|
||||||
#include <Platform/Window.h>
|
#include <Platform/Window.h>
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +1,30 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Window.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/05/25 16:11:00 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_WINDOW__
|
#ifndef __MLX_WINDOW__
|
||||||
#define __MLX_WINDOW__
|
#define __MLX_WINDOW__
|
||||||
|
|
||||||
|
#include <Maths/Vec2.h>
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
class Window
|
class Window
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Window(std::size_t w, std::size_t h, const std::string& title);
|
Window(std::size_t w, std::size_t h, const std::string& title, bool hidden = false);
|
||||||
|
|
||||||
inline void* GetWindowHandle() const noexcept { return p_window; }
|
inline Handle GetWindowHandle() const noexcept { return p_window; }
|
||||||
inline int GetWidth() const noexcept { return m_width; }
|
inline int GetWidth() const noexcept { return m_width; }
|
||||||
inline int GetHeight() const noexcept { return m_height; }
|
inline int GetHeight() const noexcept { return m_height; }
|
||||||
inline std::uint32_t GetID() const noexcept { return m_id; }
|
inline std::uint32_t GetID() const noexcept { return m_id; }
|
||||||
|
|
||||||
|
inline VkSurfaceKHR CreateVulkanSurface(VkInstance instance) const noexcept { return SDLManager::Get().CreateVulkanSurface(p_window, instance); }
|
||||||
|
inline std::vector<const char*> GetRequiredVulkanInstanceExtentions() const noexcept { return SDLManager::Get().GetRequiredVulkanInstanceExtentions(p_window); }
|
||||||
|
inline Vec2ui GetVulkanDrawableSize() const noexcept { return SDLManager::Get().GetVulkanDrawableSize(p_window); }
|
||||||
|
|
||||||
void Destroy() noexcept;
|
void Destroy() noexcept;
|
||||||
|
|
||||||
~Window() = default;
|
~Window() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void* p_window = nullptr;
|
Handle p_window = nullptr;
|
||||||
std::uint32_t m_id = -1;
|
std::uint32_t m_id = -1;
|
||||||
int m_width = 0;
|
int m_width = 0;
|
||||||
int m_height = 0;
|
int m_height = 0;
|
||||||
|
|||||||
@@ -1,31 +1,20 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* PreCompiled.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/25 17:37:23 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:25:07 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_PRE_COMPILED_HEADER__
|
#ifndef __MLX_PRE_COMPILED_HEADER__
|
||||||
#define __MLX_PRE_COMPILED_HEADER__
|
#define __MLX_PRE_COMPILED_HEADER__
|
||||||
|
|
||||||
#define VK_NO_PROTOTYPES
|
#define VK_NO_PROTOTYPES
|
||||||
|
|
||||||
#define Window X11Window // fuck X11
|
#define Window X11Window // f*ck X11
|
||||||
|
|
||||||
#include <mlx_profile.h>
|
#include <mlx_profile.h>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <volk.h>
|
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_vulkan.h>
|
#include <SDL2/SDL_vulkan.h>
|
||||||
|
|
||||||
|
#include <kvf.h>
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <list>
|
#include <list>
|
||||||
@@ -40,8 +29,6 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <unordered_set>
|
#include <unordered_set>
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
#include <function.h>
|
#include <function.h>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
@@ -82,5 +69,8 @@
|
|||||||
#include <Core/EventBus.h>
|
#include <Core/EventBus.h>
|
||||||
#include <Core/Profiler.h>
|
#include <Core/Profiler.h>
|
||||||
#include <Utils/NonOwningPtr.h>
|
#include <Utils/NonOwningPtr.h>
|
||||||
|
#include <Renderer/Vulkan/VulkanPrototypes.h>
|
||||||
|
|
||||||
|
using Handle = void*;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
83
runtime/Includes/Renderer/Buffer.h
git.filemode.normal_file
83
runtime/Includes/Renderer/Buffer.h
git.filemode.normal_file
@@ -0,0 +1,83 @@
|
|||||||
|
#ifndef __MLX_GPU_BUFFER__
|
||||||
|
#define __MLX_GPU_BUFFER__
|
||||||
|
|
||||||
|
#include <Renderer/Enums.h>
|
||||||
|
#include <Renderer/RenderCore.h>
|
||||||
|
#include <Utils/Buffer.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class GPUBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GPUBuffer() = default;
|
||||||
|
|
||||||
|
void Init(BufferType type, VkDeviceSize size, VkBufferUsageFlags usage, CPUBuffer data);
|
||||||
|
void Destroy() noexcept;
|
||||||
|
|
||||||
|
bool CopyFrom(const GPUBuffer& buffer) noexcept;
|
||||||
|
|
||||||
|
void Swap(GPUBuffer& buffer) noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE void* GetMap() const noexcept { return m_memory.map; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkBuffer Get() const noexcept { return m_buffer; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkDeviceMemory GetMemory() const noexcept { return m_memory.memory; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkDeviceSize GetSize() const noexcept { return m_memory.size; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkDeviceSize GetOffset() const noexcept { return 0; }
|
||||||
|
|
||||||
|
[[nodiscard]] inline bool IsInit() const noexcept { return m_buffer != VK_NULL_HANDLE; }
|
||||||
|
|
||||||
|
~GPUBuffer() = default;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void PushToGPU() noexcept;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
VkBuffer m_buffer = VK_NULL_HANDLE;
|
||||||
|
VmaAllocation m_allocation;
|
||||||
|
VkDeviceSize m_offset = 0;
|
||||||
|
VkDeviceSize m_size = 0;
|
||||||
|
void* p_map = nullptr;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void CreateBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaAllocationCreateInfo alloc_info);
|
||||||
|
|
||||||
|
private:
|
||||||
|
VkBufferUsageFlags m_usage = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class VertexBuffer : public GPUBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline void Init(std::uint32_t size, VkBufferUsageFlags additional_flags = 0) { GPUBuffer::Init(BufferType::LowDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | additional_flags, {}); }
|
||||||
|
void SetData(CPUBuffer data);
|
||||||
|
inline void Bind(VkCommandBuffer cmd) const noexcept { VkDeviceSize offset = 0; vkCmdBindVertexBuffers(cmd, 0, 1, &m_buffer, &offset); }
|
||||||
|
};
|
||||||
|
|
||||||
|
class IndexBuffer : public GPUBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline void Init(std::uint32_t size, VkBufferUsageFlags additional_flags = 0) { GPUBuffer::Init(BufferType::LowDynamic, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT | additional_flags, {}); }
|
||||||
|
void SetData(CPUBuffer data);
|
||||||
|
inline void Bind(VkCommandBuffer cmd) const noexcept { vkCmdBindIndexBuffer(cmd, m_buffer, 0, VK_INDEX_TYPE_UINT32); }
|
||||||
|
};
|
||||||
|
|
||||||
|
class UniformBuffer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void Init(std::uint32_t size);
|
||||||
|
void SetData(CPUBuffer data, std::size_t frame_index);
|
||||||
|
void Destroy() noexcept;
|
||||||
|
|
||||||
|
inline VkDeviceSize GetSize(int i) const noexcept { return m_buffers[i].GetSize(); }
|
||||||
|
inline VkDeviceSize GetOffset(int i) const noexcept { return m_buffers[i].GetOffset(); }
|
||||||
|
inline VkBuffer GetVk(int i) const noexcept { return m_buffers[i].Get(); }
|
||||||
|
inline GPUBuffer& Get(int i) noexcept { return m_buffers[i]; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<GPUBuffer, MAX_FRAMES_IN_FLIGHT> m_buffers;
|
||||||
|
std::array<void*, MAX_FRAMES_IN_FLIGHT> m_maps;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Buffer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 23:18:52 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 14:20:49 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_BUFFER__
|
|
||||||
#define __MLX_VK_BUFFER__
|
|
||||||
|
|
||||||
#include <Renderer/Enums.h>
|
|
||||||
#include <Renderer/Core/RenderCore.h>
|
|
||||||
#include <Renderer/Command/CommandResource.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Buffer : public CommandResource
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Buffer() = default;
|
|
||||||
|
|
||||||
void Create(BufferType type, VkDeviceSize size, VkBufferUsageFlags usage, const char* name, const void* data = nullptr);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline void MapMem(void** data) noexcept { RenderCore::Get().GetAllocator().MapMemory(m_allocation, data); m_is_mapped = true; }
|
|
||||||
inline bool IsMapped() const noexcept { return m_is_mapped; }
|
|
||||||
inline void UnmapMem() noexcept { RenderCore::Get().GetAllocator().UnmapMemory(m_allocation); m_is_mapped = false; }
|
|
||||||
|
|
||||||
void Flush(VkDeviceSize size = VK_WHOLE_SIZE, VkDeviceSize offset = 0);
|
|
||||||
bool CopyFromBuffer(const Buffer& buffer) noexcept;
|
|
||||||
|
|
||||||
inline VkBuffer& operator()() noexcept { return m_buffer; }
|
|
||||||
inline VkBuffer& Get() noexcept { return m_buffer; }
|
|
||||||
inline VkDeviceSize GetSize() const noexcept { return m_size; }
|
|
||||||
inline VkDeviceSize GetOffset() const noexcept { return m_offset; }
|
|
||||||
|
|
||||||
~Buffer() = default;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void PushToGPU() noexcept;
|
|
||||||
void Swap(Buffer& buffer) noexcept;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
VmaAllocation m_allocation;
|
|
||||||
VkBuffer m_buffer = VK_NULL_HANDLE;
|
|
||||||
VkDeviceSize m_offset = 0;
|
|
||||||
VkDeviceSize m_size = 0;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void CreateBuffer(VkBufferUsageFlags usage, VmaAllocationCreateInfo info, VkDeviceSize size, const char* name);
|
|
||||||
|
|
||||||
private:
|
|
||||||
#ifdef DEBUG
|
|
||||||
std::string m_name;
|
|
||||||
#endif
|
|
||||||
VkBufferUsageFlags m_usage = 0;
|
|
||||||
bool m_is_mapped = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* IndexBuffer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/01/25 15:05:05 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:24:46 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __VK_IBO__
|
|
||||||
#define __VK_IBO__
|
|
||||||
|
|
||||||
#include <Renderer/Buffers/Buffer.h>
|
|
||||||
#include <Renderer/Renderer.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class ConstantIndexBuffer : public Buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
inline void Create(std::uint32_t size, const std::uint16_t* data, const char* name) { Buffer::Create(BufferType::Constant, size, VK_BUFFER_USAGE_INDEX_BUFFER_BIT, name, data); }
|
|
||||||
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindIndexBuffer(*this); }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* UniformBuffer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:45:29 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:39:32 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_UBO__
|
|
||||||
#define __MLX_VK_UBO__
|
|
||||||
|
|
||||||
#include <Renderer/Buffers/Buffer.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class UniformBuffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
UniformBuffer() = default;
|
|
||||||
|
|
||||||
void Create(NonOwningPtr<class Renderer> renderer, std::uint32_t size, const char* name);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
void SetData(std::uint32_t size, const void* data);
|
|
||||||
|
|
||||||
VkDeviceSize GetSize() noexcept;
|
|
||||||
VkDeviceSize GetOffset() noexcept;
|
|
||||||
VkDeviceMemory GetDeviceMemory() noexcept;
|
|
||||||
VkBuffer& operator()() noexcept;
|
|
||||||
VkBuffer& Get() noexcept;
|
|
||||||
|
|
||||||
inline VkDeviceSize GetSize(int i) noexcept { return m_buffers[i].GetSize(); }
|
|
||||||
inline VkDeviceSize GetOffset(int i) noexcept { return m_buffers[i].GetOffset(); }
|
|
||||||
inline VkBuffer& operator()(int i) noexcept { return m_buffers[i].Get(); }
|
|
||||||
inline VkBuffer& Get(int i) noexcept { return m_buffers[i].Get(); }
|
|
||||||
|
|
||||||
~UniformBuffer() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::array<Buffer, MAX_FRAMES_IN_FLIGHT> m_buffers;
|
|
||||||
std::array<void*, MAX_FRAMES_IN_FLIGHT> m_maps;
|
|
||||||
NonOwningPtr<class Renderer> p_renderer;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_UBO__
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* VertexBuffer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:27:38 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:24:41 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_VBO__
|
|
||||||
#define __MLX_VK_VBO__
|
|
||||||
|
|
||||||
#include <Renderer/Enums.h>
|
|
||||||
#include <Renderer/Buffers/Buffer.h>
|
|
||||||
#include <Renderer/Renderer.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class RAMVertexBuffer : public Buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
inline void Create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::HighDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
|
|
||||||
void SetData(std::uint32_t size, const void* data);
|
|
||||||
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); }
|
|
||||||
};
|
|
||||||
|
|
||||||
class DeviceVertexBuffer : public Buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
inline void create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::LowDynamic, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
|
|
||||||
void SetData(std::uint32_t size, const void* data);
|
|
||||||
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); }
|
|
||||||
};
|
|
||||||
|
|
||||||
class ConstantVertexBuffer : public Buffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
inline void Create(std::uint32_t size, const void* data, const char* name) { Buffer::Create(BufferType::Constant, size, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, name, data); }
|
|
||||||
inline void Bind(Renderer& renderer) noexcept { renderer.GetActiveCmdBuffer().BindVertexBuffer(*this); }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_VBO__
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* CommandBuffer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:25:42 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:37:54 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_CMD_BUFFER__
|
|
||||||
#define __MLX_VK_CMD_BUFFER__
|
|
||||||
|
|
||||||
#include <Renderer/Core/Fence.h>
|
|
||||||
#include <Renderer/Enums.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Buffer;
|
|
||||||
class Image;
|
|
||||||
|
|
||||||
class CommandBuffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Init(CommandBufferType type, NonOwningPtr<class CommandManager> manager);
|
|
||||||
void Init(CommandBufferType type, NonOwningPtr<class CommandPool> pool);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
void BeginRecord(VkCommandBufferUsageFlags usage = 0);
|
|
||||||
void Submit(NonOwningPtr<class Semaphore> signal, NonOwningPtr<class Semaphore> wait) noexcept;
|
|
||||||
void SubmitIdle(bool shouldWaitForExecution = true) noexcept; // TODO : handle `shouldWaitForExecution` as false by default (needs to modify CmdResources lifetimes to do so)
|
|
||||||
void UpdateSubmitState() noexcept;
|
|
||||||
inline void WaitForExecution() noexcept { m_fence.Wait(); UpdateSubmitState(); m_state = CommandBufferState::Ready; }
|
|
||||||
inline void Reset() noexcept { vkResetCommandBuffer(m_cmd_buffer, 0); }
|
|
||||||
void EndRecord();
|
|
||||||
|
|
||||||
void BindVertexBuffer(Buffer& buffer) noexcept;
|
|
||||||
void BindIndexBuffer(Buffer& buffer) noexcept;
|
|
||||||
void CopyBuffer(Buffer& dst, Buffer& src) noexcept;
|
|
||||||
void CopyBufferToImage(Buffer& buffer, Image& image) noexcept;
|
|
||||||
void CopyImagetoBuffer(Image& image, Buffer& buffer) noexcept;
|
|
||||||
void TransitionImageLayout(Image& image, VkImageLayout new_layout) noexcept;
|
|
||||||
|
|
||||||
inline bool IsInit() const noexcept { return m_state != CommandBufferState::Uninit; }
|
|
||||||
inline bool IsReadyToBeUsed() const noexcept { return m_state == CommandBufferState::Ready; }
|
|
||||||
inline bool IsRecording() const noexcept { return m_state == CommandBufferState::Recording; }
|
|
||||||
inline bool HasBeenSubmitted() const noexcept { return m_state == CommandBufferState::Submitted; }
|
|
||||||
inline CommandBufferState GetCurrentState() const noexcept { return m_state; }
|
|
||||||
|
|
||||||
inline VkCommandBuffer& operator()() noexcept { return m_cmd_buffer; }
|
|
||||||
inline VkCommandBuffer& Get() noexcept { return m_cmd_buffer; }
|
|
||||||
inline Fence& GetFence() noexcept { return m_fence; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void PreTransferBarrier() noexcept;
|
|
||||||
void PostTransferBarrier() noexcept;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<NonOwningPtr<class CommandResource>> m_cmd_resources;
|
|
||||||
Fence m_fence;
|
|
||||||
VkCommandBuffer m_cmd_buffer = VK_NULL_HANDLE;
|
|
||||||
NonOwningPtr<class CmdPool> m_pool;
|
|
||||||
CommandBufferState m_state = CommandBufferState::Uninit;
|
|
||||||
CommandBufferType m_type;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_CMD_BUFFER__
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* CommandManager.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/04/02 17:48:52 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:20:53 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_COMMAND_MANAGER__
|
|
||||||
#define __MLX_COMMAND_MANAGER__
|
|
||||||
|
|
||||||
#include <Renderer/Core/RenderCore.h>
|
|
||||||
#include <Renderer/Command/CommandPool.h>
|
|
||||||
#include <Renderer/Command/CommandBuffer.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class CommandManager
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CommandManager() = default;
|
|
||||||
|
|
||||||
void Init() noexcept;
|
|
||||||
void BeginRecord(int active_image_index);
|
|
||||||
void EndRecord(int active_image_index);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline CommandPool& GetCmdPool() noexcept { return m_cmd_pool; }
|
|
||||||
inline CommandBuffer& GetCmdBuffer(int i) noexcept { return m_cmd_buffers[i]; }
|
|
||||||
|
|
||||||
~CommandManager() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::array<CommandBuffer, MAX_FRAMES_IN_FLIGHT> m_cmd_buffers;
|
|
||||||
CommandPool m_cmd_pool;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* CommandPool.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:24:12 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:33:15 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_CMD_POOL__
|
|
||||||
#define __MLX_VK_CMD_POOL__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class CommandPool
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
CommandPool() = default;
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkCommandPool& operator()() noexcept { return m_cmd_pool; }
|
|
||||||
inline VkCommandPool& Get() noexcept { return m_cmd_pool; }
|
|
||||||
|
|
||||||
~CommandPool() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkCommandPool m_cmd_pool = VK_NULL_HANDLE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_CMD_POOL__
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* CommandResource.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/12/16 20:44:29 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:37:06 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_COMMAND_RESOURCE__
|
|
||||||
#define __MLX_COMMAND_RESOURCE__
|
|
||||||
|
|
||||||
#include <Renderer/Enums.h>
|
|
||||||
#include <Core/UUID.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class CommandResource
|
|
||||||
{
|
|
||||||
friend class SingleTimeCmdManager;
|
|
||||||
|
|
||||||
public:
|
|
||||||
CommandResource() : m_uuid() {}
|
|
||||||
inline void RecordedInCmdBuffer() noexcept { m_state = CommandResourceState::Held; }
|
|
||||||
inline void RemovedFromCmdBuffer() noexcept { m_state = CommandResourceState::Free; }
|
|
||||||
inline UUID GetUUID() const noexcept { return m_uuid; }
|
|
||||||
virtual ~CommandResource() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
UUID m_uuid;
|
|
||||||
CommandResourceState m_state = CommandResourceState::Free;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* SingleTimeCmdManager.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/12/15 18:25:57 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:38:11 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_SINGLE_TIME_CMD_MANAGER__
|
|
||||||
#define __MLX_SINGLE_TIME_CMD_MANAGER__
|
|
||||||
|
|
||||||
#include <Renderer/Command/CommandBuffer.h>
|
|
||||||
#include <Renderer/Command/CommandPool.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class CommandBuffer;
|
|
||||||
|
|
||||||
class SingleTimeCmdManager
|
|
||||||
{
|
|
||||||
friend class RenderCore;
|
|
||||||
|
|
||||||
public:
|
|
||||||
SingleTimeCmdManager() = default;
|
|
||||||
|
|
||||||
void Init() noexcept;
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
void UpdateSingleTimesCmdBuffersSubmitState() noexcept;
|
|
||||||
void WaitForAllExecutions() noexcept;
|
|
||||||
|
|
||||||
inline CommandPool& GetCmdPool() noexcept { return m_pool; }
|
|
||||||
CommandBuffer& GetCmdBuffer() noexcept;
|
|
||||||
|
|
||||||
~SingleTimeCmdManager() = default;
|
|
||||||
|
|
||||||
inline static constexpr const std::uint8_t BASE_POOL_SIZE = 16;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<CommandBuffer> m_buffers;
|
|
||||||
CommandPool m_pool;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Device.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/08 19:13:42 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:47:21 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_DEVICE__
|
|
||||||
#define __MLX_VK_DEVICE__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Device
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Init();
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkDevice& operator()() noexcept { return m_device; }
|
|
||||||
inline VkDevice& Get() noexcept { return m_device; }
|
|
||||||
|
|
||||||
inline VkPhysicalDevice& GetPhysicalDevice() noexcept { return m_physical_device; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void PickPhysicalDevice();
|
|
||||||
bool CheckDeviceExtensionSupport(VkPhysicalDevice device);
|
|
||||||
int DeviceScore(VkPhysicalDevice device);
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkPhysicalDevice m_physical_device = VK_NULL_HANDLE;
|
|
||||||
VkDevice m_device = VK_NULL_HANDLE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_DEVICE__
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* DrawableResource.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/10 21:00:37 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 18:10:56 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_DRAWABLE_RESOURCE__
|
|
||||||
#define __MLX_DRAWABLE_RESOURCE__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class DrawableResource
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DrawableResource() = default;
|
|
||||||
virtual void Render(class Renderer& renderer) = 0;
|
|
||||||
virtual void ResetUpdate() {}
|
|
||||||
virtual ~DrawableResource() = default;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Fence.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/04/02 17:52:09 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:48:31 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_FENCE__
|
|
||||||
#define __MLX_VK_FENCE__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Fence
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Fence() = default;
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
|
|
||||||
inline VkFence& Get() noexcept { return m_fence; }
|
|
||||||
void Wait() noexcept;
|
|
||||||
void Reset() noexcept;
|
|
||||||
bool IsReady() const noexcept;
|
|
||||||
MLX_FORCEINLINE void WaitAndReset() noexcept { Wait(); Reset(); }
|
|
||||||
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
~Fence() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkFence m_fence = VK_NULL_HANDLE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Instance.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/08 19:03:04 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 18:44:02 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_INSTANCE__
|
|
||||||
#define __MLX_VK_INSTANCE__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Instance
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Init();
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline std::uint32_t GetInstanceVersion() const noexcept { return m_instance_version; }
|
|
||||||
|
|
||||||
inline VkInstance& operator()() noexcept { return m_instance; }
|
|
||||||
inline VkInstance& Get() noexcept { return m_instance; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<const char*> GetRequiredExtensions();
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkInstance m_instance = VK_NULL_HANDLE;
|
|
||||||
std::uint32_t m_instance_version = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_INSTANCE__
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Queues.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/08 19:01:49 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:38:50 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_QUEUES__
|
|
||||||
#define __MLX_VK_QUEUES__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Queues
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
struct QueueFamilyIndices
|
|
||||||
{
|
|
||||||
std::optional<std::uint32_t> graphics_family;
|
|
||||||
std::optional<std::uint32_t> present_family;
|
|
||||||
|
|
||||||
inline bool IsComplete() { return graphics_family.has_value() && present_family.has_value(); }
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
|
||||||
QueueFamilyIndices FindQueueFamilies(VkPhysicalDevice device);
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
|
|
||||||
inline VkQueue& GetGraphic() noexcept { return m_graphics_queue; }
|
|
||||||
inline VkQueue& GetPresent() noexcept { return m_present_queue; }
|
|
||||||
inline QueueFamilyIndices GetFamilies() noexcept
|
|
||||||
{
|
|
||||||
if(m_families.has_value())
|
|
||||||
return *m_families;
|
|
||||||
FatalError("Vulkan : cannot get queue families, not init");
|
|
||||||
return {}; // just to avoid warnings
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkQueue m_graphics_queue;
|
|
||||||
VkQueue m_present_queue;
|
|
||||||
std::optional<QueueFamilyIndices> m_families;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_QUEUES__
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* RenderCore.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/08 19:16:32 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:39:11 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_RENDER_CORE__
|
|
||||||
#define __MLX_RENDER_CORE__
|
|
||||||
|
|
||||||
#include <Renderer/Command/SingleTimeCmdManager.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorPoolManager.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorPool.h>
|
|
||||||
#include <Renderer/Core/Queues.h>
|
|
||||||
#include <Renderer/Core/Device.h>
|
|
||||||
#include <Renderer/Core/Instance.h>
|
|
||||||
#include <Renderer/Core/ValidationLayers.h>
|
|
||||||
#include <Renderer/Core/Memory.h>
|
|
||||||
|
|
||||||
#include <Utils/Singleton.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
const char* VerbaliseVkResult(VkResult result);
|
|
||||||
VkPipelineStageFlags AccessFlagsToPipelineStage(VkAccessFlags access_flags, VkPipelineStageFlags stage_flags);
|
|
||||||
|
|
||||||
#ifdef DEBUG
|
|
||||||
constexpr const bool enable_validation_layers = true;
|
|
||||||
#else
|
|
||||||
constexpr const bool enable_validation_layers = false;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
const std::vector<const char*> validation_layers = { "VK_LAYER_KHRONOS_validation" };
|
|
||||||
|
|
||||||
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
|
|
||||||
constexpr const int MAX_SETS_PER_POOL = 512;
|
|
||||||
constexpr const int NUMBER_OF_UNIFORM_BUFFERS = 1; // change this if for wathever reason more than one uniform buffer is needed
|
|
||||||
|
|
||||||
class RenderCore : public Singleton<RenderCore>
|
|
||||||
{
|
|
||||||
friend class Singleton<RenderCore>;
|
|
||||||
|
|
||||||
public:
|
|
||||||
void Init();
|
|
||||||
void Destroy();
|
|
||||||
|
|
||||||
inline bool IsInit() const noexcept { return m_is_init; }
|
|
||||||
inline Instance& GetInstance() noexcept { return m_instance; }
|
|
||||||
inline Device& GetDevice() noexcept { return m_device; }
|
|
||||||
inline Queues& GetQueue() noexcept { return m_queues; }
|
|
||||||
inline GPUallocator& GetAllocator() noexcept { return m_allocator; }
|
|
||||||
inline ValidationLayers& GetLayers() noexcept { return m_layers; }
|
|
||||||
inline CommandBuffer& GetSingleTimeCmdBuffer() noexcept { return m_cmd_manager.GetCmdBuffer(); }
|
|
||||||
inline SingleTimeCmdManager& GetSingleTimeCmdManager() noexcept { return m_cmd_manager; }
|
|
||||||
inline DescriptorPool& GetDescriptorPool() { return m_pool_manager.GetAvailablePool(); }
|
|
||||||
|
|
||||||
private:
|
|
||||||
RenderCore() = default;
|
|
||||||
~RenderCore() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
ValidationLayers m_layers;
|
|
||||||
SingleTimeCmdManager m_cmd_manager;
|
|
||||||
Queues m_queues;
|
|
||||||
DescriptorPoolManager m_pool_manager;
|
|
||||||
Device m_device;
|
|
||||||
Instance m_instance;
|
|
||||||
GPUallocator m_allocator;
|
|
||||||
bool m_is_init = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_RENDER_CORE__
|
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Semaphore.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/08 18:59:38 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:56:51 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_SEMAPHORE__
|
|
||||||
#define __MLX_VK_SEMAPHORE__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Semaphore
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Init();
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkSemaphore& Get() noexcept { return m_semaphore; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkSemaphore m_semaphore = VK_NULL_HANDLE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_SEMAPHORE__
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Surface.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/08 18:57:55 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:58:15 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_SURFACE__
|
|
||||||
#define __MLX_VK_SURFACE__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Surface
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Create(class Renderer& renderer);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
VkSurfaceFormatKHR ChooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR>& available_formats);
|
|
||||||
|
|
||||||
inline VkSurfaceKHR& operator()() noexcept { return m_surface; }
|
|
||||||
inline VkSurfaceKHR& Get() noexcept { return m_surface; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkSurfaceKHR m_surface = VK_NULL_HANDLE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_SURFACE__
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ValidationLayers.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/12/19 14:04:25 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 19:16:25 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __VK_VALIDATION_LAYERS__
|
|
||||||
#define __VK_VALIDATION_LAYERS__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class ValidationLayers
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
ValidationLayers() = default;
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
void Destroy();
|
|
||||||
|
|
||||||
bool CheckValidationLayerSupport();
|
|
||||||
void PopulateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& create_info);
|
|
||||||
|
|
||||||
VkResult SetDebugUtilsObjectNameEXT(VkObjectType object_type, std::uint64_t object_handle, const char* object_name);
|
|
||||||
|
|
||||||
~ValidationLayers() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkResult CreateDebugUtilsMessengerEXT(const VkDebugUtilsMessengerCreateInfoEXT* pCreateInfo, const VkAllocationCallbacks* pAllocator);
|
|
||||||
static VKAPI_ATTR VkBool32 VKAPI_CALL DebugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT message_severity, VkDebugUtilsMessageTypeFlagsEXT message_type, const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void* pUserData);
|
|
||||||
void DestroyDebugUtilsMessengerEXT(const VkAllocationCallbacks* pAllocator);
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkDebugUtilsMessengerEXT m_debug_messenger;
|
|
||||||
PFN_vkSetDebugUtilsObjectNameEXT f_vkSetDebugUtilsObjectNameEXT = nullptr;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
48
runtime/Includes/Renderer/Descriptor.h
git.filemode.normal_file
48
runtime/Includes/Renderer/Descriptor.h
git.filemode.normal_file
@@ -0,0 +1,48 @@
|
|||||||
|
#ifndef __MLX_DESCRIPTOR_SET__
|
||||||
|
#define __MLX_DESCRIPTOR_SET__
|
||||||
|
|
||||||
|
#include <Renderer/Enums.h>
|
||||||
|
#include <Renderer/RenderCore.h>
|
||||||
|
#include <Renderer/Pipelines/Shader.h>
|
||||||
|
#include <Renderer/Vulkan/VulkanPrototypes.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
struct Descriptor
|
||||||
|
{
|
||||||
|
NonOwningPtr<class GPUBuffer> storage_buffer_ptr;
|
||||||
|
NonOwningPtr<class GPUBuffer> uniform_buffer_ptr;
|
||||||
|
NonOwningPtr<class Image> image_ptr;
|
||||||
|
VkDescriptorType type;
|
||||||
|
ShaderType shader_type;
|
||||||
|
std::uint32_t binding;
|
||||||
|
};
|
||||||
|
|
||||||
|
class DescriptorSet
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
DescriptorSet() { m_set.fill(VK_NULL_HANDLE); }
|
||||||
|
DescriptorSet(const ShaderSetLayout& layout, VkDescriptorSetLayout vklayout, ShaderType shader_type);
|
||||||
|
|
||||||
|
void SetImage(std::size_t i, std::uint32_t binding, class Image& image);
|
||||||
|
void SetStorageBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer);
|
||||||
|
void SetUniformBuffer(std::size_t i, std::uint32_t binding, class GPUBuffer& buffer);
|
||||||
|
void Update(std::size_t i, VkCommandBuffer cmd = VK_NULL_HANDLE) noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] inline VkDescriptorSet GetSet(std::size_t i) const noexcept { return m_set[i]; }
|
||||||
|
[[nodiscard]] inline DescriptorSet Duplicate() const { return DescriptorSet{ m_set_layout, m_descriptors }; }
|
||||||
|
[[nodiscard]] inline bool IsInit() const noexcept { return m_set[0] != VK_NULL_HANDLE; }
|
||||||
|
|
||||||
|
~DescriptorSet() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
DescriptorSet(VkDescriptorSetLayout layout, const std::vector<Descriptor>& descriptors);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Descriptor> m_descriptors;
|
||||||
|
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_set;
|
||||||
|
VkDescriptorSetLayout m_set_layout;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* DescriptorPool.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/01/23 18:32:43 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 19:36:03 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __VK_DESCRIPTOR_POOL__
|
|
||||||
#define __VK_DESCRIPTOR_POOL__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class DescriptorPool
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DescriptorPool() = default;
|
|
||||||
|
|
||||||
void Init(std::vector<VkDescriptorPoolSize> sizes);
|
|
||||||
VkDescriptorSet AllocateDescriptorSet(class DescriptorSetLayout& layout);
|
|
||||||
void FreeDescriptor(VkDescriptorSet set);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkDescriptorPool& operator()() noexcept { return m_pool; }
|
|
||||||
inline VkDescriptorPool& Get() noexcept { return m_pool; }
|
|
||||||
inline std::size_t GetNumberOfSetsAllocated() const noexcept { return m_allocated_sets; }
|
|
||||||
|
|
||||||
inline bool IsInit() const noexcept { return m_pool != VK_NULL_HANDLE; }
|
|
||||||
|
|
||||||
~DescriptorPool() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkDescriptorPool m_pool = VK_NULL_HANDLE;
|
|
||||||
std::size_t m_allocated_sets = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* DescriptorPoolManager.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/20 06:26:26 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 19:40:22 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_DESCRIPTOR_POOL_MANAGER__
|
|
||||||
#define __MLX_DESCRIPTOR_POOL_MANAGER__
|
|
||||||
|
|
||||||
#include <Renderer/Descriptors/DescriptorPool.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class DescriptorPoolManager
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DescriptorPoolManager() = default;
|
|
||||||
|
|
||||||
DescriptorPool& GetAvailablePool();
|
|
||||||
void DestroyAllPools();
|
|
||||||
|
|
||||||
~DescriptorPoolManager() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::list<DescriptorPool> m_pools;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* DescriptorSet.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:42:22 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __VK_DESCRIPTOR_SET__
|
|
||||||
#define __VK_DESCRIPTOR_SET__
|
|
||||||
|
|
||||||
#include <Renderer/Core/RenderCore.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorSetLayout.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class DescriptorSet
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DescriptorSet() = default;
|
|
||||||
|
|
||||||
void Init(NonOwningPtr<class Renderer> renderer, NonOwningPtr<class DescriptorPool> pool, DescriptorSetLayout layout);
|
|
||||||
|
|
||||||
void WriteDescriptor(int binding, NonOwningPtr<class UniformBuffer> ubo) const noexcept;
|
|
||||||
void WriteDescriptor(int binding, const class Image& image) const noexcept;
|
|
||||||
|
|
||||||
inline bool IsInit() const noexcept { return p_pool && p_renderer; }
|
|
||||||
|
|
||||||
void Bind() noexcept;
|
|
||||||
|
|
||||||
DescriptorSet Duplicate();
|
|
||||||
|
|
||||||
VkDescriptorSet& operator()() noexcept;
|
|
||||||
VkDescriptorSet& Get() noexcept;
|
|
||||||
|
|
||||||
inline const DescriptorSetLayout& GetLayout() const noexcept { return m_layout; }
|
|
||||||
|
|
||||||
inline const std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT>& GetAllFramesDescriptorSets() const { return m_desc_set; }
|
|
||||||
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
~DescriptorSet() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
DescriptorSetLayout m_layout;
|
|
||||||
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> m_desc_set;
|
|
||||||
NonOwningPtr<class DescriptorPool> p_pool;
|
|
||||||
NonOwningPtr<class Renderer> p_renderer;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,38 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* DescriptorSetLayout.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/01/23 18:36:22 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 22:15:01 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __VK_DESCRIPTOR_SET_LAYOUT__
|
|
||||||
#define __VK_DESCRIPTOR_SET_LAYOUT__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class DescriptorSetLayout
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
DescriptorSetLayout() = default;
|
|
||||||
|
|
||||||
void Init(std::vector<std::pair<int, VkDescriptorType>> binds, VkShaderStageFlagBits stage);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkDescriptorSetLayout operator()() const noexcept { return m_layout; }
|
|
||||||
inline VkDescriptorSetLayout Get() const noexcept { return m_layout; }
|
|
||||||
inline const std::vector<std::pair<int, VkDescriptorType>>& GetBindings() const noexcept { return m_bindings; }
|
|
||||||
|
|
||||||
~DescriptorSetLayout() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<std::pair<int, VkDescriptorType>> m_bindings;
|
|
||||||
VkDescriptorSetLayout m_layout = VK_NULL_HANDLE;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,15 +1,3 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Enums.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/03/27 22:02:58 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:39:31 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_RENDERER_ENUMS__
|
#ifndef __MLX_RENDERER_ENUMS__
|
||||||
#define __MLX_RENDERER_ENUMS__
|
#define __MLX_RENDERER_ENUMS__
|
||||||
|
|
||||||
@@ -18,6 +6,7 @@ namespace mlx
|
|||||||
enum class BufferType
|
enum class BufferType
|
||||||
{
|
{
|
||||||
Constant = 0,
|
Constant = 0,
|
||||||
|
Staging,
|
||||||
HighDynamic, // typically stored in RAM
|
HighDynamic, // typically stored in RAM
|
||||||
LowDynamic, // typically stored in VRAM
|
LowDynamic, // typically stored in VRAM
|
||||||
|
|
||||||
@@ -25,35 +14,13 @@ namespace mlx
|
|||||||
};
|
};
|
||||||
constexpr std::size_t BufferTypeCount = static_cast<std::size_t>(BufferType::EndEnum);
|
constexpr std::size_t BufferTypeCount = static_cast<std::size_t>(BufferType::EndEnum);
|
||||||
|
|
||||||
enum class CommandResourceState
|
enum class ImageType
|
||||||
{
|
{
|
||||||
Held = 0,
|
Color = 0,
|
||||||
Free,
|
|
||||||
|
|
||||||
EndEnum
|
EndEnum
|
||||||
};
|
};
|
||||||
constexpr std::size_t CommandResourceStateCount = static_cast<std::size_t>(CommandResourceState::EndEnum);
|
constexpr std::size_t ImageTypeCount = static_cast<std::size_t>(ImageType::EndEnum);
|
||||||
|
|
||||||
enum class CommandBufferState
|
|
||||||
{
|
|
||||||
Uninit = 0, // buffer not initialized or destroyed
|
|
||||||
Ready, // buffer ready to be used after having been submitted
|
|
||||||
Idle, // buffer has recorded informations but has not been submitted
|
|
||||||
Recording, // buffer is currently recording
|
|
||||||
Submitted, // buffer has been submitted
|
|
||||||
|
|
||||||
EndEnum
|
|
||||||
};
|
|
||||||
constexpr std::size_t CommandBufferStateCount = static_cast<std::size_t>(CommandBufferState::EndEnum);
|
|
||||||
|
|
||||||
enum class CommandBufferType
|
|
||||||
{
|
|
||||||
SingleTime = 0,
|
|
||||||
LongTime,
|
|
||||||
|
|
||||||
EndEnum
|
|
||||||
};
|
|
||||||
constexpr std::size_t CommandBufferTypeCount = static_cast<std::size_t>(CommandBufferType::EndEnum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
102
runtime/Includes/Renderer/Image.h
git.filemode.normal_file
102
runtime/Includes/Renderer/Image.h
git.filemode.normal_file
@@ -0,0 +1,102 @@
|
|||||||
|
#ifndef __MLX_IMAGE__
|
||||||
|
#define __MLX_IMAGE__
|
||||||
|
|
||||||
|
#include <Maths/Vec4.h>
|
||||||
|
#include <Renderer/RenderCore.h>
|
||||||
|
#include <Renderer/Buffer.h>
|
||||||
|
#include <Utils/Buffer.h>
|
||||||
|
#include <Renderer/Enums.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class Image
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Image() = default;
|
||||||
|
|
||||||
|
inline void Init(VkImage image, VkFormat format, std::uint32_t width, std::uint32_t height, VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED) noexcept
|
||||||
|
{
|
||||||
|
m_image = image;
|
||||||
|
m_format = format;
|
||||||
|
m_width = width;
|
||||||
|
m_height = height;
|
||||||
|
m_layout = layout;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties, bool is_multisampled = false);
|
||||||
|
void CreateImageView(VkImageViewType type, VkImageAspectFlags aspectFlags, int layer_count = 1) noexcept;
|
||||||
|
void CreateSampler() noexcept;
|
||||||
|
void TransitionLayout(VkImageLayout new_layout, VkCommandBuffer cmd = VK_NULL_HANDLE);
|
||||||
|
void Clear(VkCommandBuffer cmd, Vec4f color);
|
||||||
|
|
||||||
|
void DestroySampler() noexcept;
|
||||||
|
void DestroyImageView() noexcept;
|
||||||
|
virtual void Destroy() noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkImage Get() const noexcept { return m_image; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkImage operator()() const noexcept { return m_image; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkDeviceMemory GetDeviceMemory() const noexcept { return m_memory.memory; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkImageView GetImageView() const noexcept { return m_image_view; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkFormat GetFormat() const noexcept { return m_format; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkImageTiling GetTiling() const noexcept { return m_tiling; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkImageLayout GetLayout() const noexcept { return m_layout; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkSampler GetSampler() const noexcept { return m_sampler; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE std::uint32_t GetWidth() const noexcept { return m_width; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE std::uint32_t GetHeight() const noexcept { return m_height; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE bool IsInit() const noexcept { return m_image != VK_NULL_HANDLE; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE ImageType GetType() const noexcept { return m_type; }
|
||||||
|
|
||||||
|
virtual ~Image() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
VmaAllocation m_allocation;
|
||||||
|
VkImage m_image = VK_NULL_HANDLE;
|
||||||
|
VkImageView m_image_view = VK_NULL_HANDLE;
|
||||||
|
VkSampler m_sampler = VK_NULL_HANDLE;
|
||||||
|
VkFormat m_format;
|
||||||
|
VkImageTiling m_tiling;
|
||||||
|
VkImageLayout m_layout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||||
|
ImageType m_type;
|
||||||
|
std::uint32_t m_width = 0;
|
||||||
|
std::uint32_t m_height = 0;
|
||||||
|
bool m_is_multisampled = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Texture : public Image
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Texture() = default;
|
||||||
|
Texture(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB, bool is_multisampled = false)
|
||||||
|
{
|
||||||
|
Init(std::move(pixels), width, height, format, is_multisampled);
|
||||||
|
}
|
||||||
|
inline void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format = VK_FORMAT_R8G8B8A8_SRGB, bool is_multisampled = false)
|
||||||
|
{
|
||||||
|
Image::Init(ImageType::Color, width, height, format, VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, is_multisampled);
|
||||||
|
Image::CreateImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||||
|
Image::CreateSampler();
|
||||||
|
if(pixels)
|
||||||
|
{
|
||||||
|
TransitionLayout(VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL);
|
||||||
|
GPUBuffer staging_buffer;
|
||||||
|
std::size_t size = width * height * kvfFormatSize(format);
|
||||||
|
staging_buffer.Init(BufferType::Staging, size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, pixels);
|
||||||
|
VkCommandBuffer cmd = kvfCreateCommandBuffer(RenderCore::Get().GetDevice());
|
||||||
|
kvfBeginCommandBuffer(cmd, VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
|
||||||
|
kvfCopyBufferToImage(cmd, Image::Get(), staging_buffer.Get(), staging_buffer.GetOffset(), VK_IMAGE_ASPECT_COLOR_BIT, { width, height, 1 });
|
||||||
|
vkEndCommandBuffer(cmd);
|
||||||
|
VkFence fence = kvfCreateFence(RenderCore::Get().GetDevice());
|
||||||
|
kvfSubmitSingleTimeCommandBuffer(RenderCore::Get().GetDevice(), cmd, KVF_GRAPHICS_QUEUE, fence);
|
||||||
|
kvfDestroyFence(RenderCore::Get().GetDevice(), fence);
|
||||||
|
staging_buffer.Destroy();
|
||||||
|
}
|
||||||
|
if(!pixels)
|
||||||
|
TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
|
||||||
|
else
|
||||||
|
TransitionLayout(VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
|
||||||
|
}
|
||||||
|
~Texture() override { Destroy(); }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Image.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/01/25 11:54:21 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:40:51 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_IMAGE__
|
|
||||||
#define __MLX_VK_IMAGE__
|
|
||||||
|
|
||||||
#include <Renderer/Command/CommandResource.h>
|
|
||||||
#include <Renderer/Command/CommandBuffer.h>
|
|
||||||
#include <Renderer/Command/CommandPool.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
std::uint32_t FormatSize(VkFormat format);
|
|
||||||
bool IsStencilFormat(VkFormat format);
|
|
||||||
bool IsDepthFormat(VkFormat format);
|
|
||||||
VkFormat BitsToFormat(std::uint32_t bits);
|
|
||||||
VkPipelineStageFlags LayoutToAccessMask(VkImageLayout layout, bool is_destination);
|
|
||||||
|
|
||||||
class Image : public CommandResource
|
|
||||||
{
|
|
||||||
friend class Swapchain;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Image() = default;
|
|
||||||
|
|
||||||
inline void Create(VkImage image, VkFormat format, std::uint32_t width, std::uint32_t height, VkImageLayout layout = VK_IMAGE_LAYOUT_UNDEFINED) noexcept
|
|
||||||
{
|
|
||||||
m_image = image;
|
|
||||||
m_format = format;
|
|
||||||
m_width = width;
|
|
||||||
m_height = height;
|
|
||||||
m_layout = layout;
|
|
||||||
}
|
|
||||||
void Create(std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, const char* name, bool decated_memory = false);
|
|
||||||
void CreateImageView(VkImageViewType type, VkImageAspectFlags aspect_flags) noexcept;
|
|
||||||
void CreateSampler() noexcept;
|
|
||||||
void CopyFromBuffer(class Buffer& buffer);
|
|
||||||
void CopyToBuffer(class Buffer& buffer);
|
|
||||||
void TransitionLayout(VkImageLayout new_layout, NonOwningPtr<CommandBuffer> cmd = nullptr);
|
|
||||||
virtual void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkImage Get() noexcept { return m_image; }
|
|
||||||
inline VkImage operator()() noexcept { return m_image; }
|
|
||||||
inline VkImageView GetImageView() const noexcept { return m_image_view; }
|
|
||||||
inline VkFormat GetFormat() const noexcept { return m_format; }
|
|
||||||
inline VkImageTiling GetTiling() const noexcept { return m_tiling; }
|
|
||||||
inline VkImageLayout GetLayout() const noexcept { return m_layout; }
|
|
||||||
inline VkSampler GetSampler() const noexcept { return m_sampler; }
|
|
||||||
inline std::uint32_t GetWidth() const noexcept { return m_width; }
|
|
||||||
inline std::uint32_t GetHeight() const noexcept { return m_height; }
|
|
||||||
inline bool IsInit() const noexcept { return m_image != VK_NULL_HANDLE; }
|
|
||||||
|
|
||||||
virtual ~Image() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void DestroySampler() noexcept;
|
|
||||||
void DestroyImageView() noexcept;
|
|
||||||
|
|
||||||
private:
|
|
||||||
VmaAllocation m_allocation;
|
|
||||||
VkImage m_image = VK_NULL_HANDLE;
|
|
||||||
VkImageView m_image_view = VK_NULL_HANDLE;
|
|
||||||
VkSampler m_sampler = VK_NULL_HANDLE;
|
|
||||||
#ifdef DEBUG
|
|
||||||
std::string m_name;
|
|
||||||
#endif
|
|
||||||
VkFormat m_format;
|
|
||||||
VkImageTiling m_tiling;
|
|
||||||
VkImageLayout m_layout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
||||||
std::uint32_t m_width = 0;
|
|
||||||
std::uint32_t m_height = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Texture.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:42:41 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_TEXTURE__
|
|
||||||
#define __MLX_TEXTURE__
|
|
||||||
|
|
||||||
#include <Renderer/Images/Image.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorSet.h>
|
|
||||||
#include <Renderer/Buffers/IndexBuffer.h>
|
|
||||||
#include <Renderer/Buffers/VertexBuffer.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class Texture : public Image
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Texture() = default;
|
|
||||||
|
|
||||||
void Create(std::uint8_t* pixels, std::uint32_t width, std::uint32_t height, VkFormat format, const char* name, bool dedicated_memory = false);
|
|
||||||
void Render(class Renderer& renderer, int x, int y);
|
|
||||||
void Destroy() noexcept override;
|
|
||||||
|
|
||||||
void SetPixel(int x, int y, std::uint32_t color) noexcept;
|
|
||||||
int GetPixel(int x, int y) noexcept;
|
|
||||||
|
|
||||||
inline void SetDescriptor(DescriptorSet&& set) noexcept { m_set = set; }
|
|
||||||
inline VkDescriptorSet GetSet() noexcept { return m_set.IsInit() ? m_set.Get() : VK_NULL_HANDLE; }
|
|
||||||
inline void UpdateSet(int binding) noexcept { m_set.WriteDescriptor(binding, *this); m_has_set_been_updated = true; }
|
|
||||||
inline bool HasBeenUpdated() const noexcept { return m_has_set_been_updated; }
|
|
||||||
inline constexpr void ResetUpdate() noexcept { m_has_set_been_updated = false; }
|
|
||||||
|
|
||||||
~Texture() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
void OpenCPUmap();
|
|
||||||
|
|
||||||
private:
|
|
||||||
ConstantVertexBuffer m_vbo;
|
|
||||||
ConstantIndexBuffer m_ibo;
|
|
||||||
#ifdef DEBUG
|
|
||||||
std::string m_name;
|
|
||||||
#endif
|
|
||||||
DescriptorSet m_set;
|
|
||||||
std::vector<std::uint32_t> m_cpu_map;
|
|
||||||
std::optional<Buffer> m_buf_map = std::nullopt;
|
|
||||||
void* m_map = nullptr;
|
|
||||||
bool m_has_been_modified = false;
|
|
||||||
bool m_has_set_been_updated = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
Texture* StbTextureLoad(std::filesystem::path file, int* w, int* h);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* TextureAtlas.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:52:40 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_TEXTURE_ATLAS__
|
|
||||||
#define __MLX_TEXTURE_ATLAS__
|
|
||||||
|
|
||||||
#include <Renderer/Images/Texture.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class TextureAtlas : public Image
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
TextureAtlas() = default;
|
|
||||||
|
|
||||||
void Create(std::uint8_t* pixels, std::uint32_t width, std::uint32_t height, VkFormat format, const char* name, bool dedicated_memory = false);
|
|
||||||
void Render(class Renderer& renderer, int x, int y, std::uint32_t ibo_size) const;
|
|
||||||
void Destroy() noexcept override;
|
|
||||||
|
|
||||||
inline void SetDescriptor(DescriptorSet&& set) noexcept { m_set = set; }
|
|
||||||
inline DescriptorSet GetSet() noexcept { return m_set; }
|
|
||||||
inline void UpdateSet(int binding) noexcept { m_set.WriteDescriptor(binding, *this); m_has_been_updated = true; }
|
|
||||||
inline bool HasBeenUpdated() const noexcept { return m_has_been_updated; }
|
|
||||||
inline constexpr void ResetUpdate() noexcept { m_has_been_updated = false; }
|
|
||||||
|
|
||||||
~TextureAtlas() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
DescriptorSet m_set;
|
|
||||||
bool m_has_been_updated = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* TextureDescriptor.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/11 01:00:13 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 22:08:02 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_TEXTURE_DESCRIPTOR__
|
|
||||||
#define __MLX_TEXTURE_DESCRIPTOR__
|
|
||||||
|
|
||||||
#include <Renderer/Images/Texture.h>
|
|
||||||
#include <Renderer/Core/DrawableResource.h>
|
|
||||||
#include <Utils/CombineHash.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
struct TextureRenderDescriptor : public DrawableResource
|
|
||||||
{
|
|
||||||
NonOwningPtr<Texture> texture;
|
|
||||||
int x;
|
|
||||||
int y;
|
|
||||||
|
|
||||||
TextureRenderDescriptor(NonOwningPtr<Texture> _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {}
|
|
||||||
inline bool operator==(const TextureRenderDescriptor& rhs) const { return texture == rhs.texture && x == rhs.x && y == rhs.y; }
|
|
||||||
inline void Render(class Renderer& renderer) override
|
|
||||||
{
|
|
||||||
if(!texture->IsInit())
|
|
||||||
return;
|
|
||||||
texture->Render(renderer, x, y);
|
|
||||||
}
|
|
||||||
inline void ResetUpdate() override
|
|
||||||
{
|
|
||||||
if(!texture->IsInit())
|
|
||||||
return;
|
|
||||||
texture->ResetUpdate();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace std
|
|
||||||
{
|
|
||||||
template <>
|
|
||||||
struct hash<mlx::TextureRenderDescriptor>
|
|
||||||
{
|
|
||||||
std::size_t operator()(const mlx::TextureRenderDescriptor& d) const noexcept
|
|
||||||
{
|
|
||||||
std::size_t hash = 0;
|
|
||||||
mlx::HashCombine(hash, d.texture, d.x, d.y);
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* TextureRegistry.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/01/11 00:56:15 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:43:48 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_TEXTURE_MANAGER__
|
|
||||||
#define __MLX_TEXTURE_MANAGER__
|
|
||||||
|
|
||||||
#include <Renderer/Images/TextureDescriptor.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class TextureRegistry
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
TextureRegistry() = default;
|
|
||||||
|
|
||||||
inline void Clear();
|
|
||||||
inline std::pair<NonOwningPtr<DrawableResource>, bool> RegisterTexture(NonOwningPtr<Texture> texture, int x, int y);
|
|
||||||
inline bool IsTextureKnown(NonOwningPtr<Texture> texture) noexcept;
|
|
||||||
inline void EraseTextures(NonOwningPtr<Texture> texture);
|
|
||||||
|
|
||||||
~TextureRegistry() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::unordered_set<TextureRenderDescriptor> m_texture_descriptors;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#include <Renderer/Images/TextureRegistry.inl>
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,52 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* TextureRegistry.inl :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/04/23 22:08:46 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/23 22:11:09 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <Renderer/Images/TextureRegistry.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
void TextureRegistry::Clear()
|
|
||||||
{
|
|
||||||
m_texture_descriptors.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<NonOwningPtr<DrawableResource>, bool> TextureRegistry::RegisterTexture(NonOwningPtr<Texture> texture, int x, int y)
|
|
||||||
{
|
|
||||||
MLX_PROFILE_FUNCTION();
|
|
||||||
auto res = m_texture_descriptors.emplace(texture, x, y);
|
|
||||||
return std::make_pair(static_cast<DrawableResource*>(&const_cast<TextureRenderDescriptor&>(*res.first)), res.second);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TextureRegistry::IsTextureKnown(NonOwningPtr<Texture> texture) noexcept
|
|
||||||
{
|
|
||||||
MLX_PROFILE_FUNCTION();
|
|
||||||
for(const auto& desc : m_texture_descriptors)
|
|
||||||
{
|
|
||||||
if(desc.texture == texture)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void TextureRegistry::EraseTextures(NonOwningPtr<Texture> texture)
|
|
||||||
{
|
|
||||||
MLX_PROFILE_FUNCTION();
|
|
||||||
for(auto it = m_texture_descriptors.begin(); it != m_texture_descriptors.end();)
|
|
||||||
{
|
|
||||||
if(it->texture == texture)
|
|
||||||
it = m_texture_descriptors.erase(it);
|
|
||||||
else
|
|
||||||
++it;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,24 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Memory.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/10/20 02:13:03 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/27 22:49:57 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_MEMORY__
|
#ifndef __MLX_VK_MEMORY__
|
||||||
#define __MLX_VK_MEMORY__
|
#define __MLX_VK_MEMORY__
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
class GPUallocator
|
class GPUAllocator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
GPUallocator() = default;
|
GPUAllocator() = default;
|
||||||
|
|
||||||
void Init() noexcept;
|
void Init() noexcept;
|
||||||
void Destroy() noexcept;
|
void Destroy() noexcept;
|
||||||
@@ -36,7 +24,7 @@ namespace mlx
|
|||||||
|
|
||||||
void Flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept;
|
void Flush(VmaAllocation allocation, VkDeviceSize size, VkDeviceSize offset) noexcept;
|
||||||
|
|
||||||
~GPUallocator() = default;
|
~GPUAllocator() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VmaAllocator m_allocator;
|
VmaAllocator m_allocator;
|
||||||
57
runtime/Includes/Renderer/Pipelines/Graphics.h
git.filemode.normal_file
57
runtime/Includes/Renderer/Pipelines/Graphics.h
git.filemode.normal_file
@@ -0,0 +1,57 @@
|
|||||||
|
#ifndef __MLX_GRAPHICS_PIPELINE__
|
||||||
|
#define __MLX_GRAPHICS_PIPELINE__
|
||||||
|
|
||||||
|
#include <Renderer/Image.h>
|
||||||
|
#include <Utils/NonOwningPtr.h>
|
||||||
|
#include <Renderer/Pipelines/Shader.h>
|
||||||
|
#include <Renderer/Pipelines/Pipeline.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
struct GraphicPipelineDescriptor
|
||||||
|
{
|
||||||
|
std::shared_ptr<Shader> vertex_shader;
|
||||||
|
std::shared_ptr<Shader> fragment_shader;
|
||||||
|
std::vector<NonOwningPtr<Texture>> color_attachments;
|
||||||
|
NonOwningPtr<class Renderer> renderer = nullptr;
|
||||||
|
bool clear_color_attachments = true;
|
||||||
|
bool no_vertex_inputs = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
class GraphicPipeline : public Pipeline
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
GraphicPipeline() = default;
|
||||||
|
|
||||||
|
void Init(const GraphicPipelineDescriptor& descriptor);
|
||||||
|
bool BindPipeline(VkCommandBuffer command_buffer, std::size_t framebuffer_index, std::array<float, 4> clear) noexcept;
|
||||||
|
void EndPipeline(VkCommandBuffer command_buffer) noexcept override;
|
||||||
|
void Destroy() noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] inline VkPipeline GetPipeline() const override { return m_pipeline; }
|
||||||
|
[[nodiscard]] inline VkPipelineLayout GetPipelineLayout() const override { return m_pipeline_layout; }
|
||||||
|
[[nodiscard]] inline VkPipelineBindPoint GetPipelineBindPoint() const override { return VK_PIPELINE_BIND_POINT_GRAPHICS; }
|
||||||
|
|
||||||
|
~GraphicPipeline() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void CreateFramebuffers(const std::vector<NonOwningPtr<Texture>>& render_targets, bool clear_attachments);
|
||||||
|
void TransitionAttachments(VkCommandBuffer cmd = VK_NULL_HANDLE);
|
||||||
|
|
||||||
|
// Private override to remove access
|
||||||
|
bool BindPipeline(VkCommandBuffer) noexcept override { return false; };
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<NonOwningPtr<Texture>> m_attachments;
|
||||||
|
std::vector<VkFramebuffer> m_framebuffers;
|
||||||
|
std::vector<VkClearValue> m_clears;
|
||||||
|
std::shared_ptr<Shader> p_vertex_shader;
|
||||||
|
std::shared_ptr<Shader> p_fragment_shader;
|
||||||
|
VkRenderPass m_renderpass = VK_NULL_HANDLE;
|
||||||
|
VkPipeline m_pipeline = VK_NULL_HANDLE;
|
||||||
|
VkPipelineLayout m_pipeline_layout = VK_NULL_HANDLE;
|
||||||
|
NonOwningPtr<class Renderer> p_renderer;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,36 +1,23 @@
|
|||||||
/* ************************************************************************** */
|
#ifndef __MLX_PIPELINE__
|
||||||
/* */
|
#define __MLX_PIPELINE__
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Pipeline.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/12/18 21:23:52 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/28 22:15:38 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __PIPELINE__
|
#include <Renderer/Vulkan/VulkanPrototypes.h>
|
||||||
#define __PIPELINE__
|
|
||||||
|
|
||||||
#include <Renderer/Command/CommandBuffer.h>
|
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
class GraphicPipeline
|
class Pipeline
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void init(class Renderer& renderer);
|
Pipeline() = default;
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline void BindPipeline(CommandBuffer& command_buffer) noexcept { vkCmdBindPipeline(command_buffer.Get(), VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphics_pipeline); }
|
inline virtual bool BindPipeline(VkCommandBuffer command_buffer) noexcept { vkCmdBindPipeline(command_buffer, GetPipelineBindPoint(), GetPipeline()); return true; }
|
||||||
|
inline virtual void EndPipeline([[maybe_unused]] VkCommandBuffer command_buffer) noexcept {}
|
||||||
|
|
||||||
inline const VkPipeline& GetPipeline() const noexcept { return m_graphics_pipeline; }
|
virtual VkPipeline GetPipeline() const = 0;
|
||||||
inline const VkPipelineLayout& GetPipelineLayout() const noexcept { return m_pipeline_layout; }
|
virtual VkPipelineLayout GetPipelineLayout() const = 0;
|
||||||
|
virtual VkPipelineBindPoint GetPipelineBindPoint() const = 0;
|
||||||
|
|
||||||
private:
|
virtual ~Pipeline() = default;
|
||||||
VkPipeline m_graphics_pipeline = VK_NULL_HANDLE;
|
|
||||||
VkPipelineLayout m_pipeline_layout = VK_NULL_HANDLE;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
68
runtime/Includes/Renderer/Pipelines/Shader.h
git.filemode.normal_file
68
runtime/Includes/Renderer/Pipelines/Shader.h
git.filemode.normal_file
@@ -0,0 +1,68 @@
|
|||||||
|
#ifndef __MLX_SHADER__
|
||||||
|
#define __MLX_SHADER__
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
struct ShaderSetLayout
|
||||||
|
{
|
||||||
|
std::vector<std::pair<int, VkDescriptorType> > binds;
|
||||||
|
|
||||||
|
ShaderSetLayout(std::vector<std::pair<int, VkDescriptorType> > b) : binds(std::move(b)) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ShaderPushConstantLayout
|
||||||
|
{
|
||||||
|
std::size_t offset;
|
||||||
|
std::size_t size;
|
||||||
|
|
||||||
|
ShaderPushConstantLayout(std::size_t o, std::size_t s) : offset(o), size(s) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ShaderLayout
|
||||||
|
{
|
||||||
|
std::vector<std::pair<int, ShaderSetLayout> > set_layouts;
|
||||||
|
std::vector<ShaderPushConstantLayout> push_constants;
|
||||||
|
|
||||||
|
ShaderLayout(std::vector<std::pair<int, ShaderSetLayout> > s, std::vector<ShaderPushConstantLayout> pc) : set_layouts(std::move(s)), push_constants(std::move(pc)) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class ShaderType
|
||||||
|
{
|
||||||
|
Vertex,
|
||||||
|
Fragment,
|
||||||
|
Compute
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ShaderPipelineLayoutPart
|
||||||
|
{
|
||||||
|
std::vector<VkPushConstantRange> push_constants;
|
||||||
|
std::vector<VkDescriptorSetLayout> set_layouts;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Shader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Shader(const std::vector<std::uint8_t>& bytecode, ShaderType type, ShaderLayout layout);
|
||||||
|
|
||||||
|
[[nodiscard]] inline const ShaderLayout& GetShaderLayout() const { return m_layout; }
|
||||||
|
[[nodiscard]] inline const std::vector<std::uint8_t>& GetByteCode() const noexcept { return m_bytecode; }
|
||||||
|
[[nodiscard]] inline const ShaderPipelineLayoutPart& GetPipelineLayout() const noexcept { return m_pipeline_layout_part; }
|
||||||
|
[[nodiscard]] inline VkShaderModule GetShaderModule() const noexcept { return m_module; }
|
||||||
|
[[nodiscard]] inline VkShaderStageFlagBits GetShaderStage() const noexcept { return m_stage; }
|
||||||
|
|
||||||
|
~Shader();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void GeneratePipelineLayout(ShaderLayout layout);
|
||||||
|
|
||||||
|
private:
|
||||||
|
ShaderLayout m_layout;
|
||||||
|
ShaderPipelineLayoutPart m_pipeline_layout_part;
|
||||||
|
std::vector<std::uint8_t> m_bytecode;
|
||||||
|
std::vector<VkDescriptorSetLayout> m_set_layouts;
|
||||||
|
VkShaderStageFlagBits m_stage;
|
||||||
|
VkShaderModule m_module = VK_NULL_HANDLE;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* PixelPut.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/04/24 01:46:11 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_PIXEL_PUT__
|
|
||||||
#define __MLX_PIXEL_PUT__
|
|
||||||
|
|
||||||
#include <Renderer/Images/Texture.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorSet.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class PixelPutPipeline
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
PixelPutPipeline() = default;
|
|
||||||
|
|
||||||
void Init(std::uint32_t width, std::uint32_t height, class Renderer& renderer) noexcept;
|
|
||||||
|
|
||||||
void SetPixel(int x, int y, std::uint32_t color) noexcept;
|
|
||||||
void Render(class Renderer& renderer) noexcept;
|
|
||||||
|
|
||||||
void Clear();
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
~PixelPutPipeline() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
Texture m_texture;
|
|
||||||
Buffer m_buffer;
|
|
||||||
// using vector as CPU map and not directly writting to mapped buffer to improve performances
|
|
||||||
std::vector<std::uint32_t> m_cpu_map;
|
|
||||||
void* m_buffer_map = nullptr;
|
|
||||||
std::uint32_t m_width = 0;
|
|
||||||
std::uint32_t m_height = 0;
|
|
||||||
bool m_has_been_modified = true;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
39
runtime/Includes/Renderer/RenderCore.h
git.filemode.normal_file
39
runtime/Includes/Renderer/RenderCore.h
git.filemode.normal_file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef __MLX_RENDER_CORE__
|
||||||
|
#define __MLX_RENDER_CORE__
|
||||||
|
|
||||||
|
#include <Renderer/Memory.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
constexpr const int MAX_FRAMES_IN_FLIGHT = 3;
|
||||||
|
|
||||||
|
class RenderCore : public Singleton<RenderCore>
|
||||||
|
{
|
||||||
|
friend class Singleton<RenderCore>;
|
||||||
|
|
||||||
|
public:
|
||||||
|
void Init() noexcept;
|
||||||
|
void Destroy() noexcept;
|
||||||
|
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkInstance GetInstance() const noexcept { return m_instance; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkInstance& GetInstanceRef() noexcept { return m_instance; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkDevice GetDevice() const noexcept { return m_device; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE VkPhysicalDevice GetPhysicalDevice() const noexcept { return m_physical_device; }
|
||||||
|
[[nodiscard]] MLX_FORCEINLINE GPUAllocator& GetAllocator() noexcept { return m_allocator; }
|
||||||
|
|
||||||
|
|
||||||
|
inline void WaitDeviceIdle() const noexcept { vkDeviceWaitIdle(m_device); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
RenderCore() = default;
|
||||||
|
~RenderCore() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
GPUAllocator m_allocator;
|
||||||
|
VkInstance m_instance = VK_NULL_HANDLE;
|
||||||
|
VkDevice m_device = VK_NULL_HANDLE;
|
||||||
|
VkPhysicalDevice m_physical_device = VK_NULL_HANDLE;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
29
runtime/Includes/Renderer/RenderPasses/2DPass.h
git.filemode.normal_file
29
runtime/Includes/Renderer/RenderPasses/2DPass.h
git.filemode.normal_file
@@ -0,0 +1,29 @@
|
|||||||
|
#ifndef __MLX_2D_PASS__
|
||||||
|
#define __MLX_2D_PASS__
|
||||||
|
|
||||||
|
#include <Renderer/Descriptor.h>
|
||||||
|
#include <Renderer/Pipelines/Shader.h>
|
||||||
|
#include <Renderer/Pipelines/Graphics.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class Render2DPass
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Render2DPass() = default;
|
||||||
|
void Init();
|
||||||
|
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
|
||||||
|
void Destroy();
|
||||||
|
~Render2DPass() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
GraphicPipeline m_pipeline;
|
||||||
|
std::shared_ptr<DescriptorSet> p_viewer_data_set;
|
||||||
|
std::shared_ptr<UniformBuffer> p_viewer_data_buffer;
|
||||||
|
std::shared_ptr<DescriptorSet> p_texture_set;
|
||||||
|
std::shared_ptr<Shader> p_vertex_shader;
|
||||||
|
std::shared_ptr<Shader> p_fragment_shader;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
27
runtime/Includes/Renderer/RenderPasses/FinalPass.h
git.filemode.normal_file
27
runtime/Includes/Renderer/RenderPasses/FinalPass.h
git.filemode.normal_file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef __MLX_FINAL_PASS__
|
||||||
|
#define __MLX_FINAL_PASS__
|
||||||
|
|
||||||
|
#include <Renderer/Descriptor.h>
|
||||||
|
#include <Renderer/Pipelines/Shader.h>
|
||||||
|
#include <Renderer/Pipelines/Graphics.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class FinalPass
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
FinalPass() = default;
|
||||||
|
void Init();
|
||||||
|
void Pass(class Scene& scene, class Renderer& renderer, class Texture& render_target);
|
||||||
|
void Destroy();
|
||||||
|
~FinalPass() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
GraphicPipeline m_pipeline;
|
||||||
|
std::shared_ptr<DescriptorSet> p_set;
|
||||||
|
std::shared_ptr<Shader> p_vertex_shader;
|
||||||
|
std::shared_ptr<Shader> p_fragment_shader;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
26
runtime/Includes/Renderer/RenderPasses/Passes.h
git.filemode.normal_file
26
runtime/Includes/Renderer/RenderPasses/Passes.h
git.filemode.normal_file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef __MLX_PASSES__
|
||||||
|
#define __MLX_PASSES__
|
||||||
|
|
||||||
|
#include <Renderer/Image.h>
|
||||||
|
#include <Renderer/RenderPasses/FinalPass.h>
|
||||||
|
#include <Renderer/RenderPasses/2DPass.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class RenderPasses
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
RenderPasses() = default;
|
||||||
|
void Init();
|
||||||
|
void Pass(class Scene& scene, class Renderer& renderer);
|
||||||
|
void Destroy();
|
||||||
|
~RenderPasses() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Render2DPass m_2Dpass;
|
||||||
|
FinalPass m_final;
|
||||||
|
Texture m_main_render_texture;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,31 +1,11 @@
|
|||||||
/* ************************************************************************** */
|
#ifndef __MLX_RENDERER__
|
||||||
/* */
|
#define __MLX_RENDERER__
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Renderer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/12/18 17:14:45 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/07/05 13:40:20 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __RENDERER__
|
#include <Platform/Window.h>
|
||||||
#define __RENDERER__
|
#include <Utils/NonOwningPtr.h>
|
||||||
|
#include <Renderer/RenderCore.h>
|
||||||
#include <Renderer/Vertex.h>
|
#include <Renderer/Image.h>
|
||||||
#include <Renderer/Buffers/UniformBuffer.h>
|
#include <Core/EventBus.h>
|
||||||
#include <Renderer/Core/Surface.h>
|
|
||||||
#include <Renderer/Core/RenderCore.h>
|
|
||||||
#include <Renderer/Core/Semaphore.h>
|
|
||||||
#include <Renderer/Pipelines/Pipeline.h>
|
|
||||||
#include <Renderer/Command/CommandManager.h>
|
|
||||||
#include <Renderer/Renderpass/Swapchain.h>
|
|
||||||
#include <Renderer/Renderpass/RenderPass.h>
|
|
||||||
#include <Renderer/Renderpass/FrameBuffer.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorSet.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorPool.h>
|
|
||||||
#include <Renderer/Descriptors/DescriptorSetLayout.h>
|
|
||||||
|
|
||||||
namespace mlx
|
namespace mlx
|
||||||
{
|
{
|
||||||
@@ -34,57 +14,48 @@ namespace mlx
|
|||||||
public:
|
public:
|
||||||
Renderer() = default;
|
Renderer() = default;
|
||||||
|
|
||||||
void Init(NonOwningPtr<class Texture> render_target);
|
void Init(NonOwningPtr<Window> window);
|
||||||
|
|
||||||
bool BeginFrame();
|
bool BeginFrame();
|
||||||
void EndFrame();
|
void EndFrame();
|
||||||
|
|
||||||
void Destroy();
|
[[nodiscard]] inline VkSwapchainKHR GetSwapchain() const noexcept { return m_swapchain; }
|
||||||
|
[[nodiscard]] inline VkSurfaceKHR GetSurface() const noexcept { return m_surface; }
|
||||||
|
[[nodiscard]] inline VkSemaphore GetImageAvailableSemaphore(int index) const noexcept { return m_image_available_semaphores[index]; }
|
||||||
|
[[nodiscard]] inline VkSemaphore GetRenderFinishedSemaphore(int index) const noexcept { return m_render_finished_semaphores[index]; }
|
||||||
|
[[nodiscard]] inline VkCommandBuffer GetCommandBuffer(int index) const noexcept { return m_cmd_buffers[index]; }
|
||||||
|
[[nodiscard]] inline VkCommandBuffer GetActiveCommandBuffer() const noexcept { return m_cmd_buffers[m_current_frame_index]; }
|
||||||
|
[[nodiscard]] inline const std::vector<Image>& GetSwapchainImages() const { return m_swapchain_images; }
|
||||||
|
[[nodiscard]] inline std::size_t& GetDrawCallsCounterRef() noexcept { return m_drawcalls; }
|
||||||
|
[[nodiscard]] inline std::size_t& GetPolygonDrawnCounterRef() noexcept { return m_polygons_drawn; }
|
||||||
|
[[nodiscard]] inline std::size_t GetSwapchainImageIndex() const noexcept { return m_swapchain_image_index; }
|
||||||
|
[[nodiscard]] inline std::size_t GetCurrentFrameIndex() const noexcept { return m_current_frame_index; }
|
||||||
|
[[nodiscard]] inline NonOwningPtr<Window> GetWindow() const noexcept { return m_window_ptr; }
|
||||||
|
|
||||||
inline NonOwningPtr<class Window> GetWindow() { return m_window; }
|
MLX_FORCEINLINE constexpr void RequireFramebufferResize() noexcept { m_framebuffers_resize = true; }
|
||||||
inline void SetWindow(NonOwningPtr<class Window> window) { m_window = window; }
|
|
||||||
|
|
||||||
inline Surface& GetSurface() noexcept { return m_surface; }
|
void Destroy() noexcept;
|
||||||
inline NonOwningPtr<UniformBuffer> GetUniformBuffer() noexcept { return m_uniform_buffer.get(); }
|
|
||||||
inline SwapChain& GetSwapChain() noexcept { return m_swapchain; }
|
|
||||||
inline RenderPass& GetRenderPass() noexcept { return m_pass; }
|
|
||||||
inline GraphicPipeline& GetPipeline() noexcept { return m_pipeline; }
|
|
||||||
inline CommandBuffer& GetCmdBuffer(int i) noexcept { return m_cmd.GetCmdBuffer(i); }
|
|
||||||
inline CommandBuffer& GetActiveCmdBuffer() noexcept { return m_cmd.GetCmdBuffer(m_current_frame_index); }
|
|
||||||
inline FrameBuffer& GetFrameBuffer(int i) noexcept { return m_framebuffers[i]; }
|
|
||||||
inline DescriptorSet& GetVertDescriptorSet() noexcept { return m_vert_set; }
|
|
||||||
inline DescriptorSet& GetFragDescriptorSet() noexcept { return m_frag_set; }
|
|
||||||
inline std::uint32_t GetActiveImageIndex() noexcept { return m_current_frame_index; }
|
|
||||||
inline std::uint32_t GetImageIndex() noexcept { return m_image_index; }
|
|
||||||
|
|
||||||
constexpr inline void RequireFrameBufferResize() noexcept { m_framebuffer_resized = true; }
|
|
||||||
|
|
||||||
~Renderer() = default;
|
~Renderer() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void RecreateRenderData();
|
void CreateSwapchain();
|
||||||
|
void DestroySwapchain();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GraphicPipeline m_pipeline;
|
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_image_available_semaphores;
|
||||||
CommandManager m_cmd;
|
std::array<VkSemaphore, MAX_FRAMES_IN_FLIGHT> m_render_finished_semaphores;
|
||||||
RenderPass m_pass;
|
std::array<VkCommandBuffer, MAX_FRAMES_IN_FLIGHT> m_cmd_buffers;
|
||||||
Surface m_surface;
|
std::array<VkFence, MAX_FRAMES_IN_FLIGHT> m_cmd_fences;
|
||||||
SwapChain m_swapchain;
|
std::vector<Image> m_swapchain_images;
|
||||||
std::array<Semaphore, MAX_FRAMES_IN_FLIGHT> m_render_finished_semaphores;
|
NonOwningPtr<Window> p_window;
|
||||||
std::array<Semaphore, MAX_FRAMES_IN_FLIGHT> m_image_available_semaphores;
|
VkSurfaceKHR m_surface = VK_NULL_HANDLE;
|
||||||
std::vector<FrameBuffer> m_framebuffers;
|
VkSwapchainKHR m_swapchain = VK_NULL_HANDLE;
|
||||||
|
|
||||||
DescriptorSet m_vert_set;
|
|
||||||
DescriptorSet m_frag_set;
|
|
||||||
|
|
||||||
std::unique_ptr<UniformBuffer> m_uniform_buffer;
|
|
||||||
|
|
||||||
NonOwningPtr<class Window> m_window;
|
|
||||||
NonOwningPtr<class Texture> m_render_target;
|
|
||||||
|
|
||||||
std::uint32_t m_current_frame_index = 0;
|
std::uint32_t m_current_frame_index = 0;
|
||||||
std::uint32_t m_image_index = 0;
|
std::uint32_t m_swapchain_image_index = 0;
|
||||||
bool m_framebuffer_resized = false;
|
std::size_t m_drawcalls = 0;
|
||||||
|
std::size_t m_polygons_drawn = 0;
|
||||||
|
bool m_framebuffers_resize = false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* FrameBuffer.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:19:44 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/28 22:16:02 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_FRAMEBUFFER__
|
|
||||||
#define __MLX_VK_FRAMEBUFFER__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class FrameBuffer
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Init(class RenderPass& renderpass, class Image& image);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
inline VkFramebuffer& operator()() noexcept { return m_framebuffer; }
|
|
||||||
inline VkFramebuffer& Get() noexcept { return m_framebuffer; }
|
|
||||||
inline std::uint32_t GetWidth() const noexcept { return m_width; }
|
|
||||||
inline std::uint32_t GetHeight() const noexcept { return m_height; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkFramebuffer m_framebuffer = VK_NULL_HANDLE;
|
|
||||||
std::uint32_t m_width = 0;
|
|
||||||
std::uint32_t m_height = 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_FRAMEBUFFER__
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* RenderPass.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:22:00 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/28 22:16:44 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_RENDER_PASS__
|
|
||||||
#define __MLX_VK_RENDER_PASS__
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class RenderPass
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void Init(VkFormat attachement_format, VkImageLayout layout);
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
void Begin(class CmommandBuffer& cmd, class FrameBuffer& fb);
|
|
||||||
void End(class CommandBuffer& cmd);
|
|
||||||
|
|
||||||
inline VkRenderPass& operator()() noexcept { return m_render_pass; }
|
|
||||||
inline VkRenderPass& Get() noexcept { return m_render_pass; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
VkRenderPass m_render_pass = VK_NULL_HANDLE;
|
|
||||||
bool m_is_running = false;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_RENDER_PASS__
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* Swapchain.h :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2022/10/06 18:23:27 by maldavid #+# #+# */
|
|
||||||
/* Updated: 2024/03/28 22:18:15 by maldavid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#ifndef __MLX_VK_SWAPCHAIN__
|
|
||||||
#define __MLX_VK_SWAPCHAIN__
|
|
||||||
|
|
||||||
#include <Renderer/Images/Image.h>
|
|
||||||
|
|
||||||
namespace mlx
|
|
||||||
{
|
|
||||||
class SwapChain
|
|
||||||
{
|
|
||||||
friend class GraphicPipeline;
|
|
||||||
friend class RenderPass;
|
|
||||||
friend class Renderer;
|
|
||||||
|
|
||||||
public:
|
|
||||||
struct SwapChainSupportDetails
|
|
||||||
{
|
|
||||||
VkSurfaceCapabilitiesKHR capabilities;
|
|
||||||
std::vector<VkSurfaceFormatKHR> formats;
|
|
||||||
std::vector<VkPresentModeKHR> present_modes;
|
|
||||||
};
|
|
||||||
|
|
||||||
public:
|
|
||||||
SwapChain() = default;
|
|
||||||
|
|
||||||
void Init(NonOwningPtr<class Renderer> renderer);
|
|
||||||
void Recreate();
|
|
||||||
void Destroy() noexcept;
|
|
||||||
|
|
||||||
SwapChainSupportDetails QuerySwapChainSupport(VkPhysicalDevice device);
|
|
||||||
VkExtent2D ChooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities);
|
|
||||||
VkPresentModeKHR ChooseSwapPresentMode([[maybe_unused]] const std::vector<VkPresentModeKHR> &available_present_modes);
|
|
||||||
|
|
||||||
inline VkSwapchainKHR Get() noexcept { return m_swapchain; }
|
|
||||||
inline VkSwapchainKHR operator()() noexcept { return m_swapchain; }
|
|
||||||
inline std::size_t GetImagesNumber() const noexcept { return m_images.size(); }
|
|
||||||
inline Image& GetImage(std::size_t i) noexcept { return m_images[i]; }
|
|
||||||
inline SwapChainSupportDetails GetSupport() noexcept { return m_swapchain_support; }
|
|
||||||
inline VkExtent2D GetExtent() noexcept { return m_extent; }
|
|
||||||
inline VkFormat GetImagesFormat() const noexcept { return m_swapchain_image_format; }
|
|
||||||
|
|
||||||
~SwapChain() = default;
|
|
||||||
|
|
||||||
private:
|
|
||||||
SwapChainSupportDetails m_swapchain_support;
|
|
||||||
VkSwapchainKHR m_swapchain;
|
|
||||||
std::vector<Image> m_images;
|
|
||||||
VkFormat m_swapchain_image_format;
|
|
||||||
VkExtent2D m_extent;
|
|
||||||
NonOwningPtr<class Renderer> m_renderer;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif // __MLX_VK_SWAPCHAIN__
|
|
||||||
22
runtime/Includes/Renderer/ScenesRenderer.h
git.filemode.normal_file
22
runtime/Includes/Renderer/ScenesRenderer.h
git.filemode.normal_file
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef __MLX_SCENES_RENDERER__
|
||||||
|
#define __MLX_SCENES_RENDERER__
|
||||||
|
|
||||||
|
#include <Renderer/RenderPasses/Passes.h>
|
||||||
|
|
||||||
|
namespace mlx
|
||||||
|
{
|
||||||
|
class SceneRenderer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SceneRenderer() = default;
|
||||||
|
void Init();
|
||||||
|
void Render(class Scene& scene, class Renderer& renderer); // TODO : add RTT support
|
||||||
|
void Destroy();
|
||||||
|
~SceneRenderer() = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
RenderPasses m_passes;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user