Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
buffers.cpp
Go to the documentation of this file.
1/*
2Copyright (c) 2024 Rahul Satish Vadhyar
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22
23#include "buffers.hpp"
24#include "helper.hpp"
25#include "engineStructs.hpp"
26#include "logging.hpp"
28 VkBufferUsageFlags flags, VkMemoryPropertyFlags properties)
29{
30 this->device = device;
32 this->device, size, flags, properties, &buffer, &memory);
33 LOG(2,
34 "Buffer [" + std::to_string((uint64_t)this->buffer)
35 + "] created for buffer object [" + std::to_string((uint64_t)this)
36 + "]")
37}
38
39void Chronos::Engine::Buffer::copy(void* inputData, VkCommandPool commandPool)
40{
41 // copies the data to a staging buffer in order to copy to device buffer
42 void* data;
43 VkBuffer stagingBuffer;
44 VkDeviceMemory stagingBufferMemory;
46 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
47 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
48 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
49 &stagingBuffer, &stagingBufferMemory);
50 vkMapMemory(device.device, stagingBufferMemory, 0, size, 0, &data);
51 memcpy(data, inputData, (size_t)size);
52 vkUnmapMemory(device.device, stagingBufferMemory);
54 device, stagingBuffer, buffer, size, commandPool);
55 vkDestroyBuffer(device.device, stagingBuffer, nullptr);
56 vkFreeMemory(device.device, stagingBufferMemory, nullptr);
57 LOG(3,
58 "Data [" + std::to_string((uint64_t)buffer)
59 + "] copied to device buffer [" + std::to_string((uint64_t)buffer)
60 + "]")
61}
62
64{
65 vkDestroyBuffer(device.device, buffer, nullptr);
66 vkFreeMemory(device.device, memory, nullptr);
67 LOG(2,
68 "Buffer [" + std::to_string((uint64_t)buffer)
69 + "] destroyed on device ["
70 + std::to_string((uint64_t)device.device) + "]")
71}
72
74{
75 size = sizeof(UniformBufferObject);
76 Buffer::create(device, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
77 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
78 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); // want to be visible to
79 // host and device
80 vkMapMemory(device.device, memory, 0, size, 0, &data);
81}
82
83void Chronos::Engine::UniformBuffer::update(VkExtent2D swapChainExtent, float x,
84 float y, float rotation, float x_size, float y_size)
85{
86 // This is where the shape parameters are updated
88 ubo.model = glm::translate(glm::mat4(1.0f), glm::vec3(y, -x, 0.0f));
89 ubo.model = glm::rotate(
90 ubo.model, glm::radians(rotation), glm::vec3(0.0f, 0.0f, 1.0f));
91 ubo.model = glm::scale(ubo.model, glm::vec3(y_size, x_size, 1.0f));
92 ubo.view = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.1f),
93 glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f));
94 ubo.proj = glm::ortho(-1.0f, 1.0f,
95 -(float)swapChainExtent.height / (float)swapChainExtent.width,
96 (float)swapChainExtent.height / (float)swapChainExtent.width, -100.0f,
97 100.0f);
98 ubo.proj[1][1] *= -1;
99 memcpy(data, &ubo, sizeof(ubo));
100 LOG(4,
101 "Uniform buffer object updated for buffer ["
102 + std::to_string((uint64_t)buffer) + "]")
103}
104
106{
107 size = sizeof(UniformColorBufferObject);
108 Buffer::create(device, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
109 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
110 | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); // want to be visible to
111 // host and device
112 vkMapMemory(device.device, memory, 0, size, 0, &data);
113 LOG(3,
114 "Color buffer object created for buffer ["
115 + std::to_string((uint64_t)buffer) + "]")
116}
117
119{
120 // copy the color to the buffer
122 ubo.color = color;
123 memcpy(data, &ubo, sizeof(ubo));
124 LOG(4,
125 "Color buffer object updated for buffer ["
126 + std::to_string((uint64_t)buffer) + "]")
127}
Contains the Buffer and UniformBuffer class.
VkBuffer buffer
The buffer.
Definition buffers.hpp:78
VkDeviceSize size
The size of the buffer.
Definition buffers.hpp:83
void destroy()
Destroys the buffer and frees the memory.
Definition buffers.cpp:63
void copy(void *data, VkCommandPool commandPool)
Copies the data to the buffer.
Definition buffers.cpp:39
void create(Chronos::Engine::Device device, VkBufferUsageFlags flags, VkMemoryPropertyFlags properties)
This is used to initialize the buffer.
Definition buffers.cpp:27
VkDeviceMemory memory
The device memory of the buffer.
Definition buffers.hpp:88
Chronos::Engine::Device device
The device on which the buffer is stored.
Definition buffers.hpp:94
void update(glm::vec3 color)
Updates the buffer with the new color.
Definition buffers.cpp:118
void create(Chronos::Engine::Device device)
Creates the buffer and maps the memory.
Definition buffers.cpp:105
This initializes, manages and destroys the logical and physical devices(GPU).
Definition device.hpp:47
VkDevice device
This is the logical device that is used by Vulkan.
Definition device.hpp:52
void update(VkExtent2D swapChainExtent, float x, float y, float rotation, float x_size, float y_size)
Updates the buffer with the given information.
Definition buffers.cpp:83
void create(Chronos::Engine::Device device)
Creates the buffer and maps the memory.
Definition buffers.cpp:73
Contains ShapeParams and UniformBufferObject structs.
Contains various common functions used by other classes in the Engine namespace.
#define LOG(LEVEL, MESSAGE)
Definition logging.hpp:60
void createBuffer(Chronos::Engine::Device device, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer *buffer, VkDeviceMemory *bufferMemory)
Creates a buffer of a given size, usage and properties.
Definition helper.cpp:78
void copyBuffer(Chronos::Engine::Device device, VkBuffer srcBuffer, VkBuffer dstBuffer, VkDeviceSize size, VkCommandPool commandPool)
Copies data from one buffer to another.
Definition helper.cpp:111
Uniform struct passed to shader.
Uniform struct for color passed to shader.