Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GPUTextEngine #412

Merged
merged 29 commits into from
Dec 7, 2024
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b54f04a
TTF_Draw* functions should return true on success
captain0xff Oct 1, 2024
1621a10
updated .gitignore for clangd
captain0xff Oct 1, 2024
eac1af1
added gputextengine api and example (WIP)
captain0xff Oct 17, 2024
9f80731
Merge remote-tracking branch 'origin/gpu-text-engine' into gpu-text-e…
captain0xff Oct 17, 2024
eac37e1
updated for the latest SDL_ttf changes
captain0xff Oct 17, 2024
90126e2
updated the docs
captain0xff Oct 19, 2024
e5432dc
replaced the math_3d.h with my own custom implementation
captain0xff Oct 20, 2024
32a2e01
added a script to build and load the shaders
captain0xff Oct 22, 2024
8d70098
use SDL_PI_F instead of defining M_PI
captain0xff Oct 22, 2024
6461d91
no need to link libm anymore
captain0xff Oct 22, 2024
4b09066
added perspective and look_at functions
captain0xff Oct 24, 2024
4c8e006
improved the example to look more 3D
captain0xff Oct 24, 2024
4445dbf
added one pixel gap between the glyphs
captain0xff Oct 24, 2024
1bdbb32
moved around some stuff and updated the example
captain0xff Oct 24, 2024
ed26252
added font file and license
captain0xff Oct 25, 2024
f993143
formatted the example
captain0xff Oct 25, 2024
d80f917
Merge branch 'libsdl-org:main' into gpu-text-engine
captain0xff Oct 25, 2024
0267ea1
updated for the latest SDL_ttf API changes
captain0xff Oct 25, 2024
e1bad6c
fix the CI
captain0xff Oct 25, 2024
30e0cb2
more CI fixes
captain0xff Oct 25, 2024
e9dc9b3
add braces around ifs
captain0xff Oct 25, 2024
2976b86
added dx12 and metal shaders
captain0xff Oct 27, 2024
7741273
Add d3d11 shaders
madebr Oct 28, 2024
2b16b72
add shader support for the other backends
captain0xff Oct 30, 2024
b90f8e3
destroy the created text
captain0xff Oct 30, 2024
e25680e
remove the spirv binaries
captain0xff Oct 30, 2024
aec5435
check for issues after doing shady stuff
captain0xff Oct 30, 2024
b585c91
fixed minor issues in the example
captain0xff Nov 9, 2024
2c45250
more fixes
captain0xff Nov 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions examples/testgputext.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
#include <SDL3_ttf/SDL_ttf.h>

#include "testgputext/shaders/spir-v.h"
#include "testgputext/shaders/dxbc50.h"
#include "testgputext/shaders/dxil60.h"
#include "testgputext/shaders/metal.h"
#define SDL_MATH_3D_IMPLEMENTATION
#include "testgputext/SDL_math3d.h"

#define MAX_VERTEX_COUNT 4000
#define MAX_INDEX_COUNT 6000
#define SUPPORTED_SHADER_FORMATS (SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXBC | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB)

typedef struct Vec2
{
Expand Down Expand Up @@ -64,32 +68,44 @@ void *check_error_ptr(void *ptr)

SDL_GPUShader *load_shader(
SDL_GPUDevice *device,
const unsigned char code[],
const unsigned int code_size,
bool is_vertex,
Uint32 sampler_count,
Uint32 uniform_buffer_count,
Uint32 storage_buffer_count,
Uint32 storage_texture_count)
{
SDL_GPUShaderCreateInfo shader_info = {
.code = code,
.code_size = code_size,
.entrypoint = "main",
.format = SDL_GPU_SHADERFORMAT_SPIRV,
.stage = (is_vertex) ? SDL_GPU_SHADERSTAGE_VERTEX : SDL_GPU_SHADERSTAGE_FRAGMENT,
.num_samplers = sampler_count,
.num_uniform_buffers = uniform_buffer_count,
.num_storage_buffers = storage_buffer_count,
.num_storage_textures = storage_texture_count
};
SDL_GPUShader *shader = SDL_CreateGPUShader(device, &shader_info);
if (shader == NULL) {
SDL_Log("Failed to create shader!");
return NULL;
SDL_GPUShaderCreateInfo createinfo;
createinfo.num_samplers = sampler_count;
createinfo.num_storage_buffers = storage_buffer_count;
createinfo.num_storage_textures = storage_texture_count;
createinfo.num_uniform_buffers = uniform_buffer_count;
createinfo.props = 0;

SDL_GPUShaderFormat format = SDL_GetGPUShaderFormats(device);
if (format & SDL_GPU_SHADERFORMAT_DXBC) {
createinfo.format = SDL_GPU_SHADERFORMAT_DXBC;
createinfo.code = (Uint8*)(is_vertex ? shader_vert_sm50_dxbc : shader_frag_sm50_dxbc);
createinfo.code_size = is_vertex ? SDL_arraysize(shader_vert_sm50_dxbc) : SDL_arraysize(shader_frag_sm50_dxbc);
createinfo.entrypoint = is_vertex ? "VSMain" : "PSMain";
} else if (format & SDL_GPU_SHADERFORMAT_DXIL) {
createinfo.format = SDL_GPU_SHADERFORMAT_DXIL;
createinfo.code = is_vertex ? shader_vert_sm60_dxil : shader_frag_sm60_dxil;
createinfo.code_size = is_vertex ? SDL_arraysize(shader_vert_sm60_dxil) : SDL_arraysize(shader_frag_sm60_dxil);
createinfo.entrypoint = is_vertex ? "VSMain" : "PSMain";
} else if (format & SDL_GPU_SHADERFORMAT_METALLIB) {
createinfo.format = SDL_GPU_SHADERFORMAT_METALLIB;
createinfo.code = is_vertex ? shader_vert_metal : shader_frag_metal;
createinfo.code_size = is_vertex ? shader_frag_metal_len : shader_frag_metal_len;
captain0xff marked this conversation as resolved.
Show resolved Hide resolved
createinfo.entrypoint = is_vertex ? "vs_main" : "fs_main";
} else {
createinfo.format = SDL_GPU_SHADERFORMAT_SPIRV;
createinfo.code = is_vertex ? shader_vert_spv : shader_frag_spv;
createinfo.code_size = is_vertex ? shader_vert_spv_len : shader_frag_spv_len;
createinfo.entrypoint = "main";
}

return shader;
createinfo.stage = is_vertex ? SDL_GPU_SHADERSTAGE_VERTEX : SDL_GPU_SHADERSTAGE_FRAGMENT;
return SDL_CreateGPUShader(device, &createinfo);
}

void queue_text_sequence(GeometryData *geometry_data, TTF_GPUAtlasDrawSequence *sequence, SDL_FColor *colour)
Expand Down Expand Up @@ -222,11 +238,11 @@ int main(int argc, char *argv[])

context.window = check_error_ptr(SDL_CreateWindow("GPU text test", 800, 600, 0));

context.device = check_error_ptr(SDL_CreateGPUDevice(SDL_GPU_SHADERFORMAT_SPIRV, true, NULL));
context.device = check_error_ptr(SDL_CreateGPUDevice(SUPPORTED_SHADER_FORMATS, true, NULL));
check_error_bool(SDL_ClaimWindowForGPUDevice(context.device, context.window));

SDL_GPUShader *vertex_shader = load_shader(context.device, shader_vert_spv, shader_vert_spv_len, true, 0, 1, 0, 0);
SDL_GPUShader *fragment_shader = load_shader(context.device, shader_frag_spv, shader_frag_spv_len, false, 1, 0, 0, 0);
SDL_GPUShader *vertex_shader = load_shader(context.device, true, 0, 1, 0, 0);
SDL_GPUShader *fragment_shader = load_shader(context.device, false, 1, 0, 0, 0);
captain0xff marked this conversation as resolved.
Show resolved Hide resolved

SDL_GPUGraphicsPipelineCreateInfo pipeline_create_info = {
.target_info = {
Expand Down
Loading