# HG changeset patch # User Mike Becker # Date 1742420617 -3600 # Node ID 9bc544f4ce8feaa5bce9addf357caecec5da4543 # Parent 2187a732f4d741ee8da01572006d37c2d59ac436 go back to reading shader code from files apart from using a nice C23 feature there is nothing really useful about embedding the code directly diff -r 2187a732f4d7 -r 9bc544f4ce8f src/Makefile --- a/src/Makefile Tue Mar 18 22:43:31 2025 +0100 +++ b/src/Makefile Wed Mar 19 22:43:37 2025 +0100 @@ -74,7 +74,7 @@ $(BUILD_DIR)/glcontext.o: glcontext.c ascension/glcontext.h \ ascension/primitives.h ascension/mesh.h ascension/shader.h \ - ascension/error.h shader_codes.h ascension/shader.h + ascension/error.h @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< diff -r 2187a732f4d7 -r 9bc544f4ce8f src/ascension/shader.h --- a/src/ascension/shader.h Tue Mar 18 22:43:31 2025 +0100 +++ b/src/ascension/shader.h Wed Mar 19 22:43:37 2025 +0100 @@ -28,9 +28,14 @@ #ifndef ASCENSION_SHADER_H #define ASCENSION_SHADER_H -typedef struct AscShaderCodes { +typedef struct AscShaderCodeFiles { const char *vtx; const char *frag; +} AscShaderCodeFiles; + +typedef struct AscShaderCodes { + char *vtx; + char *frag; } AscShaderCodes; typedef struct AscShaderProgram { @@ -47,6 +52,21 @@ } AscShaderSprite; /** + * Loads shader codes from files. + * + * @param files the structure containing the file names + * @param codes the structure where to store the loaded codes + */ +int asc_shader_load_code_files(AscShaderCodeFiles files, AscShaderCodes *codes); + +/** + * Deallocates the memory for the source codes. + * + * @param codes the structure containing pointers to the source codes + */ +void asc_shader_free_codes(AscShaderCodes codes); + +/** * Destroys a shader program. * * @param program the program diff -r 2187a732f4d7 -r 9bc544f4ce8f src/glcontext.c --- a/src/glcontext.c Tue Mar 18 22:43:31 2025 +0100 +++ b/src/glcontext.c Wed Mar 19 22:43:37 2025 +0100 @@ -48,13 +48,21 @@ cx_strfree(&buf); } -#include "shader_codes.h" - static void asc_shader_initialize_predefined(AscGLContext *ctx) { + // TODO: deal with different working dirs AscShaderSprite *sprite = &ctx->shader.sprite; - sprite->program = asc_shader_program_create(asc_shader_codes_sprite); + AscShaderCodes codes; + if (asc_shader_load_code_files((AscShaderCodeFiles){ + .vtx = "./shader/sprite_vtx.glsl", + .frag = "./shader/sprite_frag.glsl" + }, &codes)) { + asc_error("Loading sprite shader failed."); + return; + } + sprite->program = asc_shader_program_create(codes); sprite->depth = glGetUniformLocation(sprite->program.id, "depth"); sprite->tex = glGetUniformLocation(sprite->program.id, "texture"); + asc_shader_free_codes(codes); } static void asc_shader_destroy_predefined(AscGLContext *ctx) { diff -r 2187a732f4d7 -r 9bc544f4ce8f src/shader.c --- a/src/shader.c Tue Mar 18 22:43:31 2025 +0100 +++ b/src/shader.c Wed Mar 19 22:43:37 2025 +0100 @@ -30,6 +30,8 @@ #include #include +#include +#include /** * Compiles a shader from the given source code. @@ -131,3 +133,30 @@ } return prog; } + +static int asc_shader_load_code_file(const char *filename, char **code) { + if (filename == NULL) { + *code = NULL; + return 0; + } + FILE *f = fopen(filename, "r"); + if (f == NULL) return -1; + CxBuffer buffer; + cxBufferInit(&buffer, NULL, 1024, NULL, 0); + cx_stream_copy(f, &buffer, (cx_read_func) fread, cxBufferWriteFunc); + cxBufferPut(&buffer, '\0'); + *code = realloc(buffer.space, buffer.size); + return *code == NULL ? -1 : 0; +} + +int asc_shader_load_code_files(AscShaderCodeFiles files, AscShaderCodes *codes) { + int ret = 0; + ret |= asc_shader_load_code_file(files.vtx, &codes->vtx); + ret |= asc_shader_load_code_file(files.frag, &codes->frag); + return ret; +} + +void asc_shader_free_codes(AscShaderCodes codes) { + free(codes.vtx); + free(codes.frag); +} diff -r 2187a732f4d7 -r 9bc544f4ce8f src/shader_codes.h --- a/src/shader_codes.h Tue Mar 18 22:43:31 2025 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* -* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * Copyright 2025 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifdef ASC_SHADER_CODES_LOADED -#error "Shader codes are supposed to be included once!" -#endif -#define ASC_SHADER_CODES_LOADED - -#include "ascension/shader.h" - -#define asc_embed(name, file) \ - __asm__(".section .rodata\n" \ - ".global " #name "\n" \ - #name ":\n" \ - ".incbin \"" file "\"\n" \ - ".byte 0\n" \ - ); \ - extern const char name[] - -#ifdef __has_embed -const char asc_shader_code_sprite_vtx[] = { -#embed "../shader/sprite_vtx.glsl" suffix(, 0) -}; -const char asc_shader_code_sprite_frag[] = { -#embed "../shader/sprite_frag.glsl" suffix(, 0) -}; -#else -#warning "C23 support for #embed is missing - falling back to inline assembler" -asc_embed(asc_shader_code_sprite_vtx, "../shader/sprite_vtx.glsl"); -asc_embed(asc_shader_code_sprite_frag, "../shader/sprite_frag.glsl"); -#endif - -static AscShaderCodes asc_shader_codes_sprite = { - asc_shader_code_sprite_vtx, - asc_shader_code_sprite_frag -}; -