vk: pregenerate index buffer in Vk_DrawAlias

This commit is contained in:
Denis Pauk 2024-08-03 00:18:18 +03:00
parent 06167fc711
commit d9a240a935
3 changed files with 42 additions and 48 deletions

View file

@ -1123,41 +1123,33 @@ GenStripIndexes(uint16_t *data, int from, int to)
}
}
static void
UpdateIndexBuffer(int index, uint16_t *data, VkDeviceSize bufferSize, VkDeviceSize *dstOffset)
{
uint16_t *iboData = NULL;
VK_VERIFY(buffer_invalidate(&vk_dynIndexBuffers[index].resource));
iboData = (uint16_t *)QVk_GetIndexBuffer(bufferSize, dstOffset, index);
memcpy(iboData, data, bufferSize);
VK_VERIFY(buffer_flush(&vk_dynIndexBuffers[index].resource));
}
static void
RebuildTriangleIndexBuffer()
{
VkDeviceSize dstOffset = 0;
VkDeviceSize bufferSize = 3 * vk_config.triangle_index_count * sizeof(uint16_t);
uint16_t *iboData = NULL;
uint16_t *fanData = malloc(bufferSize);
uint16_t *stripData = malloc(bufferSize);
GenFanIndexes(fanData, 0, vk_config.triangle_index_count);
GenStripIndexes(stripData, 0, vk_config.triangle_index_count);
for (int i = 0; i < NUM_DYNBUFFERS; ++i)
{
VK_VERIFY(buffer_invalidate(&vk_dynIndexBuffers[i].resource));
iboData = (uint16_t *)QVk_GetIndexBuffer(bufferSize, &dstOffset, i);
if ((i%2) == 0)
{
memcpy(iboData, fanData, bufferSize);
}
else
{
memcpy(iboData, stripData, bufferSize);
}
VK_VERIFY(buffer_flush(&vk_dynIndexBuffers[i].resource));
}
uint16_t *data = malloc(bufferSize);
GenFanIndexes(data, 0, vk_config.triangle_index_count);
UpdateIndexBuffer(0, data, bufferSize, &dstOffset);
vk_triangleFanIbo = &vk_dynIndexBuffers[0].resource.buffer;
vk_triangleStripIbo = &vk_dynIndexBuffers[1].resource.buffer;
vk_triangleFanIboUsage = ((bufferSize % 4) == 0) ? bufferSize : (bufferSize + 4 - (bufferSize % 4));
free(fanData);
free(stripData);
GenStripIndexes(data, 0, vk_config.triangle_index_count);
UpdateIndexBuffer(1, data, bufferSize, &dstOffset);
vk_triangleStripIbo = &vk_dynIndexBuffers[1].resource.buffer;
free(data);
}
static void CreateStagingBuffer(VkDeviceSize size, qvkstagingbuffer_t *dstBuffer, int i)
@ -2167,6 +2159,8 @@ qboolean QVk_Init(void)
CreateStagingBuffers();
// assign a dynamic index buffer for triangle fan emulation
RebuildTriangleIndexBuffer();
vk_triangleFanIboUsage = ROUNDUP(3 * vk_config.triangle_index_count * sizeof(uint16_t), 4);
CreatePipelines();
CreateSamplers();
@ -2653,8 +2647,11 @@ QVk_CheckTriangleIbo(VkDeviceSize indexCount)
if (indexCount > vk_config.triangle_index_count)
{
vk_config.triangle_index_count *= BUFFER_RESIZE_FACTOR;
R_Printf(PRINT_ALL, "Resizing triangle index buffer to %u indices.\n", vk_config.triangle_index_count);
R_Printf(PRINT_ALL, "%s: Resizing triangle index buffer to %u indices.\n",
__func__, vk_config.triangle_index_count);
RebuildTriangleIndexBuffer();
vk_triangleFanIboUsage = ROUNDUP(3 * vk_config.triangle_index_count * sizeof(uint16_t), 4);
}
}

View file

@ -191,17 +191,20 @@ static void
Vk_DrawAliasFrameLerpCommands(VkDescriptorSet *descriptorSets, uint32_t *uboOffset,
int *order, int *order_end, float alpha, qvkpipeline_t *pipeline,
dxtrivertx_t *verts, vec4_t *s_lerped, int verts_count, const float *shadelight,
const float *shadevector, qboolean iscolor, int numtis)
const float *shadevector, qboolean iscolor, int num_tris)
{
int vertCounts[2] = { 0, 0 };
int pipeCounters[2] = { 0, 0 };
VkDeviceSize maxTriangleFanIdxCnt = 0;
VkBuffer fan, strip;
if (Mesh_VertsRealloc(verts_count))
{
Com_Error(ERR_FATAL, "%s: can't allocate memory", __func__);
}
fan = QVk_GetTriangleFanIbo(num_tris);
strip = QVk_GetTriangleStripIbo(num_tris);
drawInfo[0][0].firstVertex = 0;
drawInfo[1][0].firstVertex = 0;
@ -233,7 +236,6 @@ Vk_DrawAliasFrameLerpCommands(VkDescriptorSet *descriptorSets, uint32_t *uboOffs
}
drawInfo[pipelineIdx][pipeCounters[pipelineIdx]].vertexCount = count;
maxTriangleFanIdxCnt = Q_max(maxTriangleFanIdxCnt, ((count - 2) * 3));
if (iscolor)
{
@ -325,10 +327,6 @@ Vk_DrawAliasFrameLerpCommands(VkDescriptorSet *descriptorSets, uint32_t *uboOffs
drawInfo[pipelineIdx][pipeCounters[pipelineIdx]].firstVertex = vertCounts[pipelineIdx];
}
VkBuffer fan, strip;
fan = QVk_GetTriangleFanIbo(numtis);
strip = QVk_GetTriangleStripIbo(numtis);
for (int p = 0; p < 2; p++)
{
VkDeviceSize vaoSize = sizeof(modelvert) * vertCounts[p];
@ -346,7 +344,7 @@ Vk_DrawAliasFrameLerpCommands(VkDescriptorSet *descriptorSets, uint32_t *uboOffs
{
int i;
vkCmdBindIndexBuffer(vk_activeCmdbuffer, strip, 0, VK_INDEX_TYPE_UINT16);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, strip, 0 /* buffer offset */, VK_INDEX_TYPE_UINT16);
for (i = 0; i < pipeCounters[p]; i++)
{
@ -357,7 +355,7 @@ Vk_DrawAliasFrameLerpCommands(VkDescriptorSet *descriptorSets, uint32_t *uboOffs
{
int i;
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, 0, VK_INDEX_TYPE_UINT16);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, 0 /* buffer offset */, VK_INDEX_TYPE_UINT16);
for (i = 0; i < pipeCounters[p]; i++)
{
@ -468,9 +466,13 @@ Vk_DrawAliasFrameLerp(entity_t *currententity, dmdx_t *paliashdr, float backlerp
static void
Vk_DrawAliasShadow(int *order, int *order_end, int posenum,
float height, float lheight, vec4_t *s_lerped, const float *shadevector,
uint32_t *uboOffset, VkDescriptorSet *uboDescriptorSet
)
uint32_t *uboOffset, VkDescriptorSet *uboDescriptorSet, int num_tris)
{
VkBuffer fan, strip;
fan = QVk_GetTriangleFanIbo(num_tris);
strip = QVk_GetTriangleStripIbo(num_tris);
while (1)
{
int i, count;
@ -538,12 +540,12 @@ Vk_DrawAliasShadow(int *order, int *order_end, int posenum,
if (pipelineIdx == TRIANGLE_STRIP)
{
vkCmdBindIndexBuffer(vk_activeCmdbuffer, QVk_GetTriangleStripIbo((i - 2) * 3), 0, VK_INDEX_TYPE_UINT16);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, strip, 0, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (i - 2) * 3, 1, 0, 0, 0);
}
else
{
vkCmdBindIndexBuffer(vk_activeCmdbuffer, QVk_GetTriangleFanIbo((i - 2) * 3), 0, VK_INDEX_TYPE_UINT16);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, 0, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (i - 2) * 3, 1, 0, 0, 0);
}
}
@ -900,7 +902,8 @@ R_DrawAliasModel(entity_t *currententity, const model_t *currentmodel)
order + Q_min(paliashdr->num_glcmds,
mesh_nodes[i].ofs_glcmds + mesh_nodes[i].num_glcmds),
currententity->frame, height, lheight,
s_lerped, shadevector, &uboOffset, &uboDescriptorSet);
s_lerped, shadevector, &uboOffset, &uboDescriptorSet,
paliashdr->num_tris);
}
}
}

View file

@ -312,7 +312,6 @@ void Vk_Mem_f(void)
vk_config.vertex_buffer_size / 1024,
100.f * vk_config.vertex_buffer_usage / vk_config.vertex_buffer_size,
vk_config.vertex_buffer_max_usage / 1024);
R_Printf(PRINT_ALL, "Index : %u/%uB (%.1f%% max: %uB)\n",
vk_config.index_buffer_usage,
vk_config.index_buffer_size,
@ -323,9 +322,4 @@ void Vk_Mem_f(void)
vk_config.uniform_buffer_size / 1024,
100.f * vk_config.uniform_buffer_usage / vk_config.uniform_buffer_size,
vk_config.uniform_buffer_max_usage / 1024);
R_Printf(PRINT_ALL, "Triangle index: %u/%u (%.1f%% max: %u)\n",
vk_config.triangle_index_usage,
vk_config.triangle_index_count,
100.f * vk_config.triangle_index_usage / vk_config.triangle_index_count,
vk_config.triangle_index_max_usage);
}