vk: get rid of QVk_GetTriangleFanIbo

This commit is contained in:
Denis Pauk 2024-08-08 00:51:23 +03:00
parent 1382b02a3c
commit a60ec01de9
3 changed files with 34 additions and 64 deletions

View file

@ -329,7 +329,6 @@ void QVk_DestroyPipeline(qvkpipeline_t *pipeline);
uint8_t* QVk_GetVertexBuffer(VkDeviceSize size, VkBuffer *dstBuffer, VkDeviceSize *dstOffset); uint8_t* QVk_GetVertexBuffer(VkDeviceSize size, VkBuffer *dstBuffer, VkDeviceSize *dstOffset);
uint8_t* QVk_GetUniformBuffer(VkDeviceSize size, uint32_t *dstOffset, VkDescriptorSet *dstUboDescriptorSet); uint8_t* QVk_GetUniformBuffer(VkDeviceSize size, uint32_t *dstOffset, VkDescriptorSet *dstUboDescriptorSet);
uint8_t* QVk_GetStagingBuffer(VkDeviceSize size, int alignment, VkCommandBuffer *cmdBuffer, VkBuffer *buffer, uint32_t *dstOffset); uint8_t* QVk_GetStagingBuffer(VkDeviceSize size, int alignment, VkCommandBuffer *cmdBuffer, VkBuffer *buffer, uint32_t *dstOffset);
VkBuffer QVk_GetTriangleFanIbo(VkDeviceSize indexCount, VkDeviceSize *dstOffset);
void GenFanIndexes(uint16_t *data, int from, int to); void GenFanIndexes(uint16_t *data, int from, int to);
void GenStripIndexes(uint16_t *data, int from, int to); void GenStripIndexes(uint16_t *data, int from, int to);
VkBuffer* UpdateIndexBuffer(uint16_t *data, VkDeviceSize bufferSize, VkDeviceSize *dstOffset); VkBuffer* UpdateIndexBuffer(uint16_t *data, VkDeviceSize bufferSize, VkDeviceSize *dstOffset);

View file

@ -236,10 +236,6 @@ static qvkstagingbuffer_t vk_stagingBuffers[NUM_DYNBUFFERS];
static int vk_activeDynBufferIdx = 0; static int vk_activeDynBufferIdx = 0;
static int vk_activeSwapBufferIdx = 0; static int vk_activeSwapBufferIdx = 0;
static int vk_dynIndex = 0; static int vk_dynIndex = 0;
/* cache tri between calls */
static VkBuffer *vk_fanBuffercache = NULL;
static VkDeviceSize vk_fanOffset = 0;
static int vk_fanBuffersize = 0;
// swap buffers used if primary dynamic buffers get full // swap buffers used if primary dynamic buffers get full
#define NUM_SWAPBUFFER_SLOTS 4 #define NUM_SWAPBUFFER_SLOTS 4
@ -2138,10 +2134,6 @@ qboolean QVk_Init(void)
CreateDynamicBuffers(); CreateDynamicBuffers();
// create staging buffers // create staging buffers
CreateStagingBuffers(); CreateStagingBuffers();
/* reset fan buffer */
vk_fanOffset = 0;
vk_fanBuffersize = 512;
vk_fanBuffercache = NULL;
CreatePipelines(); CreatePipelines();
CreateSamplers(); CreateSamplers();
@ -2178,9 +2170,6 @@ VkResult QVk_BeginFrame(const VkViewport* viewport, const VkRect2D* scissor)
vk_config.vertex_buffer_usage = 0; vk_config.vertex_buffer_usage = 0;
vk_config.index_buffer_usage = 0; vk_config.index_buffer_usage = 0;
vk_config.uniform_buffer_usage = 0; vk_config.uniform_buffer_usage = 0;
/* reset fan buffer */
vk_fanOffset = 0;
vk_fanBuffercache = NULL;
ReleaseSwapBuffers(); ReleaseSwapBuffers();
@ -2617,37 +2606,6 @@ uint8_t *QVk_GetStagingBuffer(VkDeviceSize size, int alignment, VkCommandBuffer
return data; return data;
} }
VkBuffer
QVk_GetTriangleFanIbo(VkDeviceSize indexCount, VkDeviceSize *dstOffset)
{
VkDeviceSize bufferSize;
VkBuffer *buffer;
uint16_t *data;
if (vk_fanBuffercache && (vk_fanBuffersize > indexCount))
{
*dstOffset = vk_fanOffset;
return *vk_fanBuffercache;
}
indexCount = ROUNDUP(Q_max(indexCount, vk_fanBuffersize), 4);
bufferSize = 3 * indexCount * sizeof(uint16_t);
data = malloc(bufferSize);
GenFanIndexes(data, 0, indexCount);
buffer = UpdateIndexBuffer(data, bufferSize, dstOffset);
free(data);
/* save to cache */
vk_fanOffset = *dstOffset;
vk_fanBuffersize = indexCount;
vk_fanBuffercache = buffer;
return *buffer;
}
void QVk_SubmitStagingBuffers() void QVk_SubmitStagingBuffers()
{ {
for (int i = 0; i < NUM_DYNBUFFERS; ++i) for (int i = 0; i < NUM_DYNBUFFERS; ++i)

View file

@ -103,9 +103,8 @@ DrawVkFlowingPoly(msurface_t *fa, image_t *texture, const float *color)
QVk_BindPipeline(&vk_drawPolyPipeline); QVk_BindPipeline(&vk_drawPolyPipeline);
VkDeviceSize vboOffset, dstOffset;
VkDeviceSize vboOffset, fanOffset; VkBuffer vbo, *buffer;
VkBuffer vbo, fan;
uint32_t uboOffset; uint32_t uboOffset;
VkDescriptorSet uboDescriptorSet; VkDescriptorSet uboDescriptorSet;
uint8_t *vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * p->numverts, &vbo, &vboOffset); uint8_t *vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * p->numverts, &vbo, &vboOffset);
@ -123,11 +122,13 @@ DrawVkFlowingPoly(msurface_t *fa, image_t *texture, const float *color)
vkCmdPushConstants(vk_activeCmdbuffer, vk_drawTexQuadPipeline[vk_state.current_renderpass].layout, vkCmdPushConstants(vk_activeCmdbuffer, vk_drawTexQuadPipeline[vk_state.current_renderpass].layout,
VK_SHADER_STAGE_FRAGMENT_BIT, 17 * sizeof(float), sizeof(gamma), &gamma); VK_SHADER_STAGE_FRAGMENT_BIT, 17 * sizeof(float), sizeof(gamma), &gamma);
Mesh_VertsRealloc((p->numverts - 2) * 3);
GenFanIndexes(vertIdxData, 0, p->numverts - 2);
buffer = UpdateIndexBuffer(vertIdxData, (p->numverts - 2) * 3 * sizeof(uint16_t), &dstOffset);
fan = QVk_GetTriangleFanIbo((p->numverts - 2) * 3, &fanOffset);
vkCmdBindDescriptorSets(vk_activeCmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vk_drawPolyPipeline.layout, 0, 2, descriptorSets, 1, &uboOffset); vkCmdBindDescriptorSets(vk_activeCmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vk_drawPolyPipeline.layout, 0, 2, descriptorSets, 1, &uboOffset);
vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset); vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, fanOffset, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (p->numverts - 2) * 3, 1, 0, 0, 0); vkCmdDrawIndexed(vk_activeCmdbuffer, (p->numverts - 2) * 3, 1, 0, 0, 0);
} }
@ -508,8 +509,8 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
for (p = surf->polys; p; p = p->chain) for (p = surf->polys; p; p = p->chain)
{ {
VkBuffer fan; VkBuffer *buffer;
VkDeviceSize fanOffset; VkDeviceSize dstOffset;
memcpy(verts_buffer, p->verts, sizeof(mvtx_t) * nv); memcpy(verts_buffer, p->verts, sizeof(mvtx_t) * nv);
@ -522,9 +523,12 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
uint8_t *vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset); uint8_t *vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, verts_buffer, sizeof(mvtx_t) * nv); memcpy(vertData, verts_buffer, sizeof(mvtx_t) * nv);
fan = QVk_GetTriangleFanIbo((nv - 2) * 3, &fanOffset); Mesh_VertsRealloc((nv - 2) * 3);
GenFanIndexes(vertIdxData, 0, nv - 2);
buffer = UpdateIndexBuffer(vertIdxData, (nv - 2) * 3 * sizeof(uint16_t), &dstOffset);
vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset); vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, fanOffset, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0); vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
} }
} }
@ -535,16 +539,19 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
for (p = surf->polys; p; p = p->chain) for (p = surf->polys; p; p = p->chain)
{ {
VkDeviceSize fanOffset; VkDeviceSize dstOffset;
uint8_t *vertData; uint8_t *vertData;
VkBuffer fan; VkBuffer *buffer;
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset); vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, p->verts, sizeof(mvtx_t) * nv); memcpy(vertData, p->verts, sizeof(mvtx_t) * nv);
fan = QVk_GetTriangleFanIbo((nv - 2) * 3, &fanOffset); Mesh_VertsRealloc((nv - 2) * 3);
GenFanIndexes(vertIdxData, 0, nv - 2);
buffer = UpdateIndexBuffer(vertIdxData, (nv - 2) * 3 * sizeof(uint16_t), &dstOffset);
vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset); vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, fanOffset, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0); vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
} }
} }
@ -565,8 +572,8 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
for (p = surf->polys; p; p = p->chain) for (p = surf->polys; p; p = p->chain)
{ {
VkDeviceSize vboOffset, fanOffset; VkDeviceSize vboOffset, dstOffset;
VkBuffer vbo, fan; VkBuffer vbo, *buffer;
uint8_t *vertData; uint8_t *vertData;
memcpy(verts_buffer, p->verts, sizeof(mvtx_t) * nv); memcpy(verts_buffer, p->verts, sizeof(mvtx_t) * nv);
@ -580,9 +587,12 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset); vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, verts_buffer, sizeof(mvtx_t) * nv); memcpy(vertData, verts_buffer, sizeof(mvtx_t) * nv);
fan = QVk_GetTriangleFanIbo((nv - 2) * 3, &fanOffset); Mesh_VertsRealloc((nv - 2) * 3);
GenFanIndexes(vertIdxData, 0, nv - 2);
buffer = UpdateIndexBuffer(vertIdxData, (nv - 2) * 3 * sizeof(uint16_t), &dstOffset);
vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset); vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, fanOffset, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0); vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
} }
} }
@ -592,16 +602,19 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
//========== //==========
for (p = surf->polys; p; p = p->chain) for (p = surf->polys; p; p = p->chain)
{ {
VkDeviceSize vboOffset, fanOffset; VkDeviceSize vboOffset, dstOffset;
VkBuffer vbo, fan; VkBuffer vbo, *buffer;
uint8_t *vertData; uint8_t *vertData;
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset); vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, p->verts, sizeof(mvtx_t) * nv); memcpy(vertData, p->verts, sizeof(mvtx_t) * nv);
fan = QVk_GetTriangleFanIbo((nv - 2) * 3, &fanOffset); Mesh_VertsRealloc((nv - 2) * 3);
GenFanIndexes(vertIdxData, 0, nv - 2);
buffer = UpdateIndexBuffer(vertIdxData, (nv - 2) * 3 * sizeof(uint16_t), &dstOffset);
vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset); vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, fan, fanOffset, VK_INDEX_TYPE_UINT16); vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0); vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
} }
//========== //==========