Skip to content

Commit

Permalink
textures
Browse files Browse the repository at this point in the history
  • Loading branch information
WinteryFox committed Nov 29, 2023
1 parent a190f4d commit 39a5442
Show file tree
Hide file tree
Showing 24 changed files with 548 additions and 264 deletions.
2 changes: 1 addition & 1 deletion src/editor/shaders/triangle.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ layout(location = 1) in vec2 uv;

layout(location = 0) out vec4 color;

layout(binding = 0) uniform sampler2D albedo;
layout(binding = 1) uniform sampler2D albedo;

void main() {
color = texture(albedo, uv) * vec4(vertex_color, 1.0);
Expand Down
9 changes: 5 additions & 4 deletions src/engine/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ namespace Vixen {

virtual ~Buffer() = default;

virtual char* map() = 0;

virtual void unmap() = 0;

/**
* Map the buffer, write to it and unmap the buffer from host memory.
* @param data A pointer pointing to the start of the data.
Expand All @@ -34,6 +30,11 @@ namespace Vixen {

[[nodiscard]] Usage getBufferUsage() const;

private:
virtual char* map() = 0;

virtual void unmap() = 0;

protected:
const Usage bufferUsage;

Expand Down
3 changes: 3 additions & 0 deletions src/engine/vk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ add_library(
VkDescriptorSetLayout.h
VkDescriptorSet.cpp
VkDescriptorSet.h
VkSampler.cpp
VkSampler.h
)
target_link_libraries(
VkVixen
Expand All @@ -70,6 +72,7 @@ target_link_libraries(
spirv-cross-core
spirv-cross-glsl
spirv-cross-reflect
freeimage
)

target_compile_definitions(VkVixen PRIVATE -DVK_NO_PROTOTYPES)
Expand Down
6 changes: 3 additions & 3 deletions src/engine/vk/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ namespace Vixen::Vk {
const std::vector<const char *> &extensions,
GraphicsCard gpu,
VkSurfaceKHR surface
) : device(VK_NULL_HANDLE),
) : gpu(gpu),
device(VK_NULL_HANDLE),
allocator(VK_NULL_HANDLE),
gpu(gpu),
surface(surface) {
graphicsQueueFamily = gpu.getQueueFamilyWithFlags(VK_QUEUE_GRAPHICS_BIT)[0];
presentQueueFamily = gpu.getSurfaceSupportedQueues(surface)[0];
transferQueueFamily = gpu.getQueueFamilyWithFlags(VK_QUEUE_TRANSFER_BIT)[0];

std::set<uint32_t> queueFamilies = {
std::set queueFamilies = {
graphicsQueueFamily.index,
presentQueueFamily.index,
transferQueueFamily.index,
Expand Down
47 changes: 27 additions & 20 deletions src/engine/vk/VkBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace Vixen::Vk {
: Buffer(bufferUsage, size),
device(device),
allocation(VK_NULL_HANDLE),
buffer(VK_NULL_HANDLE) {
buffer(VK_NULL_HANDLE),
data(nullptr) {
VmaAllocationCreateFlags allocationFlags = 0;
VkBufferUsageFlags bufferUsageFlags = 0;
VkMemoryPropertyFlags requiredFlags = 0;
Expand Down Expand Up @@ -65,38 +66,29 @@ namespace Vixen::Vk {
),
"Failed to create Vk buffer"
);

if (allocationFlags & VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT)
data = map();
}

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

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

void VkBuffer::write(const char* data, const size_t dataSize, const size_t offset) {
void VkBuffer::write(const char* d, const size_t dataSize, const size_t offset) {
if (!data)
throw std::runtime_error("This buffer is not mapped and thus not writable");
if (offset + dataSize > size)
throw std::runtime_error("Buffer overflow");

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

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

return static_cast<char*>(data);
}

void VkBuffer::unmap() {
vmaUnmapMemory(device->getAllocator(), allocation);
memcpy(data + offset, d, dataSize);
}

::VkBuffer VkBuffer::getBuffer() const {
Expand Down Expand Up @@ -138,4 +130,19 @@ namespace Vixen::Vk {

return destination;
}

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

return static_cast<char*>(data);
}

void VkBuffer::unmap() {
if (data)
vmaUnmapMemory(device->getAllocator(), allocation);
}
}
37 changes: 20 additions & 17 deletions src/engine/vk/VkBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,27 @@ namespace Vixen::Vk {

::VkBuffer buffer;

char* data;

public:
VkBuffer(const std::shared_ptr<Device> &device, Usage bufferUsage, const size_t &size);
VkBuffer(const std::shared_ptr<Device>& device, Usage bufferUsage, const size_t& size);

VkBuffer(const VkBuffer &) = delete;
VkBuffer(const VkBuffer&) = delete;

VkBuffer &operator=(const VkBuffer &) = delete;
VkBuffer& operator=(const VkBuffer&) = delete;

VkBuffer(VkBuffer &&o) noexcept;
VkBuffer(VkBuffer&& o) noexcept;

~VkBuffer() override;

char *map() override;

void unmap() override;

void write(const char *data, size_t dataSize, size_t offset) override;
void write(const char* d, size_t dataSize, size_t offset) override;

/**
* Copies data from one buffer to another.
* @param destination The destination buffer to copy the data to.
* @param destinationOffset The offset from the destination buffer to copy into.
*/
void transfer(VkBuffer &destination, size_t destinationOffset);
void transfer(VkBuffer& destination, size_t destinationOffset);

[[nodiscard]] ::VkBuffer getBuffer() const;

Expand All @@ -47,24 +45,29 @@ namespace Vixen::Vk {
* @param data Pointer to the start of the data.
* @return Returns the resulting device local buffer.
*/
static VkBuffer stage(const std::shared_ptr<Device> &device, Usage usage, size_t size, const char *data);
static VkBuffer stage(const std::shared_ptr<Device>& device, Usage usage, size_t size, const char* data);

template<typename F>
template <typename F>
static VkBuffer stage(
const std::shared_ptr<Device> &device,
const Usage usage,
const size_t size,
F lambda
const std::shared_ptr<Device>& device,
const Usage usage,
const size_t size,
F lambda
) {
auto source = VkBuffer(device, usage | Usage::TRANSFER_SRC, size);
auto destination = VkBuffer(device, usage | Usage::TRANSFER_DST, size);

void *data = source.map();
void* data = source.map();
lambda(data);
source.unmap();
source.transfer(destination, 0);

return destination;
}

private:
char* map() override;

void unmap() override;
};
}
23 changes: 23 additions & 0 deletions src/engine/vk/VkDescriptorSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,29 @@ namespace Vixen::Vk {
vkUpdateDescriptorSets(device->getDevice(), 1, &write, 0, nullptr);
}

void VkDescriptorSet::updateCombinedImageSampler(const uint32_t binding, const VkSampler &sampler, const VkImageView &view) const {
const VkDescriptorImageInfo imageInfo{
.sampler = sampler.getSampler(),
.imageView = view.getImageView(),
.imageLayout = view.getImage()->getLayout()
};

const VkWriteDescriptorSet write{
.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
.pNext = nullptr,
.dstSet = set,
.dstBinding = binding,
.dstArrayElement = 0,
.descriptorCount = 1,
.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
.pImageInfo = &imageInfo,
.pBufferInfo = nullptr,
.pTexelBufferView = nullptr
};

vkUpdateDescriptorSets(device->getDevice(), 1, &write, 0, nullptr);
}

::VkDescriptorSet VkDescriptorSet::getSet() const {
return set;
}
Expand Down
10 changes: 9 additions & 1 deletion src/engine/vk/VkDescriptorSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "Device.h"
#include "VkDescriptorPool.h"
#include "VkBuffer.h"
#include "VkSampler.h"
#include "VkImageView.h"

namespace Vixen::Vk {
class VkDescriptorSet {
Expand Down Expand Up @@ -31,7 +33,13 @@ namespace Vixen::Vk {

void updateUniformBuffer(
uint32_t binding,
const VkBuffer &buffer
const VkBuffer& buffer
) const;

void updateCombinedImageSampler(
uint32_t binding,
const VkSampler &sampler,
const VkImageView &view
) const;

[[nodiscard]] ::VkDescriptorSet getSet() const;
Expand Down
Loading

0 comments on commit 39a5442

Please sign in to comment.