taisei/src/util/io.c
Andrei Alexeyev a5fd6fe5d9
Texturing overhaul: GPU compression, sRGB sampling, swizzles, etc. (#240)
* WIP compressed textures, swizzles, sRGB sampling, ...

* refactor texture type info & fix random bugs

* fix preprocessing of sRGB textures

* handle y-flipped basis textures

* glcommon: better WebGL compat for compressed format detection

* missed WEBGL_compressed_texture_pvrtc

* implement compressed texture xcoding and uploading

* Add basis_universal submodule

* Reorganize texture loader code

Clean up some code
Isolate Basis Universal loader into a separate module

* Add wrapper script for encoding .basis textures

* basisu: honor custom metadata written by the mkbasis.py script

* mkbasis.py: add --incredibly-slow and --dry-run

* Move pixmap code from util/ to pixmap/

* Add an on-disk transcode cache for basis textures to speed up loads

* Compress texture cache with zlib

* Use readable format names for basisu cache filenames

* basisu: mip bias test code

* basisu: small caching cleanup

* add TAISEI_BASISU_MIP_BIAS env variable

* Improve OpenGL format matching heuristics

* Document considerations for compressed format priority

* Remove dead code

* Enable two forgotten formats, BC3_RGBA and ATC_RGBA

Also prefer BC7 over BC1/BC3

* Recognize GL_ANGLE_compressed_texture_etc for ETC2 textures

* Default depth buffers to 24-bit; remove ANGLE hack

* Fix glcommon_check_extension for GLES2/legacy gl

* Add renderer feature bit for texture swizzle masks

* glcommon: Fixup internal formats for GLES2

Sized internal formats are not allowed in GLES2

* Fix emscripten compile errors

* Update basis_universal

* remove more dead code

* revert irrelevant stage4 change

* shut up UBSan

* basisu: shut up some debug spam

* Add normalmap sampling helper to util.glslh

* basisu: add a gray-alpha mode

* mkbasis.py: Abort if image dimansions aren't multiples of 4

* Add basic Basis Universal encoding documentation (WIP)

* doc/basisu: Add paragraph about modes; minor tweaks

* basisu: workarounds for GL texture size requirements

* gles20: fix uncompressed sRGB formats

* Partial workaround for missing swizzles in gles2 and webgl

* remove invalid assertion

* New renderer API to expose glDrawBuffers-like functionality

* stagedraw: disable all color outputs for copy_depth pass

required for WebGL compatibility

* support GL_ANGLE_request_extension

* emscripten: include *.basis in gfx package

Also fix a potential problem when more than one .pkgdir is used to
construct emscripten packages

* Don't rely on emscripten runtime to enable webgl extensions
2020-08-15 14:51:12 +03:00

179 lines
3.5 KiB
C

/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#include "taisei.h"
#include "io.h"
#include "log.h"
#include "vfs/public.h"
#include "assert.h"
#include "stringops.h"
#include "rwops/rwops_autobuf.h"
#include "util/crap.h"
char *SDL_RWgets(SDL_RWops *rwops, char *buf, size_t bufsize) {
char c, *ptr = buf, *end = buf + bufsize - 1;
assert(end > ptr);
while((c = SDL_ReadU8(rwops)) && ptr <= end) {
if((*ptr++ = c) == '\n')
break;
}
if(ptr == buf)
return NULL;
if(ptr > end) {
*end = 0;
log_warn("Line too long (%zu bytes max): %s", bufsize, buf);
} else {
*ptr = 0;
}
return buf;
}
char *SDL_RWgets_realloc(SDL_RWops *rwops, char **buf, size_t *bufsize) {
char c, *ptr = *buf, *end = *buf + *bufsize - 1;
assert(end >= ptr);
while((c = SDL_ReadU8(rwops))) {
*ptr++ = c;
if(ptr > end) {
ptrdiff_t ofs = ptr - *buf;
*bufsize *= 2;
*buf = realloc(*buf, *bufsize);
end = *buf + *bufsize - 1;
ptr = *buf + ofs;
*end = 0;
}
if(c == '\n') {
break;
}
}
if(ptr == *buf)
return NULL;
assert(ptr <= end);
*ptr = 0;
return *buf;
}
size_t SDL_RWprintf(SDL_RWops *rwops, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char *str = vstrfmt(fmt, args);
va_end(args);
size_t ret = SDL_RWwrite(rwops, str, 1, strlen(str));
free(str);
return ret;
}
void tsfprintf(FILE *out, const char *restrict fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(out, fmt, args);
va_end(args);
}
char *try_path(const char *prefix, const char *name, const char *ext) {
char *p = strjoin(prefix, name, ext, NULL);
if(vfs_query(p).exists) {
return p;
}
free(p);
return NULL;
}
static void *SDL_RWreadAll_known_size(SDL_RWops *rwops, size_t file_size, size_t *out_size) {
char *start = malloc(file_size);
char *end = start + file_size;
char *pbuf = start;
assert(end >= start);
for(;;) {
size_t read = SDL_RWread(rwops, pbuf, 1, end - pbuf);
assert(read <= end - pbuf);
if(read == 0) {
*out_size = pbuf - start;
return start;
}
pbuf += read;
}
}
void *SDL_RWreadAll(SDL_RWops *rwops, size_t *out_size, size_t max_size) {
ssize_t file_size = SDL_RWsize(rwops);
if(file_size >= 0) {
if(max_size && file_size > max_size) {
SDL_SetError("File is too large (%zu bytes; max is %zu)", file_size, max_size);
return NULL;
}
size_t sz;
void *result = SDL_RWreadAll_known_size(rwops, file_size, &sz);
if(result) {
*out_size = sz;
}
return result;
}
static const size_t chunk_size = BUFSIZ;
void *buf;
SDL_RWops *autobuf = NOT_NULL(SDL_RWAutoBuffer(&buf, chunk_size));
void *chunk = NOT_NULL(malloc(chunk_size));
size_t total_size = 0;
for(;;) {
size_t read = SDL_RWread(rwops, chunk, 1, chunk_size);
if(read == 0) {
free(chunk);
SDL_RWclose(rwops);
buf = memdup(buf, total_size);
SDL_RWclose(autobuf);
*out_size = total_size;
return buf;
}
size_t write = SDL_RWwrite(autobuf, chunk, 1, chunk_size);
if(UNLIKELY(write != read)) {
free(chunk);
SDL_RWclose(rwops);
SDL_RWclose(autobuf);
return NULL;
}
total_size += write;
if(max_size && UNLIKELY(total_size > max_size)) {
SDL_SetError("File is too large (%zu bytes read so far; max is %zu)", total_size, max_size);
free(chunk);
SDL_RWclose(rwops);
SDL_RWclose(autobuf);
return NULL;
}
}
}