Sun, 08 Jun 2025 14:58:19 +0200
refactor shader management - resolves #684
/* * 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. */ #include "ascension/2d/sprite.h" #include "ascension/context.h" #include "ascension/glcontext.h" #include "ascension/error.h" #include "ascension/mesh.h" #include "ascension/constants.h" #include <GL/glew.h> struct asc_sprite_shader_s { AscShaderProgram program; int tex; }; static void *asc_sprite_shader_create(bool rect) { AscShaderCodes codes; if (asc_shader_load_code_files((AscShaderCodeInfo){ .files.vtx = "sprite_vtx.glsl", .files.frag = "sprite_frag.glsl", .defines.frag = rect ? "#define USE_RECT" : NULL, }, &codes)) { asc_error("Loading sprite shader failed."); return NULL; } struct asc_sprite_shader_s *shader = asc_shader_create(codes, sizeof(*shader)); if (asc_has_error()) { asc_shader_free_codes(codes); return NULL; } shader->tex = glGetUniformLocation(shader->program.gl_id, "tex"); asc_shader_free_codes(codes); asc_error_catch_all_gl(); return shader; } static AscShaderProgram *asc_sprite_shader_rect_create() { return asc_sprite_shader_create(true); } static AscShaderProgram *asc_sprite_shader_uv_create() { return asc_sprite_shader_create(false); } static void asc_sprite_destroy(AscSceneNode *node) { AscSprite *sprite = (AscSprite *) node; asc_mesh_destroy(&sprite->mesh); sprite->texture->refcount--; } static void asc_sprite_update(AscSceneNode *node) { AscSprite *sprite = (AscSprite *) node; // calculate texture parameters asc_vec2f uv_scale; if (sprite->texture_scale_mode == ASC_TEXTURE_SCALE_REPEAT) { uv_scale = asc_texture_calculate_uv_scale(sprite->texture, asc_vec2u_new(sprite->width, sprite->height), sprite->texture_scale); } else { uv_scale = sprite->texture_scale; } // update mesh asc_mesh_plane_2d(&sprite->mesh, .size = asc_vec2f_new(sprite->width, sprite->height), .uv_scale = uv_scale ); } AscSceneNode *asc_sprite_create(struct asc_sprite_create_args args) { AscSprite *sprite = cxZallocDefault(sizeof(AscSprite)); // sprite parameters args.texture->refcount++; sprite->texture = args.texture; sprite->width = args.width; sprite->height = args.height; sprite->texture_scale_mode = args.texture_scale_mode; sprite->texture_scale.u = ASC_NONZERO_OR(1.f, args.texture_scale_x); sprite->texture_scale.v = ASC_NONZERO_OR(1.f, args.texture_scale_y); // basic node parameters AscSceneNode *node = (AscSceneNode *) sprite; asc_scene_node_name(node, args.name); node->render_group = args.opaque ? ASC_RENDER_GROUP_SPRITE_OPAQUE_UV : ASC_RENDER_GROUP_SPRITE_BLEND_UV; node->update_func = asc_sprite_update; node->destroy_func = asc_sprite_destroy; node->position = asc_vec3f_new(args.x, args.y, ASC_SCENE_2D_DEPTH_OFFSET); node->scale = asc_vec3f_one; asc_node_update(node); return node; } const AscShaderProgram *asc_sprite_shader_rect(void) { return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_RECT, asc_sprite_shader_rect_create); } const AscShaderProgram *asc_sprite_shader_uv(void) { return asc_shader_lookup_or_create(ASC_SHADER_SPRITE_UV, asc_sprite_shader_uv_create); } void asc_sprite_draw(const AscShaderProgram *program, const AscSprite *node) { asc_ptr_cast(const struct asc_sprite_shader_s, shader, program); // Upload model matrix glUniformMatrix4fv(shader->program.model, 1, GL_FALSE, node->data.world_transform); // Bind texture asc_texture_bind(node->texture, shader->tex, 0); // Draw mesh asc_mesh_draw_triangle_strip(&node->mesh); } void asc_sprite_set_size(AscSceneNode *node, unsigned width, unsigned height) { AscSprite *sprite = (AscSprite*) node; sprite->width = width; sprite->height = height; asc_node_update(node); }