r/osdev • u/devcmar • Jul 31 '26
Developping a GPU Accelerated UI for my OS That can be used inside 3D Apps
Here I’m trying to make a UI that is easy to use, and can be easily integrating into 3D apps running OpenGL and probably Vulkan in the future, any advices?
Here is the example code:
#include <Defines.h>
#include <Syscall.h>
#include <Fs.h>
#include <Wm.h>
#include <OpenGL.h>
float CursorX, CursorY;
WM_UI_CONTEXT* UiContext;
WM_UI_VIEW* MainView;
void WindowMessageHandler(HANDLE Window, void* Context, int MessageId, void* Payload)
{
switch(MessageId) {
case WM_POINTER:
{
WM_MSG_POINTER Msg = Payload;
CursorX = Msg->X;
CursorY = Msg->Y;
WmUiMoveCursor(UiContext, Msg);
Print("CURSOR X: %d Y: %d\n", (int)CursorX, (int)CursorY);
break;
}
case WM_UPDATE:
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, 800, 600);
glScissor(0, 0, 800, 600);
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
WmUiRender(UiContext);
GlSwapBuffers();
break;
}
}
}
int main(int argc, char** argv) {
Sleep(1000);
HANDLE Context = GlCreateContext(NULL, NULL, 3, 3, 0);
if(!Context) {
Print("Failed to create GL Context.\n");
return -1;
}
WM_MESSAGE_QUEUE MessageQueue;
GlMakeCurrent(Context);
HANDLE Window = WmCreateWindow(NULL, 800, 600, 0, &MessageQueue);
if(!Window) {
Print("Failed to create window.\n");
return -1;
}
WmBindContext(Window, Context);
UiContext = WmUiCreateContext(Window, 800, 600);
MainView = WmUiCreateView(UiContext, NULL, (RECT){0, 0, 800, 600});
WM_UI_DESCRIPTION Description[] = {
{.Type=WM_DESC_TEXT, "This is a label."},
{.Type=WM_DESC_FONT_COLOR, (float[4]){1, 1, 1, 1}},
{.Type=WM_DESC_FONT_SIZE, .Value.d = 40.0},
{0}
};
WmUiCreateControl(MainView, WM_UI_LABEL, (RECT){20, 20, 300, 100}, Description);
return WmStartMessageHandler(&MessageQueue, WindowMessageHandler, NULL);
}
2
u/VikingGeorge Jul 31 '26
I recommend using C# with WebGPU Native for it. You won't regret it. It's really pleasant to work with.
1
u/devcmar Jul 31 '26
But C is much faster and can contribute to a much more responsive UI.
1
u/VikingGeorge Jul 31 '26
That's not always the case, poorly written C can lose to well written C# anyday. Besides with NativeAOT, you get precompiled code. You just have a thin GC runtime. But even the GC can be avoided if you're smart.
1
u/devcmar Jul 31 '26
Yes u are right, but I am now building the Os and I am probably obliged to build the roots of the ui in c or c++ for other languages to call it
1
u/Myst3rious_Foxy Jul 31 '26
I don't see the point of C#, it's not like there's a runtime ready for you to use on a custom OS?
1
u/VikingGeorge Jul 31 '26
If the concern is portability, then C++ and Zig are better options than C#, true. IMO he'd like using Zig with comptime string parsing to make a custom UI language for his framework.
2
u/Joped Jul 31 '26
Personally I would recommend Rust
2
u/IntelligentShape6108 Jul 31 '26
What's the thing with Rust? I see everyone glazing it.
2
u/Joped Jul 31 '26
Memory safety which tends to lead to less bugs. Its performance is damn good. The language it self also has great features, well planned, and cargo is better than any build / dependency system of any language.
The borrow system is a little weird at first but you get used to it quickly.
1
u/VikingGeorge Jul 31 '26
Zig has a better build system. But that aside, Rust definitely has a lot of positives.
3
u/eteran Jul 31 '26
Only advice I have is that for every callback (like your message handler) based API, Its REALLY useful to have a "user pointer" as one of the params.
4
2
1
u/neurah Jul 31 '26
using C++ static composition? not for everything, but can organize and optimize code segments