buffer
This commit is contained in:
parent
05c19c3f94
commit
86d7adb07f
9 changed files with 84 additions and 108 deletions
15
Crafter.Grahpics-Mesh.cppm
Normal file
15
Crafter.Grahpics-Mesh.cppm
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
export module Crafter.Graphics:Mesh;
|
||||||
|
|
||||||
|
namespace Crafter {
|
||||||
|
export class Mesh {
|
||||||
|
public:
|
||||||
|
vks::Buffer buffer;
|
||||||
|
Mesh();
|
||||||
|
private:
|
||||||
|
CameraBufferData data;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -15,17 +15,14 @@ module;
|
||||||
module Crafter.Graphics;
|
module Crafter.Graphics;
|
||||||
using namespace Crafter;
|
using namespace Crafter;
|
||||||
|
|
||||||
Camera::Camera() {
|
Camera::Camera() : buffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) {
|
||||||
old::Camera camera;
|
old::Camera camera;
|
||||||
camera.type = old::Camera::CameraType::lookat;
|
camera.type = old::Camera::CameraType::lookat;
|
||||||
camera.setPerspective(60.0f, 128 / 128, 0.1f, 512.0f);
|
camera.setPerspective(60.0f, 128 / 128, 0.1f, 512.0f);
|
||||||
camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f));
|
camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f));
|
||||||
camera.setTranslation(glm::vec3(0.0f, 0.0f, -5.0f));
|
camera.setTranslation(glm::vec3(0.0f, 0.0f, -5.0f));
|
||||||
|
|
||||||
VulkanDevice::CreateBuffer(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &buffer, sizeof(CameraBufferData));
|
buffer.value->projection = camera.matrices.perspective;
|
||||||
VulkanDevice::CHECK_VK_RESULT(buffer.map());
|
buffer.value->view = camera.matrices.view;
|
||||||
data.projection = camera.matrices.perspective;
|
buffer.value->model = glm::mat4(1.0f);
|
||||||
data.view = camera.matrices.view;
|
|
||||||
data.model = glm::mat4(1.0f);
|
|
||||||
memcpy(buffer.mapped, &data, sizeof(CameraBufferData));
|
|
||||||
}
|
}
|
||||||
|
|
@ -12,6 +12,7 @@ module;
|
||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
|
|
||||||
export module Crafter.Graphics:Camera;
|
export module Crafter.Graphics:Camera;
|
||||||
|
import :VulkanBuffer;
|
||||||
|
|
||||||
namespace Crafter {
|
namespace Crafter {
|
||||||
struct CameraBufferData {
|
struct CameraBufferData {
|
||||||
|
|
@ -21,9 +22,7 @@ namespace Crafter {
|
||||||
};
|
};
|
||||||
export class Camera {
|
export class Camera {
|
||||||
public:
|
public:
|
||||||
vks::Buffer buffer;
|
Buffer<CameraBufferData, 1> buffer;
|
||||||
Camera();
|
Camera();
|
||||||
private:
|
|
||||||
CameraBufferData data;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,4 +22,10 @@ namespace Crafter {
|
||||||
std::uint8_t b;
|
std::uint8_t b;
|
||||||
std::uint8_t a;
|
std::uint8_t a;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export struct Vertex_xf32_yf32_zf32 {
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
54
Crafter.Graphics-VulkanBuffer.cppm
Normal file
54
Crafter.Graphics-VulkanBuffer.cppm
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
module;
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
#include "VulkanInitializers.hpp"
|
||||||
|
|
||||||
|
export module Crafter.Graphics:VulkanBuffer;
|
||||||
|
import :VulkanDevice;
|
||||||
|
|
||||||
|
namespace Crafter {
|
||||||
|
export template <typename T, std::uint32_t count>
|
||||||
|
class Buffer {
|
||||||
|
public:
|
||||||
|
T* value;
|
||||||
|
VkDescriptorBufferInfo descriptor;
|
||||||
|
Buffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags) {
|
||||||
|
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, sizeof(T)*count);
|
||||||
|
VulkanDevice::CHECK_VK_RESULT(vkCreateBuffer(VulkanDevice::device, &bufferCreateInfo, nullptr, &buffer));
|
||||||
|
|
||||||
|
// Create the memory backing up the buffer handle
|
||||||
|
VkMemoryRequirements memReqs;
|
||||||
|
VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();
|
||||||
|
vkGetBufferMemoryRequirements(VulkanDevice::device, buffer, &memReqs);
|
||||||
|
memAlloc.allocationSize = memReqs.size;
|
||||||
|
// Find a memory type index that fits the properties of the buffer
|
||||||
|
memAlloc.memoryTypeIndex = VulkanDevice::GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags);
|
||||||
|
// If the buffer has VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT set we also need to enable the appropriate flag during allocation
|
||||||
|
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo{};
|
||||||
|
if (usageFlags & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
|
||||||
|
allocFlagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR;
|
||||||
|
allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
|
||||||
|
memAlloc.pNext = &allocFlagsInfo;
|
||||||
|
}
|
||||||
|
VulkanDevice::CHECK_VK_RESULT(vkAllocateMemory(VulkanDevice::device, &memAlloc, nullptr, &memory));
|
||||||
|
|
||||||
|
alignment = memReqs.alignment;
|
||||||
|
usageFlags = usageFlags;
|
||||||
|
memoryPropertyFlags = memoryPropertyFlags;
|
||||||
|
|
||||||
|
descriptor.offset = 0;
|
||||||
|
descriptor.buffer = buffer;
|
||||||
|
descriptor.range = sizeof(T)*count;
|
||||||
|
|
||||||
|
VulkanDevice::CHECK_VK_RESULT(vkBindBufferMemory(VulkanDevice::device, buffer, memory, 0));
|
||||||
|
VulkanDevice::CHECK_VK_RESULT(vkMapMemory(VulkanDevice::device, memory, 0, sizeof(T)*count, 0, reinterpret_cast<void**>(&value)));
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
VkBuffer buffer = VK_NULL_HANDLE;
|
||||||
|
VkDeviceMemory memory = VK_NULL_HANDLE;
|
||||||
|
VkDeviceSize alignment = 0;
|
||||||
|
VkMemoryPropertyFlags memoryPropertyFlags;
|
||||||
|
VkBufferUsageFlags usageFlags;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
@ -283,97 +283,3 @@ std::uint32_t VulkanDevice::GetMemoryType(uint32_t typeBits, VkMemoryPropertyFla
|
||||||
|
|
||||||
throw std::runtime_error("Could not find a matching memory type");
|
throw std::runtime_error("Could not find a matching memory type");
|
||||||
}
|
}
|
||||||
|
|
||||||
void VulkanDevice::CreateBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize size, VkBuffer *buffer, VkDeviceMemory *memory, void *data)
|
|
||||||
{
|
|
||||||
// Create the buffer handle
|
|
||||||
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, size);
|
|
||||||
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
||||||
CHECK_VK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, buffer));
|
|
||||||
|
|
||||||
// Create the memory backing up the buffer handle
|
|
||||||
VkMemoryRequirements memReqs;
|
|
||||||
VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();
|
|
||||||
vkGetBufferMemoryRequirements(device, *buffer, &memReqs);
|
|
||||||
memAlloc.allocationSize = memReqs.size;
|
|
||||||
// Find a memory type index that fits the properties of the buffer
|
|
||||||
memAlloc.memoryTypeIndex = GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags);
|
|
||||||
// If the buffer has VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT set we also need to enable the appropriate flag during allocation
|
|
||||||
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo{};
|
|
||||||
if (usageFlags & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
|
|
||||||
allocFlagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR;
|
|
||||||
allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
|
|
||||||
memAlloc.pNext = &allocFlagsInfo;
|
|
||||||
}
|
|
||||||
CHECK_VK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, memory));
|
|
||||||
|
|
||||||
// If a pointer to the buffer data has been passed, map the buffer and copy over the data
|
|
||||||
if (data != nullptr)
|
|
||||||
{
|
|
||||||
void *mapped;
|
|
||||||
CHECK_VK_RESULT(vkMapMemory(device, *memory, 0, size, 0, &mapped));
|
|
||||||
memcpy(mapped, data, size);
|
|
||||||
// If host coherency hasn't been requested, do a manual flush to make writes visible
|
|
||||||
if ((memoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0)
|
|
||||||
{
|
|
||||||
VkMappedMemoryRange mappedRange = vks::initializers::mappedMemoryRange();
|
|
||||||
mappedRange.memory = *memory;
|
|
||||||
mappedRange.offset = 0;
|
|
||||||
mappedRange.size = size;
|
|
||||||
vkFlushMappedMemoryRanges(device, 1, &mappedRange);
|
|
||||||
}
|
|
||||||
vkUnmapMemory(device, *memory);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach the memory to the buffer object
|
|
||||||
CHECK_VK_RESULT(vkBindBufferMemory(device, *buffer, *memory, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void VulkanDevice::CreateBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, vks::Buffer *buffer, VkDeviceSize size, void *data)
|
|
||||||
{
|
|
||||||
buffer->device = device;
|
|
||||||
|
|
||||||
// Create the buffer handle
|
|
||||||
VkBufferCreateInfo bufferCreateInfo = vks::initializers::bufferCreateInfo(usageFlags, size);
|
|
||||||
CHECK_VK_RESULT(vkCreateBuffer(device, &bufferCreateInfo, nullptr, &buffer->buffer));
|
|
||||||
|
|
||||||
// Create the memory backing up the buffer handle
|
|
||||||
VkMemoryRequirements memReqs;
|
|
||||||
VkMemoryAllocateInfo memAlloc = vks::initializers::memoryAllocateInfo();
|
|
||||||
vkGetBufferMemoryRequirements(device, buffer->buffer, &memReqs);
|
|
||||||
memAlloc.allocationSize = memReqs.size;
|
|
||||||
// Find a memory type index that fits the properties of the buffer
|
|
||||||
memAlloc.memoryTypeIndex = GetMemoryType(memReqs.memoryTypeBits, memoryPropertyFlags);
|
|
||||||
// If the buffer has VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT set we also need to enable the appropriate flag during allocation
|
|
||||||
VkMemoryAllocateFlagsInfoKHR allocFlagsInfo{};
|
|
||||||
if (usageFlags & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
|
|
||||||
allocFlagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR;
|
|
||||||
allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
|
|
||||||
memAlloc.pNext = &allocFlagsInfo;
|
|
||||||
}
|
|
||||||
CHECK_VK_RESULT(vkAllocateMemory(device, &memAlloc, nullptr, &buffer->memory));
|
|
||||||
|
|
||||||
buffer->alignment = memReqs.alignment;
|
|
||||||
buffer->size = size;
|
|
||||||
buffer->usageFlags = usageFlags;
|
|
||||||
buffer->memoryPropertyFlags = memoryPropertyFlags;
|
|
||||||
|
|
||||||
// If a pointer to the buffer data has been passed, map the buffer and copy over the data
|
|
||||||
if (data != nullptr)
|
|
||||||
{
|
|
||||||
CHECK_VK_RESULT(buffer->map());
|
|
||||||
memcpy(buffer->mapped, data, size);
|
|
||||||
if ((memoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0) {
|
|
||||||
buffer->flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer->unmap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize a default descriptor that covers the whole buffer size
|
|
||||||
buffer->setupDescriptor();
|
|
||||||
|
|
||||||
// Attach the memory to the buffer object
|
|
||||||
CHECK_VK_RESULT(buffer->bind());
|
|
||||||
}
|
|
||||||
|
|
@ -25,8 +25,6 @@ export namespace Crafter {
|
||||||
inline static VkFormat depthFormat = VK_FORMAT_UNDEFINED;
|
inline static VkFormat depthFormat = VK_FORMAT_UNDEFINED;
|
||||||
static void CreateDevice();
|
static void CreateDevice();
|
||||||
static void CHECK_VK_RESULT(VkResult result);
|
static void CHECK_VK_RESULT(VkResult result);
|
||||||
static void CreateBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, VkDeviceSize size, VkBuffer *buffer, VkDeviceMemory *memory, void *data = nullptr);
|
|
||||||
static void CreateBuffer(VkBufferUsageFlags usageFlags, VkMemoryPropertyFlags memoryPropertyFlags, vks::Buffer *buffer, VkDeviceSize size, void *data = nullptr);
|
|
||||||
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags properties);
|
static std::uint32_t GetMemoryType(std::uint32_t typeBits, VkMemoryPropertyFlags properties);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -12,3 +12,4 @@ export import :VulkanShader;
|
||||||
export import :VulkanElement;
|
export import :VulkanElement;
|
||||||
export import :Camera;
|
export import :Camera;
|
||||||
export import :VulkanElementFromPipeline;
|
export import :VulkanElementFromPipeline;
|
||||||
|
export import :VulkanBuffer;
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
"standard": "c++26",
|
"standard": "c++26",
|
||||||
"source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-Camera"],
|
"source_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics-UiElement", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-WindowWaylandVulkan", "VulkanBuffer", "VulkanTools", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-Camera"],
|
||||||
"c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"],
|
"c_files": ["wayland-xdg-decoration-unstable-v1-client-protocol", "xdg-shell-protocol", "shm"],
|
||||||
"module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-VulkanElementFromPipeline", "Crafter.Graphics-Camera"],
|
"module_files": ["Crafter.Graphics-Window","Crafter.Graphics-WindowWayland","Crafter.Graphics-WindowWaylandWayland", "Crafter.Graphics", "Crafter.Graphics-UiElement", "Crafter.Graphics-Types", "Crafter.Graphics-VulkanDevice", "Crafter.Graphics-VulkanPipeline", "Crafter.Graphics-VulkanShader", "Crafter.Graphics-WindowWaylandVulkan", "Crafter.Graphics-VulkanElement", "Crafter.Graphics-VulkanElementFromPipeline", "Crafter.Graphics-Camera", "Crafter.Graphics-VulkanBuffer"],
|
||||||
"build_dir": "./build",
|
"build_dir": "./build",
|
||||||
"output_dir": "./bin",
|
"output_dir": "./bin",
|
||||||
"type":"library",
|
"type":"library",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue