{examples,Audio}: optimize needless setComponent calls away

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-05-07 22:15:34 -07:00 committed by Stephen Gutekanst
parent 57767c2f9f
commit 6feaad630e
5 changed files with 19 additions and 15 deletions

View file

@ -162,10 +162,10 @@ fn tick(
var q = try entities.query(.{
.ids = mach.Entities.Mod.read(.id),
.followers = Mod.read(.follower),
.positions = Renderer.Mod.read(.position),
.positions = Renderer.Mod.write(.position),
});
while (q.next()) |v| {
for (v.ids, v.positions) |id, position| {
for (v.ids, v.positions) |id, *position| {
// Nested query to find all the other follower entities that we should move away from.
// We will avoid all other follower entities if we're too close to them.
// This is not very efficient, but it works!
@ -204,7 +204,7 @@ fn tick(
new_position = new_position.lerp(&vec3(0, 0, 0), move_speed / avoidance_div);
// Finally, update our entity position.
try renderer.set(id, .position, new_position);
position.* = new_position;
}
}

View file

@ -135,37 +135,36 @@ fn audioTick(entities: *mach.Entities.Mod, audio: *Mod) !void {
var did_state_change = false;
var q = try entities.query(.{
.ids = mach.Entities.Mod.read(.id),
.samples_slices = Mod.read(.samples),
.channels = Mod.read(.channels),
.playings = Mod.read(.playing),
.indexes = Mod.read(.index),
.playings = Mod.write(.playing),
.indexes = Mod.write(.index),
});
while (q.next()) |v| {
for (v.ids, v.samples_slices, v.channels, v.playings, v.indexes) |id, samples, channels, playing, index| {
if (!playing) continue;
for (v.samples_slices, v.channels, v.playings, v.indexes) |samples, channels, *playing, *index| {
if (!playing.*) continue;
const channels_diff = player_channels - channels + 1;
const to_read = @min(samples.len - index, mixing_buffer.items.len) / channels_diff;
const to_read = @min(samples.len - index.*, mixing_buffer.items.len) / channels_diff;
if (channels == 1 and player_channels > 1) {
// Duplicate samples for mono sounds
var i: usize = 0;
for (samples[index..][0..to_read]) |sample| {
for (samples[index.*..][0..to_read]) |sample| {
mixSamplesDuplicate(mixing_buffer.items[i..][0..player_channels], sample);
i += player_channels;
}
} else {
mixSamples(mixing_buffer.items[0..to_read], samples[index..][0..to_read]);
mixSamples(mixing_buffer.items[0..to_read], samples[index.*..][0..to_read]);
}
if (index + to_read >= samples.len) {
if (index.* + to_read >= samples.len) {
// No longer playing, we've read all samples
did_state_change = true;
try audio.set(id, .playing, false);
try audio.set(id, .index, 0);
playing.* = false;
index.* = 0;
continue;
}
try audio.set(id, .index, index + to_read);
index.* = index.* + to_read;
}
}
if (audio.state().on_state_change) |on_state_change_event| {

View file

@ -96,6 +96,7 @@ fn updatePipeline(
}
}
// TODO: optimize by removing this component set call and instead use a .write() query
try sprite_pipeline.set(pipeline_id, .num_sprites, num_sprites);
if (num_sprites > 0) {
encoder.writeBuffer(built.transforms, 0, gfx.SpritePipeline.cp_transforms[0..i]);

View file

@ -155,6 +155,8 @@ fn buildPipeline(
pipeline_id: mach.EntityID,
texture: *gpu.Texture,
) !void {
// TODO: optimize by removing the component get/set calls in this function where possible
// and instead use .write() queries
const opt_texture2 = sprite_pipeline.get(pipeline_id, .texture2);
const opt_texture3 = sprite_pipeline.get(pipeline_id, .texture3);
const opt_texture4 = sprite_pipeline.get(pipeline_id, .texture4);

View file

@ -179,6 +179,8 @@ fn buildPipeline(
text_pipeline: *Mod,
pipeline_id: mach.EntityID,
) !void {
// TODO: optimize by removing the component get/set calls in this function where possible
// and instead use .write() queries
const opt_shader = text_pipeline.get(pipeline_id, .shader);
const opt_texture_sampler = text_pipeline.get(pipeline_id, .texture_sampler);
const opt_blend_state = text_pipeline.get(pipeline_id, .blend_state);