Add 2D Batch renderer

This commit is contained in:
Thraix
2023-03-08 12:05:57 +01:00
parent c975ed2674
commit 796de92a56
21 changed files with 439 additions and 48 deletions
@@ -35,21 +35,49 @@ namespace Copium
}
}
void DescriptorSet::AddSampler(const Sampler& sampler, uint32_t binding)
void DescriptorSet::AddSampler(const Sampler& sampler, uint32_t binding, int arrayIndex)
{
for (size_t i = 0; i < descriptorSets.size(); ++i)
{
AddSampler(sampler, binding, i, arrayIndex);
}
}
void DescriptorSet::AddSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex)
{
CP_ASSERT(index >= 0 && index < descriptorSets.size(), "AddSampler : index is out of range");
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(index);
VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSets[index];
descriptorWrite.dstBinding = binding;
descriptorWrite.dstArrayElement = arrayIndex;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pBufferInfo = nullptr;
descriptorWrite.pImageInfo = &imageInfo;
descriptorWrite.pTexelBufferView = nullptr;
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
}
void DescriptorSet::AddSamplers(const std::vector<const Sampler*>& samplers, uint32_t binding)
{
for (size_t i = 0; i < descriptorSets.size(); ++i) {
VkDescriptorImageInfo imageInfo = sampler.GetDescriptorImageInfo(i);
VkWriteDescriptorSet descriptorWrite{};
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrite.dstSet = descriptorSets[i];
descriptorWrite.dstBinding = binding;
descriptorWrite.dstArrayElement = 0;
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrite.descriptorCount = 1;
descriptorWrite.pBufferInfo = nullptr;
descriptorWrite.pImageInfo = &imageInfo;
descriptorWrite.pTexelBufferView = nullptr;
vkUpdateDescriptorSets(vulkan.GetDevice(), 1, &descriptorWrite, 0, nullptr);
std::vector<VkWriteDescriptorSet> descriptorWrites{samplers.size()};
for (size_t j = 0; j < samplers.size(); j++)
{
VkDescriptorImageInfo imageInfo = samplers[j]->GetDescriptorImageInfo(i);
descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[j].dstSet = descriptorSets[i];
descriptorWrites[j].dstBinding = binding;
descriptorWrites[j].dstArrayElement = j;
descriptorWrites[j].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[j].descriptorCount = 1;
descriptorWrites[j].pBufferInfo = nullptr;
descriptorWrites[j].pImageInfo = &imageInfo;
descriptorWrites[j].pTexelBufferView = nullptr;
}
vkUpdateDescriptorSets(vulkan.GetDevice(), descriptorWrites.size(), descriptorWrites.data(), 0, nullptr);
}
}
@@ -24,7 +24,9 @@ namespace Copium
~DescriptorSet();
void AddUniform(const UniformBuffer& uniformBuffer, uint32_t binding);
void AddSampler(const Sampler& sampler, uint32_t binding);
void AddSampler(const Sampler& sampler, uint32_t binding, int arrayIndex = 0);
void AddSampler(const Sampler& sampler, uint32_t binding, int index, int arrayIndex = 0);
void AddSamplers(const std::vector<const Sampler*>& sampler, uint32_t binding);
operator VkDescriptorSet() const;
};
}
@@ -142,8 +142,8 @@ namespace Copium
VkPipelineDepthStencilStateCreateInfo depthStencilCreateInfo{};
depthStencilCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
depthStencilCreateInfo.depthTestEnable = VK_TRUE;
depthStencilCreateInfo.depthWriteEnable = VK_TRUE;
depthStencilCreateInfo.depthTestEnable = creator.depthTest ? VK_TRUE : VK_FALSE;
depthStencilCreateInfo.depthWriteEnable = creator.depthTest ? VK_TRUE : VK_FALSE;
depthStencilCreateInfo.depthCompareOp = VK_COMPARE_OP_LESS;
depthStencilCreateInfo.depthBoundsTestEnable = VK_FALSE;
depthStencilCreateInfo.minDepthBounds = 0.0f;
@@ -155,9 +155,9 @@ namespace Copium
VkPipelineColorBlendAttachmentState colorBlendAttachment{}; // TODO: Add to PipelineCreator
colorBlendAttachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT;
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT;
colorBlendAttachment.blendEnable = VK_FALSE;
colorBlendAttachment.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
colorBlendAttachment.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO;
@@ -35,4 +35,9 @@ namespace Copium
{
frontFace = cullFrontFace;
}
void PipelineCreator::SetDepthTest(bool depthTest)
{
this->depthTest = depthTest;
}
}
@@ -28,6 +28,7 @@ namespace Copium
VkCullModeFlags cullMode = VK_CULL_MODE_BACK_BIT;
VkFrontFace frontFace = VK_FRONT_FACE_CLOCKWISE;
VkRenderPass renderPass = VK_NULL_HANDLE;
bool depthTest = true;
public:
PipelineCreator(VkRenderPass renderPass, const std::string& vertexShader, const std::string& fragmentShader);
@@ -37,5 +38,6 @@ namespace Copium
void SetPrimitiveTopology(VkPrimitiveTopology primitiveTopology);
void SetCullMode(VkCullModeFlags flags);
void SetCullFrontFace(VkFrontFace cullFrontFace);
void SetDepthTest(bool depthTest);
};
}