Files
kaizen/external/SDL/test/testgpurender_msdf.c
T
iris 00cc9309cb Squashed 'external/ircolib/' changes from ce3cd726c..de6e324bd
de6e324bd separate emu thread
10d3daf86 Roms List improvements
95d202f37 Let's make the rom list process on a separate thread so the emulator doesnt take ages to load.
fc306967f Wow the ROM Header was just completely busted. Game list view works now
bad1691ee fuck this shit
2b59e5f46 game list in progress
d26417b83 remappable inputs in progress
ac4af8106 input
e72abc240 update readme
430139dc9 Qt6 frontend
3080d4d45 Fix this small bug too
08cd13b85 Cop0 unused functions do not actually pose a threat (as per manual). They don't do anything, so shall we.
61bb4fb44 make idle loop detection a little more specific with where the load goes
b037de4c3 SAZDFsdff
12e81e73e need to figure out why n64-systemtest loops indefinitely at some address that appears to be valid (i think it's me not invalidating the cache properly)
204f0e13b idle skipping seems to work!
cb8bb634a sdkfjlasdf
58e5c89c1 Fix compilation issue on my machine (no idea)
24fb2898e attempting more serious idle skipping
214719577 Place rsp.Step inside cached interpreter. Gains about 3 more fps
bb97dcc23 mmmmm
920b77d38 wjkhasdfjhkasdf
430ccdab4 it's a start...
4f42a673a Cached interpreter plays Mario 64. Start looking into RSP as well
c9a030787 idle skipping works!
5fbda03ce new idea
366637aba Idle skipping... maybe?
609fa2fb0 Cache instructions implemented but broken lmao. Commented out for now
e140a6d12 - Stop using inheritance for CPU, instead use composition. - Introduce KAIZEN_JIT_ENABLED optional define instead of relying on __aarch64__ and the like. - More cache work
68e613057 prep cache impl
811b4d809 fix clang format
fda755f7d idk
d5024ebbf small MI refactor in preparation of (eventually) implementing the RDRAM interface properly
694b45341 Merge commit '206dcdedf195fb320913584180edb12c7731e396' as 'external/SDL'
206dcdedf Squashed 'external/SDL/' content from commit 4d17b99d0a
4d16e1cb4 need to update sdl
848b19920 Fix compilation error
db61b5299 Merge commit 'e94a94559f28e49678fbcf72199a5258137b0fe9' as 'external/imgui'
e94a94559 Squashed 'external/imgui/' content from commit 02e9b8cac
52edb3757 need to update imgui
c1a705e86 Emulate weird JALR behaviour
4b4c32f4b Fix exception for "unusable COP1" in 4 instructions i missed accidentally (again)
df5828142 Bug putting 0s in the log everywhere
f8b580048 Make isviewer a sink to file
8241e9735 Fix exception for "unusable COP1" in 4 instructions i missed accidentally
b29715f20 small changes
d9a620bc1 make use of my new small utility library
0d1aa938e Add 'external/ircolib/' from commit 'ce3cd726c8df8388d554abf8bb55d55020eb4450'
e64eb40b3 Fuck git

git-subtree-dir: external/ircolib
git-subtree-split: de6e324bde
2026-06-15 11:56:38 +02:00

331 lines
9.6 KiB
C

/*
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
#include "testutils.h"
/* This font was created with:
* ./msdf-atlas-gen.exe -font OpenSans-VariableFont_wdth,wght.ttf -chars '[32,126]' -type msdf -potr -yorigin top -imageout msdf_font.png -csv msdf_font.csv
*/
/* This is the distance field range in pixels used when generating the font atlas, defaults to 2 */
#define DISTANCE_FIELD_RANGE 2.0f
/* MSDF shaders */
#include "testgpurender_msdf.frag.dxil.h"
#include "testgpurender_msdf.frag.msl.h"
#include "testgpurender_msdf.frag.spv.h"
typedef struct
{
float distance_field_range;
float texture_width;
float texture_height;
float padding;
} MSDFShaderUniforms;
static SDLTest_CommonState *state = NULL;
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
static SDL_Texture *font_texture = NULL;
static SDL_GPUDevice *device = NULL;
static SDL_GPUShader *shader = NULL;
static SDL_GPURenderState *render_state = NULL;
typedef struct
{
bool loaded;
SDL_FRect src;
SDL_FRect dst;
float advance;
} GlyphInfo;
static GlyphInfo glyphs[128];
static bool LoadFontTexture(void)
{
font_texture = LoadTexture(renderer, "msdf_font.png", false);
if (!font_texture) {
SDL_Log("Failed to create font texture: %s", SDL_GetError());
return false;
}
SDL_SetTextureBlendMode(font_texture, SDL_BLENDMODE_BLEND);
/* Set the font color, doesn't need to be done every frame */
SDL_SetTextureColorMod(font_texture, 0, 0, 0);
return true;
}
static bool LoadFontLayout(void)
{
const char *file = "msdf_font.csv";
char *path;
int offset = 0, len, codepoint;
float src_left, src_top, src_right, src_bottom;
float dst_left, dst_top, dst_right, dst_bottom;
float advance;
char *font_layout;
path = GetNearbyFilename(file);
if (path) {
font_layout = (char *)SDL_LoadFile(path, NULL);
SDL_free(path);
} else {
font_layout = (char *)SDL_LoadFile(file, NULL);
}
if (!font_layout) {
SDL_Log("Failed to load font layout: %s", SDL_GetError());
return false;
}
while (SDL_sscanf(&font_layout[offset], "%d,%f,%f,%f,%f,%f,%f,%f,%f,%f%n",
&codepoint, &advance,
&dst_left, &dst_top, &dst_right, &dst_bottom,
&src_left, &src_top, &src_right, &src_bottom, &len) == 10) {
if (codepoint >= 0 && codepoint < SDL_arraysize(glyphs)) {
GlyphInfo *glyph = &glyphs[codepoint];
glyph->loaded = true;
glyph->src.x = src_left;
glyph->src.y = src_top;
glyph->src.w = src_right - src_left;
glyph->src.h = src_bottom - src_top;
glyph->dst.x = dst_left;
glyph->dst.y = dst_top;
glyph->dst.w = dst_right - dst_left;
glyph->dst.h = dst_bottom - dst_top;
glyph->advance = advance;
}
offset += len;
}
SDL_free(font_layout);
return true;
}
static float MeasureText(const char *text, float font_size)
{
float width = 0.0f;
while (*text) {
GlyphInfo *glyph;
Uint32 codepoint = SDL_StepUTF8(&text, NULL);
if (codepoint >= SDL_arraysize(glyphs)) {
continue;
}
glyph = &glyphs[codepoint];
if (!glyph->loaded) {
continue;
}
width += (glyph->advance * font_size);
}
return width;
}
static void RenderText(const char *text, float font_size, float x, float y)
{
SDL_FRect dst;
/* The y coordinate is actually the baseline for the text */
while (*text) {
GlyphInfo *glyph;
Uint32 codepoint = SDL_StepUTF8(&text, NULL);
if (codepoint >= SDL_arraysize(glyphs)) {
continue;
}
glyph = &glyphs[codepoint];
if (!glyph->loaded) {
continue;
}
dst.x = x + glyph->dst.x * font_size;
dst.y = y + glyph->dst.y * font_size;
dst.w = glyph->dst.w * font_size;
dst.h = glyph->dst.h * font_size;
SDL_RenderTexture(renderer, font_texture, &glyph->src, &dst);
x += (glyph->advance * font_size);
}
}
static bool InitGPURenderState(void)
{
SDL_GPUShaderFormat formats;
SDL_GPUShaderCreateInfo info;
SDL_GPURenderStateCreateInfo createinfo;
MSDFShaderUniforms uniforms;
device = SDL_GetGPURendererDevice(renderer);
if (!device) {
SDL_Log("Couldn't get GPU device");
return false;
}
formats = SDL_GetGPUShaderFormats(device);
if (formats == SDL_GPU_SHADERFORMAT_INVALID) {
SDL_Log("Couldn't get supported shader formats: %s", SDL_GetError());
return false;
}
SDL_zero(info);
if (formats & SDL_GPU_SHADERFORMAT_SPIRV) {
info.format = SDL_GPU_SHADERFORMAT_SPIRV;
info.code = testgpurender_msdf_frag_spv;
info.code_size = testgpurender_msdf_frag_spv_len;
} else if (formats & SDL_GPU_SHADERFORMAT_DXIL) {
info.format = SDL_GPU_SHADERFORMAT_DXIL;
info.code = testgpurender_msdf_frag_dxil;
info.code_size = testgpurender_msdf_frag_dxil_len;
} else if (formats & SDL_GPU_SHADERFORMAT_MSL) {
info.format = SDL_GPU_SHADERFORMAT_MSL;
info.code = testgpurender_msdf_frag_msl;
info.code_size = testgpurender_msdf_frag_msl_len;
} else {
SDL_Log("No supported shader format found");
return false;
}
info.num_samplers = 1;
info.num_uniform_buffers = 1;
info.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
shader = SDL_CreateGPUShader(device, &info);
if (!shader) {
SDL_Log("Couldn't create shader: %s", SDL_GetError());
return false;
}
SDL_zero(createinfo);
createinfo.fragment_shader = shader;
render_state = SDL_CreateGPURenderState(renderer, &createinfo);
if (!render_state) {
SDL_Log("Couldn't create render state: %s", SDL_GetError());
return false;
}
SDL_zero(uniforms);
uniforms.distance_field_range = DISTANCE_FIELD_RANGE;
uniforms.texture_width = (float)font_texture->w;
uniforms.texture_height = (float)font_texture->h;
if (!SDL_SetGPURenderStateFragmentUniforms(render_state, 0, &uniforms, sizeof(uniforms))) {
SDL_Log("Couldn't set uniform data: %s", SDL_GetError());
return false;
}
return true;
}
static void QuitGPURenderState(void)
{
if (render_state) {
SDL_DestroyGPURenderState(render_state);
render_state = NULL;
}
if (shader) {
SDL_ReleaseGPUShader(device, shader);
shader = NULL;
}
}
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
const char *description = "GPU render MSDF example";
SDL_SetAppMetadata(description, "1.0", "com.example.testgpurender_msdf");
state = SDLTest_CommonCreateState(argv, 0);
if (!state) {
return SDL_APP_FAILURE;
}
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
return SDL_APP_FAILURE;
}
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
window = SDL_CreateWindow(description, 640, 480, 0);
if (!window) {
SDL_Log("Couldn't create window: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
renderer = SDL_CreateRenderer(window, SDL_GPU_RENDERER);
if (!renderer) {
SDL_Log("Couldn't create renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_SetRenderVSync(renderer, 1);
if (!LoadFontTexture() || !LoadFontLayout()) {
return SDL_APP_FAILURE;
}
if (!InitGPURenderState()) {
return SDL_APP_FAILURE;
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_QUIT ||
(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_ESCAPE)) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
}
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
const char *text = "Hello world!";
float text_width;
float text_height;
float x, y;
int output_width, output_height;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
text_height = 72.0f;
text_width = MeasureText(text, text_height);
SDL_GetCurrentRenderOutputSize(renderer, &output_width, &output_height);
x = (output_width - text_width) / 2;
y = (output_height - text_height) / 2;
SDL_SetGPURenderState(renderer, render_state);
RenderText("Hello World!", text_height, x, y);
SDL_SetGPURenderState(renderer, NULL);
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE; /* carry on with the program! */
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
/* SDL will clean up the window/renderer for us. */
QuitGPURenderState();
SDL_Quit();
SDLTest_CommonDestroyState(state);
}