diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-05 19:09:10 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-02-05 19:09:10 -0500 |
| commit | a9fb380f9d0b53cde554b265d9c6b66d26183c38 (patch) | |
| tree | 8ea3648c497d4a3ac8ce2f6dafa2beca324e7249 /src/font.cpp | |
| parent | 61e179f9580c985cb5ca80ea732fc7572d31c489 (diff) | |
pke: mtsdf displays, bypass fg/bg not working
Diffstat (limited to 'src/font.cpp')
| -rw-r--r-- | src/font.cpp | 72 |
1 files changed, 58 insertions, 14 deletions
diff --git a/src/font.cpp b/src/font.cpp index 6292039..acc80e8 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -27,11 +27,12 @@ struct FontTypeData { struct FontInstanceBufferItem { glm::vec4 bg_color; glm::vec4 fg_color; - glm::ivec4 bounds; + glm::vec2 sprite_region_min; + glm::vec2 sprite_region_max; float width; }; -const VkDeviceSize instance_buffer_item_size = (sizeof(glm::vec4) * 3) + sizeof(float); +const VkDeviceSize instance_buffer_item_size = (sizeof(glm::vec4) * 2) + (sizeof(glm::vec2) * 2) + sizeof(float); // BucketContainer<FontType, TextRenderHandle, 2> bktFont; @@ -157,7 +158,7 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle pk_arr arr_inst_mem_reqs(arr_vert_mem_reqs); pk_arr arr_texture_mem_reqs(arr_vert_mem_reqs); VkDeviceSize index; - constexpr VkDeviceSize expectedBufferCount = 3; + constexpr VkDeviceSize expectedBufferCount = 4; constexpr VkDeviceSize startingGlyphCount = 4; assert(fontTextureHandle != AssetHandle_MAX); assert(glyphsHandle != AssetHandle_MAX); @@ -193,6 +194,8 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle assert(txtr_bytes != nullptr); assert(txtr_chan == 4); uint32_t imageSizeBytes = txtr_x * txtr_y * txtr_chan; + ft->atlas_size.x = (float)txtr_x; + ft->atlas_size.y = (float)txtr_y; /* * vulkan setup @@ -213,13 +216,19 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle { for (uint64_t idx = 0; idx < expectedBufferCount; ++idx) { if (idx == 0) { + // vertex bufferCI.size = sizeof(glm::vec2) * 4; } else if (idx == 1) { + // uv bufferCI.size = sizeof(glm::vec2) * 4; } else if (idx == 2) { + // atlas size + bufferCI.size = sizeof(glm::vec2) * 4; + } else if (idx == 3) { + // index bufferCI.size = sizeof(uint16_t) * 6; } - if (idx == 2) { + if (idx == 3) { bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT; } else { bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; @@ -236,7 +245,8 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle bufferCI.size = 0; bufferCI.size += sizeof(glm::vec4); // fgColor bufferCI.size += sizeof(glm::vec4); // bgColor - bufferCI.size += sizeof(glm::vec4); // sprite_region + bufferCI.size += sizeof(glm::vec2); // sprite_region_min + bufferCI.size += sizeof(glm::vec2); // sprite_region_max bufferCI.size += sizeof(float); // width bufferCI.size *= startingGlyphCount; bufferCI.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; @@ -349,6 +359,20 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle runningOffset += sizeUV; index += 1; + // atlas_size + uint32_t offsetAtlasSize = runningOffset; + uint32_t sizeAtlasSize = sizeof(ft->atlas_size) * 4; + ft->bindings.atlasSizeBD.firstBinding = index; + ft->bindings.atlasSizeBD.bindingCount = 1; + alignmentPadding = sizeAtlasSize % combined_vert_mem_reqs.alignment; + alignmentPadding = alignmentPadding == 0 ? 0 : combined_vert_mem_reqs.alignment - alignmentPadding; + sizeAtlasSize += alignmentPadding; + bufferCI.size = sizeAtlasSize; + vkCreateBuffer(vkDevice, &bufferCI, vkAllocator, &ft->bindings.atlasSizeBD.buffer); + vkBindBufferMemory(vkDevice, ft->bindings.atlasSizeBD.buffer, ft->deviceMemoryVert, offsetAtlasSize); + runningOffset += sizeAtlasSize; + index += 1; + // 2023-09-27 - JCB // I don't know where else to put this ft->bindings.instanceBD.firstBinding = index; @@ -373,7 +397,6 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle assert(runningOffset == combined_vert_mem_reqs.size); - runningOffset = 0; char *dstPtr = nullptr; @@ -389,6 +412,15 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle memcpy(dstPtr, srcPtr, sizeUV); runningOffset += sizeUV; + glm::vec2 atlas_size_buffer[4]; + for (int i = 0; i < 4; ++i) { + atlas_size_buffer[i] = ft->atlas_size; + } + dstPtr = static_cast<char *>(tmpBufferDetails.deviceData) + runningOffset; + srcPtr = reinterpret_cast<char *>(atlas_size_buffer); + memcpy(dstPtr, srcPtr, sizeAtlasSize); + runningOffset += sizeAtlasSize; + dstPtr = static_cast<char *>(tmpBufferDetails.deviceData) + runningOffset; srcPtr = reinterpret_cast<char *>(pkeIntrinsicsPlane.index); memcpy(dstPtr, srcPtr, sizeIndex); @@ -416,6 +448,11 @@ FontTypeIndex FontType_RegisterFont(pk_cstr title, AssetHandle fontTextureHandle vkCmdCopyBuffer(tmpBufferDetails.cmdBuffer, tmpBufferDetails.buffer, ft->bindings.uvBD.buffer, 1, &bufferCopys[index]); index+=1; + bufferCopys[index].srcOffset = offsetAtlasSize; + bufferCopys[index].size = sizeAtlasSize; + vkCmdCopyBuffer(tmpBufferDetails.cmdBuffer, tmpBufferDetails.buffer, ft->bindings.atlasSizeBD.buffer, 1, &bufferCopys[index]); + index+=1; + bufferCopys[index].srcOffset = offsetIndex; bufferCopys[index].size = sizeIndex; vkCmdCopyBuffer(tmpBufferDetails.cmdBuffer, tmpBufferDetails.buffer, ft->bindings.indexBD.buffer, 1, &bufferCopys[index]); @@ -693,6 +730,13 @@ void FontType_Unload(FontTypeIndex idx) { ft->bindings.uvBD.bindingCount = 0; ft->bindings.uvBD.offsets[0] = 0; + if (ft->bindings.atlasSizeBD.buffer != VK_NULL_HANDLE) + vkDestroyBuffer(vkDevice, ft->bindings.atlasSizeBD.buffer, vkAllocator); + ft->bindings.atlasSizeBD.buffer = VK_NULL_HANDLE; + ft->bindings.atlasSizeBD.firstBinding = 0; + ft->bindings.atlasSizeBD.bindingCount = 0; + ft->bindings.atlasSizeBD.offsets[0] = 0; + if (ft->bindings.indexBD.buffer != VK_NULL_HANDLE) vkDestroyBuffer(vkDevice, ft->bindings.indexBD.buffer, vkAllocator); ft->bindings.indexBD.buffer = VK_NULL_HANDLE; @@ -706,7 +750,6 @@ void FontType_Unload(FontTypeIndex idx) { ft->bindings.instanceBD.firstBinding = 0; ft->bindings.instanceBD.bindingCount = 0; ft->bindings.instanceCounter = 0; - ft->bindings.instanceBufferMaxCount = 0; ft->bindings.instanceBD.offsets[0] = 0; if (ft->textureImageView != VK_NULL_HANDLE) @@ -797,14 +840,15 @@ FontRender FontType_AddStringRender(FontTypeIndex idx_ft, pk_cstr cstr) { } buf_item = &instance_buffer_items.Push(); - buf_item->fg_color = glm::vec4(0.4, 0.5, 0.4, 0); - buf_item->bg_color = glm::vec4(1.0, 1.0, 1.0, 0); - buf_item->bounds = fgc->bounds; - buf_item->width = 10; + buf_item->fg_color = glm::vec4(0.4, 0.5, 0.4, 1); + buf_item->bg_color = glm::vec4(0.0, 0.0, 0.0, 1); + buf_item->sprite_region_min = fgc->sprite_region_min; + buf_item->sprite_region_max = fgc->sprite_region_max; + buf_item->width = 2; } } - VkDeviceSize item_count_orig = ft->bindings.instanceBufferMaxCount; - VkDeviceSize item_count_new = ft->bindings.instanceBufferMaxCount + count; + VkDeviceSize item_count_orig = ft->bindings.instanceCounter; + VkDeviceSize item_count_new = ft->bindings.instanceCounter + count; // copy existing buffer to new buffer VkDeviceSize byteCount = instance_buffer_item_size * item_count_new; @@ -887,7 +931,7 @@ FontRender FontType_AddStringRender(FontTypeIndex idx_ft, pk_cstr cstr) { ft->deviceMemoryInst = new_memory; ft->bindings.instanceBD.buffer = newBuffer; - ft->bindings.instanceBufferMaxCount = item_count_new; + ft->bindings.instanceCounter = item_count_new; } // create tmp local buffer & copy data to graphics card |
