Skip to content

Commit

Permalink
basic implementation of descriptor sets
Browse files Browse the repository at this point in the history
  • Loading branch information
WinteryFox committed Nov 22, 2023
1 parent b3c1678 commit c2ab86c
Show file tree
Hide file tree
Showing 24 changed files with 667 additions and 366 deletions.
15 changes: 7 additions & 8 deletions src/editor/shaders/triangle.vert
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ layout(location = 1) in vec3 color;

layout(location = 0) out vec3 vertColor;

//layout(binding = 0) uniform ModelViewProjection {
// mat4 model;
// mat4 view;
// mat4 projection;
//} mvp;
layout(binding = 0) uniform ModelViewProjection {
mat4 model;
mat4 view;
mat4 projection;
} mvp;

void main() {
// gl_Position = mvp.projection * mvp.view * mvp.model * vec4(position, 1.0);
gl_Position = vec4(position, 1.0);
gl_Position = mvp.projection * mvp.view * mvp.model * vec4(position, 1.0);
vertColor = color;
}
}
4 changes: 2 additions & 2 deletions src/engine/Buffer.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#include "Buffer.h"

namespace Vixen {
Buffer::Buffer(Usage bufferUsage, const size_t &size)
Buffer::Buffer(const Usage bufferUsage, const std::size_t &size)
: bufferUsage(bufferUsage),
size(size) {}

Buffer::Usage Buffer::getBufferUsage() const {
return bufferUsage;
}

size_t Buffer::getSize() const {
std::size_t Buffer::getSize() const {
return size;
}
}
13 changes: 6 additions & 7 deletions src/engine/Buffer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <cstdio>
#include <cstdint>
#include <cstring>

namespace Vixen {
/**
Expand All @@ -18,7 +16,9 @@ namespace Vixen {
TRANSFER_SRC = 1 << 4,
};

virtual char *map() = 0;
virtual ~Buffer() = default;

virtual char* map() = 0;

virtual void unmap() = 0;

Expand All @@ -28,9 +28,9 @@ namespace Vixen {
* @param dataSize The size of the data.
* @param offset The offset within this buffer to start writing from.
*/
virtual void write(const char *data, size_t dataSize, size_t offset) = 0;
virtual void write(const char* data, std::size_t dataSize, std::size_t offset) = 0;

[[nodiscard]] size_t getSize() const;
[[nodiscard]] std::size_t getSize() const;

[[nodiscard]] Usage getBufferUsage() const;

Expand All @@ -43,9 +43,8 @@ namespace Vixen {
* Create a new buffer. Where the allocation is made is determined by the allocation usage.
* @param bufferUsage Specifies how the buffer will be used and what data it will hold.
* @param size The size of this buffer measured in bytes.
* @param allocationUsage Specifies how this buffer's allocated memory will be used.
*/
Buffer(Usage bufferUsage, const std::size_t &size);
Buffer(Usage bufferUsage, const std::size_t& size);
};

inline Buffer::Usage operator|(Buffer::Usage a, Buffer::Usage b) {
Expand Down
31 changes: 21 additions & 10 deletions src/engine/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Camera.h"

namespace Vixen {
Camera::Camera(float fieldOfView, float nearPlane, float farPlane, glm::vec3 clearColor)
: position(0, 0, 0),
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),
Expand All @@ -16,19 +16,30 @@ namespace Vixen {
// glm::mat4 translation = glm::translate({1.0}, -position);
//
// return rot * translation;
// return glm::lookAt(
// position,
// position + rotation,
// {0.0f, 1.0f, 0.0f}
// );
return glm::lookAt(
position,
position + rotation,
{0.0f, 1.0f, 0.0f}
glm::vec3{2.0f, 2.0f, 2.0f},
glm::vec3{0.0f, 0.0f, 0.0f},
glm::vec3{0.0f, 0.0f, 1.0f}
);
}

glm::mat4 Camera::perspective(float aspectRatio) const {
glm::mat4 Camera::perspective(const float aspectRatio) const {
// return glm::perspective(
// glm::radians(fieldOfView),
// aspectRatio,
// nearPlane,
// farPlane
// );
return glm::perspective(
glm::radians(fieldOfView),
aspectRatio,
nearPlane,
farPlane
glm::radians(45.0f),
1.7f,
0.1f,
10.0f
);
}

Expand Down
18 changes: 9 additions & 9 deletions src/engine/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtx/quaternion.hpp>

namespace Vixen {
class Camera {
public:
explicit Camera(
float fieldOfView = 114.0f,
float nearPlane = 0.01f,
float farPlane = 1000.0f,
glm::vec3 clearColor = {0.0f, 0.0f, 0.0f}
glm::vec3 position = {},
float fieldOfView = 114.0f,
float nearPlane = 0.1f,
float farPlane = 1000.0f,
glm::vec3 clearColor = {0.0f, 0.0f, 0.0f}
);

[[nodiscard]] glm::mat4 view() const;

[[nodiscard]] glm::mat4 perspective(float aspectRatio) const;

public:
[[nodiscard]] const glm::vec3 &getPosition() const;
[[nodiscard]] const glm::vec3& getPosition() const;

// [[nodiscard]] const glm::quat &getRotation() const;
// [[nodiscard]] const glm::quat &getRotation() const;

[[nodiscard]] glm::vec3 getEulerRotation() const;

[[nodiscard]] float getNearPlane() const;

[[nodiscard]] float getFarPlane() const;

[[nodiscard]] const glm::vec3 &getClearColor() const;
[[nodiscard]] const glm::vec3& getClearColor() const;

private:
glm::vec3 position;
Expand Down
22 changes: 14 additions & 8 deletions src/engine/ShaderModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,35 @@ namespace Vixen {

public:
ShaderModule(
Stage stage,
std::string entrypoint,
const std::vector<Binding> &bindings,
const std::vector<IO> &inputs
const Stage stage,
std::string entrypoint,
const std::vector<Binding>& bindings,
const std::vector<IO>& inputs,
const std::vector<IO>& uniformBuffers
) : stage(stage),
entrypoint(std::move(entrypoint)),
bindings(bindings),
inputs(inputs) {}
inputs(inputs),
uniformBuffers(uniformBuffers) {}

[[nodiscard]] Stage getStage() const {
return stage;
}

[[nodiscard]] const std::string &getEntrypoint() const {
[[nodiscard]] const std::string& getEntrypoint() const {
return entrypoint;
}

[[nodiscard]] const std::vector<Binding> &getBindings() const {
[[nodiscard]] const std::vector<Binding>& getBindings() const {
return bindings;
}

[[nodiscard]] const std::vector<IO> &getInputs() const {
[[nodiscard]] const std::vector<IO>& getInputs() const {
return inputs;
}

[[nodiscard]] std::vector<IO> getUniformBuffers() const {
return uniformBuffers;
}
};
}
2 changes: 2 additions & 0 deletions src/engine/vk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ add_library(
VkDescriptorPool.h
VkDescriptorSetLayout.cpp
VkDescriptorSetLayout.h
VkDescriptorSet.cpp
VkDescriptorSet.h
)
target_link_libraries(
VkVixen
Expand Down
118 changes: 62 additions & 56 deletions src/engine/vk/VkBuffer.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "VkBuffer.h"

namespace Vixen::Vk {
VkBuffer::VkBuffer(const std::shared_ptr<Device> &device, Usage bufferUsage, const size_t &size)
: Buffer(bufferUsage, size),
device(device),
allocation(VK_NULL_HANDLE),
buffer(VK_NULL_HANDLE) {
VkBufferUsageFlags bufferUsageFlags;
VkBuffer::VkBuffer(const std::shared_ptr<Device>& device, const Usage bufferUsage, const size_t& size)
: Buffer(bufferUsage, size),
device(device),
allocation(VK_NULL_HANDLE),
buffer(VK_NULL_HANDLE) {
VkBufferUsageFlags bufferUsageFlags = 0;
if (bufferUsage & Usage::VERTEX)
bufferUsageFlags |= VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
if (bufferUsage & Usage::INDEX)
Expand All @@ -18,57 +18,63 @@ namespace Vixen::Vk {
if (bufferUsage & Usage::UNIFORM)
bufferUsageFlags |= VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;

VkBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.size = size;
bufferCreateInfo.usage = bufferUsageFlags;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
const VkBufferCreateInfo bufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = size,
.usage = bufferUsageFlags,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = 0,
.pQueueFamilyIndices = nullptr
};

VmaAllocationCreateInfo allocationCreateInfo = {
// TODO: Only add this flag if necessary (e.g. staging buffer)
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO
constexpr VmaAllocationCreateInfo allocationCreateInfo = {
// TODO: Only add this flag if necessary (e.g. staging buffer)
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
};

checkVulkanResult(
vmaCreateBuffer(
device->getAllocator(),
&bufferCreateInfo,
&allocationCreateInfo,
&buffer,
&allocation,
nullptr
),
"Failed to create Vk buffer"
vmaCreateBuffer(
device->getAllocator(),
&bufferCreateInfo,
&allocationCreateInfo,
&buffer,
&allocation,
nullptr
),
"Failed to create Vk buffer"
);
}

VkBuffer::VkBuffer(VkBuffer &&o) noexcept
: Buffer(o.bufferUsage, o.size),
allocation(std::exchange(o.allocation, nullptr)),
buffer(std::exchange(o.buffer, nullptr)) {}
VkBuffer::VkBuffer(VkBuffer&& o) noexcept
: Buffer(o.bufferUsage, o.size),
allocation(std::exchange(o.allocation, nullptr)),
buffer(std::exchange(o.buffer, nullptr)) {}

VkBuffer::~VkBuffer() {
vmaDestroyBuffer(device->getAllocator(), buffer, allocation);
}

void VkBuffer::write(const char *data, size_t dataSize, size_t offset) {
void VkBuffer::write(const char* data, const size_t dataSize, const size_t offset) {
if (offset + dataSize > size)
throw std::runtime_error("Buffer overflow");

void *d = map();
memcpy(static_cast<char *>(d) + offset, data, dataSize);
void* d = map();
memcpy(static_cast<char*>(d) + offset, data, dataSize);
unmap();
}

char *VkBuffer::map() {
void *data;
char* VkBuffer::map() {
void* data;
checkVulkanResult(
vmaMapMemory(device->getAllocator(), allocation, &data),
"Failed to map buffer"
vmaMapMemory(device->getAllocator(), allocation, &data),
"Failed to map buffer"
);

return static_cast<char *>(data);
return static_cast<char*>(data);
}

void VkBuffer::unmap() {
Expand All @@ -80,31 +86,31 @@ namespace Vixen::Vk {
}

void VkBuffer::transfer(
VkBuffer &destination,
size_t destinationOffset
VkBuffer& destination,
size_t destinationOffset
) {
device->getTransferCommandPool()
->allocateCommandBuffer(VkCommandBuffer::Level::PRIMARY)
.record(
VkCommandBuffer::Usage::SINGLE,
[this, &destination, &destinationOffset](auto commandBuffer) {
VkBufferCopy region{
.srcOffset = 0,
.dstOffset = destinationOffset,
.size = size,
};

vkCmdCopyBuffer(commandBuffer, buffer, destination.getBuffer(), 1, &region);
}
)
.submit(device->getTransferQueue(), {}, {}, {});
->allocateCommandBuffer(VkCommandBuffer::Level::PRIMARY)
.record(
VkCommandBuffer::Usage::SINGLE,
[this, &destination, &destinationOffset](auto commandBuffer) {
VkBufferCopy region{
.srcOffset = 0,
.dstOffset = destinationOffset,
.size = size,
};

vkCmdCopyBuffer(commandBuffer, buffer, destination.getBuffer(), 1, &region);
}
)
.submit(device->getTransferQueue(), {}, {}, {});
}

VkBuffer VkBuffer::stage(
const std::shared_ptr<Device> &device,
Usage usage,
size_t size,
const char *data
const std::shared_ptr<Device>& device,
const Usage usage,
const size_t size,
const char* data
) {
auto source = VkBuffer(device, usage | Usage::TRANSFER_SRC, size);
auto destination = VkBuffer(device, usage | Usage::TRANSFER_DST, size);
Expand Down
Loading

0 comments on commit c2ab86c

Please sign in to comment.