particle render speedup

This commit is contained in:
John Alanbrook 2024-09-30 11:26:56 -05:00
parent b3b3aa9803
commit 3717e84fe7
3 changed files with 52 additions and 8 deletions

View file

@ -7,13 +7,16 @@ emitter.spawn_timer = 0;
emitter.pps = 0; emitter.pps = 0;
emitter.color = Color.white; emitter.color = Color.white;
emitter.draw = function () { var ssbo;
var drawnum = 0;
emitter.draw = function (arr) {
var pars = Object.values(this.particles); var pars = Object.values(this.particles);
if (pars.length === 0) return; if (pars.length === 0) return;
render.use_shader(this.shader); // render.make_particle_ssbo(pars, ssbo);
render.use_mat(this); for (var i of pars)
render.make_particle_ssbo(pars, this.ssbo); arr.push(i);
render.draw(this.shape, this.ssbo, pars.length); // drawnum += pars.length;
}; };
emitter.kill = function () { emitter.kill = function () {
@ -108,7 +111,44 @@ function update_emitters(dt) {
} }
function draw_emitters() { function draw_emitters() {
for (var e of emitters) e.draw(); ssbo ??= render.make_textssbo();
render.use_shader("shaders/baseparticle.cg");
var buckets = new Map();
var base = 0;
for (var e of emitters) {
var bucket = buckets.get(e.diffuse);
if (!bucket)
buckets.set(e.diffuse, [e]);
else
bucket.push(e);
}
for (var bucket of buckets.values()) {
drawnum = 0;
var append = [];
bucket[0].baseinstance = base;
render.use_mat(bucket[0]);
for (var e of bucket)
e.draw(append);
render.make_particle_ssbo(append, ssbo);
render.draw(bucket[0].shape, ssbo, append.length);
base += append.length;
}
// for (var e of emitters) e.draw();
// render.draw(shape.centered_quad, ssbo, drawnum);
} }
return { make_emitter, update_emitters, draw_emitters }; function stat_emitters()
{
var stat = {};
stat.emitters = emitters.length;
var particles = 0;
for (var e of emitters) particles += Object.values(e.particles).length;
stat.particles = particles
return stat;
}
return { make_emitter, update_emitters, draw_emitters, stat_emitters };

View file

@ -352,6 +352,9 @@ var get_snapshot = function()
snap.mallinfo ??= {}; snap.mallinfo ??= {};
Object.assign(snap.mallinfo, os.mallinfo()); Object.assign(snap.mallinfo, os.mallinfo());
snap.particles ??= {};
Object.assign(snap.particles, stat_emitters());
snap.obj ??= {}; snap.obj ??= {};
for (var i of classes) { for (var i of classes) {
var proto = globalThis[`${i}_proto`]; var proto = globalThis[`${i}_proto`];

View file

@ -20,10 +20,11 @@ out vec2 uv;
out vec4 color0; out vec4 color0;
uniform mat4 vp; uniform mat4 vp;
uniform float baseinstance;
void main() void main()
{ {
particle p = par[gl_InstanceIndex]; particle p = par[int(baseinstance)+gl_InstanceIndex];
gl_Position = vp * p.model * vec4(a_pos, 0.0, 1.0); gl_Position = vp * p.model * vec4(a_pos, 0.0, 1.0);
uv = a_uv; uv = a_uv;
color0 = p.color; color0 = p.color;