25#include "shaders/colorVert.hpp"
26#include "shaders/colorFrag.hpp"
29 VkSampler textureSampler, VkRenderPass* renderPass)
40 colorVert_spv, colorVert_spv_len, colorFrag_spv, colorFrag_spv_len);
45 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
46 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
51 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
52 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
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] });
66 vertexBuffer.destroy();
67 indexBuffer.destroy();
69 colorBuffers[i].destroy();
76 std::vector<VkDescriptorSetLayout> layouts(
78 VkDescriptorSetAllocateInfo allocInfo {};
79 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
80 allocInfo.descriptorPool = descriptorPool;
82 allocInfo.pSetLayouts = layouts.data();
84 if (vkAllocateDescriptorSets(
85 device->device, &allocInfo, descriptorSets.data())
87 throw std::runtime_error(
"failed to allocate descriptor sets!");
90 VkDescriptorBufferInfo bufferInfo {};
91 bufferInfo.buffer = uniformBuffers[i].buffer;
92 bufferInfo.offset = 0;
95 VkDescriptorBufferInfo bufferInfo2 {};
96 bufferInfo2.buffer = colorBuffers[i].buffer;
97 bufferInfo2.offset = 0;
100 std::array<VkWriteDescriptorSet, 2> descriptorWrites {};
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;
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;
118 vkUpdateDescriptorSets(device->device,
119 static_cast<uint32_t
>(descriptorWrites.size()),
120 descriptorWrites.data(), 0,
nullptr);
124std::vector<VkDescriptorType>
127 return std::vector<VkDescriptorType> { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
128 VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER };
131std::vector<VkShaderStageFlagBits>
134 return std::vector<VkShaderStageFlagBits> { VK_SHADER_STAGE_VERTEX_BIT,
135 VK_SHADER_STAGE_FRAGMENT_BIT };
150 attributeDescriptions.size());
151 for (
int i = 0; i < static_cast<int>(attributeDescriptions.size()); i++) {
155 pipelineAttributes.
topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
156 pipelineAttributes.
frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
159 = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT
160 | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
163 = VK_BLEND_FACTOR_ONE;
165 = VK_BLEND_FACTOR_ZERO;
169 = VK_BLEND_FACTOR_ONE;
171 = VK_BLEND_FACTOR_ZERO;
175 return pipelineAttributes;
179 uint32_t imageIndex,
float bgColor[3], VkViewport& viewport,
180 VkRect2D& scissor, std::vector<VkCommandBuffer>& commandBuffers)
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);
Contains the class for creating a rectangle filled with texture.
VkDeviceSize size
The size of the buffer.
void copy(void *data, VkCommandPool commandPool)
Copies the data to the buffer.
void create(Chronos::Engine::Device device, VkBufferUsageFlags flags, VkMemoryPropertyFlags properties)
This is used to initialize the buffer.
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
void createDescriptorSets() 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).
virtual void destroy()=0
Destroys the object and releases associated resources.
VkCommandPool commandPool
Chronos::Engine::SwapChain * swapChain
Chronos::Engine::Device * device
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.
VkRenderPass * renderPass
Contains ShapeParams and UniformBufferObject structs.
static VkVertexInputBindingDescription getBindingDescription()
Creates a VkVertexInputBindingDescription for the vertex based on the size of the vertex.
static std::array< VkVertexInputAttributeDescription, 1 > getAttributeDescriptions()
Generates the VkVertexInputAttributeDescription for the vertex based on pos attribute of the vertex.
Structure defining attributes required for creating a graphics pipeline.
VkPrimitiveTopology topology
VkPipelineColorBlendAttachmentState colorBlendAttachment
std::vector< VkVertexInputBindingDescription > bindingDescriptions
std::vector< VkVertexInputAttributeDescription > attributeDescriptions