Repository navigation
async compute queue system #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc49526
game frame limiter
dariormt 3208aee
compute queue
dariormt 4c2bf7f
Merge remote-tracking branch 'origin/dev' into feature/working-dario
dariormt df2d85c
tidi
dariormt 4452246
tidi
dariormt 33182d9
fuck off, just pass already
dariormt 5952160
optimized voxel volume
dariormt fa0d8f0
Merge remote-tracking branch 'origin/dev' into feature/working-dario
dariormt d6700a2
Tons of fixes, async compute docs and also compute meshed voxels?
dariormt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Mirrors DebrisSimulation::Params | ||
| struct Params | ||
| { | ||
| float dt; | ||
| uint count; | ||
| }; | ||
|
|
||
| [[vk::push_constant]] | ||
| Params gParams; | ||
|
|
||
| // xyz position w radius | ||
| [[vk::binding(0, 0)]] | ||
| StructuredBuffer<float4> gPrevious; | ||
|
|
||
| [[vk::binding(1, 0)]] | ||
| RWStructuredBuffer<float4> gCurrent; | ||
|
|
||
| // xyz velocity w unused | ||
| [[vk::binding(2, 0)]] | ||
| RWStructuredBuffer<float4> gVelocities; | ||
|
|
||
| static const float3 kGravity = float3(0.0, -9.81, 0.0); | ||
|
|
||
| [shader("compute")] | ||
| [numthreads(64, 1, 1)] | ||
| void integrateMain(uint3 id : SV_DispatchThreadID) | ||
| { | ||
| const uint i = id.x; | ||
| if (i >= gParams.count) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| float4 particle = gPrevious[i]; | ||
| float3 velocity = gVelocities[i].xyz + kGravity * gParams.dt; | ||
| particle.xyz += velocity * gParams.dt; | ||
|
|
||
| // Ground plane bounce so the example stays on screen | ||
| if (particle.y < particle.w) | ||
| { | ||
| particle.y = particle.w; | ||
| velocity.y = -velocity.y * 0.4; | ||
| velocity.xz *= 0.8; | ||
| } | ||
|
|
||
| gCurrent[i] = particle; | ||
| gVelocities[i] = float4(velocity, 0.0); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /// @file debris_simulation.cpp | ||
| /// @author dario | ||
| /// @date 10/10/2026 | ||
|
|
||
| #include "debris_simulation.hpp" | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| namespace example { | ||
|
|
||
| namespace { | ||
| constexpr vk::DeviceSize k_particle_bytes = sizeof(glm::vec4) * DebrisSimulation::k_max_particles; | ||
| constexpr uint32_t k_workgroup_size = 64; | ||
| } | ||
|
|
||
| DebrisSimulation::DebrisSimulation() | ||
| : AsyncComputeTask({.shader_uri = "core://shaders/debris.slang", .entries = {"integrateMain"}, .debug_name = "Debris"}) { | ||
| // The renderer draws positions and gameplay reads them back. Velocities never leave the task | ||
| m_positions = addRingBuffer(k_particle_bytes, true); | ||
| m_velocities = addPersistentBuffer(k_particle_bytes); | ||
|
|
||
| // Mirrors the bindings in debris.slang | ||
| bind(0, m_positions, Copy::previous); | ||
| bind(1, m_positions, Copy::current); | ||
| bind(2, m_velocities); | ||
| } | ||
|
|
||
| void DebrisSimulation::spawn(std::span<const glm::vec4> positions, std::span<const glm::vec4> velocities) { | ||
| m_count = static_cast<uint32_t>(std::min({positions.size(), velocities.size(), size_t {k_max_particles}})); | ||
| upload(m_positions, positions.first(m_count)); | ||
| upload(m_velocities, velocities.first(m_count)); | ||
| } | ||
|
|
||
| void DebrisSimulation::step(float dt) { | ||
| if (m_count == 0) { | ||
| return; | ||
| } | ||
| const Params params {.dt = dt, .count = m_count}; | ||
| AsyncComputeTask::step([&](StepContext& ctx) { ctx.dispatch((m_count + k_workgroup_size - 1) / k_workgroup_size, params); }); | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /// @file debris_simulation.hpp | ||
| /// @author dario | ||
| /// @date 10/10/2026 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <glm/glm.hpp> | ||
| #include <span> | ||
| #include <toast/renderer/async_compute_task.hpp> | ||
|
|
||
| namespace example { | ||
|
|
||
| /// Debris particles integrated on the async compute queue | ||
| class DebrisSimulation : public renderer::AsyncComputeTask { | ||
| public: | ||
| static constexpr uint32_t k_max_particles = 65536; | ||
|
|
||
| DebrisSimulation(); | ||
|
|
||
| /// Physics thread before the first step | ||
| void spawn(std::span<const glm::vec4> positions, std::span<const glm::vec4> velocities); | ||
|
|
||
| /// Physics thread. Returns right away | ||
| void step(float dt); | ||
|
|
||
| /// Physics thread. Newest finished positions and false until one exists | ||
| auto readPositions(std::span<glm::vec4> out) -> bool { | ||
| return read(m_positions, out); | ||
| } | ||
|
|
||
| /// For the render pass. xyz position w radius | ||
| [[nodiscard]] | ||
| auto positionsId() const -> BufferId { | ||
| return m_positions; | ||
| } | ||
|
|
||
| private: | ||
| /// Mirrors debris.slang Params | ||
| struct Params { | ||
| float dt; | ||
| uint32_t count; | ||
| }; | ||
|
|
||
| BufferId m_positions = 0; | ||
| BufferId m_velocities = 0; | ||
| uint32_t m_count = 0; | ||
| }; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Mirrors renderer/depth_convention.hpp through TOAST_REVERSED_Z | ||
| // Shadow maps keep standard depth and do not use these | ||
|
|
||
| // The depth buffer clears to it | ||
| public static const float kFarDepth = TOAST_REVERSED_Z ? 0.0 : 1.0; | ||
|
|
||
| public static const float kNearDepth = TOAST_REVERSED_Z ? 1.0 : 0.0; | ||
|
|
||
| // Sky pixels | ||
| public bool isFarDepth(float depth) | ||
| { | ||
| #if TOAST_REVERSED_Z | ||
| return depth <= 0.0; | ||
| #else | ||
| return depth >= 1.0; | ||
| #endif | ||
| } | ||
|
|
||
| public bool isCloserDepth(float a, float b) | ||
| { | ||
| #if TOAST_REVERSED_Z | ||
| return a > b; | ||
| #else | ||
| return a < b; | ||
| #endif | ||
| } | ||
|
|
||
| public float closerDepth(float a, float b) | ||
| { | ||
| #if TOAST_REVERSED_Z | ||
| return max(a, b); | ||
| #else | ||
| return min(a, b); | ||
| #endif | ||
| } | ||
|
|
||
| public float fartherDepth(float a, float b) | ||
| { | ||
| #if TOAST_REVERSED_Z | ||
| return min(a, b); | ||
| #else | ||
| return max(a, b); | ||
| #endif | ||
| } | ||
|
|
||
| // Clip space distance from the near plane and >= 0 on or beyond it | ||
| public float clipNearDistance(float4 clip) | ||
| { | ||
| #if TOAST_REVERSED_Z | ||
| return clip.w - clip.z; | ||
| #else | ||
| return clip.z; | ||
| #endif | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| uid = "olYeZGySqTs" | ||
| type = "slang_module" | ||
| created_at = "2026-10-09T17:15:00.0000000Z" | ||
| modified_at = "2026-10-09T17:15:00.0000000Z" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.