new UI system

This commit is contained in:
Jorijn van der Graaf 2026-05-01 23:35:37 +02:00
commit 216972e73a
82 changed files with 4837 additions and 3243 deletions

BIN
examples/VulkanUI/Inter.ttf Normal file

Binary file not shown.

View file

@ -1,133 +1,85 @@
#include "vulkan/vulkan.h"
#include <cassert>
import Crafter.Graphics;
using namespace Crafter;
import std;
import Crafter.Event;
import Crafter.Math;
import std;
using namespace Crafter;
int main() {
Device::Initialize();
Window window(1280, 720, "HelloVulkan");
VkCommandBuffer cmd = window.StartInit();
DescriptorHeapVulkan descriptorHeap;
descriptorHeap.Initialize(1,2,0);
VkSpecializationMapEntry entry = {
.constantID = 0,
.offset = 0,
.size = sizeof(uint16_t)
};
VkSpecializationInfo specilizationInfo = {
.mapEntryCount = 1,
.pMapEntries = &entry,
.dataSize = sizeof(uint16_t),
.pData = &descriptorHeap.bufferStartElement
};
std::array<VulkanShader, 1> shaders{{
{"raygen.spv", "main", VK_SHADER_STAGE_RAYGEN_BIT_KHR, &specilizationInfo}
}};
ShaderBindingTableVulkan shaderTable;
shaderTable.Init(shaders);
std::array<VkRayTracingShaderGroupCreateInfoKHR, 1> raygenGroups {{
{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = 0,
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
},
}};
std::array<VkRayTracingShaderGroupCreateInfoKHR, 0> missGroups;
std::array<VkRayTracingShaderGroupCreateInfoKHR, 0> hitGroups;
PipelineRTVulkan pipeline;
pipeline.Init(cmd, raygenGroups, missGroups, hitGroups, shaderTable);
Window window(1280, 720, "VulkanUI");
window.StartInit();
window.FinishInit();
RenderingElement2DVulkan<true, true> element(
{
0.5, //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor
0.5, //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor
0.5, //relativeSizeX: the relative x size this element should be scaled to compared to its parent
0.5, //relativeSizeY: the relative y size this element should be scaled to compared to its parent
0.5, //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle)
0.5, //anchorOffsetY: the amount this element's anchor should be offset from the top left corner (0.5 to place it in the middle)
0 //z: this elements Z position
},
2,
1
Font font("Inter.ttf");
UI::Theme theme = UI::themes::default_dark();
theme.defaultFont = &font;
// ─────────────────────────────────────────────────────────────────────
// Wire the scene: it auto-creates a descriptor heap, plugs into the
// window's pass list, hooks mouse + update events, and drives a
// compute-shader UI pass per frame.
// ─────────────────────────────────────────────────────────────────────
UI::UIScene scene;
scene.Initialize(window, "ui.comp.spv");
scene.background(UI::Color{0.06f, 0.07f, 0.10f, 1.0f});
scene.Root(
UI::VStack{}
.padding(20)
.spacing(12)
.children(
UI::Text{"Crafter UI — V1"}.font(font).size(28),
UI::Text{}.font(font).size(14).runs(
UI::TextRun{"Click "},
UI::TextRun{"Quit"}.color(UI::Color{1.0f, 0.55f, 0.55f}).bold(),
UI::TextRun{" to close the window. Tabs switch on click. "},
UI::TextRun{"Have fun!"}.color(UI::Color{0.55f, 0.85f, 1.0f})
),
UI::HStack{}
.width(UI::Length::Frac(1))
.spacing(8)
.children(
UI::Button{"Play"} .font(font).style(theme.primary) .onClick([]{ std::println(std::cerr, "[click] Play"); }),
UI::Button{"Options"}.font(font).style(theme.secondary).onClick([]{ std::println(std::cerr, "[click] Options"); }),
UI::Spacer{},
UI::Button{"Quit"} .font(font).style(theme.danger) .onClick([]{ std::println(std::cerr, "[click] Quit"); std::_Exit(0); })
),
UI::ProgressBar{}
.value(0.42f)
.size(UI::Length::Frac(1), UI::Length::Px(20))
.foreground(theme.primary.background),
UI::TabView{}
.font(font)
.width(UI::Length::Frac(1))
.height(UI::Length::Px(220))
.tab("Graphics", UI::VStack{}.padding(8).spacing(8).children(
UI::Text{"Resolution"}.font(font).size(14),
UI::InputField{"1920x1080"}.font(font).style(theme.input),
UI::Text{"Max lights"}.font(font).size(14),
UI::InputField{"32"}.font(font).style(theme.input)
))
.tab("Input", UI::VStack{}.padding(8).spacing(8).children(
UI::Text{"Mouse sensitivity"}.font(font).size(14),
UI::InputField{"1.0"}.font(font).style(theme.input)
))
.tab("Audio", UI::VStack{}.padding(8).spacing(8).children(
UI::Text{"Master volume"}.font(font).size(14),
UI::InputField{"80"}.font(font).style(theme.input)
))
)
);
for(std::uint8_t i = 0; i < Window::numFrames; i++) {
reinterpret_cast<VulkanBuffer<Vector<_Float16, 4, 4>, true>*>(element.buffers[i])->value[0] = {1, 0, 0, 1};
reinterpret_cast<VulkanBuffer<Vector<_Float16, 4, 4>, true>*>(element.buffers[i])->value[1] = {0, 1, 0, 1};
reinterpret_cast<VulkanBuffer<Vector<_Float16, 4, 4>, true>*>(element.buffers[i])->FlushDevice();
}
RendertargetVulkan rendertarget(1280, 720, {&element});
VkImageDescriptorInfoEXT imageInfo0 = {
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
.pView = &window.imageViews[0],
.layout = VK_IMAGE_LAYOUT_GENERAL
};
VkImageDescriptorInfoEXT imageInfo1 = {
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
.pView = &window.imageViews[1],
.layout = VK_IMAGE_LAYOUT_GENERAL
};
VkImageDescriptorInfoEXT imageInfo2 = {
.sType = VK_STRUCTURE_TYPE_IMAGE_DESCRIPTOR_INFO_EXT,
.pView = &window.imageViews[2],
.layout = VK_IMAGE_LAYOUT_GENERAL
};
std::array<VkResourceDescriptorInfoEXT, 9> infos;
infos[0] = {
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.data = { .pImage = &imageInfo0 }
};
infos[1] = {
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.data = { .pImage = &imageInfo1 }
};
infos[2] = {
.sType = VK_STRUCTURE_TYPE_RESOURCE_DESCRIPTOR_INFO_EXT,
.type = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.data = { .pImage = &imageInfo2 }
};
std::array<VkHostAddressRangeEXT, 9> ranges;
ranges[0] = {
.address = descriptorHeap.resourceHeap[0].value,
.size = Device::descriptorHeapProperties.imageDescriptorSize
};
ranges[1] = {
.address = descriptorHeap.resourceHeap[1].value,
.size = Device::descriptorHeapProperties.imageDescriptorSize
},
ranges[2] = {
.address = descriptorHeap.resourceHeap[2].value,
.size = Device::descriptorHeapProperties.imageDescriptorSize
},
rendertarget.WriteDescriptors(infos, ranges, 3, descriptorHeap.bufferStartOffset, descriptorHeap);
window.pipeline = &pipeline;
window.descriptorHeap = &descriptorHeap;
window.Render();
window.SaveFrame("frame.png");
window.StartUpdate(); // continuous rendering — UIScene re-emits per frame
window.StartSync();
}

View file

@ -0,0 +1,27 @@
import std;
import Crafter.Build;
namespace fs = std::filesystem;
using namespace Crafter;
extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> args) {
std::vector<std::string> graphicsArgs(args.begin(), args.end());
Configuration* graphics = LocalProject({
.projectFile = "../../project.cpp",
.args = graphicsArgs,
});
Configuration cfg;
cfg.path = "./";
cfg.name = "VulkanUI";
cfg.outputName = "VulkanUI";
ApplyStandardArgs(cfg, args);
cfg.dependencies = { graphics };
std::array<fs::path, 0> ifaces = {};
std::array<fs::path, 1> impls = { "main" };
cfg.GetInterfacesAndImplementations(ifaces, impls);
cfg.files.push_back("Inter.ttf");
return cfg;
}

View file

@ -1,23 +0,0 @@
{
"name": "crafter-graphics",
"configurations": [
{
"name": "executable",
"implementations": ["main"],
"dependencies": [
{
"path":"../../project.json",
"configuration":"lib-wayland-vulkan-debug"
}
],
"debug": true,
"shaders": [
{
"path":"raygen.glsl",
"type": 6,
"entrypoint":"main"
}
]
}
]
}

View file

@ -1,48 +0,0 @@
#version 460
#extension GL_EXT_ray_tracing : enable
#extension GL_EXT_shader_image_load_formatted : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable
#extension GL_EXT_descriptor_heap : enable
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable
struct UIScaledData{
int16_t x;
int16_t y;
int16_t sizeX;
int16_t sizeY;
uint16_t bufferX;
uint16_t bufferY;
};
layout(std430, descriptor_heap) buffer UIScaledDataBuffer {
uint16_t count;
uint16_t pad[5];
UIScaledData data[];
} UITransformBuffer[];
layout(std430, descriptor_heap) buffer UIPixelBufferr {
f16vec4 pixels[];
} UIPixelBuffer[];
layout(constant_id = 0) const uint16_t bufferStart = 0us;
layout(descriptor_heap) uniform writeonly image2D image[];
void main()
{
uvec2 pixel = gl_LaunchIDEXT.xy;
uvec2 resolution = gl_LaunchSizeEXT.xy;
vec4 hitValue = vec4(0);
for (uint16_t i = 1us; i < UITransformBuffer[bufferStart].count+1; i++) {
if(pixel.x > UITransformBuffer[bufferStart].data[i].x && pixel.x < UITransformBuffer[bufferStart].data[i].x + UITransformBuffer[bufferStart].data[i].sizeX && pixel.y > UITransformBuffer[bufferStart].data[i].y && pixel.y < UITransformBuffer[bufferStart].data[i].y + UITransformBuffer[bufferStart].data[i].sizeY) {
int16_t srcX = int16_t(float(pixel.x - UITransformBuffer[bufferStart].data[i].x) * float(UITransformBuffer[bufferStart].data[i].bufferX) / float(UITransformBuffer[bufferStart].data[i].sizeX));
int16_t srcY = int16_t(float(pixel.y - UITransformBuffer[bufferStart].data[i].y) * float(UITransformBuffer[bufferStart].data[i].bufferY) / float(UITransformBuffer[bufferStart].data[i].sizeY));
hitValue = vec4(UIPixelBuffer[bufferStart + 1].pixels[srcY * UITransformBuffer[bufferStart].data[i].bufferX + srcX]);
}
}
imageStore(image[0], ivec2(pixel), hitValue);
}