Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
Chronos::Engine::ColoredRectangle Class Reference

#include <ColoredRectangle.hpp>

Inheritance diagram for Chronos::Engine::ColoredRectangle:
Collaboration diagram for Chronos::Engine::ColoredRectangle:

Public Member Functions

void init (Chronos::Engine::Device *device, VkCommandPool commandPool, Chronos::Engine::SwapChain *swapChain, VkSampler textureSampler, VkRenderPass *renderPass)
 
void update (uint32_t currentFrame) override
 Updates the object for the current frame.
 
void destroy () override
 Destroys the object and releases associated resources.
 
void render (uint32_t currentFrame, uint32_t imageIndex, float bgColor[3], VkViewport &viewport, VkRect2D &scissor, std::vector< VkCommandBuffer > &commandBuffers) override
 
- Public Member Functions inherited from Chronos::Engine::Object
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.
 
void recreateGraphicsPipeline ()
 Recreates the graphics pipeline for the object.
 

Public Attributes

const std::vector< uint16_t > indices = std::vector<uint16_t> { 0, 1, 2, 2, 3, 0 }
 
Chronos::Manager::ShapeParams params
 
const std::vector< ColorVertexvertices
 
Chronos::Engine::Buffer vertexBuffer
 The vertex buffer that is used to store the vertices.
 
Chronos::Engine::Buffer indexBuffer
 The index buffer that is used to store the indices.
 
- Public Attributes inherited from Chronos::Engine::Object
VkPipeline graphicsPipeline
 
VkPipelineLayout pipelineLayout
 
std::vector< VkDescriptorSet > descriptorSets
 
Chronos::Engine::ObjectType objectType
 

Private Member Functions

void createDescriptorSets () override
 
std::vector< VkDescriptorType > getDescriptorTypes () override
 
std::vector< VkShaderStageFlagBits > getDescriptorStages () override
 
PipelineAttributes getPipelineAttributes () override
 

Private Attributes

std::vector< Chronos::Engine::ColorBuffercolorBuffers
 

Additional Inherited Members

- Protected Member Functions inherited from Chronos::Engine::Object
void createGraphicsPipeline ()
 Creates the graphics pipeline for the object.
 
void createDescriptorPool ()
 Creates the Vulkan descriptor pool for the object.
 
void createDescriptorSetLayout ()
 Creates the Vulkan descriptor set layout for the object.
 
- Protected Attributes inherited from Chronos::Engine::Object
Chronos::Engine::Devicedevice
 
Chronos::Engine::SwapChainswapChain
 
VkCommandPool commandPool
 
VkSampler textureSampler
 
VkRenderPass * renderPass
 
VkDescriptorSetLayout descriptorSetLayout
 
VkDescriptorPool descriptorPool
 
std::vector< Chronos::Engine::UniformBufferuniformBuffers
 

Detailed Description

Definition at line 33 of file ColoredRectangle.hpp.

Member Function Documentation

◆ createDescriptorSets()

void Chronos::Engine::ColoredRectangle::createDescriptorSets ( )
overrideprivatevirtual

Implements Chronos::Engine::Object.

Definition at line 74 of file ColoredRectangle.cpp.

75{
76 std::vector<VkDescriptorSetLayout> layouts(
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();
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}
std::vector< Chronos::Engine::ColorBuffer > colorBuffers
VkDevice device
This is the logical device that is used by Vulkan.
Definition device.hpp:52
VkDescriptorSetLayout descriptorSetLayout
Definition object.hpp:149
std::vector< VkDescriptorSet > descriptorSets
Definition object.hpp:135
VkDescriptorPool descriptorPool
Definition object.hpp:152
Chronos::Engine::Device * device
Definition object.hpp:140
std::vector< Chronos::Engine::UniformBuffer > uniformBuffers
Definition object.hpp:183
#define MAX_FRAMES_IN_FLIGHT
The number of frames in flight.

References MAX_FRAMES_IN_FLIGHT.

◆ destroy()

void Chronos::Engine::ColoredRectangle::destroy ( )
overridevirtual

Destroys the object and releases associated resources.

This method is responsible for cleaning up the object's resources, including uniform buffers, descriptor pool, descriptor set layout, graphics pipeline, and pipeline layout.

Implements Chronos::Engine::Object.

Definition at line 64 of file ColoredRectangle.cpp.

65{
68 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
69 colorBuffers[i].destroy();
70 }
72}
void destroy()
Destroys the buffer and frees the memory.
Definition buffers.cpp:63
Chronos::Engine::Buffer vertexBuffer
The vertex buffer that is used to store the vertices.
Chronos::Engine::Buffer indexBuffer
The index buffer that is used to store the indices.
virtual void destroy()=0
Destroys the object and releases associated resources.
Definition object.cpp:235

References Chronos::Engine::Object::destroy(), and MAX_FRAMES_IN_FLIGHT.

Here is the call graph for this function:

◆ getDescriptorStages()

std::vector< VkShaderStageFlagBits > Chronos::Engine::ColoredRectangle::getDescriptorStages ( )
overrideprivatevirtual

Implements Chronos::Engine::Object.

Definition at line 132 of file ColoredRectangle.cpp.

133{
134 return std::vector<VkShaderStageFlagBits> { VK_SHADER_STAGE_VERTEX_BIT,
135 VK_SHADER_STAGE_FRAGMENT_BIT };
136}

◆ getDescriptorTypes()

std::vector< VkDescriptorType > Chronos::Engine::ColoredRectangle::getDescriptorTypes ( )
overrideprivatevirtual

Implements Chronos::Engine::Object.

Definition at line 125 of file ColoredRectangle.cpp.

126{
127 return std::vector<VkDescriptorType> { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
128 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER };
129}

◆ getPipelineAttributes()

Chronos::Engine::PipelineAttributes Chronos::Engine::ColoredRectangle::getPipelineAttributes ( )
overrideprivatevirtual

Implements Chronos::Engine::Object.

Definition at line 139 of file ColoredRectangle.cpp.

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}
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

References Chronos::Engine::PipelineAttributes::attributeDescriptions, Chronos::Engine::PipelineAttributes::bindingDescriptions, Chronos::Engine::PipelineAttributes::colorBlendAttachment, Chronos::Engine::PipelineAttributes::frontFace, Chronos::Engine::ColorVertex::getAttributeDescriptions(), Chronos::Engine::ColorVertex::getBindingDescription(), and Chronos::Engine::PipelineAttributes::topology.

Here is the call graph for this function:

◆ init()

void Chronos::Engine::ColoredRectangle::init ( Chronos::Engine::Device device,
VkCommandPool  commandPool,
Chronos::Engine::SwapChain swapChain,
VkSampler  textureSampler,
VkRenderPass *  renderPass 
)

Definition at line 27 of file ColoredRectangle.cpp.

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}
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
const std::vector< uint16_t > indices
const std::vector< ColorVertex > vertices
VkCommandPool commandPool
Definition object.hpp:143
Chronos::Engine::SwapChain * swapChain
Definition object.hpp:141
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

References colorBuffers, Chronos::Engine::Object::commandPool, Chronos::Engine::Buffer::copy(), Chronos::Engine::Buffer::create(), Chronos::Engine::Object::device, indexBuffer, indices, Chronos::Engine::Object::init(), MAX_FRAMES_IN_FLIGHT, Chronos::Engine::Object::renderPass, Chronos::Engine::Buffer::size, Chronos::Engine::Object::swapChain, Chronos::Engine::Object::textureSampler, Chronos::Engine::TypeColoredRectangle, vertexBuffer, and vertices.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ render()

void Chronos::Engine::ColoredRectangle::render ( uint32_t  currentFrame,
uint32_t  imageIndex,
float  bgColor[3],
VkViewport &  viewport,
VkRect2D &  scissor,
std::vector< VkCommandBuffer > &  commandBuffers 
)
overridevirtual

Implements Chronos::Engine::Object.

Definition at line 178 of file ColoredRectangle.cpp.

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}
VkBuffer buffer
The buffer.
Definition buffers.hpp:78
VkPipeline graphicsPipeline
Definition object.hpp:130
VkPipelineLayout pipelineLayout
Definition object.hpp:133

◆ update()

void Chronos::Engine::ColoredRectangle::update ( uint32_t  currentFrame)
overridevirtual

Updates the object for the current frame.

This pure virtual method must be implemented by derived classes to perform any updates required for the object during each frame. The currentFrame parameter represents the index of the current frame in the application.

Parameters
currentFrameIndex of the current frame.

Implements Chronos::Engine::Object.

Definition at line 56 of file ColoredRectangle.cpp.

Member Data Documentation

◆ colorBuffers

std::vector<Chronos::Engine::ColorBuffer> Chronos::Engine::ColoredRectangle::colorBuffers
private

Definition at line 66 of file ColoredRectangle.hpp.

◆ indexBuffer

Chronos::Engine::Buffer Chronos::Engine::ColoredRectangle::indexBuffer

The index buffer that is used to store the indices.

Definition at line 59 of file ColoredRectangle.hpp.

◆ indices

const std::vector<uint16_t> Chronos::Engine::ColoredRectangle::indices = std::vector<uint16_t> { 0, 1, 2, 2, 3, 0 }

Definition at line 40 of file ColoredRectangle.hpp.

41{ 0, 1, 2, 2, 3, 0 };

◆ params

Chronos::Manager::ShapeParams Chronos::Engine::ColoredRectangle::params

Definition at line 42 of file ColoredRectangle.hpp.

◆ vertexBuffer

Chronos::Engine::Buffer Chronos::Engine::ColoredRectangle::vertexBuffer

The vertex buffer that is used to store the vertices.

Definition at line 51 of file ColoredRectangle.hpp.

◆ vertices

const std::vector<ColorVertex> Chronos::Engine::ColoredRectangle::vertices
Initial value:
= std::vector<ColorVertex> { { { -0.5f, -0.5f } },
{ { 0.5f, -0.5f } }, { { 0.5f, 0.5f } },
{ { -0.5f, 0.5f } } }

Definition at line 43 of file ColoredRectangle.hpp.

44 { { { -0.5f, -0.5f } },
45 { { 0.5f, -0.5f } }, { { 0.5f, 0.5f } },
46 { { -0.5f, 0.5f } } };

The documentation for this class was generated from the following files: