sprite ssbo batch render

This commit is contained in:
John Alanbrook 2024-07-22 19:07:02 -05:00
parent d4b057dc6f
commit 9d51858266
7 changed files with 107 additions and 6 deletions

View file

@ -149,6 +149,7 @@ sprite.inputs.kp1 = function() { this.setanchor("ul"); }
component.sprite = function(obj) {
var sp = Object.create(sprite);
sp.gameobject = obj;
sp.transform = obj.transform;
sp.guid = prosperon.guid();
allsprites[sp.guid] = sp;
return sp;

View file

@ -358,6 +358,7 @@ function process() {
}
var st = profile.now();
prosperon.window_render(window.size);
profile.addreport(null, "window render", st);
prosperon.render();
profile.addreport(profcache, "render frame", st);
frames.push(profile.secs(profile.now() - startframe));

View file

@ -24,6 +24,7 @@ function unique_name(list, name = "new_object") {
};
var entity = {
drawlayer: -1,
get_comp_by_name(name) {
var comps = [];
for (var c of Object.values(this.components))

View file

@ -13,14 +13,13 @@ emitter.draw = function()
if (amt === 0) return;
render.use_shader(this.shader);
render.use_mat(this);
var ts = [];
for (var p of Object.values(this.particles)) ts.push([p.transform,p.color]);
render.make_particle_ssbo(ts, this.ssbo);
render.make_particle_ssbo(Object.values(this.particles), this.ssbo);
render.draw(this.shape, this.ssbo, amt);
}
var std_spawn = function(par)
{
}
var std_step = function(p)

View file

@ -8,6 +8,8 @@ var cur = {};
render.use_shader = function(shader)
{
if (typeof shader === 'string')
shader = render.make_shader(shader);
if (cur.shader === shader) return;
cur.shader = shader;
cur.globals = {};
@ -435,10 +437,12 @@ var circleshader;
var polyshader;
var slice9shader;
var parshader;
var spritessboshader;
render.init = function() {
textshader = render.make_shader("shaders/text_base.cg");
render.spriteshader = render.make_shader("shaders/sprite.cg");
spritessboshader = render.make_shader("shaders/sprite_ssbo.cg");
render.postshader = render.make_shader("shaders/simplepost.cg");
slice9shader = render.make_shader("shaders/9slice.cg");
circleshader = render.make_shader("shaders/circle.cg");

60
shaders/sprite_ssbo.cg Normal file
View file

@ -0,0 +1,60 @@
#blend mix
#depth off
#primitive triangle
#cull none
@vs vs
in vec3 a_pos;
in vec2 a_uv;
out vec2 uv;
vec3 pos;
struct sprite {
mat4 model;
vec4 rect;
};
readonly buffer ssbo {
sprite sprites[];
};
uniform mat4 projection;
uniform mat4 view;
uniform mat4 vp;
uniform vec2 diffuse_size;
void main()
{
sprite s = sprites[gl_InstanceIndex];
pos = a_pos;
uv = a_uv;
pos *= vec3(diffuse_size * s.rect.zw,1);
uv = (uv*s.rect.zw)+s.rect.xy;
gl_Position = vp * s.model * vec4(pos, 1.0);
}
@end
@fs fs
in vec2 uv;
out vec4 color;
texture2D diffuse;
sampler smp;
uniform vec4 shade;
void frag()
{
color = texture(sampler2D(diffuse,smp), uv);
color *= shade;
}
void main()
{
frag();
}
@end
@program sprite vs fs

View file

@ -963,15 +963,49 @@ JSC_CCALL(render_make_particle_ssbo,
for (int i = 0; i < js_arrlen(array); i++) {
JSValue sub = js_getpropidx(array,i);
ms[i].model = transform2mat(*js2transform(js_getpropidx(sub, 0)));
ms[i].color = js2vec4(js_getpropidx(sub,1));
ms[i].model = transform2mat(*js2transform(js_getpropstr(sub, "transform")));
ms[i].color = js2vec4(js_getpropstr(sub,"color"));
}
sg_append_buffer(*b, (&(sg_range){
.ptr = ms,
.size = size
}));
)
typedef struct sprite_ss {
HMM_Mat4 model;
HMM_Vec4 rect;
} sprite_ss;
JSC_CCALL(render_make_sprite_ssbo,
JSValue array = argv[0];
size_t size = js_arrlen(array)*(sizeof(sprite_ss));
sg_buffer *b = js2sg_buffer(argv[1]);
if (!b) return JS_UNDEFINED;
sprite_ss ms[js_arrlen(array)];
if (sg_query_buffer_will_overflow(*b, size)) {
sg_destroy_buffer(*b);
*b = sg_make_buffer(&(sg_buffer_desc){
.type = SG_BUFFERTYPE_STORAGEBUFFER,
.size = size,
.usage = SG_USAGE_STREAM,
.label = "transform buffer"
});
}
for (int i = 0; i < js_arrlen(array); i++) {
JSValue sub = js_getpropidx(array,i);
ms[i].model = transform2mat(*js2transform(js_getpropstr(sub, "transform")));
ms[i].rect = js2vec4(js_getpropstr(sub,"rect"));
}
sg_append_buffer(*b, (&(sg_range){
.ptr = ms,
.size = size
}));
)
JSC_CCALL(render_make_t_ssbo,
@ -1051,7 +1085,8 @@ static const JSCFunctionListEntry js_render_funcs[] = {
MIST_FUNC_DEF(render, imgui_end, 0),
MIST_FUNC_DEF(render, imgui_init, 0),
MIST_FUNC_DEF(render, make_t_ssbo, 2),
MIST_FUNC_DEF(render, make_particle_ssbo, 2)
MIST_FUNC_DEF(render, make_particle_ssbo, 2),
MIST_FUNC_DEF(render, make_sprite_ssbo, 2)
};
JSC_CCALL(gui_scissor, sg_apply_scissor_rect(js2number(argv[0]), js2number(argv[1]), js2number(argv[2]), js2number(argv[3]), 0))