Dynamic on load text shaders

This commit is contained in:
John Alanbrook 2024-05-03 16:26:40 -05:00
parent 79e4772f93
commit 97e258ae7c
6 changed files with 44 additions and 92 deletions

View file

@ -5,9 +5,6 @@
#include "stb_ds.h" #include "stb_ds.h"
#include "gameobject.h" #include "gameobject.h"
//#include "diffuse.sglsl.h"
#include "unlit.sglsl.h"
#include "render.h" #include "render.h"
#include "HandmadeMath.h" #include "HandmadeMath.h"
@ -460,14 +457,15 @@ void model_free(model *m)
sg_bindings primitive_bind(primitive *p) sg_bindings primitive_bind(primitive *p)
{ {
sg_bindings b = {0}; sg_bindings b = {0};
b.vertex_buffers[unlit_attr_slot("a_pos")] = p->pos; b.vertex_buffers[MAT_POS] = p->pos;
b.vertex_buffers[5] = p->uv; b.vertex_buffers[MAT_UV] = p->uv;
b.vertex_buffers[3] = p->norm; b.vertex_buffers[MAT_NORM] = p->norm;
//b.vertex_buffers[unlit_attr_slot("a_bone")] = p->bone; b.vertex_buffers[MAT_BONE] = p->bone;
//b.vertex_buffers[unlit_attr_slot("a_weight")] = p->weight; b.vertex_buffers[MAT_WEIGHT] = p->weight;
b.vertex_buffers[MAT_COLOR] = p->color;
b.index_buffer = p->idx; b.index_buffer = p->idx;
b.fs.images[unlit_image_slot(SG_SHADERSTAGE_FS, "diffuse")] = p->mat->diffuse->id; b.fs.images[0] = p->mat->diffuse->id;
b.fs.samplers[unlit_sampler_slot(SG_SHADERSTAGE_FS, "smp")] = tex_sampler; b.fs.samplers[0] = tex_sampler;
return b; return b;
} }
@ -495,9 +493,6 @@ void model_draw_go(model *model, gameobject *go, gameobject *cam)
}); });
*/ */
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vs_p, SG_RANGE_REF(vp.e));
float ambient[4] = {1.0,1.0,1.0,1.0};
sg_apply_uniforms(SG_SHADERSTAGE_FS, SLOT_lightf, SG_RANGE_REF(ambient));
for (int i = 0; i < arrlen(model->meshes); i++) { for (int i = 0; i < arrlen(model->meshes); i++) {
HMM_Mat4 mod = *model->meshes[i].m; HMM_Mat4 mod = *model->meshes[i].m;
mod = HMM_MulM4(mod, gom); mod = HMM_MulM4(mod, gom);
@ -505,10 +500,6 @@ void model_draw_go(model *model, gameobject *go, gameobject *cam)
for (int j = 0; j < arrlen(msh.primitives); j++) { for (int j = 0; j < arrlen(msh.primitives); j++) {
sg_bindings b = primitive_bind(msh.primitives+j); sg_bindings b = primitive_bind(msh.primitives+j);
sg_apply_bindings(&b); sg_apply_bindings(&b);
sg_apply_uniforms(SG_SHADERSTAGE_VS, SLOT_vmodel, &(sg_range){
.ptr = mod.em,
.size = sizeof(mod)
});
sg_draw(0, msh.primitives[j].idx_count, 1); sg_draw(0, msh.primitives[j].idx_count, 1);
} }
} }

View file

@ -11,7 +11,6 @@
#include <window.h> #include <window.h>
#include "resources.h" #include "resources.h"
#include "debugdraw.h" #include "debugdraw.h"
#include "text.sglsl.h"
#include "render.h" #include "render.h"
#include "stb_image_write.h" #include "stb_image_write.h"
@ -22,9 +21,7 @@
struct sFont *use_font; struct sFont *use_font;
static sg_shader fontshader;
static sg_bindings bind_text; static sg_bindings bind_text;
static sg_pipeline pipe_text;
struct text_vert { struct text_vert {
struct draw_p pos; struct draw_p pos;
struct draw_p wh; struct draw_p wh;
@ -36,26 +33,6 @@ struct text_vert {
static struct text_vert *text_buffer; static struct text_vert *text_buffer;
void font_init() { void font_init() {
fontshader = sg_make_shader(text_shader_desc(sg_query_backend()));
pipe_text = sg_make_pipeline(&(sg_pipeline_desc){
.shader = fontshader,
.layout = {
.attrs = {
[ATTR_vs_vert].format = SG_VERTEXFORMAT_FLOAT2,
[ATTR_vs_vert].buffer_index = 1,
[ATTR_vs_pos].format = SG_VERTEXFORMAT_FLOAT2,
[ATTR_vs_wh].format = SG_VERTEXFORMAT_FLOAT2,
[ATTR_vs_uv].format = SG_VERTEXFORMAT_USHORT2N,
[ATTR_vs_st].format = SG_VERTEXFORMAT_USHORT2N,
[ATTR_vs_vColor].format = SG_VERTEXFORMAT_UBYTE4N,
},
.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE
},
.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP,
.colors[0].blend = blend_trans,
.label = "text",
});
bind_text.vertex_buffers[1] = sprite_quad; bind_text.vertex_buffers[1] = sprite_quad;
bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ bind_text.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
@ -201,7 +178,7 @@ void draw_char_box(struct Character c, HMM_Vec2 cursor, float scale, struct rgba
draw_box(b, wh, color); draw_box(b, wh, color);
} }
void text_flush(HMM_Mat4 *proj) { void text_flush() {
if (arrlen(text_buffer) == 0) return; if (arrlen(text_buffer) == 0) return;
sg_range verts; sg_range verts;
@ -217,9 +194,7 @@ void text_flush(HMM_Mat4 *proj) {
sg_append_buffer(bind_text.vertex_buffers[0], &verts); sg_append_buffer(bind_text.vertex_buffers[0], &verts);
sg_apply_pipeline(pipe_text);
sg_apply_bindings(&bind_text); sg_apply_bindings(&bind_text);
sg_apply_uniforms(SG_SHADERSTAGE_VS, 0, SG_RANGE_REF(*proj));
sg_draw(0, 4, arrlen(text_buffer)); sg_draw(0, 4, arrlen(text_buffer));
arrsetlen(text_buffer, 0); arrsetlen(text_buffer, 0);
} }
@ -377,7 +352,7 @@ int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, f
if (*wordstart == '\e') if (*wordstart == '\e')
wordstart = esc_color(wordstart, &usecolor, color); wordstart = esc_color(wordstart, &usecolor, color);
sdrawCharacter(use_font->Characters[*wordstart], HMM_AddV2(cursor, HMM_MulV2F((HMM_Vec2){1,-1},scale)), scale, (rgba){0,0,0,255}); //sdrawCharacter(use_font->Characters[*wordstart], HMM_AddV2(cursor, HMM_MulV2F((HMM_Vec2){1,-1},scale)), scale, (rgba){0,0,0,255});
sdrawCharacter(use_font->Characters[*wordstart], cursor, scale, usecolor); sdrawCharacter(use_font->Characters[*wordstart], cursor, scale, usecolor);
cursor.X += use_font->Characters[*wordstart].Advance * tracking * scale; cursor.X += use_font->Characters[*wordstart].Advance * tracking * scale;

View file

@ -40,7 +40,6 @@ void text_settype(struct sFont *font);
struct boundingbox text_bb(const char *text, float scale, float lw, float tracking); struct boundingbox text_bb(const char *text, float scale, float lw, float tracking);
int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking); int renderText(const char *text, HMM_Vec2 pos, float scale, struct rgba color, float lw, int caret, float tracking);
// void text_frame(); void text_flush();
void text_flush(HMM_Mat4 *proj);
#endif #endif

View file

@ -625,7 +625,10 @@ JSC_CCALL(render_line3d,
); );
JSC_CCALL(render_emitters, emitters_draw(&useproj)) JSC_CCALL(render_emitters, emitters_draw(&useproj))
JSC_CCALL(render_flush, debug_flush(&useproj); text_flush(&useproj); ) JSC_CCALL(render_flush, debug_flush(&useproj); )
JSC_CCALL(render_flushtext, text_flush())
JSC_CCALL(render_end_pass, JSC_CCALL(render_end_pass,
sg_end_pass(); sg_end_pass();
sg_commit(); sg_commit();
@ -737,6 +740,27 @@ JSC_CCALL(render_pipeline3d,
return number2js(pipe.id); return number2js(pipe.id);
) )
JSC_CCALL(render_pipelinetext,
sg_shader fontshader = js2shader(argv[0]);
sg_pipeline_desc p = {0};
p.shader = fontshader;
sg_vertex_layout_state st = {0};
st.attrs[0].format = SG_VERTEXFORMAT_FLOAT2;
st.attrs[0].buffer_index = 1;
st.attrs[1].format = SG_VERTEXFORMAT_FLOAT2;
st.attrs[2].format = SG_VERTEXFORMAT_FLOAT2;
st.attrs[3].format = SG_VERTEXFORMAT_USHORT2N;
st.attrs[4].format = SG_VERTEXFORMAT_USHORT2N;
st.attrs[5].format = SG_VERTEXFORMAT_UBYTE4N;
st.buffers[0].step_func = SG_VERTEXSTEP_PER_INSTANCE;
p.layout = st;
p.primitive_type = SG_PRIMITIVETYPE_TRIANGLE_STRIP;
p.colors[0].blend = blend_trans;
sg_pipeline pipe = sg_make_pipeline(&p);
return number2js(pipe.id);
)
JSC_CCALL(render_pipeline, JSC_CCALL(render_pipeline,
sg_shader sgshader = js2shader(argv[0]); sg_shader sgshader = js2shader(argv[0]);
@ -801,8 +825,8 @@ JSC_CCALL(render_spdraw,
sg_bindings bind = {0}; sg_bindings bind = {0};
bind.vertex_buffers[0] = sprite_quad; bind.vertex_buffers[0] = sprite_quad;
for (int i = 0; i < js_arrlen(argv[1]); i++) { for (int i = 0; i < js_arrlen(argv[0]); i++) {
bind.fs.images[i] = js2texture(js_getpropidx(argv[1], i))->id; bind.fs.images[i] = js2texture(js_getpropidx(argv[0], i))->id;
bind.fs.samplers[i] = std_sampler; bind.fs.samplers[i] = std_sampler;
} }
@ -824,6 +848,7 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, line, 3), MIST_FUNC_DEF(render, line, 3),
MIST_FUNC_DEF(render, line3d, 2), MIST_FUNC_DEF(render, line3d, 2),
MIST_FUNC_DEF(render, emitters, 0), MIST_FUNC_DEF(render, emitters, 0),
MIST_FUNC_DEF(render, flushtext, 0),
MIST_FUNC_DEF(render, flush, 0), MIST_FUNC_DEF(render, flush, 0),
MIST_FUNC_DEF(render, end_pass, 0), MIST_FUNC_DEF(render, end_pass, 0),
MIST_FUNC_DEF(render, text_size, 3), MIST_FUNC_DEF(render, text_size, 3),
@ -832,6 +857,7 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, clear_color, 1), MIST_FUNC_DEF(render, clear_color, 1),
MIST_FUNC_DEF(render, pipeline, 1), MIST_FUNC_DEF(render, pipeline, 1),
MIST_FUNC_DEF(render, pipeline3d, 1), MIST_FUNC_DEF(render, pipeline3d, 1),
MIST_FUNC_DEF(render, pipelinetext, 1),
MIST_FUNC_DEF(render, setuniv3, 2), MIST_FUNC_DEF(render, setuniv3, 2),
MIST_FUNC_DEF(render, setuniv, 2), MIST_FUNC_DEF(render, setuniv, 2),
MIST_FUNC_DEF(render, spdraw, 2), MIST_FUNC_DEF(render, spdraw, 2),

View file

@ -295,6 +295,9 @@ void render_init() {
1, 1, 1, 1 1, 1, 1, 1
}; };
sg_limits ll = sg_query_limits();
printf("attribute limits %d\n", ll.max_vertex_attrs);
sg_gif.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){ sg_gif.bind.vertex_buffers[0] = sg_make_buffer(&(sg_buffer_desc){
.size = sizeof(gif_quad), .size = sizeof(gif_quad),
.data = gif_quad, .data = gif_quad,

View file

@ -1,42 +0,0 @@
@vs vs
in vec2 vert;
in vec2 pos;
in vec2 wh;
in vec2 uv;
in vec2 st;
in vec4 vColor;
out vec2 TexCoords;
out vec4 fColor;
uniform vs_params { mat4 projection; };
void main()
{
gl_Position = projection * vec4(pos + (vert * wh), 0.0, 1.0);
TexCoords = uv + vec2(vert.x*st.x, st.y - vert.y*st.y);
fColor = vColor;
}
@end
@fs fs
in vec2 TexCoords;
in vec4 fColor;
out vec4 color;
uniform texture2D text;
uniform sampler smp;
void main()
{
float lettera = texture(sampler2D(text,smp),TexCoords).r;
if (lettera < 0.1f) discard;
color = fColor;
}
@end
@program text vs fs