define an auxiliary textures interface for sprite/text shaders

This commit is contained in:
Andrei Alexeyev 2018-07-24 21:00:44 +03:00
parent b67d81853d
commit 6c1746e366
No known key found for this signature in database
GPG key ID: 363707CD4C7FE8A4
9 changed files with 61 additions and 17 deletions

View file

@ -35,6 +35,10 @@ OUT(0) vec4 fragColor;
UNIFORM(0) sampler2D tex;
// For why this is 7, see NUM_SPRITE_AUX_TEXTURES api.h.
// This line is here just in case you're grepping for "R_MAX_TEXUNITS".
UNIFORM(64) sampler2D tex_aux[7];
VARYING(0) vec2 texCoordRaw;
VARYING(1) vec2 texCoord;
VARYING(2) vec2 texCoordOverlay;

View file

@ -4,8 +4,6 @@
#include "interface/sprite.glslh"
#include "lib/util.glslh"
UNIFORM(1) sampler2D trans;
float tc_mask(vec2 tc) {
return float(tc.x >= 0 && tc.x <= 1 && tc.y >= 0 && tc.y <= 1);
}
@ -33,5 +31,5 @@ void main(void) {
fragColor = mix(shadowfrag, textfrag, sqrt(textfrag.a));
tc_overlay = clamp(tc_overlay,0.01,0.99); // The overlay coordinates are outside of [0,1] in the padding region, so we make sure there are no wrap around artifacts when a bit of text is distorted to this region.
fragColor *= clamp((texture(trans, tc_overlay).r + 0.5) * 2.5 * t-0.5, 0.0, 1.0);
fragColor *= clamp((texture(tex_aux[0], tc_overlay).r + 0.5) * 2.5 * t-0.5, 0.0, 1.0);
}

View file

@ -350,6 +350,8 @@ typedef union ShaderCustomParams {
Color color;
} ShaderCustomParams;
#define NUM_SPRITE_AUX_TEXTURES (R_MAX_TEXUNITS - 1)
typedef struct SpriteParams {
const char *sprite;
Sprite *sprite_ptr;
@ -357,6 +359,8 @@ typedef struct SpriteParams {
const char *shader;
ShaderProgram *shader_ptr;
Texture *aux_textures[NUM_SPRITE_AUX_TEXTURES];
const Color *color;
BlendMode blend;

View file

@ -25,7 +25,8 @@ static struct SpriteBatchState {
VertexArray varr;
VertexBuffer vbuf;
uint base_instance;
Texture *tex;
Texture *primary_texture;
Texture *aux_textures[R_MAX_TEXUNITS - 1];
ShaderProgram *shader;
BlendMode blend;
Framebuffer *framebuffer;
@ -137,8 +138,23 @@ void r_flush_sprites(void) {
glm_mat4_copy(_r_sprite_batch.projection, *r_mat_current_ptr(MM_PROJECTION));
r_vertex_array(&_r_sprite_batch.varr);
r_texture_ptr(0, _r_sprite_batch.tex);
r_shader_ptr(_r_sprite_batch.shader);
r_texture_ptr(0, _r_sprite_batch.primary_texture);
int aux_samplers[NUM_SPRITE_AUX_TEXTURES] = { 0 };
for(uint i = 0; i < NUM_SPRITE_AUX_TEXTURES; ++i) {
Texture *tex = _r_sprite_batch.aux_textures[i];
if(tex != NULL) {
r_texture_ptr(i + 1, tex);
aux_samplers[i] = i + 1;
}
}
r_uniform_int("tex", 0);
r_uniform("tex_aux[0]", NUM_SPRITE_AUX_TEXTURES, aux_samplers);
r_framebuffer(_r_sprite_batch.framebuffer);
r_blend(_r_sprite_batch.blend);
r_capability(RCAP_DEPTH_TEST, _r_sprite_batch.depth_test_enabled);
@ -235,9 +251,18 @@ void r_draw_sprite(const SpriteParams *params) {
spr = get_sprite(params->sprite);
}
if(spr->tex != _r_sprite_batch.tex) {
if(spr->tex != _r_sprite_batch.primary_texture) {
r_flush_sprites();
_r_sprite_batch.tex = spr->tex;
_r_sprite_batch.primary_texture = spr->tex;
}
for(uint i = 0; i < NUM_SPRITE_AUX_TEXTURES; ++i) {
Texture *aux_tex = params->aux_textures[i];
if(aux_tex != NULL && aux_tex != _r_sprite_batch.aux_textures[i]) {
r_flush_sprites();
_r_sprite_batch.aux_textures[i] = aux_tex;
}
}
ShaderProgram *prog = params->shader_ptr;
@ -356,3 +381,15 @@ void _r_sprite_batch_end_frame(void) {
#endif
}
void _r_sprite_batch_texture_deleted(Texture *tex) {
if(_r_sprite_batch.primary_texture == tex) {
_r_sprite_batch.primary_texture = NULL;
}
for(uint i = 0; i < NUM_SPRITE_AUX_TEXTURES; ++i) {
if(_r_sprite_batch.aux_textures[i] == tex) {
_r_sprite_batch.aux_textures[i] = NULL;
}
}
}

View file

@ -9,6 +9,9 @@
#pragma once
#include "taisei.h"
#include "../api.h"
void _r_sprite_batch_init(void);
void _r_sprite_batch_shutdown(void);
void _r_sprite_batch_end_frame(void);
void _r_sprite_batch_texture_deleted(Texture *tex);

View file

@ -12,6 +12,7 @@
#include "../api.h"
#include "../common/matstack.h"
#include "../common/backend.h"
#include "../common/sprite_batch.h"
#include "texture.h"
#include "shader_object.h"
#include "shader_program.h"
@ -515,6 +516,8 @@ uint gl33_activate_texunit(uint unit) {
}
void gl33_texture_deleted(Texture *tex) {
_r_sprite_batch_texture_deleted(tex);
for(uint i = 0; i < R_MAX_TEXUNITS; ++i) {
if(R.texunits.indexed[i].tex2d.pending == tex) {
R.texunits.indexed[i].tex2d.pending = NULL;

View file

@ -822,6 +822,7 @@ static double _text_draw(Font *font, const char *text, const TextParams *params)
sp.color = params->color;
sp.blend = params->blend;
sp.shader_params = params->shader_params;
memcpy(sp.aux_textures, params->aux_textures, sizeof(sp.aux_textures));
if(sp.color == NULL) {
// XXX: sprite batch code defaults this to RGB(1, 1, 1)

View file

@ -66,6 +66,7 @@ typedef struct TextParams {
struct { double x, y; } pos;
const Color *color;
const ShaderCustomParams *shader_params;
Texture *aux_textures[NUM_SPRITE_AUX_TEXTURES];
BlendMode blend;
Alignment align;
} TextParams;

View file

@ -74,25 +74,18 @@ static void stagetext_draw_single(StageText *txt) {
txt->custom.predraw(txt, t, 1.0 - f);
}
r_state_push();
r_texture(1, "titletransition");
r_shader("text_stagetext");
r_uniform_int("trans", 1);
TextParams params = { 0 };
params.font_ptr = txt->font;
params.align = txt->align;
params.blend = BLEND_PREMUL_ALPHA;
params.shader_ptr = r_shader_current();
params.shader_ptr = r_shader_get("text_stagetext");
params.shader_params = &(ShaderCustomParams){{ 1 - f }},
params.aux_textures[0] = get_tex("titletransition");
params.pos.x = creal(txt->pos)+10*f*f;
params.pos.y = cimag(txt->pos)+10*f*f;
params.color = &txt->color;
text_draw(txt->text, &params);
r_state_pop();
text_draw(txt->text, &params);
}
void stagetext_draw(void) {