libsdl-org / SDL_ttf

Support for TrueType (.ttf) font files with Simple Directmedia Layer.
zlib License
402 stars 131 forks source link

Added GPUTextEngine #412

Open captain0xff opened 1 month ago

captain0xff commented 1 month ago

This PR extends the new text rendering api to support the gpu api. It's still not ready for merging but the reason I am creating the PR is so that the API can be reviewed.

Things that I still need to do:-

I will need some help with the shaders because I don't have much idea about the workflow needed for the Metal and Direct3D backends.

captain0xff commented 1 month ago

Looks like stuff broke after pulling the latest changes. I am working on fixing them.

captain0xff commented 1 month ago

Fixed. The SDL_gpu_textengine.c is mostly similar to the SDL_renderer_textengine.c. I have just adjusted most stuff for the gpu api. I have added TTF_GetGPUTextDrawData to return the user the needed geometry data to draw the text and removed the Draw function. The UpdateGPUTexture method is stolen from the renderer gpu backend and can/should be improved. Apart from this I have updated the CreateDrawSequence to cache the positions along with the texcoords and indices.

slouken commented 1 month ago

In general, this looks good so far!

captain0xff commented 1 month ago

I have removed the hardcoded shader stuff and took the script from src/render/gpu/shader to build my shaders. I have only tested the workflow for spirv and vulkan since I only have access to a linux machine atm. I will appreciate if someone can help generate the shaders for other platforms or even provide some input about them. The renderer gpu backend doesn't seem to have any hlsl or msl shader present so I assume that the script generates them from spirv provided you have spirv-cross in your path. Next, I will clean up and improve the example. It may take some time depending on how quickly I can grasp the math behind perspective and lookAt functions and implement them. I can copy them from somewhere but I like to understand the math behind stuff I do. Also, that can cause licensing issues. After, that hopefully that the PR will be ready for a final review once I address the CI.

captain0xff commented 4 weeks ago

I have improved the example and also formatted it

captain0xff commented 4 weeks ago

I have fixed the CI. I am marking the PR ready. It still needs the shaders for the other platforms but I don't have access to a mac and windows machine.

madebr commented 4 weeks ago

I have fixed the CI. I am marking the PR ready. It still needs the shaders for the other platforms but I don't have access to a mac and windows machine.

At https://github.com/libsdl-org/SDL_gpu_shadercross/pull/27, we're working on a cli util that can generate shaders for various formats. There are binaries available for msvc, linux and macos on the GitHub actions page. Perhaps you can give it a spin?

captain0xff commented 3 weeks ago

With the help of a person from the FNA discord I have added the d3d12 and metal shaders. Though note that they are added from linux and are untested. The d3d11 shaders can also be added from linux but wine is needed. I use debian sid and at it's current state there are some issues with wine. @madebr I have seen you experimenting with wine in the past so can you do it? What you need to do is compile this thing https://github.com/mozilla/fxc2 (check the makefile) and add a scipt named fxc that runs the compiled program with wine, somewhere in your path. Then you can just run the ./build-shader.sh script.

captain0xff commented 3 weeks ago

After the d3d11 shaders are added I will hook these shaders with the example.

madebr commented 3 weeks ago

I don't think you need to commit all files in the shaders directory. See https://github.com/libsdl-org/SDL/tree/a10578acbdd86924574c9d5863b698b90a4ae212/src/render/gpu/shaders, there are no .spv files there.

thatcosmonaut commented 3 weeks ago

Now that we have a CLI for shadercross we should be building shaders with that instead, no need for another shader frontend dependency.

captain0xff commented 3 weeks ago

I don't think you need to commit all files in the shaders directory. See https://github.com/libsdl-org/SDL/tree/a10578acbdd86924574c9d5863b698b90a4ae212/src/render/gpu/shaders, there are no .spv files there.

Yeah, pushed them by mistake. I will remove those files.

captain0xff commented 3 weeks ago

Note, the example seems to leak memory. But it's probably due to https://github.com/libsdl-org/SDL_ttf/issues/425 and so not caused by me.

madebr commented 3 weeks ago

Note, the example seems to leak memory. But it's probably due to #425 and so not caused by me.

You can verify by linking to SDL3_test, and calling SDLTest_TrackAllocations(void); at the very beginning, and calling SDLTest_LogAllocations(void) at the very end.

captain0xff commented 3 weeks ago

Well it's show allocations and those are increasing.

captain0xff commented 3 weeks ago

Just to make sure, this PR isn't waiting for something from my end right?

captain0xff commented 2 weeks ago

Done. Thanks for the review.

captain0xff commented 1 week ago

https://github.com/libsdl-org/SDL/issues/11398 was closed as not planned. So, this PR is again ready I hope?

slouken commented 1 week ago

@captain0xff, can you rebase and resolve conflicts?

slouken commented 1 week ago

Actually, never mind, I can squash this as a single commit.

captain0xff commented 1 week ago

Yes, that will be better. Most of the commit messages are not very sensible.