Skip to content

Commit

Permalink
clear color for specific attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
maxortner01 committed Oct 29, 2024
1 parent f614370 commit adec0a7
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@
[submodule "extern/sse2neon"]
path = extern/sse2neon
url = https://github.com/DLTcollab/sse2neon
[submodule "extern/implot"]
path = extern/implot
url = https://github.com/epezent/implot
1 change: 1 addition & 0 deletions extern/implot
Submodule implot added at f15659
2 changes: 1 addition & 1 deletion include/midnight/Graphics/RenderFrame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace mn::Graphics
MN_SYMBOL void startRender(std::optional<std::shared_ptr<Image>> image = std::nullopt);
MN_SYMBOL void endRender();

MN_SYMBOL void clear(std::tuple<float, float, float> color, float alpha = 1.f, std::optional<std::shared_ptr<Image>> image = std::nullopt) const;
MN_SYMBOL void clear(std::tuple<float, float, float> color, float alpha = 1.f, std::optional<std::shared_ptr<Image>> image = std::nullopt, int attachment_index = -1) const;

MN_SYMBOL void setPushConstant(const Pipeline& pipeline, const void* data) const;
template<typename T>
Expand Down
162 changes: 162 additions & 0 deletions include/midnight/Math/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ namespace mn::Math
return m;
}

template<std::size_t R, std::size_t C, typename T>
static Mat<C, R, T> transpose(const Mat<R, C, T>& mat)
{
Mat<C, R, T> m;
for (int r = 0; r < R; r++)
for (int c = 0; c < C; c++)
m.m[c][r] = mat.m[r][c];
return m;
}

template<typename T>
static Mat<4, 4, T> rotationX(const Angle& angle)
{
Expand Down Expand Up @@ -305,7 +315,159 @@ namespace mn::Math
return rotatedVector;
}

template<typename T>
static Mat<4, 4, T> rotationFromQuaternion(Math::Vec4f quaternion)
{
// Extract quaternion components
float w = x(quaternion);
float x = y(quaternion);
float y = z(quaternion);
float z = Math::w(quaternion);

Mat<4, 4, T> ret;
ret.m[0][0] = 1 - 2 * (y * y + z * z);
ret.m[0][1] = 2 * (x * y - z * w);
ret.m[0][2] = 2 * (x * z + y * w);
ret.m[0][3] = 0;

ret.m[1][0] = 2 * (x * y + z * w);
ret.m[1][1] = 1 - 2 * (x * x + z * z);
ret.m[1][2] = 2 * (y * z - x * w);
ret.m[1][3] = 0;

ret.m[2][0] = 2 * (x * z - y * w);
ret.m[2][1] = 2 * (y * z + x * w);
ret.m[2][2] = 1 - 2 * (x * x + y * y);
ret.m[2][3] = 0;

ret.m[3][0] = 0;
ret.m[3][1] = 0;
ret.m[3][2] = 0;
ret.m[3][3] = 1;

return ret;
}

static Vec3<Angle> eulerAxisAngle(Vec3f axis, Angle angle)
{
double halfAngle = angle.asRadians() / 2;
double sinHalfAngle = sin(halfAngle);
double w = cos(halfAngle);
double x = Math::x(axis) * sinHalfAngle;
double y = Math::y(axis) * sinHalfAngle;
double z = Math::z(axis) * sinHalfAngle;

// Step 2: Convert quaternion to Euler angles
Vec3<Angle> angles;

// Yaw (z-axis rotation)
Math::z(angles) = Angle::radians( atan2(2 * (y * w + x * z), 1 - 2 * (y * y + z * z)) );

// Pitch (y-axis rotation)
double sinp = 2 * (x * w - y * z);
if (fabs(sinp) >= 1)
Math::y(angles) = Angle::radians( copysign(M_PI / 2, sinp) ); // use 90 degrees if out of range
else
Math::y(angles) = Angle::radians( asin(sinp) );

// Roll (x-axis rotation)
Math::x(angles) = Angle::radians( atan2(2 * (x * w + y * z), 1 - 2 * (x * x + y * y)) );

return angles;
}

template<typename T>
static Mat<4, 4, T> rotationAxisAngle(Vec3f axis, Angle angle)
{
Mat<4, 4, T> ret;

// Precompute trigonometric values
float cosTheta = cos(angle.asRadians());
float sinTheta = sin(angle.asRadians());
float oneMinusCos = 1 - cosTheta;

// Axis components
float ux = Math::x(axis);
float uy = Math::y(axis);
float uz = Math::z(axis);

// Populate the rotation matrix based on the formula
ret.m[0][0] = cosTheta + ux * ux * oneMinusCos;
ret.m[0][1] = ux * uy * oneMinusCos - uz * sinTheta;
ret.m[0][2] = ux * uz * oneMinusCos + uy * sinTheta;
ret.m[0][3] = 0;

ret.m[1][0] = uy * ux * oneMinusCos + uz * sinTheta;
ret.m[1][1] = cosTheta + uy * uy * oneMinusCos;
ret.m[1][2] = uy * uz * oneMinusCos - ux * sinTheta;
ret.m[1][3] = 0;

ret.m[2][0] = uz * ux * oneMinusCos - uy * sinTheta;
ret.m[2][1] = uz * uy * oneMinusCos + ux * sinTheta;
ret.m[2][2] = cosTheta + uz * uz * oneMinusCos;
ret.m[2][3] = 0;

ret.m[3][0] = 0;
ret.m[3][1] = 0;
ret.m[3][2] = 0;
ret.m[3][3] = 1;

return ret;
}

static Vec3<Angle> eulerFromQuaternion(Math::Vec4f quaternion)
{
const auto w = Math::x(quaternion);
const auto x = Math::y(quaternion);
const auto y = Math::z(quaternion);
const auto z = Math::w(quaternion);

float roll, pitch, yaw;

// Yaw (z-axis rotation)
yaw = atan2(2 * (y * w + x * z), 1 - 2 * (y * y + z * z));

// Pitch (y-axis rotation)
double sinp = 2 * (x * w - y * z);
if (fabs(sinp) >= 1)
pitch = copysign(M_PI / 2, sinp); // use 90 degrees if out of range
else
pitch = asin(sinp);

// Roll (x-axis rotation)
roll = atan2(2 * (x * w + y * z), 1 - 2 * (x * x + y * y));

return Vec3<Angle>{
Angle::radians(pitch),
Angle::radians(roll),
Angle::radians(yaw)
};

}

template<typename T>
static Mat<4, 4, T> rotationUsingQuaternion(Vec3<Angle> angles)
{
// Calculate half angles
const auto pitch = x(angles).asRadians();
const auto roll = z(angles).asRadians();
const auto yaw = y(angles).asRadians();

T cy = cos(yaw * 0.5);
T sy = sin(yaw * 0.5);
T cp = cos(pitch * 0.5);
T sp = sin(pitch * 0.5);
T cr = cos(roll * 0.5);
T sr = sin(roll * 0.5);

// Create the quaternion
return rotationFromQuaternion<T>({
cr * cp * cy + sr * sp * sy, // w component
sr * cp * cy - cr * sp * sy, // x component
cr * sp * cy + sr * cp * sy, // y component
cr * cp * sy - sr * sp * cy // z component
});
}

template<typename T>
using Mat2 = Mat<2, 2, T>;
Expand Down
11 changes: 10 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ set(IMGUI_SOURCES
${MIDNIGHT_BASE_DIR}/extern/imgui/backends/imgui_impl_vulkan.h
)

set(IMPLOT_SOURCES
${MIDNIGHT_BASE_DIR}/extern/implot/implot_internal.h
${MIDNIGHT_BASE_DIR}/extern/implot/implot_items.cpp
${MIDNIGHT_BASE_DIR}/extern/implot/implot.cpp
${MIDNIGHT_BASE_DIR}/extern/implot/implot.h
${MIDNIGHT_BASE_DIR}/extern/implot/implot_demo.cpp
)

add_library(midnight-graphics SHARED
${MIDNIGHT_BASE_DIR}/src/Graphics/Backend/Instance.cpp
${MIDNIGHT_BASE_DIR}/src/Graphics/Backend/Device.cpp
Expand All @@ -62,14 +70,15 @@ add_library(midnight-graphics SHARED
${MIDNIGHT_BASE_DIR}/src/Math/Angle.cpp

${SPIRV_REFLECT}
${IMGUI_SOURCES})
${IMGUI_SOURCES} ${IMPLOT_SOURCES})

target_link_libraries(midnight-graphics PRIVATE SDL3::SDL3 Vulkan::Vulkan ${SHADERC} GPUOpen::VulkanMemoryAllocator simple-lua)
target_include_directories(midnight-graphics
PUBLIC
${MIDNIGHT_BASE_DIR}/include
${MIDNIGHT_BASE_DIR}/include/midnight
${MIDNIGHT_BASE_DIR}/extern/imgui
${MIDNIGHT_BASE_DIR}/extern/implot
${MIDNIGHT_BASE_DIR}/extern/sse2neon
PRIVATE
${MIDNIGHT_BASE_DIR}/extern/spirv-reflect
Expand Down
10 changes: 6 additions & 4 deletions src/Graphics/RenderFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void RenderFrame::endRender()
((PFN_vkCmdEndRenderingKHR)(pvkCmdEndRenderingKHR))(frame_data->command_buffer->getHandle().as<VkCommandBuffer>());
}

void RenderFrame::clear(std::tuple<float, float, float> color, float alpha, std::optional<std::shared_ptr<Image>> image) const
void RenderFrame::clear(std::tuple<float, float, float> color, float alpha, std::optional<std::shared_ptr<Image>> image, int attachment_index) const
{
const auto use_image = ( image ? *image : this->image );

Expand All @@ -160,18 +160,20 @@ void RenderFrame::clear(std::tuple<float, float, float> color, float alpha, std:
const auto cmdBuffer = frame_data->command_buffer->getHandle().as<VkCommandBuffer>();

const auto& color_attachments = use_image->getColorAttachments();
for (const auto& a : color_attachments)
for (int i = 0; i < color_attachments.size(); i++)
{
if (attachment_index >= 0 && i != attachment_index) continue;

_transition_image(
cmdBuffer,
static_cast<VkImage>(a.handle),
static_cast<VkImage>(color_attachments[i].handle),
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_GENERAL);

//clear image
vkCmdClearColorImage(
frame_data->command_buffer->getHandle().as<VkCommandBuffer>(),
static_cast<VkImage>(a.handle),
static_cast<VkImage>(color_attachments[i].handle),
VK_IMAGE_LAYOUT_GENERAL,
&clearValue,
1,
Expand Down
3 changes: 3 additions & 0 deletions src/Graphics/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Graphics/Backend/Instance.hpp>

#include <imgui.h>
#include <implot.h>
#include <backends/imgui_impl_vulkan.h>
#include <backends/imgui_impl_sdl3.h>

Expand Down Expand Up @@ -50,6 +51,7 @@ Window::Window(const Math::Vec2u& size, const std::string& name)

// IF IMGUI
ImGui::CreateContext();
ImPlot::CreateContext();
ImGui_ImplSDL3_InitForVulkan(handle.as<SDL_Window*>());
const auto& instance = Backend::Instance::get();

Expand Down Expand Up @@ -455,6 +457,7 @@ Window::~Window()

ImGui_ImplVulkan_Shutdown();
ImGui_ImplSDL3_Shutdown();
ImPlot::DestroyContext();
ImGui::DestroyContext();

device->destroySwapchain(swapchain);
Expand Down

0 comments on commit adec0a7

Please sign in to comment.