X-Git-Url: https://harrygodden.com/git/?p=vg.git;a=blobdiff_plain;f=vg_tex.h;h=f570b11684fe367af76f8819dec2501050b5e307;hp=5c7c7f6b26a8ca088d875b61ca96b85aa2450201;hb=HEAD;hpb=4c48fe01a5d1983be89b7dce6f08e6b708cfbb05 diff --git a/vg_tex.h b/vg_tex.h index 5c7c7f6..24a99a0 100644 --- a/vg_tex.h +++ b/vg_tex.h @@ -1,198 +1,57 @@ -/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ -#ifndef VG_TEX_H -#define VG_TEX_H - -#include "vg/vg.h" -#include "vg/vg_log.h" - -#define VG_TEXTURE_NO_MIP 0x1 -#define VG_TEXTURE_REPEAT 0x2 -#define VG_TEXTURE_CLAMP 0x4 -#define VG_TEXTURE_NEAREST 0x8 -#define VG_TEXTURE_ALLOCATED_INTERNAL 0x10 - -VG_STATIC void *vg_qoi_malloc( size_t size ) -{ - return vg_linear_alloc( vg_mem.scratch, size ); -} - -VG_STATIC void vg_qoi_free( void *ptr ) -{ - -} - -#define QOI_IMPLEMENTATION -#define QOI_NO_STDIO -#define QOI_MALLOC(sz) vg_qoi_malloc( sz ) -#define QOI_FREE(p) vg_qoi_free( p ) - -#include "submodules/qoi/qoi.h" - -struct vg_tex2d -{ - const char *path; - u32 flags; - GLuint name; -}; +/* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved + * + * A portion of this file is copied and altered from the QOI projects' source, + * Originally written by Dominic Szablewski. It is slightly modified. + * For the original unaltered QOI header, you can find it here: + * https://github.com/phoboslab/qoi/blob/master/qoi.h + * + * Copyright (C) 2021, Dominic Szablewski + * SPDX-License-Identifier: MIT + * + * MIT License + Copyright (c) 2022 Dominic Szablewski - https://phoboslab.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + SOFTWARE. +*/ + +#pragma once +#include "vg_log.h" +#include "vg_image.h" +#include "vg_engine.h" struct vg_sprite { v4f uv_xywh; }; -VG_STATIC void vg_tex2d_bind( vg_tex2d *tex, u32 id ) -{ - if( !(tex->flags & VG_TEXTURE_ALLOCATED_INTERNAL) ) - { - vg_error( "Tried to use '%s' while unloaded!\n", tex->path ); - return; - } - - glActiveTexture( GL_TEXTURE0 + id ); - glBindTexture( GL_TEXTURE_2D, tex->name ); -} - -static inline void vg_tex2d_mipmap(void) -{ - glGenerateMipmap( GL_TEXTURE_2D ); -} +#define VG_TEX2D_LINEAR 0x1 +#define VG_TEX2D_NEAREST 0x2 +#define VG_TEX2D_REPEAT 0x4 +#define VG_TEX2D_CLAMP 0x8 +#define VG_TEX2D_NOMIP 0x10 -static inline void vg_tex2d_linear(void) -{ - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); -} - -static inline void vg_tex2d_nearest(void) -{ - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); -} +/* options to create texutres; call only from loader thread. + * *dest will be replaced synchronously by the main thread when ready. */ -static inline void vg_tex2d_linear_mipmap(void) -{ - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, - GL_LINEAR_MIPMAP_LINEAR ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); -} - -static inline void vg_tex2d_repeat(void) -{ - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); -} - -static inline void vg_tex2d_clamp(void) -{ - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); -} +void vg_tex2d_replace_with_error_async( GLuint *dest ); -VG_STATIC GLuint vg_tex2d_new(void) -{ - GLuint texture_name; - glGenTextures( 1, &texture_name ); - glBindTexture( GL_TEXTURE_2D, texture_name ); - - return texture_name; -} - -VG_STATIC void vg_tex2d_set_error(void) -{ - u32 tex_err[4] = - { - 0xffff00ff, - 0xff000000, - 0xff000000, - 0xffff00ff - }; - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); -} - -VG_STATIC void vg_tex2d_qoi( void *mem, u32 size, const char *name ) -{ - qoi_desc info; - u8 *tex_buffer = qoi_decode( mem, size, &info, 4 ); - - if( tex_buffer ) - { - vg_info( "Texture decoded: [%u %u] %s\n", - info.width, info.height, name ); - - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, - 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); - - QOI_FREE(tex_buffer); - } - else - { - vg_error( "File size: %u\n", size ); - vg_tex2d_set_error(); - } -} - -VG_STATIC GLuint vg_tex2d_rgba( const char *path ) -{ - GLuint texture_name = vg_tex2d_new(); - - vg_linear_clear( vg_mem.scratch ); - u32 size; - void *file = vg_file_read( vg_mem.scratch, path, &size ); - - if( file ) - { - vg_tex2d_qoi( file, size, path ); - } - else - { - vg_error( "Loading texture failed (%s)\n", path ); - vg_tex2d_set_error(); - } - - return texture_name; -} - -VG_STATIC void vg_tex2d_init( vg_tex2d *textures[], int num ) -{ - for( int i=0; iname = vg_tex2d_rgba( tex->path ); - if( !(tex->flags & VG_TEXTURE_NO_MIP) ) - vg_tex2d_mipmap(); - - if( tex->flags & VG_TEXTURE_NEAREST ) - { - if( tex->flags & VG_TEXTURE_NO_MIP ) - vg_error( "Invalid texture settings\n" ); - else - vg_tex2d_nearest(); - } - else - { - if( tex->flags & VG_TEXTURE_NO_MIP ) - vg_tex2d_linear(); - else - vg_tex2d_linear_mipmap(); - } - - if( tex->flags & VG_TEXTURE_CLAMP ) - vg_tex2d_clamp(); - else - vg_tex2d_repeat(); - - tex->flags |= VG_TEXTURE_ALLOCATED_INTERNAL; - } -} - -VG_STATIC void vg_tex2d_free( vg_tex2d *textures[], int num ) -{ - for( int i = 0; i < num; i ++ ) - { - glDeleteTextures( 1, &textures[i]->name ); - } -} +void vg_tex2d_load_qoi_async( const u8 *bytes, u32 size, + u32 flags, GLuint *dest ); -#endif /* VG_TEX_H */ +void vg_tex2d_load_qoi_async_file( const char *path, u32 flags, GLuint *dest );