async runner order adjustment
[vg.git] / vg_tex.h
index ef7f0e99bfb55db0350a3dbf0589d1bb3150bb32..68d9d458654721dc17a06f6acd6eea2b54a7e20c 100644 (file)
--- a/vg_tex.h
+++ b/vg_tex.h
@@ -1,4 +1,35 @@
-/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2023 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.
+*/
+
 #ifndef VG_TEX_H
 #define VG_TEX_H
 
@@ -6,6 +37,21 @@
 #include "vg/vg.h"
 #include "vg/vg_log.h"
 
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#include "vg/submodules/stb/stb_image_write.h"
+
+/* its a sad day. */
+#if 0
+#define STBI_MALLOC(X) 
+#define STBI_REALLOC(X,Y)
+#define STBI_FREE(X)
+#endif
+
+#define STBI_ONLY_JPEG
+#define STBI_NO_THREAD_LOCALS
+#define STB_IMAGE_IMPLEMENTATION
+#include "vg/submodules/stb/stb_image.h"
+
 struct vg_sprite
 {
        v4f uv_xywh;
@@ -17,12 +63,15 @@ struct vg_sprite
 #define VG_TEX2D_CLAMP   0x8
 #define VG_TEX2D_NOMIP   0x10
 
-static u8 const_vg_tex2d_err[] =
-{
-   0x00, 0xff, 0xff, 0xff,
-   0xff, 0xff, 0x00, 0xff,
-   0x00, 0x00, 0xff, 0xff,
-   0xff, 0xff, 0x00, 0xff
+static u8 const_vg_tex2d_err[] ={
+   0xff,0x00,0xff,0xff, 0x00,0x00,0x00,0xff,
+   0xff,0x00,0xff,0xff, 0x00,0x00,0x00,0xff,
+   0x00,0x00,0x00,0xff, 0xff,0x00,0xff,0xff, 
+   0x00,0x00,0x00,0xff, 0xff,0x00,0xff,0xff, 
+   0xff,0x00,0xff,0xff, 0x00,0x00,0x00,0xff,
+   0xff,0x00,0xff,0xff, 0x00,0x00,0x00,0xff,
+   0x00,0x00,0x00,0xff, 0xff,0x00,0xff,0xff, 
+   0x00,0x00,0x00,0xff, 0xff,0x00,0xff,0xff, 
 };
 
 #define QOI_SRGB   0
@@ -80,8 +129,12 @@ struct texture_load_info{
    u8 *rgba;
 };
 
-VG_STATIC void async_vg_tex2d_upload( void *payload, u32 size )
+static void async_vg_tex2d_upload( void *payload, u32 size )
 {
+   if( vg_thread_purpose() != k_thread_purpose_main ){
+      vg_fatal_error( "Catastrophic programming error.\n" );
+   }
+
    struct texture_load_info *info = payload;
 
        glGenTextures( 1, info->dest );
@@ -120,7 +173,7 @@ VG_STATIC void async_vg_tex2d_upload( void *payload, u32 size )
    }
 }
 
-VG_STATIC void vg_tex2d_replace_with_error( GLuint *dest )
+static void vg_tex2d_replace_with_error( GLuint *dest )
 {
    u32 hdr_size = vg_align8(sizeof(struct texture_load_info));
 
@@ -129,14 +182,14 @@ VG_STATIC void vg_tex2d_replace_with_error( GLuint *dest )
    
    info->dest = dest;
    info->flags = VG_TEX2D_NEAREST|VG_TEX2D_REPEAT|VG_TEX2D_NOMIP;
-   info->width = 2;
-   info->height = 2;
+   info->width = 4;
+   info->height = 4;
    info->rgba = const_vg_tex2d_err;
 
    vg_async_dispatch( call, async_vg_tex2d_upload );
 }
 
-VG_STATIC 
+static 
 void vg_tex2d_load_qoi_async( const u8 *bytes, u32 size, 
                               u32 flags, GLuint *dest )
 {
@@ -270,7 +323,7 @@ void vg_tex2d_load_qoi_async( const u8 *bytes, u32 size,
    vg_async_dispatch( call, async_vg_tex2d_upload );
 }
 
-VG_STATIC 
+static 
 void vg_tex2d_load_qoi_async_file( const char *path, u32 flags, GLuint *dest )
 {
    vg_linear_clear( vg_mem.scratch );