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

#include <TexturedRectangle.hpp>

Inheritance diagram for Chronos::Engine::TexturedRectangle:
Collaboration diagram for Chronos::Engine::TexturedRectangle:

Public Member Functions

void init (Chronos::Engine::Device *device, VkCommandPool commandPool, Chronos::Engine::SwapChain *swapChain, VkSampler textureSampler, Chronos::Engine::Texture texture, 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 }
 
const std::vector< TexturedVertexvertices
 
Chronos::Engine::Texture texture
 
Chronos::Manager::ShapeParams params
 
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
 

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 34 of file TexturedRectangle.hpp.

Member Function Documentation

◆ createDescriptorSets()

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

Implements Chronos::Engine::Object.

Definition at line 67 of file TexturedRectangle.cpp.

68{
69 std::vector<VkDescriptorSetLayout> layouts(
71 VkDescriptorSetAllocateInfo allocInfo {};
72 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
73 allocInfo.descriptorPool = descriptorPool;
74 allocInfo.descriptorSetCount = static_cast<uint32_t>(MAX_FRAMES_IN_FLIGHT);
75 allocInfo.pSetLayouts = layouts.data();
77 if (vkAllocateDescriptorSets(
78 device->device, &allocInfo, descriptorSets.data())
79 != VK_SUCCESS) {
80 throw std::runtime_error("failed to allocate descriptor sets!");
81 }
82 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
83 VkDescriptorBufferInfo bufferInfo {};
84 bufferInfo.buffer = uniformBuffers[i].buffer;
85 bufferInfo.offset = 0;
86 bufferInfo.range = sizeof(UniformBufferObject);
87
88 VkDescriptorImageInfo imageInfo {};
89 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
90 imageInfo.imageView = texture.textureImageView;
91 imageInfo.sampler = textureSampler;
92
93 std::array<VkWriteDescriptorSet, 2> descriptorWrites {};
94 descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
95 descriptorWrites[0].dstSet = descriptorSets[i];
96 descriptorWrites[0].dstBinding = 0;
97 descriptorWrites[0].dstArrayElement = 0;
98 descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
99 descriptorWrites[0].descriptorCount = 1;
100 descriptorWrites[0].pBufferInfo = &bufferInfo;
101
102 descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
103 descriptorWrites[1].dstSet = descriptorSets[i];
104 descriptorWrites[1].dstBinding = 1;
105 descriptorWrites[1].dstArrayElement = 0;
106 descriptorWrites[1].descriptorType
107 = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
108 descriptorWrites[1].descriptorCount = 1;
109 descriptorWrites[1].pImageInfo = &imageInfo;
110
111 vkUpdateDescriptorSets(device->device,
112 static_cast<uint32_t>(descriptorWrites.size()),
113 descriptorWrites.data(), 0, nullptr);
114 }
115}
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
VkImageView textureImageView
The image view for the texture.
Definition texture.hpp:149
#define MAX_FRAMES_IN_FLIGHT
The number of frames in flight.

References MAX_FRAMES_IN_FLIGHT.

◆ destroy()

void Chronos::Engine::TexturedRectangle::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 60 of file TexturedRectangle.cpp.

61{
65}
void destroy()
Destroys the buffer and frees the memory.
Definition buffers.cpp:63
virtual void destroy()=0
Destroys the object and releases associated resources.
Definition object.cpp:235
Chronos::Engine::Buffer indexBuffer
The index buffer that is used to store the indices.
Chronos::Engine::Buffer vertexBuffer
The vertex buffer that is used to store the vertices.

References Chronos::Engine::Object::destroy().

Here is the call graph for this function:

◆ getDescriptorStages()

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

Implements Chronos::Engine::Object.

Definition at line 125 of file TexturedRectangle.cpp.

126{
127 return std::vector<VkShaderStageFlagBits> { VK_SHADER_STAGE_VERTEX_BIT,
128 VK_SHADER_STAGE_FRAGMENT_BIT };
129}

◆ getDescriptorTypes()

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

Implements Chronos::Engine::Object.

Definition at line 118 of file TexturedRectangle.cpp.

119{
120 return std::vector<VkDescriptorType> { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
121 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER };
122}

◆ getPipelineAttributes()

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

Implements Chronos::Engine::Object.

Definition at line 132 of file TexturedRectangle.cpp.

133{
134 Chronos::Engine::PipelineAttributes pipelineAttributes;
135
136 auto bindingDescription = TexturedVertex::getBindingDescription();
137 auto attributeDescriptions = TexturedVertex::getAttributeDescriptions();
138
139 pipelineAttributes.bindingDescriptions.resize(1);
140 pipelineAttributes.bindingDescriptions[0] = bindingDescription;
141
142 pipelineAttributes.attributeDescriptions.resize(
143 attributeDescriptions.size());
144 for (int i = 0; i < static_cast<int>(attributeDescriptions.size()); i++) {
145 pipelineAttributes.attributeDescriptions[i] = attributeDescriptions[i];
146 }
147
148 pipelineAttributes.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
149 pipelineAttributes.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
150
151 pipelineAttributes.colorBlendAttachment.colorWriteMask
152 = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT
153 | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
154 pipelineAttributes.colorBlendAttachment.blendEnable = VK_FALSE;
155 pipelineAttributes.colorBlendAttachment.srcColorBlendFactor
156 = VK_BLEND_FACTOR_ONE;
157 pipelineAttributes.colorBlendAttachment.dstColorBlendFactor
158 = VK_BLEND_FACTOR_ZERO; // Optional
159 pipelineAttributes.colorBlendAttachment.colorBlendOp
160 = VK_BLEND_OP_ADD; // Optional
161 pipelineAttributes.colorBlendAttachment.srcAlphaBlendFactor
162 = VK_BLEND_FACTOR_ONE; // Optional
163 pipelineAttributes.colorBlendAttachment.dstAlphaBlendFactor
164 = VK_BLEND_FACTOR_ZERO; // Optional
165 pipelineAttributes.colorBlendAttachment.alphaBlendOp
166 = VK_BLEND_OP_ADD; // Optional
167
168 return pipelineAttributes;
169}
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
static VkVertexInputBindingDescription getBindingDescription()
Creates a VkVertexInputBindingDescription for the vertex based on the size of the vertex.
Definition Vertex.hpp:86
static std::array< VkVertexInputAttributeDescription, 2 > getAttributeDescriptions()
Generates the VkVertexInputAttributeDescription for the vertex based on attributes(pos,...
Definition Vertex.hpp:101

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

Here is the call graph for this function:

◆ init()

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

Definition at line 28 of file TexturedRectangle.cpp.

32{
33 this->texture = texture;
34
37 Chronos::Engine::ObjectType::TypeTexturedRectangle, textureVert_spv, textureVert_spv_len,
38 textureFrag_spv, textureFrag_spv_len);
39
40 // create the vertex and index buffers and copy the data
41 vertexBuffer.size = sizeof(vertices[0]) * vertices.size();
43 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
44 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
45 vertexBuffer.copy((void*)vertices.data(), commandPool);
46
47 indexBuffer.size = sizeof(indices[0]) * indices.size();
49 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
50 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
51 indexBuffer.copy((void*)indices.data(), commandPool);
52}
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
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
const std::vector< uint16_t > indices
const std::vector< TexturedVertex > vertices

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

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

◆ render()

void Chronos::Engine::TexturedRectangle::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 171 of file TexturedRectangle.cpp.

174{
175
176 vkCmdBindPipeline(commandBuffers[currentFrame],
177 VK_PIPELINE_BIND_POINT_GRAPHICS, this->graphicsPipeline);
178 vkCmdSetViewport(commandBuffers[currentFrame], 0, 1, &viewport);
179 vkCmdSetScissor(commandBuffers[currentFrame], 0, 1, &scissor);
180 VkBuffer vertexBuffers[] = { this->vertexBuffer.buffer };
181 VkDeviceSize offsets[] = { 0 };
182 vkCmdBindVertexBuffers(
183 commandBuffers[currentFrame], 0, 1, vertexBuffers, offsets);
184 vkCmdBindIndexBuffer(commandBuffers[currentFrame], this->indexBuffer.buffer,
185 0, VK_INDEX_TYPE_UINT16);
186 vkCmdBindDescriptorSets(commandBuffers[currentFrame],
187 VK_PIPELINE_BIND_POINT_GRAPHICS, this->pipelineLayout, 0, 1,
188 &this->descriptorSets[currentFrame], 0, nullptr);
189 vkCmdDrawIndexed(commandBuffers[currentFrame],
190 static_cast<uint32_t>(this->indices.size()), 1, 0, 0, 0);
191}
VkBuffer buffer
The buffer.
Definition buffers.hpp:78
VkPipeline graphicsPipeline
Definition object.hpp:130
VkPipelineLayout pipelineLayout
Definition object.hpp:133

◆ update()

void Chronos::Engine::TexturedRectangle::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 54 of file TexturedRectangle.cpp.

Member Data Documentation

◆ indexBuffer

Chronos::Engine::Buffer Chronos::Engine::TexturedRectangle::indexBuffer

The index buffer that is used to store the indices.

Definition at line 64 of file TexturedRectangle.hpp.

◆ indices

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

Definition at line 41 of file TexturedRectangle.hpp.

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

◆ params

Chronos::Manager::ShapeParams Chronos::Engine::TexturedRectangle::params

Definition at line 52 of file TexturedRectangle.hpp.

◆ texture

Chronos::Engine::Texture Chronos::Engine::TexturedRectangle::texture

Definition at line 51 of file TexturedRectangle.hpp.

◆ vertexBuffer

Chronos::Engine::Buffer Chronos::Engine::TexturedRectangle::vertexBuffer

The vertex buffer that is used to store the vertices.

Definition at line 56 of file TexturedRectangle.hpp.

◆ vertices

const std::vector<TexturedVertex> Chronos::Engine::TexturedRectangle::vertices
Initial value:
= std::vector<TexturedVertex> { { { -0.5f, -0.5f },
{ 1.0f, 0.0f } },
{ { 0.5f, -0.5f }, { 0.0f, 0.0f } },
{ { 0.5f, 0.5f }, { 0.0f, 1.0f } },
{ { -0.5f, 0.5f }, { 1.0f, 1.0f } } }

Definition at line 44 of file TexturedRectangle.hpp.

45 { { { -0.5f, -0.5f },
46 { 1.0f, 0.0f } },
47 { { 0.5f, -0.5f }, { 0.0f, 0.0f } },
48 { { 0.5f, 0.5f }, { 0.0f, 1.0f } },
49 { { -0.5f, 0.5f }, { 1.0f, 1.0f } } };

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