@@ -104,7 +104,7 @@ struct TextOverlay
104
104
stb_font_consolas_24_latin1 (stbFontData, font24pixels, fontHeight);
105
105
106
106
// 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 ;
108
108
for (auto & buffer : vertexBuffers) {
109
109
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));
110
110
// These buffers are frequently written to, so we map them persistent
@@ -246,7 +246,7 @@ struct TextOverlay
246
246
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
247
247
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
248
248
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);
250
250
VkPipelineRasterizationStateCreateInfo rasterizationState = vks::initializers::pipelineRasterizationStateCreateInfo (VK_POLYGON_MODE_FILL, VK_CULL_MODE_BACK_BIT, VK_FRONT_FACE_CLOCKWISE, 0 );
251
251
VkPipelineColorBlendStateCreateInfo colorBlendState = vks::initializers::pipelineColorBlendStateCreateInfo (1 , &blendAttachmentState);
252
252
VkPipelineDepthStencilStateCreateInfo depthStencilState = vks::initializers::pipelineDepthStencilStateCreateInfo (VK_FALSE, VK_FALSE, VK_COMPARE_OP_LESS_OR_EQUAL);
@@ -285,7 +285,7 @@ struct TextOverlay
285
285
}
286
286
287
287
// 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)
289
289
{
290
290
const uint32_t firstChar = STB_FONT_consolas_24_latin1_FIRST_CHAR;
291
291
@@ -318,33 +318,35 @@ struct TextOverlay
318
318
}
319
319
320
320
// Generate a uv mapped quad per char in the new text
321
- for (auto letter : text)
321
+ for (auto letter : text)
322
322
{
323
323
stb_fontchar *charData = &stbFontData[(uint32_t )letter - firstChar];
324
324
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 ];
348
350
349
351
x += charData->advance * charW;
350
352
@@ -353,18 +355,14 @@ struct TextOverlay
353
355
}
354
356
355
357
// Issue the draw commands for the characters of the overlay
356
- void draw (VkCommandBuffer cmdBuffer)
358
+ void draw (VkCommandBuffer cmdBuffer)
357
359
{
358
360
vkCmdBindPipeline (cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);
359
361
vkCmdBindDescriptorSets (cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0 , 1 , &descriptorSet, 0 , NULL );
360
362
VkDeviceSize offsets = 0 ;
361
363
vkCmdBindVertexBuffers (cmdBuffer, 0 , 1 , &vertexBuffers[currentBuffer].buffer , &offsets);
362
364
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 );
368
366
}
369
367
};
370
368
0 commit comments