diff --git a/.gitignore b/.gitignore index 51c5133..95be30d 100644 --- a/.gitignore +++ b/.gitignore @@ -346,3 +346,5 @@ ASALocalRun/ # BeatPulse healthcheck temp database healthchecksdb .DS_Store + +**/generated/** \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e21c2c..e78193a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -140,9 +140,6 @@ if(WIN32) COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $) endif() -add_custom_command(TARGET Engine POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/res $) - install( TARGETS AssetViewer diff --git a/gdb/Seele_lldb.py b/gdb/Seele_lldb.py index 3673c93..cbc50d6 100644 --- a/gdb/Seele_lldb.py +++ b/gdb/Seele_lldb.py @@ -64,6 +64,130 @@ class ListSyntheticProvider: def has_children(self): return True +def Tree_SummaryProvider(valobj, internal_dict): + size = valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0) + return f"Tree(size={size})" + +class TreeSyntheticProvider: + def __init__(self, valobj, internal_dict): + self.valobj = valobj + self.update() + + def num_children(self): + return self.size + + def get_child_index(self, name): + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self, index): + if index < 0 or index >= self.size: + return None + # Collect all nodes via in-order traversal, then return the one at `index` + nodes = [] + self._collect(self.root, nodes) + if index >= len(nodes): + return None + return nodes[index].GetChildMemberWithName('data') + + def _collect(self, node, out): + if not node.IsValid(): + return + self._collect(node.GetChildMemberWithName('leftChild'), out) + out.append(node) + self._collect(node.GetChildMemberWithName('rightChild'), out) + + def update(self): + self.root = self.valobj.GetChildMemberWithName('root') + self.size = self.valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0) + + def has_children(self): + return True + +def Set_SummaryProvider(valobj, internal_dict): + size = valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0) + return f"Set(size={size})" + +class SetSyntheticProvider: + def __init__(self, valobj, internal_dict): + self.valobj = valobj + self.update() + + def num_children(self): + return self.size + + def get_child_index(self, name): + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self, index): + if index < 0 or index >= self.size: + return None + nodes = [] + self._collect(self.root, nodes) + if index >= len(nodes): + return None + return nodes[index].GetChildMemberWithName('data') + + def _collect(self, node, out): + if not node.IsValid(): + return + self._collect(node.GetChildMemberWithName('leftChild'), out) + out.append(node) + self._collect(node.GetChildMemberWithName('rightChild'), out) + + def update(self): + self.root = self.valobj.GetChildMemberWithName('root') + self.size = self.valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0) + + def has_children(self): + return True + +def Map_SummaryProvider(valobj, internal_dict): + size = valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0) + return f"Map(size={size})" + +class MapSyntheticProvider: + def __init__(self, valobj, internal_dict): + self.valobj = valobj + self.update() + + def num_children(self): + return self.size + + def get_child_index(self, name): + try: + return int(name.lstrip('[').rstrip(']')) + except: + return -1 + + def get_child_at_index(self, index): + if index < 0 or index >= self.size: + return None + nodes = [] + self._collect(self.root, nodes) + if index >= len(nodes): + return None + return nodes[index].GetChildMemberWithName('data') + + def _collect(self, node, out): + if not node.IsValid(): + return + self._collect(node.GetChildMemberWithName('leftChild'), out) + out.append(node) + self._collect(node.GetChildMemberWithName('rightChild'), out) + + def update(self): + self.root = self.valobj.GetChildMemberWithName('root') + self.size = self.valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0) + + def has_children(self): + return True + def Vector_SummaryProvider(valobj, internal_dict): # Get the template parameters to determine vector length type_name = valobj.GetTypeName() @@ -93,6 +217,14 @@ def __lldb_init_module(debugger, internal_dict): debugger.HandleCommand('type summary add -F Seele_lldb.List_SummaryProvider -x "^Seele::List<.*>$"') debugger.HandleCommand('type synthetic add -l Seele_lldb.ListSyntheticProvider -x "^Seele::List<.*>$"') + # Register Set formatter + debugger.HandleCommand('type summary add -F Seele_lldb.Set_SummaryProvider -x "^Seele::Set<.*>$"') + debugger.HandleCommand('type synthetic add -l Seele_lldb.SetSyntheticProvider -x "^Seele::Set<.*>$"') + + # Register Map formatter + debugger.HandleCommand('type summary add -F Seele_lldb.Map_SummaryProvider -x "^Seele::Map<.*>$"') + debugger.HandleCommand('type synthetic add -l Seele_lldb.MapSyntheticProvider -x "^Seele::Map<.*>$"') + # Register Vector formatter debugger.HandleCommand('type summary add -F Seele_lldb.Vector_SummaryProvider -x "^glm::(detail::)?t?vec[234]?<.*>$"') diff --git a/res/fonts/fonts b/res/fonts/fonts new file mode 120000 index 0000000..5e449fc --- /dev/null +++ b/res/fonts/fonts @@ -0,0 +1 @@ +fonts/ \ No newline at end of file diff --git a/res/shaders/TextPass.slang b/res/shaders/TextPass.slang deleted file mode 100644 index cd2642f..0000000 --- a/res/shaders/TextPass.slang +++ /dev/null @@ -1,65 +0,0 @@ -import Common; - -struct VertexInput -{ - uint vertexId : SV_VertexID; - uint instanceId : SV_InstanceID; -}; - -struct VertexOutput -{ - float4 position : SV_Position; - float2 texCoords : TEXCOORD; - uint glyphIndex : GLYPHINDEX; -}; - -struct GlyphInstanceData -{ - float x; - float y; - float z; - float width; - float height; - uint glyphIndex; -}; -struct TextData -{ - StructuredBuffer glyphs; - SamplerState glyphSampler; - Texture2D glyphTextures[]; -}; -ParameterBlock pText; - - -[shader("vertex")] -VertexOutput vertexMain(VertexInput input) -{ - float xpos = pText.glyphs[input.instanceId].x; - float ypos = pText.glyphs[input.instanceId].y; - - float w = pText.glyphs[input.instanceId].width; - float h = pText.glyphs[input.instanceId].height; - - const float4 coordinates[4] = { - float4(xpos, ypos, 0, 0), - float4(xpos, ypos + h, 0, 1), - float4(xpos + w, ypos, 1, 0), - float4(xpos + w, ypos + h, 1, 1), - }; - float4 vertex = coordinates[input.vertexId]; - VertexOutput output; - output.texCoords = vertex.zw; - output.position = mul(pViewParams.projectionMatrix, float4(vertex.xy, pText.glyphs[input.instanceId].z-1, 1)); - output.glyphIndex = pText.glyphs[input.instanceId].glyphIndex; - return output; -} - -[shader("fragment")] -float4 fragmentMain( - float4 position : SV_Position, - float2 texCoords : TEXCOORD, - uint glyphIndex : GLYPHINDEX -) : SV_Target -{ - return float4(0,0,0,pText.glyphTextures[glyphIndex].Sample(pText.glyphSampler, texCoords)); -} \ No newline at end of file diff --git a/res/shaders/UIPass.slang b/res/shaders/UIPass.slang index 7b4de78..698475a 100644 --- a/res/shaders/UIPass.slang +++ b/res/shaders/UIPass.slang @@ -44,7 +44,17 @@ VertexOutput vertexMain(uint vertexId : SV_VertexID, uint instanceId: SV_Instanc float4(xMax, yMax, 1, 1) }; VertexOutput output; - output.position = mul(pViewParams.projectionMatrix, float4(coordinates[vertexId].xy, style.z-1, 1)); + // The UI is a 2D overlay in *screen* pixel space (top-left origin, y down). + // The 3D scene camera's projectionMatrix is meaningless for these coordinates, + // so build a 2D ortho screen->NDC projection from the viewport size instead: + // ndcX = (x / W) * 2 - 1 + // ndcY = 1 - (y / H) * 2 (flip y: screen is top-down, NDC is bottom-up) + // The UI must NOT be transformed by pViewParams.projectionMatrix (the 3D camera). + float2 ndc = float2( + coordinates[vertexId].x * pViewParams.invScreenDimensions.x * 2.0 - 1.0, + 1.0 - coordinates[vertexId].y * pViewParams.invScreenDimensions.y * 2.0 + ); + output.position = float4(ndc, style.z, 1.0); output.texCoords = coordinates[vertexId].zw; output.style = style; return output; @@ -59,5 +69,12 @@ float4 fragmentMain( if(style.textureIndex == -1) { return float4(style.color, style.opacity); } - return pParams.textures[style.textureIndex].Sample(pParams.sampler, texCoords.xy); -} \ No newline at end of file + // Glyph quads: the texture is R8_UNORM (coverage in the R channel, A baked to 1.0). + // Reading it as a Texture2D gives (R=coverage, G=0, B=0, A=1) — so the alpha we want + // is in .r, NOT .a (using .a would make every pixel opaque -> the "black cutout"). + // Output the pure text color + coverage as alpha; the pipeline's SRC_ALPHA blend then + // applies the coverage exactly once (do NOT premultiply here or it'd be squared). + float4 tex = pParams.textures[style.textureIndex].Sample(pParams.sampler, texCoords.xy); + float coverage = tex.r; + return float4(style.color, coverage * style.opacity); +} diff --git a/res/shaders/shaders b/res/shaders/shaders new file mode 120000 index 0000000..c3a3246 --- /dev/null +++ b/res/shaders/shaders @@ -0,0 +1 @@ +shaders/ \ No newline at end of file diff --git a/res/textures/textures b/res/textures/textures new file mode 120000 index 0000000..3ba3bba --- /dev/null +++ b/res/textures/textures @@ -0,0 +1 @@ +textures/ \ No newline at end of file diff --git a/src/Engine/Asset/FontAsset.cpp b/src/Engine/Asset/FontAsset.cpp index 5cfe8ce..783f427 100644 --- a/src/Engine/Asset/FontAsset.cpp +++ b/src/Engine/Asset/FontAsset.cpp @@ -45,11 +45,15 @@ UVector2 FontAsset::shapeText(std::string_view view, uint32 fontSize, Array::max()); Vector2 max = Vector2(std::numeric_limits::lowest()); int32 xppem, yppem; hb_font_get_scale(hb_font, &xppem, &yppem); - Vector2 cursor = Vector2(0); + // Baseline (in pixels, relative to the text block's top-left) = the font's ascender. + // FreeType gives it in 1/64-pixel units after FT_Set_Pixel_Sizes; convert to pixels. + // Screen space is y-down; the baseline sits `ascender` px *below* the block's top edge. + // (FT metrics ascender is positive-up, so the baseline screen y = +ascender in y-down.) + float ascenderPx = (float)ft_face->size->metrics.ascender / 64.0f; + Vector2 cursor = Vector2(0, ascenderPx); for (uint32 i = 0; i < num_glyphs; ++i) { hb_codepoint_t glyphid = glyphs[i].codepoint; float xOffset = (float)positions[i].x_offset * fontSize / xppem; @@ -58,17 +62,37 @@ UVector2 FontAsset::shapeText(std::string_view view, uint32 fontSize, Array=0, so max.x is the full width). + // Height = line height from the font metrics: ascender (above baseline) plus the + // descender depth (FT descender is negative-up => its magnitude is below baseline). + // This is the correct auto-size: it fits the whole em, including descenders. + float descenderPx = (float)ft_face->size->metrics.descender / 64.0f; // <= 0 (down is negative-up) + float lineHeightPx = ascenderPx - descenderPx; + if (max.x <= 0.0f) { // empty string + return UVector2(0, 0); + } + return UVector2((uint32)std::ceil(max.x), (uint32)std::ceil(lineHeightPx)); } void FontAsset::loadGlyphs(uint32 fontSize) { diff --git a/src/Engine/Graphics/RenderPass/UIPass.cpp b/src/Engine/Graphics/RenderPass/UIPass.cpp index 0608383..a58a96e 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.cpp +++ b/src/Engine/Graphics/RenderPass/UIPass.cpp @@ -9,26 +9,7 @@ using namespace Seele; UIPass::UIPass(Gfx::PGraphics graphics, UI::PSystem system) : RenderPass(graphics), system(system) { - glyphInstanceBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{.name = "GlyphInstanceBuffer"}); elementBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{.name = "RenderStyleElements"}); - textDescriptorLayout = graphics->createDescriptorLayout("pText"); - textDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ - .name = GLYPHINSTANCE_NAME, - .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, - }); - textDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ - .name = GLYPHSAMPLER_NAME, - .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER, - }); - textDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ - .name = TEXTURES_NAME, - .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, - .descriptorCount = 1024, - .access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT, - }); - textPipelineLayout = graphics->createPipelineLayout("TextPipeline"); - textPipelineLayout->addDescriptorLayout(viewParamsLayout); - textPipelineLayout->addDescriptorLayout(textDescriptorLayout); uiDescriptorLayout = graphics->createDescriptorLayout("pParams"); uiDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ @@ -48,7 +29,6 @@ UIPass::UIPass(Gfx::PGraphics graphics, UI::PSystem system) : RenderPass(graphic uiPipelineLayout = graphics->createPipelineLayout("UIPipeline"); uiPipelineLayout->addDescriptorLayout(viewParamsLayout); uiPipelineLayout->addDescriptorLayout(uiDescriptorLayout); - // todo: } UIPass::~UIPass() {} @@ -60,7 +40,6 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform system->processInput(); system->beginFrame(UVector2(viewport->getWidth(), viewport->getHeight())); renderElements = system->render(); - glyphs.clear(); usedTextures.clear(); elements.clear(); for (auto& render : renderElements) { @@ -82,20 +61,6 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform .textureIndex = textureIndex, }); } - glyphInstanceBuffer->rotateBuffer(sizeof(GlyphInstanceData) * glyphs.size()); - glyphInstanceBuffer->updateContents(0, sizeof(GlyphInstanceData) * glyphs.size(), glyphs.data()); - glyphInstanceBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, - Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT); - - textDescriptorLayout->reset(); - textDescriptorSet = textDescriptorLayout->allocateDescriptorSet(); - textDescriptorSet->updateBuffer(GLYPHINSTANCE_NAME, 0, glyphInstanceBuffer); - textDescriptorSet->updateSampler(GLYPHSAMPLER_NAME, 0, glyphSampler); - for (uint32 i = 0; i < usedTextures.size(); ++i) { - textDescriptorSet->updateTexture(TEXTURES_NAME, i, usedTextures[i]->getDefaultView()); - } - textDescriptorSet->writeChanges(); - elementBuffer->rotateBuffer(sizeof(RenderElementStyle) * elements.size()); elementBuffer->updateContents(0, sizeof(RenderElementStyle) * elements.size(), elements.data()); elementBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, @@ -103,7 +68,7 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform uiDescriptorLayout->reset(); uiDescriptorSet = uiDescriptorLayout->allocateDescriptorSet(); uiDescriptorSet->updateBuffer(ELEMENT_NAME, 0, elementBuffer); - uiDescriptorSet->updateSampler(GLYPHSAMPLER_NAME, 0, glyphSampler); + uiDescriptorSet->updateSampler(GLYPHSAMPLER_NAME, 0, uiSampler); for (uint32 i = 0; i < usedTextures.size(); ++i) { uiDescriptorSet->updateTexture(TEXTURES_NAME, i, usedTextures[i]->getDefaultView()); } @@ -113,7 +78,7 @@ void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform void UIPass::render() { graphics->beginRenderPass(renderPass); Array commands; - Gfx::ORenderCommand command = graphics->createRenderCommand("TextPassCommand"); + Gfx::ORenderCommand command = graphics->createRenderCommand("UIPassCommand"); command->setViewport(viewport); command->bindPipeline(uiPipeline); command->bindDescriptor({viewParamsSet, uiDescriptorSet}); @@ -175,52 +140,15 @@ void UIPass::createRenderPass() { Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, }, }; - renderPass = graphics->createRenderPass(std::move(layout), dependency, viewport->getRenderArea(), "TextPass"); + renderPass = graphics->createRenderPass(std::move(layout), dependency, viewport->getRenderArea(), "UIPass"); - graphics->beginShaderCompilation(ShaderCompilationInfo{ - .name = "TextVertex", - .modules = {"TextPass"}, - .entryPoints = {{"vertexMain", "TextPass"}, {"fragmentMain", "TextPass"}}, - .rootSignature = textPipelineLayout, - }); - textPipelineLayout->create(); - textVertexShader = graphics->createVertexShader({0}); - textFragmentShader = graphics->createFragmentShader({1}); - - glyphSampler = graphics->createSampler({ + uiSampler = graphics->createSampler({ .magFilter = Gfx::SE_FILTER_LINEAR, .minFilter = Gfx::SE_FILTER_LINEAR, .addressModeU = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, .addressModeV = Gfx::SE_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE, }); - textPipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{ - .topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP, - .vertexShader = textVertexShader, - .fragmentShader = textFragmentShader, - .renderPass = renderPass, - .pipelineLayout = textPipelineLayout, - .rasterizationState = - { - .cullMode = Gfx::SE_CULL_MODE_NONE, - }, - .colorBlend = - { - - .attachmentCount = 1, - .blendAttachments = {{ - .blendEnable = true, - .srcColorBlendFactor = Gfx::SE_BLEND_FACTOR_SRC_ALPHA, - .dstColorBlendFactor = Gfx::SE_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, - .srcAlphaBlendFactor = Gfx::SE_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA, - .dstAlphaBlendFactor = Gfx::SE_BLEND_FACTOR_ZERO, - .alphaBlendOp = Gfx::SE_BLEND_OP_ADD, - .colorWriteMask = Gfx::SE_COLOR_COMPONENT_R_BIT | Gfx::SE_COLOR_COMPONENT_G_BIT | Gfx::SE_COLOR_COMPONENT_B_BIT | - Gfx::SE_COLOR_COMPONENT_A_BIT, - }}, - }, - }); - graphics->beginShaderCompilation(ShaderCompilationInfo{ .name = "UIVertex", .modules = {"UIPass"}, diff --git a/src/Engine/Graphics/RenderPass/UIPass.h b/src/Engine/Graphics/RenderPass/UIPass.h index b914329..3fe3c1f 100644 --- a/src/Engine/Graphics/RenderPass/UIPass.h +++ b/src/Engine/Graphics/RenderPass/UIPass.h @@ -1,7 +1,6 @@ #pragma once #include "Graphics/Shader.h" #include "RenderPass.h" -#include "Asset/FontAsset.h" #include "UI/System.h" namespace Seele { @@ -28,20 +27,6 @@ class UIPass : public RenderPass { void key(KeyCode code, InputAction action, KeyModifierFlags modifier, char text = 0) { system->key(code, action, (KeyModifier)modifier, text); } private: - struct GlyphData { - Vector2 bearing; - Vector2 size; - uint32 advance; - }; - struct GlyphInstanceData { - float x; - float y; - float z; - float width; - float height; - uint32 glyphIndex; - }; - struct RenderElementStyle { float x; float y; @@ -59,14 +44,6 @@ class UIPass : public RenderPass { Gfx::RenderTargetAttachment depthAttachment; Gfx::OTexture2D depthBuffer; - Gfx::ODescriptorLayout textDescriptorLayout; - Gfx::ODescriptorSet textDescriptorSet; - - Gfx::OVertexShader textVertexShader; - Gfx::OFragmentShader textFragmentShader; - Gfx::OPipelineLayout textPipelineLayout; - Gfx::PGraphicsPipeline textPipeline; - Gfx::ODescriptorLayout uiDescriptorLayout; Gfx::ODescriptorSet uiDescriptorSet; @@ -79,13 +56,10 @@ class UIPass : public RenderPass { UI::PSystem system; Array renderElements; - Array glyphs; Array elements; - Gfx::OShaderBuffer glyphInstanceBuffer; - constexpr static const char* GLYPHINSTANCE_NAME = "glyphData"; Gfx::OShaderBuffer elementBuffer; constexpr static const char* ELEMENT_NAME = "elements"; - Gfx::OSampler glyphSampler; + Gfx::OSampler uiSampler; constexpr static const char* GLYPHSAMPLER_NAME = "glyphSampler"; Array usedTextures; constexpr static const char* TEXTURES_NAME = "textures";