Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
ColoredRectangle.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 "ColoredRectangle.hpp"
24#include "engineStructs.hpp"
25#include "shaders/colorVert.hpp"
26#include "shaders/colorFrag.hpp"
28 VkCommandPool commandPool, Chronos::Engine::SwapChain* swapChain,
29 VkSampler textureSampler, VkRenderPass* renderPass)
30{
31
33 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
34 colorBuffers[i].create(*device);
35 }
36
40 colorVert_spv, colorVert_spv_len, colorFrag_spv, colorFrag_spv_len);
41
42 // create the vertex and index buffers and copy the data
43 vertexBuffer.size = sizeof(vertices[0]) * vertices.size();
45 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
46 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
47 vertexBuffer.copy((void*)vertices.data(), commandPool);
48
49 indexBuffer.size = sizeof(indices[0]) * indices.size();
51 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
52 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
53 indexBuffer.copy((void*)indices.data(), commandPool);
54}
55
57{
58 uniformBuffers[currentFrame].update(swapChain->swapChainExtent, params.x,
59 params.y, params.rotation, params.xSize, params.ySize);
60 colorBuffers[currentFrame].update(
61 { params.color[0], params.color[1], params.color[2] });
62}
63
65{
66 vertexBuffer.destroy();
67 indexBuffer.destroy();
68 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
69 colorBuffers[i].destroy();
70 }
72}
73
75{
76 std::vector<VkDescriptorSetLayout> layouts(
77 MAX_FRAMES_IN_FLIGHT, descriptorSetLayout);
78 VkDescriptorSetAllocateInfo allocInfo {};
79 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
80 allocInfo.descriptorPool = descriptorPool;
81 allocInfo.descriptorSetCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
82 allocInfo.pSetLayouts = layouts.data();
83 descriptorSets.resize(MAX_FRAMES_IN_FLIGHT);
84 if (vkAllocateDescriptorSets(
85 device->device, &allocInfo, descriptorSets.data())
86 != VK_SUCCESS) {
87 throw std::runtime_error("failed to allocate descriptor sets!");
88 }
89 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
90 VkDescriptorBufferInfo bufferInfo {};
91 bufferInfo.buffer = uniformBuffers[i].buffer;
92 bufferInfo.offset = 0;
93 bufferInfo.range = sizeof(UniformBufferObject);
94
95 VkDescriptorBufferInfo bufferInfo2 {};
96 bufferInfo2.buffer = colorBuffers[i].buffer;
97 bufferInfo2.offset = 0;
98 bufferInfo2.range = sizeof(UniformColorBufferObject);
99
100 std::array<VkWriteDescriptorSet, 2> descriptorWrites {};
101
102 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
103 descriptorWrites[0].dstSet = descriptorSets[i];
104 descriptorWrites[0].dstBinding = 0;
105 descriptorWrites[0].dstArrayElement = 0;
106 descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
107 descriptorWrites[0].descriptorCount = 1;
108 descriptorWrites[0].pBufferInfo = &bufferInfo;
109
110 descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
111 descriptorWrites[1].dstSet = descriptorSets[i];
112 descriptorWrites[1].dstBinding = 1;
113 descriptorWrites[1].dstArrayElement = 0;
114 descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
115 descriptorWrites[1].descriptorCount = 1;
116 descriptorWrites[1].pBufferInfo = &bufferInfo2;
117
118 vkUpdateDescriptorSets(device->device,
119 static_cast<uint32_t>(descriptorWrites.size()),
120 descriptorWrites.data(), 0, nullptr);
121 }
122}
123
124std::vector<VkDescriptorType>
126{
127 return std::vector<VkDescriptorType> { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
128 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER };
129}
130
131std::vector<VkShaderStageFlagBits>
133{
134 return std::vector<VkShaderStageFlagBits> { VK_SHADER_STAGE_VERTEX_BIT,
135 VK_SHADER_STAGE_FRAGMENT_BIT };
136}
137
140{
141 Chronos::Engine::PipelineAttributes pipelineAttributes;
142
143 auto bindingDescription = ColorVertex::getBindingDescription();
144 auto attributeDescriptions = ColorVertex::getAttributeDescriptions();
145
146 pipelineAttributes.bindingDescriptions.resize(1);
147 pipelineAttributes.bindingDescriptions[0] = bindingDescription;
148
149 pipelineAttributes.attributeDescriptions.resize(
150 attributeDescriptions.size());
151 for (int i = 0; i < static_cast<int>(attributeDescriptions.size()); i++) {
152 pipelineAttributes.attributeDescriptions[i] = attributeDescriptions[i];
153 }
154
155 pipelineAttributes.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
156 pipelineAttributes.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
157
158 pipelineAttributes.colorBlendAttachment.colorWriteMask
159 = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT
160 | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
161 pipelineAttributes.colorBlendAttachment.blendEnable = VK_FALSE;
162 pipelineAttributes.colorBlendAttachment.srcColorBlendFactor
163 = VK_BLEND_FACTOR_ONE;
164 pipelineAttributes.colorBlendAttachment.dstColorBlendFactor
165 = VK_BLEND_FACTOR_ZERO; // Optional
166 pipelineAttributes.colorBlendAttachment.colorBlendOp
167 = VK_BLEND_OP_ADD; // Optional
168 pipelineAttributes.colorBlendAttachment.srcAlphaBlendFactor
169 = VK_BLEND_FACTOR_ONE; // Optional
170 pipelineAttributes.colorBlendAttachment.dstAlphaBlendFactor
171 = VK_BLEND_FACTOR_ZERO; // Optional
172 pipelineAttributes.colorBlendAttachment.alphaBlendOp
173 = VK_BLEND_OP_ADD; // Optional
174
175 return pipelineAttributes;
176}
177
179 uint32_t imageIndex, float bgColor[3], VkViewport& viewport,
180 VkRect2D& scissor, std::vector<VkCommandBuffer>& commandBuffers)
181{
182
183 vkCmdBindPipeline(commandBuffers[currentFrame],
184 VK_PIPELINE_BIND_POINT_GRAPHICS, this->graphicsPipeline);
185 vkCmdSetViewport(commandBuffers[currentFrame], 0, 1, &viewport);
186 vkCmdSetScissor(commandBuffers[currentFrame], 0, 1, &scissor);
187 VkBuffer vertexBuffers[] = { this->vertexBuffer.buffer };
188 VkDeviceSize offsets[] = { 0 };
189 vkCmdBindVertexBuffers(
190 commandBuffers[currentFrame], 0, 1, vertexBuffers, offsets);
191 vkCmdBindIndexBuffer(commandBuffers[currentFrame], this->indexBuffer.buffer,
192 0, VK_INDEX_TYPE_UINT16);
193 vkCmdBindDescriptorSets(commandBuffers[currentFrame],
194 VK_PIPELINE_BIND_POINT_GRAPHICS, this->pipelineLayout, 0, 1,
195 &this->descriptorSets[currentFrame], 0, nullptr);
196 vkCmdDrawIndexed(commandBuffers[currentFrame],
197 static_cast<uint32_t>(this->indices.size()), 1, 0, 0, 0);
198}
Contains the class for creating a rectangle filled with texture.
VkDeviceSize size
The size of the buffer.
Definition buffers.hpp:83
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
void init(Chronos::Engine::Device *device, VkCommandPool commandPool, Chronos::Engine::SwapChain *swapChain, VkSampler textureSampler, VkRenderPass *renderPass)
const std::vector< uint16_t > indices
Chronos::Engine::Buffer vertexBuffer
The vertex buffer that is used to store the vertices.
std::vector< Chronos::Engine::ColorBuffer > colorBuffers
std::vector< VkShaderStageFlagBits > getDescriptorStages() override
PipelineAttributes getPipelineAttributes() override
const std::vector< ColorVertex > vertices
Chronos::Engine::Buffer indexBuffer
The index buffer that is used to store the indices.
void update(uint32_t currentFrame) override
Updates the object for the current frame.
std::vector< VkDescriptorType > getDescriptorTypes() override
void render(uint32_t currentFrame, uint32_t imageIndex, float bgColor[3], VkViewport &viewport, VkRect2D &scissor, std::vector< VkCommandBuffer > &commandBuffers) override
void destroy() override
Destroys the object and releases associated resources.
This initializes, manages and destroys the logical and physical devices(GPU).
Definition device.hpp:47
virtual void destroy()=0
Destroys the object and releases associated resources.
Definition object.cpp:235
VkCommandPool commandPool
Definition object.hpp:143
Chronos::Engine::SwapChain * swapChain
Definition object.hpp:141
Chronos::Engine::Device * device
Definition object.hpp:140
void init(Chronos::Engine::Device *device, VkCommandPool commandPool, SwapChain *swapChain, VkSampler textureSampler, VkRenderPass *renderPass, ObjectType objectType, unsigned char *vertShaderCode, int vertShaderCodeSize, unsigned char *fragShaderCode, int fragShaderCodeSize)
Initializes the object.
Definition object.cpp:53
VkRenderPass * renderPass
Definition object.hpp:147
Contains ShapeParams and UniformBufferObject structs.
static VkVertexInputBindingDescription getBindingDescription()
Creates a VkVertexInputBindingDescription for the vertex based on the size of the vertex.
Definition Vertex.hpp:46
static std::array< VkVertexInputAttributeDescription, 1 > getAttributeDescriptions()
Generates the VkVertexInputAttributeDescription for the vertex based on pos attribute of the vertex.
Definition Vertex.hpp:61
Structure defining attributes required for creating a graphics pipeline.
Definition object.hpp:43
VkPrimitiveTopology topology
Definition object.hpp:49
VkPipelineColorBlendAttachmentState colorBlendAttachment
Definition object.hpp:53
std::vector< VkVertexInputBindingDescription > bindingDescriptions
Definition object.hpp:45
std::vector< VkVertexInputAttributeDescription > attributeDescriptions
Definition object.hpp:47
Uniform struct passed to shader.
Uniform struct for color passed to shader.
#define MAX_FRAMES_IN_FLIGHT
The number of frames in flight.