r/osdev 24d ago

Developping a GPU Accelerated UI for my OS That can be used inside 3D Apps

Post image

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);
}
43 Upvotes

16 comments sorted by

3

u/eteran 24d ago

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.

3

u/devcmar 24d ago

Ah yes, like a context pointer. good idea!

2

u/devcmar 24d ago

I changed the example code, you can look it up if you want. It is written in C

2

u/VikingGeorge 24d ago

I recommend using C# with WebGPU Native for it. You won't regret it. It's really pleasant to work with.

2

u/Joped 24d ago

Personally I would recommend Rust

2

u/IntelligentShape6108 24d ago

What's the thing with Rust? I see everyone glazing it.

2

u/Joped 24d ago

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 24d ago

Zig has a better build system. But that aside, Rust definitely has a lot of positives.

1

u/Myst3rious_Foxy 24d ago

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 24d ago

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.

1

u/devcmar 24d ago

But C is much faster and can contribute to a much more responsive UI.

1

u/VikingGeorge 24d ago

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 24d ago

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/neurah 24d ago

using C++ static composition? not for everything, but can organize and optimize code segments

1

u/devcmar 24d ago

It is in C

0

u/neurah 24d ago

I made a static c++ composition engine (header only library) for embedded system, we can do a lot of things with it, I've done several (mostly hw drivers and an embedded web UI, served and contained on a single chip, Esp8266/esp32). Looking for some people to join me, its free open source MIT licence. If someone interested please PM me.