Chronos 0.0
A advanced 2D rendering and animation system
Loading...
Searching...
No Matches
TexturedRectangle.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 "TexturedRectangle.hpp"
24#include "engineStructs.hpp"
25#include "shaders/textureVert.hpp"
26#include "shaders/textureFrag.hpp"
27
29 VkCommandPool commandPool, Chronos::Engine::SwapChain* swapChain,
30 VkSampler textureSampler, Chronos::Engine::Texture texture,
31 VkRenderPass* renderPass)
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}
53
55{
56 uniformBuffers[currentFrame].update(swapChain->swapChainExtent, params.x,
57 params.y, params.rotation, params.xSize, params.ySize);
58}
59
61{
62 vertexBuffer.destroy();
63 indexBuffer.destroy();
65}
66
68{
69 std::vector<VkDescriptorSetLayout> layouts(
70 MAX_FRAMES_IN_FLIGHT, descriptorSetLayout);
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();
76 descriptorSets.resize(MAX_FRAMES_IN_FLIGHT);
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}
116
117std::vector<VkDescriptorType>
119{
120 return std::vector<VkDescriptorType> { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
121 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER };
122}
123
124std::vector<VkShaderStageFlagBits>
126{
127 return std::vector<VkShaderStageFlagBits> { VK_SHADER_STAGE_VERTEX_BIT,
128 VK_SHADER_STAGE_FRAGMENT_BIT };
129}
130
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}
170
172 uint32_t imageIndex, float bgColor[3], VkViewport& viewport,
173 VkRect2D& scissor, std::vector<VkCommandBuffer>& commandBuffers)
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}
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
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
This class holds the Vulkan data and objects needed for a texture.
Definition texture.hpp:134
void update(uint32_t currentFrame) override
Updates the object for the current frame.
void destroy() override
Destroys the object and releases associated resources.
Chronos::Engine::Buffer indexBuffer
The index buffer that is used to store the indices.
std::vector< VkShaderStageFlagBits > getDescriptorStages() override
void init(Chronos::Engine::Device *device, VkCommandPool commandPool, Chronos::Engine::SwapChain *swapChain, VkSampler textureSampler, Chronos::Engine::Texture texture, VkRenderPass *renderPass)
const std::vector< uint16_t > indices
const std::vector< TexturedVertex > vertices
void render(uint32_t currentFrame, uint32_t imageIndex, float bgColor[3], VkViewport &viewport, VkRect2D &scissor, std::vector< VkCommandBuffer > &commandBuffers) override
PipelineAttributes getPipelineAttributes() override
std::vector< VkDescriptorType > getDescriptorTypes() override
Chronos::Engine::Buffer vertexBuffer
The vertex buffer that is used to store the vertices.
Contains ShapeParams and UniformBufferObject structs.
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
Uniform struct passed to shader.
#define MAX_FRAMES_IN_FLIGHT
The number of frames in flight.