taisei/src/util/graphics.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

188 lines
4.7 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 "graphics.h"
#include "global.h"
#include "video.h"
#include "pixmap/pixmap.h"
void set_ortho(float w, float h) {
r_mat_proj_ortho(0, w, h, 0, -100, 100);
// FIXME: should we take this out of here and call it explicitly instead?
r_disable(RCAP_DEPTH_TEST);
}
void colorfill(float r, float g, float b, float a) {
if(r <= 0 && g <= 0 && b <= 0 && a <= 0) {
return;
}
r_shader_standard_notex();
r_color4(r,g,b,a);
r_mat_mv_push();
r_mat_mv_scale(SCREEN_W,SCREEN_H,1);
r_mat_mv_translate(0.5,0.5,0);
r_draw_quad();
r_mat_mv_pop();
r_color4(1,1,1,1);
r_shader_standard();
}
void fade_out(float f) {
colorfill(0, 0, 0, f);
}
void draw_fragments(const DrawFragmentsParams *params) {
int i = 0;
const Color *fill_clr = params->color.fill;
const Color *back_clr = params->color.back;
const Color *frag_clr = params->color.frag;
if(params->alpha < 1) {
fill_clr = color_mul_scalar(COLOR_COPY(fill_clr), params->alpha);
frag_clr = color_mul_scalar(COLOR_COPY(frag_clr), params->alpha);
back_clr = color_mul_scalar(COLOR_COPY(back_clr), params->alpha);
}
static Color prev_back_clr;
ShaderProgram *prog_saved = r_shader_current();
ShaderProgram *prog_wanted = res_shader("sprite_circleclipped_indicator");
if(memcmp(&prev_back_clr, back_clr, sizeof(prev_back_clr))) {
// HACK/FIXME: we can't pass more than 4 floats via custom params, and we don't have auto-flush based on uniforms state, so...
prev_back_clr = *back_clr;
r_flush_sprites();
}
r_shader_ptr(prog_wanted);
r_uniform_vec4_rgba("back_color", back_clr);
r_uniform_vec2_vec("origin_ofs", (float*)&params->origin_offset);
r_flush_sprites();
float spacing = params->spacing + params->fill->w;
r_mat_mv_push();
r_mat_mv_translate(params->pos.x - spacing, params->pos.y, 0);
while(i < params->filled.elements) {
r_mat_mv_translate(spacing, 0, 0);
r_draw_sprite(&(SpriteParams) {
.sprite_ptr = params->fill,
.shader_params = &(ShaderCustomParams){{ 1 }},
.color = fill_clr,
});
i++;
}
if(params->filled.fragments) {
r_mat_mv_translate(spacing, 0, 0);
r_draw_sprite(&(SpriteParams) {
.sprite_ptr = params->fill,
.shader_params = &(ShaderCustomParams){{ params->filled.fragments / (float)params->limits.fragments }},
.color = frag_clr,
});
i++;
}
while(i < params->limits.elements) {
r_mat_mv_translate(spacing, 0, 0);
r_draw_sprite(&(SpriteParams) {
.sprite_ptr = params->fill,
.shader_params = &(ShaderCustomParams){{ 0 }},
.color = frag_clr,
});
i++;
}
r_mat_mv_pop();
r_shader_ptr(prog_saved);
}
double draw_fraction(double value, Alignment a, double pos_x, double pos_y, Font *f_int, Font *f_fract, const Color *c_int, const Color *c_fract, bool zero_pad) {
double val_int, val_fract;
char buf_int[4], buf_fract[4];
val_fract = modf(value, &val_int);
bool kern_int = font_get_kerning_enabled(f_int);
bool kern_fract = font_get_kerning_enabled(f_fract);
font_set_kerning_enabled(f_int, 0);
font_set_kerning_enabled(f_fract, 0);
snprintf(buf_int, sizeof(buf_int), zero_pad ? "%02i" : "%i", (int)val_int);
snprintf(buf_fract, sizeof(buf_fract), ".%02i", (int)floor(val_fract * 100));
double w_int = text_width(f_int, buf_int, 0);
double w_fract = text_width(f_fract, buf_fract, 0);
double w_total = w_int + w_fract;
switch(a) {
case ALIGN_CENTER:
pos_x -= w_total * 0.5;
break;
case ALIGN_RIGHT:
pos_x -= w_total;
break;
case ALIGN_LEFT:
break;
default: UNREACHABLE;
}
double ofs_int = font_get_descent(f_int);
double ofs_fract = font_get_descent(f_fract) - ofs_int;
ofs_int = 0;
pos_x += text_draw(buf_int, &(TextParams) {
.pos = { pos_x, pos_y + ofs_int },
.color = c_int,
.font_ptr = f_int,
.align = ALIGN_LEFT,
});
pos_x += text_draw(buf_fract, &(TextParams) {
.pos = { pos_x, pos_y + ofs_fract },
.color = c_fract,
.font_ptr = f_fract,
.align = ALIGN_LEFT,
});
font_set_kerning_enabled(f_int, kern_int);
font_set_kerning_enabled(f_fract, kern_fract);
return pos_x;
}
void draw_framebuffer_attachment(Framebuffer *fb, double width, double height, FramebufferAttachment attachment) {
CullFaceMode cull_saved = r_cull_current();
r_cull(CULL_BACK);
r_mat_mv_push();
r_uniform_sampler("tex", r_framebuffer_get_attachment(fb, attachment));
r_mat_mv_scale(width, height, 1);
r_mat_mv_translate(0.5, 0.5, 0);
r_draw_quad();
r_mat_mv_pop();
r_cull(cull_saved);
}
void draw_framebuffer_tex(Framebuffer *fb, double width, double height) {
draw_framebuffer_attachment(fb, width, height, FRAMEBUFFER_ATTACH_COLOR0);
}