vk: use single draw in Vk_RenderLightmappedPoly

This commit is contained in:
Denis Pauk 2024-08-09 01:16:55 +03:00
parent a60ec01de9
commit 1a02a6423b

View file

@ -400,8 +400,9 @@ DrawTextureChains(entity_t *currententity)
static void
Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha,
const entity_t *currententity)
Vk_RenderLightmappedPoly(msurface_t *surf, float alpha,
const entity_t *currententity, VkDescriptorSet *uboDescriptorSet,
uint32_t *uboOffset)
{
int i, nv = surf->polys->numverts;
int map;
@ -410,29 +411,6 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
unsigned lmtex = surf->lightmaptexturenum;
mpoly_t *p;
struct {
float model[16];
float viewLightmaps;
} lmapPolyUbo;
lmapPolyUbo.viewLightmaps = r_lightmap->value ? 1.f : 0.f;
if (modelMatrix)
{
memcpy(lmapPolyUbo.model, modelMatrix, sizeof(float) * 16);
}
else
{
Mat_Identity(lmapPolyUbo.model);
}
QVk_BindPipeline(&vk_drawPolyLmapPipeline);
uint32_t uboOffset;
VkDescriptorSet uboDescriptorSet;
uint8_t *uboData = QVk_GetUniformBuffer(sizeof(lmapPolyUbo), &uboOffset, &uboDescriptorSet);
memcpy(uboData, &lmapPolyUbo, sizeof(lmapPolyUbo));
for (map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++)
{
if (r_newrefdef.lightstyles[surf->styles[map]].white != surf->cached_light[map])
@ -457,14 +435,6 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
Com_Error(ERR_FATAL, "%s: can't allocate memory", __func__);
}
VkDescriptorSet descriptorSets[] = {
image->vk_texture.descriptorSet,
uboDescriptorSet,
vk_state.lightmap_textures[lmtex].descriptorSet
};
vkCmdBindDescriptorSets(vk_activeCmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, vk_drawPolyLmapPipeline.layout, 0, 3, descriptorSets, 1, &uboOffset);
if (is_dynamic)
{
int smax, tmax, size;
@ -484,145 +454,72 @@ Vk_RenderLightmappedPoly(msurface_t *surf, const float *modelMatrix, float alpha
R_SetCacheState(surf, &r_newrefdef);
lmtex = surf->lightmaptexturenum;
QVk_UpdateTextureData(&vk_state.lightmap_textures[surf->lightmaptexturenum],
(byte*)temp, surf->light_s, surf->light_t, smax, tmax);
}
else
{
lmtex = surf->lightmaptexturenum + DYNLIGHTMAP_OFFSET;
QVk_UpdateTextureData(&vk_state.lightmap_textures[lmtex],
(byte*)temp, surf->light_s, surf->light_t, smax, tmax);
}
c_brush_polys++;
//==========
//PGM
if (surf->texinfo->flags & SURF_SCROLL)
{
float sscroll, tscroll;
R_FlowingScroll(&r_newrefdef, surf->texinfo->flags, &sscroll, &tscroll);
VkBuffer vbo;
VkDeviceSize vboOffset;
for (p = surf->polys; p; p = p->chain)
{
VkBuffer *buffer;
VkDeviceSize dstOffset;
memcpy(verts_buffer, p->verts, sizeof(mvtx_t) * nv);
for (i = 0; i < nv; i++)
{
verts_buffer[i].texCoord[0] += sscroll;
verts_buffer[i].texCoord[1] += tscroll;
}
uint8_t *vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, verts_buffer, sizeof(mvtx_t) * nv);
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);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
}
}
else
{
VkDeviceSize vboOffset;
VkBuffer vbo;
for (p = surf->polys; p; p = p->chain)
{
VkDeviceSize dstOffset;
uint8_t *vertData;
VkBuffer *buffer;
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, p->verts, sizeof(mvtx_t) * nv);
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);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
}
}
//PGM
//==========
QVk_UpdateTextureData(&vk_state.lightmap_textures[lmtex],
(byte*)temp, surf->light_s, surf->light_t, smax, tmax);
}
else
VkDeviceSize vboOffset, dstOffset;
VkBuffer vbo, *buffer;
uint8_t *vertData;
float sscroll = 0, tscroll = 0;
int pos_vect = 0, index_pos = 0;
c_brush_polys++;
//==========
//PGM
if (surf->texinfo->flags & SURF_SCROLL)
{
c_brush_polys++;
//==========
//PGM
if (surf->texinfo->flags & SURF_SCROLL)
{
float sscroll, tscroll;
R_FlowingScroll(&r_newrefdef, surf->texinfo->flags, &sscroll, &tscroll);
for (p = surf->polys; p; p = p->chain)
{
VkDeviceSize vboOffset, dstOffset;
VkBuffer vbo, *buffer;
uint8_t *vertData;
memcpy(verts_buffer, p->verts, sizeof(mvtx_t) * nv);
for (i = 0; i < nv; i++)
{
verts_buffer[i].texCoord[0] += sscroll;
verts_buffer[i].texCoord[1] += tscroll;
}
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, verts_buffer, sizeof(mvtx_t) * nv);
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);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
}
}
else
{
//PGM
//==========
for (p = surf->polys; p; p = p->chain)
{
VkDeviceSize vboOffset, dstOffset;
VkBuffer vbo, *buffer;
uint8_t *vertData;
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * nv, &vbo, &vboOffset);
memcpy(vertData, p->verts, sizeof(mvtx_t) * nv);
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);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, (nv - 2) * 3, 1, 0, 0, 0);
}
//==========
//PGM
}
//PGM
//==========
R_FlowingScroll(&r_newrefdef, surf->texinfo->flags, &sscroll, &tscroll);
}
for (p = surf->polys; p; p = p->chain)
{
if (Mesh_VertsRealloc(pos_vect + nv))
{
Com_Error(ERR_FATAL, "%s: can't allocate memory", __func__);
}
memcpy(verts_buffer + pos_vect, p->verts, sizeof(mvtx_t) * nv);
for (i = 0; i < nv; i++)
{
verts_buffer[pos_vect + i].texCoord[0] += sscroll;
verts_buffer[pos_vect + i].texCoord[1] += tscroll;
}
GenFanIndexes(vertIdxData + index_pos,
pos_vect, nv - 2 + pos_vect);
pos_vect += nv;
index_pos += (nv - 2) * 3;
}
QVk_BindPipeline(&vk_drawPolyLmapPipeline);
VkDescriptorSet descriptorSets[] = {
image->vk_texture.descriptorSet,
*uboDescriptorSet,
vk_state.lightmap_textures[lmtex].descriptorSet
};
vkCmdBindDescriptorSets(vk_activeCmdbuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
vk_drawPolyLmapPipeline.layout, 0, 3, descriptorSets, 1, uboOffset);
vertData = QVk_GetVertexBuffer(sizeof(mvtx_t) * pos_vect, &vbo, &vboOffset);
memcpy(vertData, verts_buffer, sizeof(mvtx_t) * pos_vect);
buffer = UpdateIndexBuffer(vertIdxData, index_pos * sizeof(uint16_t), &dstOffset);
vkCmdBindVertexBuffers(vk_activeCmdbuffer, 0, 1, &vbo, &vboOffset);
vkCmdBindIndexBuffer(vk_activeCmdbuffer, *buffer, dstOffset, VK_INDEX_TYPE_UINT16);
vkCmdDrawIndexed(vk_activeCmdbuffer, index_pos, 1, 0, 0, 0);
//PGM
//==========
}
static void
@ -646,6 +543,27 @@ R_DrawInlineBModel(entity_t *currententity, const model_t *currentmodel, float *
alpha = .25f;
}
struct {
float model[16];
float viewLightmaps;
} lmapPolyUbo;
lmapPolyUbo.viewLightmaps = r_lightmap->value ? 1.f : 0.f;
if (modelMatrix)
{
memcpy(lmapPolyUbo.model, modelMatrix, sizeof(float) * 16);
}
else
{
Mat_Identity(lmapPolyUbo.model);
}
uint32_t uboOffset;
VkDescriptorSet uboDescriptorSet;
uint8_t *uboData = QVk_GetUniformBuffer(sizeof(lmapPolyUbo), &uboOffset, &uboDescriptorSet);
memcpy(uboData, &lmapPolyUbo, sizeof(lmapPolyUbo));
/* draw texture */
for (i = 0; i < currentmodel->nummodelsurfaces; i++, psurf++)
{
@ -669,7 +587,8 @@ R_DrawInlineBModel(entity_t *currententity, const model_t *currentmodel, float *
}
else if (!(psurf->flags & SURF_DRAWTURB) && !r_showtris->value)
{
Vk_RenderLightmappedPoly(psurf, modelMatrix, alpha, currententity);
Vk_RenderLightmappedPoly(psurf, alpha, currententity,
&uboDescriptorSet, &uboOffset);
}
else
{
@ -834,6 +753,19 @@ R_RecursiveWorldNode(entity_t *currententity, mnode_t *node)
return;
}
struct {
float model[16];
float viewLightmaps;
} lmapPolyUbo;
lmapPolyUbo.viewLightmaps = r_lightmap->value ? 1.f : 0.f;
Mat_Identity(lmapPolyUbo.model);
uint32_t uboOffset;
VkDescriptorSet uboDescriptorSet;
uint8_t *uboData = QVk_GetUniformBuffer(sizeof(lmapPolyUbo), &uboOffset, &uboDescriptorSet);
memcpy(uboData, &lmapPolyUbo, sizeof(lmapPolyUbo));
/* draw stuff */
for (c = node->numsurfaces,
surf = r_worldmodel->surfaces + node->firstsurface;
@ -864,7 +796,8 @@ R_RecursiveWorldNode(entity_t *currententity, mnode_t *node)
{
if (!(surf->flags & SURF_DRAWTURB) && !r_showtris->value)
{
Vk_RenderLightmappedPoly(surf, NULL, 1.f, currententity);
Vk_RenderLightmappedPoly(surf, 1.f, currententity,
&uboDescriptorSet, &uboOffset);
}
else
{