25#include "shaders/textureVert.hpp"
26#include "shaders/textureFrag.hpp"
31 VkRenderPass* renderPass)
38 textureFrag_spv, textureFrag_spv_len);
43 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
44 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
49 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
50 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
56 uniformBuffers[currentFrame].update(swapChain->swapChainExtent, params.x,
57 params.y, params.rotation, params.xSize, params.ySize);
62 vertexBuffer.destroy();
63 indexBuffer.destroy();
69 std::vector<VkDescriptorSetLayout> layouts(
71 VkDescriptorSetAllocateInfo allocInfo {};
72 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
73 allocInfo.descriptorPool = descriptorPool;
75 allocInfo.pSetLayouts = layouts.data();
77 if (vkAllocateDescriptorSets(
78 device->device, &allocInfo, descriptorSets.data())
80 throw std::runtime_error(
"failed to allocate descriptor sets!");
83 VkDescriptorBufferInfo bufferInfo {};
84 bufferInfo.buffer = uniformBuffers[i].buffer;
85 bufferInfo.offset = 0;
88 VkDescriptorImageInfo imageInfo {};
89 imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
90 imageInfo.imageView = texture.textureImageView;
91 imageInfo.sampler = textureSampler;
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;
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;
111 vkUpdateDescriptorSets(device->device,
112 static_cast<uint32_t
>(descriptorWrites.size()),
113 descriptorWrites.data(), 0,
nullptr);
117std::vector<VkDescriptorType>
120 return std::vector<VkDescriptorType> { VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
121 VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER };
124std::vector<VkShaderStageFlagBits>
127 return std::vector<VkShaderStageFlagBits> { VK_SHADER_STAGE_VERTEX_BIT,
128 VK_SHADER_STAGE_FRAGMENT_BIT };
143 attributeDescriptions.size());
144 for (
int i = 0; i < static_cast<int>(attributeDescriptions.size()); i++) {
148 pipelineAttributes.
topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
149 pipelineAttributes.
frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
152 = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT
153 | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
156 = VK_BLEND_FACTOR_ONE;
158 = VK_BLEND_FACTOR_ZERO;
162 = VK_BLEND_FACTOR_ONE;
164 = VK_BLEND_FACTOR_ZERO;
168 return pipelineAttributes;
172 uint32_t imageIndex,
float bgColor[3], VkViewport& viewport,
173 VkRect2D& scissor, std::vector<VkCommandBuffer>& commandBuffers)
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);
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.
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
This class holds the Vulkan data and objects needed for a texture.
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)
void createDescriptorSets() override
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::Texture texture
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.
VkPrimitiveTopology topology
VkPipelineColorBlendAttachmentState colorBlendAttachment
std::vector< VkVertexInputBindingDescription > bindingDescriptions
std::vector< VkVertexInputAttributeDescription > attributeDescriptions
static VkVertexInputBindingDescription getBindingDescription()
Creates a VkVertexInputBindingDescription for the vertex based on the size of the vertex.
static std::array< VkVertexInputAttributeDescription, 2 > getAttributeDescriptions()
Generates the VkVertexInputAttributeDescription for the vertex based on attributes(pos,...