Skip to content

Commit

Permalink
make some classes properly follow the rule of 5
Browse files Browse the repository at this point in the history
  • Loading branch information
WinteryFox committed Nov 26, 2023
1 parent dd1d141 commit f17f9b8
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 87 deletions.
50 changes: 23 additions & 27 deletions src/engine/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
#include "Camera.h"

namespace Vixen {
Camera::Camera(const glm::vec3 position, const float fieldOfView, const float nearPlane, const float farPlane, const glm::vec3 clearColor)
: position(position),
// rotation(glm::quat{}),
rotation({}),
fieldOfView(fieldOfView),
nearPlane(nearPlane),
farPlane(farPlane),
clearColor(clearColor) {}
Camera::Camera(const glm::vec3 position, const float fieldOfView, const float nearPlane, const float farPlane,
const glm::vec3 clearColor)
: position(position),
// rotation(glm::quat{}),
rotation({}),
fieldOfView(fieldOfView),
nearPlane(nearPlane),
farPlane(farPlane),
clearColor(clearColor) {}

glm::mat4 Camera::view() const {
// const auto &reverse = glm::conjugate(rotation);
// glm::mat4 rot = glm::toMat4(reverse);
// glm::mat4 translation = glm::translate({1.0}, -position);
//
// return rot * translation;
// return glm::lookAt(
// position,
// position + rotation,
// {0.0f, 1.0f, 0.0f}
// );
// const auto &reverse = glm::conjugate(rotation);
// glm::mat4 rot = glm::toMat4(reverse);
// glm::mat4 translation = glm::translate({1.0}, -position);
//
// return rot * translation;
return glm::lookAt(
glm::vec3{2.0f, 2.0f, 2.0f},
glm::vec3{0.0f, 0.0f, 0.0f},
glm::vec3{0.0f, 0.0f, 1.0f}
position,
glm::vec3(0, 0, 0),
{0.0f, 1.0f, 0.0f}
);
}

Expand All @@ -43,16 +39,16 @@ namespace Vixen {
);
}

const glm::vec3 &Camera::getPosition() const {
const glm::vec3& Camera::getPosition() const {
return position;
}

// const glm::quat &Camera::getRotation() const {
// return rotation;
// }
// const glm::quat &Camera::getRotation() const {
// return rotation;
// }

glm::vec3 Camera::getEulerRotation() const {
// return glm::eulerAngles(rotation);
// return glm::eulerAngles(rotation);
return rotation;
}

Expand All @@ -64,7 +60,7 @@ namespace Vixen {
return farPlane;
}

const glm::vec3 &Camera::getClearColor() const {
const glm::vec3& Camera::getClearColor() const {
return clearColor;
}
}
19 changes: 19 additions & 0 deletions src/engine/vk/VkPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ namespace Vixen::Vk {
);
}

VkPipeline::VkPipeline(VkPipeline&& other) noexcept
: device(std::move(other.device)),
program(std::move(other.program)),
config(other.config),
renderPass(std::move(other.renderPass)),
pipelineLayout(std::move(other.pipelineLayout)),
pipeline(std::exchange(other.pipeline, nullptr)) {}

VkPipeline const& VkPipeline::operator=(VkPipeline&& other) noexcept {
std::swap(device, other.device);
std::swap(program, other.program);
std::swap(config, other.config);
std::swap(renderPass, other.renderPass);
std::swap(pipelineLayout, other.pipelineLayout);
std::swap(pipeline, other.pipeline);

return *this;
}

VkPipeline::~VkPipeline() {
vkDestroyPipeline(device->getDevice(), pipeline, nullptr);
}
Expand Down
8 changes: 6 additions & 2 deletions src/engine/vk/VkPipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ namespace Vixen::Vk {
VkPipeline(const std::shared_ptr<Device> &device, const Swapchain &swapchain, const VkShaderProgram &program,
const Config &config);

VkPipeline(const VkPipeline &) = delete;
VkPipeline(VkPipeline& other) = delete;

VkPipeline &operator=(const VkPipeline &) = delete;
VkPipeline& operator=(const VkPipeline& other) = delete;

VkPipeline(VkPipeline&& other) noexcept;

VkPipeline const& operator=(VkPipeline&& other) noexcept;

~VkPipeline();

Expand Down
11 changes: 11 additions & 0 deletions src/engine/vk/VkPipelineLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ namespace Vixen::Vk {
);
}

VkPipelineLayout::VkPipelineLayout(VkPipelineLayout&& fp) noexcept
: device(std::move(device)),
layout(std::exchange(fp.layout, nullptr)) {}

VkPipelineLayout const& VkPipelineLayout::operator=(VkPipelineLayout&& fp) noexcept {
std::swap(device, fp.device);
std::swap(layout, fp.layout);

return *this;
}

VkPipelineLayout::~VkPipelineLayout() {
vkDestroyPipelineLayout(device->getDevice(), layout, nullptr);
}
Expand Down
8 changes: 6 additions & 2 deletions src/engine/vk/VkPipelineLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ namespace Vixen::Vk {
public:
VkPipelineLayout(const std::shared_ptr<Device> &device, const VkShaderProgram &program);

VkPipelineLayout(const VkPipelineLayout &) = delete;
VkPipelineLayout(VkPipelineLayout& other) = delete;

VkPipelineLayout &operator=(const VkPipelineLayout &) = delete;
VkPipelineLayout& operator=(const VkPipelineLayout& other) = delete;

VkPipelineLayout(VkPipelineLayout&& fp) noexcept;

VkPipelineLayout const& operator=(VkPipelineLayout&& fp) noexcept;

~VkPipelineLayout();

Expand Down
121 changes: 68 additions & 53 deletions src/engine/vk/VkRenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,101 @@

namespace Vixen::Vk {
VkRenderPass::VkRenderPass(
const std::shared_ptr<Device> &device,
const VkShaderProgram &program,
const Swapchain &swapchain
const std::shared_ptr<Device>& device,
const VkShaderProgram& program,
const Swapchain& swapchain
) : device(device),
renderPass(VK_NULL_HANDLE) {
attachments.emplace_back(VkAttachmentDescription{
.format = swapchain.getFormat().format,
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
.format = swapchain.getFormat().format,
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_STORE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
});

VkAttachmentReference colorAttachmentRef{
.attachment = 0,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
.attachment = 0,
.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
};

attachments.emplace_back(VkAttachmentDescription{
.format = device->getGpu().pickFormat(
{
VK_FORMAT_D32_SFLOAT,
VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D24_UNORM_S8_UINT
},
VK_IMAGE_TILING_OPTIMAL,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
),
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
.format = device->getGpu().pickFormat(
{
VK_FORMAT_D32_SFLOAT,
VK_FORMAT_D32_SFLOAT_S8_UINT,
VK_FORMAT_D24_UNORM_S8_UINT
},
VK_IMAGE_TILING_OPTIMAL,
VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT
),
.samples = VK_SAMPLE_COUNT_1_BIT,
.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
.storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
});

VkAttachmentReference depthAttachmentRef{
.attachment = 1,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
.attachment = 1,
.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL
};

VkSubpassDescription subpass{
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1,
.pColorAttachments = &colorAttachmentRef,
.pDepthStencilAttachment = &depthAttachmentRef
.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
.colorAttachmentCount = 1,
.pColorAttachments = &colorAttachmentRef,
.pDepthStencilAttachment = &depthAttachmentRef
};

VkSubpassDependency dependency{
.srcSubpass = VK_SUBPASS_EXTERNAL,
.dstSubpass = 0,
.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
.srcSubpass = VK_SUBPASS_EXTERNAL,
.dstSubpass = 0,
.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT |
VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT,
.srcAccessMask = 0,
.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT |
VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT
};

VkRenderPassCreateInfo info{
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = static_cast<uint32_t>(attachments.size()),
.pAttachments = attachments.data(),
.subpassCount = 1,
.pSubpasses = &subpass,
.dependencyCount = 1,
.pDependencies = &dependency
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
.attachmentCount = static_cast<uint32_t>(attachments.size()),
.pAttachments = attachments.data(),
.subpassCount = 1,
.pSubpasses = &subpass,
.dependencyCount = 1,
.pDependencies = &dependency
};

checkVulkanResult(
vkCreateRenderPass(device->getDevice(), &info, nullptr, &renderPass),
"Failed to create render pass"
vkCreateRenderPass(device->getDevice(), &info, nullptr, &renderPass),
"Failed to create render pass"
);
}

VkRenderPass::VkRenderPass(VkRenderPass&& other) noexcept
: device(std::move(other.device)),
renderPass(std::exchange(other.renderPass, nullptr)),
attachments(std::move(other.attachments)),
references(std::move(other.references)) {}

VkRenderPass const& VkRenderPass::operator=(VkRenderPass&& other) noexcept {
std::swap(device, other.device);
std::swap(renderPass, other.renderPass);
std::swap(attachments, other.attachments);
std::swap(references, other.references);

return *this;
}

VkRenderPass::~VkRenderPass() {
vkDestroyRenderPass(device->getDevice(), renderPass, nullptr);
}
Expand Down
8 changes: 6 additions & 2 deletions src/engine/vk/VkRenderPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ namespace Vixen::Vk {
const Swapchain &swapchain
);

VkRenderPass(const VkRenderPass &) = delete;
VkRenderPass(VkRenderPass& other) = delete;

VkRenderPass &operator=(const VkRenderPass &) = delete;
VkRenderPass& operator=(const VkRenderPass& other) = delete;

VkRenderPass(VkRenderPass&& other) noexcept;

VkRenderPass const& operator=(VkRenderPass&& other) noexcept;

~VkRenderPass();

Expand Down
2 changes: 1 addition & 1 deletion src/engine/vk/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ int main() {
}
);

auto camera = Vixen::Camera({0.0f, 0.0f, -5.0f});
auto camera = Vixen::Camera(glm::vec3{0.0f, 0.0f, 2.0f});

const std::vector<VkDescriptorPoolSize> sizes{
{
Expand Down

0 comments on commit f17f9b8

Please sign in to comment.