indirect with count

This commit is contained in:
Joshua Ashton 2022-08-15 21:51:01 +00:00
parent 785fa73510
commit abf83ce992
3 changed files with 78 additions and 11 deletions

View File

@ -174,6 +174,26 @@ static VulkanResult<VkPipeline> CreateGraphicsPipeline(VkDevice device, const Mi
return VulkanResult<VkPipeline>::Success(pipeline);
}
struct alignas(16) Mesh
{
uint32_t textureIdx;
uint32_t vertexOffset; // in sizeof(Vertex)
uint32_t indexOffset; // in sizeof(IndexType)
uint32_t indexCount;
AABB bounds;
};
struct alignas(16) Renderable
{
mat4 transform;
uint32_t meshIdx;
vec3 color; // should replace with something more interesting lmao
};
// look at moving me if binary is big
SmallVector<Mesh, 1024> g_meshes;
SmallVector<Renderable, 1024> g_renderables;
int main(int argc, char** argv)
{
(void)argc; (void)argv;
@ -200,8 +220,6 @@ int main(int argc, char** argv)
if (!r_objData)
return 1;
auto r_mesh = ParseOBJ(*r_objData);
auto r_buffer = r_renderContext->CreateBuffer(256 * 1024 * 1024);
if (!r_buffer)
return 1;
@ -218,8 +236,32 @@ int main(int argc, char** argv)
auto r_fsSky = r_renderContext->CreateShader(fs_SkyGradient);
if (!r_fsSky) return 1;
auto r_mesh = ParseOBJ(*r_objData);
auto newVertexSlice = *pooler.AllocSlice(sizeof(StaticVertex) * r_mesh->vertexData.VertexCount(), sizeof(StaticVertex));
auto newIndexSlice = *pooler.AllocSlice(sizeof(IndexType) * r_mesh->indices.Size(), sizeof(IndexType));
r_mesh->vertexData.GetStaticVertices().Copy((uint8_t*)(r_buffer->ptr) + newVertexSlice.offset);
r_mesh->indices.Copy ((uint8_t*)(r_buffer->ptr) + newIndexSlice.offset);
uint32_t meshIdx = uint32_t(g_meshes.EmplaceBack(Mesh
{
.textureIdx = 0,
.vertexOffset = uint32_t(newVertexSlice.offset / sizeof(StaticVertex)),
.indexOffset = uint32_t(newIndexSlice.offset / sizeof(IndexType)),
.indexCount = uint32_t(r_mesh->indices.Size()),
.bounds = r_mesh->bounds,
}));
g_renderables.EmplaceBack(Renderable
{
.transform = mat4{},
.meshIdx = meshIdx,
.color = 1_vec3,
});
//
#pragma region "make descriptor n shit"
VkSamplerCreateInfo samplerInfo =
{
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
@ -376,6 +418,7 @@ int main(int argc, char** argv)
.enableDepthWrites = false,
.vertexAttributes = Span<const VkVertexInputAttributeDescription>(nullptr, 0),
});
#pragma endregion
struct ImageUpload
{
@ -443,8 +486,6 @@ int main(int argc, char** argv)
camera.transform.orientation = eulerAnglesToQuaternion(EulerAngles{ 0_deg, 0_deg, 0_deg });
auto meshData = r_mesh->vertexData.View();
auto vertexSlice = *pooler.AllocSlice(meshData.size, 16);
auto indexSlice = *pooler.AllocSlice(r_mesh->indices.Size() * sizeof(IndexType), 16);
auto r_depthImage = r_renderContext->CreateImage(pooler, 1280, 720, depthFormat, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
auto depthImageView = *r_renderContext->CreateImageView(*r_depthImage, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT);
@ -471,13 +512,27 @@ int main(int argc, char** argv)
};
vkUpdateDescriptorSets(r_renderContext->Device(), Size(writeDescriptorSet), writeDescriptorSet, 0, nullptr);
meshData.Copy ((uint8_t*)(r_buffer->ptr) + vertexSlice.offset);
r_mesh->indices.Copy((uint8_t*)(r_buffer->ptr) + indexSlice.offset);
Input::InputHandler handler;
r_window->EnableRelativeMouse(true);
auto indirectBuffer = *pooler.AllocSlice(sizeof(VkDrawIndexedIndirectCommand), 16);
auto indirectPtr = reinterpret_cast<VkDrawIndexedIndirectCommand *>(indirectBuffer.ptr);
*indirectPtr = VkDrawIndexedIndirectCommand
{
.indexCount = g_meshes[0].indexCount,
.instanceCount = 1u,
.firstIndex = g_meshes[0].indexOffset,
.vertexOffset = int32_t(g_meshes[0].vertexOffset),
.firstInstance = 1u,
};
auto countBuffer = *pooler.AllocSlice(sizeof(uint32_t), 16);
auto countPtr = reinterpret_cast<uint32_t*>(countBuffer.ptr);
*countPtr = 1;
auto indexSlice = *pooler.AllocSlice(r_mesh->indices.Size() * sizeof(IndexType), 16);
auto t1 = Time::now();
while (r_window->Update(handler))
{
@ -576,8 +631,12 @@ int main(int argc, char** argv)
vkCmdPushConstants(cmdBuf, pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(PushConstants), &pushConstants);
vkCmdBindDescriptorSets(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, nullptr);
vkCmdBindVertexBuffers2(cmdBuf, 0, 1, &vertexSlice.buffer, &vertexSlice.offset, &vertexSlice.size, nullptr);
vkCmdBindIndexBuffer(cmdBuf, r_buffer->buffer, indexSlice.offset, VulkanIndexType);
VkDeviceSize zero = 0;
VkDeviceSize wholeSize = VK_WHOLE_SIZE;
VkDeviceSize validationBruhMoment = r_buffer->size;
//vkCmdBindVertexBuffers2(cmdBuf, 0, 1, &r_buffer->buffer, &zero, &wholeSize, nullptr);
vkCmdBindVertexBuffers2(cmdBuf, 0, 1, &r_buffer->buffer, &zero, &validationBruhMoment, nullptr);
vkCmdBindIndexBuffer(cmdBuf, r_buffer->buffer, zero, VulkanIndexType);
const VkImageMemoryBarrier undefinedToColorBarrier =
{
@ -654,7 +713,7 @@ int main(int argc, char** argv)
vkCmdDraw(cmdBuf, 3, 1, 0, 0);
vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
vkCmdDrawIndexed(cmdBuf, r_mesh->indices.Size(), 1, 0, 0, 0);
vkCmdDrawIndexedIndirectCount(cmdBuf, indirectBuffer.buffer, indirectBuffer.offset, countBuffer.buffer, countBuffer.offset, 1024u, sizeof(StaticVertex));
}
vkCmdEndRendering(cmdBuf);

View File

@ -118,9 +118,16 @@ namespace orange
VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME,
};
VkPhysicalDeviceVulkan12Features vulkan12features =
{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
.drawIndirectCount = VK_TRUE,
};
VkPhysicalDeviceDynamicRenderingFeatures dynamicRenderingFeatures =
{
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_FEATURES,
.pNext = &vulkan12features,
.dynamicRendering = VK_TRUE,
};

View File

@ -100,7 +100,8 @@ namespace orange
VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT |
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT |
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT,
};
VkBuffer buffer = VK_NULL_HANDLE;