taisei/src/rwops/rwops_autobuf.c

107 lines
2.1 KiB
C
Raw Permalink Normal View History

2017-09-12 03:28:15 +02:00
/*
* This software is licensed under the terms of the MIT License.
2017-09-12 03:28:15 +02:00
* See COPYING for further information.
* ---
2024-05-16 23:30:41 +02:00
* Copyright (c) 2011-2024, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2024, Andrei Alexeyev <akari@taisei-project.org>.
2017-09-12 03:28:15 +02:00
*/
2017-02-22 21:52:25 +01:00
#include "rwops_autobuf.h"
#define BUFFER(rw) ((Buffer*)((rw)->hidden.unknown.data1))
typedef struct Buffer {
SDL_RWops *memrw;
void *data;
void **ptr;
size_t size;
2017-02-22 21:52:25 +01:00
} Buffer;
static void auto_realloc(Buffer *b, size_t newsize) {
size_t pos = SDL_RWtell(b->memrw);
SDL_RWclose(b->memrw);
2017-02-22 21:52:25 +01:00
b->size = newsize;
b->data = mem_realloc(b->data, b->size);
b->memrw = SDL_RWFromMem(b->data, b->size);
if(b->ptr) {
*b->ptr = b->data;
}
2017-02-22 21:52:25 +01:00
if(pos > 0) {
SDL_RWseek(b->memrw, pos, RW_SEEK_SET);
}
2017-02-22 21:52:25 +01:00
}
static int auto_close(SDL_RWops *rw) {
if(rw) {
Buffer *b = BUFFER(rw);
SDL_RWclose(b->memrw);
mem_free(b->data);
mem_free(b);
SDL_FreeRW(rw);
}
return 0;
2017-02-22 21:52:25 +01:00
}
static int64_t auto_seek(SDL_RWops *rw, int64_t offset, int whence) {
return SDL_RWseek(BUFFER(rw)->memrw, offset, whence);
2017-02-22 21:52:25 +01:00
}
static int64_t auto_size(SDL_RWops *rw) {
// return SDL_RWsize(BUFFER(rw)->memrw);
return BUFFER(rw)->size;
2017-02-22 21:52:25 +01:00
}
static size_t auto_read(SDL_RWops *rw, void *ptr, size_t size, size_t maxnum) {
return SDL_RWread(BUFFER(rw)->memrw, ptr, size, maxnum);
2017-02-22 21:52:25 +01:00
}
static size_t auto_write(SDL_RWops *rw, const void *ptr, size_t size, size_t maxnum) {
Buffer *b = BUFFER(rw);
size_t newsize = b->size;
2017-02-22 21:52:25 +01:00
while(size * maxnum > newsize - SDL_RWtell(rw)) {
newsize *= 2;
}
2017-02-22 21:52:25 +01:00
if(newsize > b->size) {
auto_realloc(b, newsize);
}
2017-02-22 21:52:25 +01:00
return SDL_RWwrite(BUFFER(rw)->memrw, ptr, size, maxnum);
2017-02-22 21:52:25 +01:00
}
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 13:51:12 +02:00
SDL_RWops *SDL_RWAutoBuffer(void **ptr, size_t initsize) {
SDL_RWops *rw = SDL_AllocRW();
2017-02-22 21:52:25 +01:00
if(UNLIKELY(!rw)) {
return NULL;
}
2017-02-22 21:52:25 +01:00
rw->type = SDL_RWOPS_UNKNOWN;
rw->seek = auto_seek;
rw->size = auto_size;
rw->read = auto_read;
rw->write = auto_write;
rw->close = auto_close;
2017-02-22 21:52:25 +01:00
auto b = ALLOC(Buffer, {
.size = initsize,
.data = mem_alloc(initsize),
.ptr = ptr,
});
b->memrw = SDL_RWFromMem(b->data, b->size);
if(ptr) {
*ptr = b->data;
}
2017-02-22 21:52:25 +01:00
rw->hidden.unknown.data1 = b;
2017-02-22 21:52:25 +01:00
return rw;
2017-02-22 21:52:25 +01:00
}