Skip to content

Commit 162c57d

Browse files
Merge pull request #1231 from GDBobby/patch-1
Reducing draw calls in textoverlay example
2 parents 11c3875 + a84db7a commit 162c57d

File tree

1 file changed

+31
-33
lines changed

1 file changed

+31
-33
lines changed

examples/textoverlay/textoverlay.cpp

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct TextOverlay
104104
stb_font_consolas_24_latin1(stbFontData, font24pixels, fontHeight);
105105

106106
// Vertex buffers containing the text information per max. concurrent frames
107-
VkDeviceSize bufferSize = TEXTOVERLAY_MAX_CHAR_COUNT * sizeof(glm::vec4);
107+
VkDeviceSize bufferSize = TEXTOVERLAY_MAX_CHAR_COUNT * sizeof(glm::vec4) * 6;
108108
for (auto& buffer : vertexBuffers) {
109109
VK_CHECK_RESULT(vulkanDevice->createBuffer(VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &buffer, bufferSize));
110110
// These buffers are frequently written to, so we map them persistent
@@ -246,7 +246,7 @@ struct TextOverlay
246246
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
247247
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
248248

249-
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, 0, VK_FALSE);
249+
VkPipelineInputAssemblyStateCreateInfo inputAssemblyState = vks::initializers::pipelineInputAssemblyStateCreateInfo(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, 0, VK_FALSE);
250250
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo(VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0);
251251
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo(1, &blendAttachmentState);
252252
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo(VK_FALSE, VK_FALSE, VK_COMPARE_OP_LESS_OR_EQUAL);
@@ -285,7 +285,7 @@ struct TextOverlay
285285
}
286286

287287
// Add text to the current buffer
288-
void addText(std::string text, float x, float y, TextAlign align)
288+
void addText(std::string text, float x, float y, TextAlign align)
289289
{
290290
const uint32_t firstChar = STB_FONT_consolas_24_latin1_FIRST_CHAR;
291291

@@ -318,33 +318,35 @@ struct TextOverlay
318318
}
319319

320320
// Generate a uv mapped quad per char in the new text
321-
for (auto letter : text)
321+
for (auto letter : text)
322322
{
323323
stb_fontchar *charData = &stbFontData[(uint32_t)letter - firstChar];
324324

325-
mapped->x = (x + (float)charData->x0 * charW);
326-
mapped->y = (y + (float)charData->y0 * charH);
327-
mapped->z = charData->s0;
328-
mapped->w = charData->t0;
329-
mapped++;
330-
331-
mapped->x = (x + (float)charData->x1 * charW);
332-
mapped->y = (y + (float)charData->y0 * charH);
333-
mapped->z = charData->s1;
334-
mapped->w = charData->t0;
335-
mapped++;
336-
337-
mapped->x = (x + (float)charData->x0 * charW);
338-
mapped->y = (y + (float)charData->y1 * charH);
339-
mapped->z = charData->s0;
340-
mapped->w = charData->t1;
341-
mapped++;
342-
343-
mapped->x = (x + (float)charData->x1 * charW);
344-
mapped->y = (y + (float)charData->y1 * charH);
345-
mapped->z = charData->s1;
346-
mapped->w = charData->t1;
347-
mapped++;
325+
const uint32_t letterOffset = numLetters * 6;
326+
327+
mapped[letterOffset + 0].x = (x + (float)charData->x0 * charW);
328+
mapped[letterOffset + 0].y = (y + (float)charData->y0 * charH);
329+
mapped[letterOffset + 0].z = charData->s0;
330+
mapped[letterOffset + 0].w = charData->t0;
331+
332+
mapped[letterOffset + 1].x = (x + (float)charData->x1 * charW);
333+
mapped[letterOffset + 1].y = (y + (float)charData->y0 * charH);
334+
mapped[letterOffset + 1].z = charData->s1;
335+
mapped[letterOffset + 1].w = charData->t0;
336+
337+
mapped[letterOffset + 2].x = (x + (float)charData->x0 * charW);
338+
mapped[letterOffset + 2].y = (y + (float)charData->y1 * charH);
339+
mapped[letterOffset + 2].z = charData->s0;
340+
mapped[letterOffset + 2].w = charData->t1;
341+
342+
mapped[letterOffset + 3] = mapped[letterOffset + 1];
343+
344+
mapped[letterOffset + 4].x = (x + (float)charData->x1 * charW);
345+
mapped[letterOffset + 4].y = (y + (float)charData->y1 * charH);
346+
mapped[letterOffset + 4].z = charData->s1;
347+
mapped[letterOffset + 4].w = charData->t1;
348+
349+
mapped[letterOffset + 5] = mapped[letterOffset + 2];
348350

349351
x += charData->advance * charW;
350352

@@ -353,18 +355,14 @@ struct TextOverlay
353355
}
354356

355357
// Issue the draw commands for the characters of the overlay
356-
void draw(VkCommandBuffer cmdBuffer)
358+
void draw(VkCommandBuffer cmdBuffer)
357359
{
358360
vkCmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
359361
vkCmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSet, 0, NULL);
360362
VkDeviceSize offsets = 0;
361363
vkCmdBindVertexBuffers(cmdBuffer, 0, 1, &vertexBuffers[currentBuffer].buffer, &offsets);
362364
vkCmdBindVertexBuffers(cmdBuffer, 1, 1, &vertexBuffers[currentBuffer].buffer, &offsets);
363-
// One draw command for every character. This is okay for a debug overlay, but not optimal
364-
// In a real-world application one would try to batch draw commands
365-
for (uint32_t j = 0; j < numLetters; j++) {
366-
vkCmdDraw(cmdBuffer, 4, 1, j * 4, 0);
367-
}
365+
vkCmdDraw(cmdBuffer, 6 * numLetters, 1, 0, 0);
368366
}
369367
};
370368

0 commit comments

Comments
 (0)