Still dont know

This commit is contained in:
Dynamitos committed 2026-09-03 20:08:45 +02:00
1 parent bedeb8f059
commit b1004d5c80
11 files changed
+334 -265

No files matched your search

+6
View File
@@ -1,8 +1,13 @@
#pragma once
#include "Containers/Array.h"
#include "Math/Vector.h"
namespace Seele {
namespace Component {
struct BoundaryPlane {
Vector normal; // inward normal (from solid toward fluid)
float d; // plane offset: dot(normal, pos) + d < 0 means solid
};
struct FluidGrid {
bool paused = true;
float dt = 0.1f;
@@ -11,6 +16,7 @@ struct FluidGrid {
UVector gridSize = {128, 128, 128};
Vector gravity = {0.0f, -9.81f, 0.0f};
bool enableGravity = true;
Array<BoundaryPlane> extraPlanes;
};
} // namespace Component
} // namespace Seele
@@ -13,12 +13,14 @@
using namespace Seele;
// Reinitialization is in pseudo-time; keep it gentle to avoid eroding the interface each frame.
constexpr static int reinitInterval = 20;
constexpr static int reinitIterations = 1;
constexpr static float reinitDtau = 0.05f;
constexpr static int reinitInterval = 5;
constexpr static int reinitIterations = 5;
constexpr static float reinitDtau = 0.5f;
constexpr static int velocityDiffuseIterations = 8;
constexpr static int pressureSolveIterations = 24;
constexpr static int pressureSolveIterations = 100;
constexpr static int densityDiffuseIterations = 8;
constexpr static float pressureSORomega = 1.95f;
constexpr static float diffusionSORomega = 1.0f;
#define SWAP(a, b) \
{ \
@@ -41,6 +43,14 @@ SimulationComputePass::SimulationComputePass(Gfx::PGraphics graphics, PScene sce
.name = "b",
.uniformLength = sizeof(int),
});
setBounds.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "numPlanes",
.uniformLength = sizeof(uint32),
});
setBounds.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "planes",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
setBounds.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "grid",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
@@ -51,29 +61,27 @@ SimulationComputePass::SimulationComputePass(Gfx::PGraphics graphics, PScene sce
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "SetBound",
.modules = {"SetBound"},
.entryPoints = {{"setBound", "SetBound"}, {"setBoundEdges", "SetBound"}, {"setBoundCorners", "SetBound"}},
.entryPoints = {{"setBound", "SetBound"}},
.rootSignature = setBounds.pipelineLayout,
});
setBounds.pipelineLayout->create();
setBounds.shader = graphics->createComputeShader({0});
setBounds.shaderEdges = graphics->createComputeShader({1});
setBounds.shaderCorners = graphics->createComputeShader({2});
setBounds.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = setBounds.shader,
.pipelineLayout = setBounds.pipelineLayout,
});
setBounds.pipelineEdges = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = setBounds.shaderEdges,
.pipelineLayout = setBounds.pipelineLayout,
});
setBounds.pipelineCorners = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = setBounds.shaderCorners,
.pipelineLayout = setBounds.pipelineLayout,
});
}
{
linearSolve.pipelineLayout = graphics->createPipelineLayout("LinearSolvePipelineLayout");
linearSolve.descriptorLayout = graphics->createDescriptorLayout("params");
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "grid",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "grid0",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "a",
.uniformLength = sizeof(float),
@@ -83,16 +91,12 @@ SimulationComputePass::SimulationComputePass(Gfx::PGraphics graphics, PScene sce
.uniformLength = sizeof(float),
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "next",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.name = "omega",
.uniformLength = sizeof(float),
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "current",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "grid0",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.name = "color",
.uniformLength = sizeof(int),
});
linearSolve.descriptorLayout->create();
linearSolve.pipelineLayout->addDescriptorLayout(linearSolve.descriptorLayout);
@@ -241,7 +245,11 @@ SimulationComputePass::SimulationComputePass(Gfx::PGraphics graphics, PScene sce
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
reinitialize.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "phi0",
.name = "phiPrev",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
reinitialize.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "phiSign",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
reinitialize.descriptorLayout->create();
@@ -326,17 +334,13 @@ void SimulationComputePass::render() {
for (int iteration = 0; iteration < velocityDiffuseIterations; ++iteration) {
float a = data.dt * data.viscosity * (data.gridSize.x - 2) * (data.gridSize.x - 2);
float c = 1 + 6 * a;
linearSolvePass(data.velocityXNextBuffer, data.velocityXBuffer, data.velocityX0Buffer, a, c, data.gridSize);
linearSolvePass(data.velocityYNextBuffer, data.velocityYBuffer, data.velocityY0Buffer, a, c, data.gridSize);
linearSolvePass(data.velocityZNextBuffer, data.velocityZBuffer, data.velocityZ0Buffer, a, c, data.gridSize);
linearSolvePass(data.velocityXBuffer, data.velocityX0Buffer, a, c, diffusionSORomega, data.gridSize);
linearSolvePass(data.velocityYBuffer, data.velocityY0Buffer, a, c, diffusionSORomega, data.gridSize);
linearSolvePass(data.velocityZBuffer, data.velocityZ0Buffer, a, c, diffusionSORomega, data.gridSize);
setBoundsPass(data.velocityXNextBuffer, 1, data.gridSize);
setBoundsPass(data.velocityYNextBuffer, 2, data.gridSize);
setBoundsPass(data.velocityZNextBuffer, 3, data.gridSize);
SWAP(data.velocityXBuffer, data.velocityXNextBuffer);
SWAP(data.velocityYBuffer, data.velocityYNextBuffer);
SWAP(data.velocityZBuffer, data.velocityZNextBuffer);
setBoundsPass(data.velocityXBuffer, 1, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityYBuffer, 2, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityZBuffer, 3, data.gridSize, data.planeBuffer, data.numPlanes);
}
SWAP(data.velocityX0Buffer, data.velocityXBuffer);
@@ -346,29 +350,22 @@ void SimulationComputePass::render() {
graphics->beginDebugRegion("Project");
Gfx::PShaderBuffer divergence = data.velocityXBuffer;
Gfx::PShaderBuffer pressureCurrent = data.velocityYBuffer;
Gfx::PShaderBuffer pressureNext = data.velocityZBuffer;
divergencePass(divergence, pressureCurrent, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, data.gridSize);
Gfx::PShaderBuffer pressure = data.velocityYBuffer;
divergencePass(divergence, pressure, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, data.gridSize);
// Divergence/pressure buffers are aliased to velocity buffers whose boundary
// cells still contain stale velocity data. Zero the boundaries before solving.
setBoundsPass(divergence, 0, data.gridSize);
setBoundsPass(pressureCurrent, 0, data.gridSize);
setBoundsPass(divergence, 0, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(pressure, 0, data.gridSize, data.planeBuffer, data.numPlanes);
for (int iteration = 0; iteration < pressureSolveIterations; ++iteration) {
linearSolvePass(pressureNext, pressureCurrent, divergence, 1, 6, data.gridSize);
setBoundsPass(pressureNext, 0, data.gridSize);
// swap pressure buffers
SWAP(pressureCurrent, pressureNext);
linearSolvePass(pressure, divergence, 1, 6, pressureSORomega, data.gridSize);
setBoundsPass(pressure, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
projectPass(data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, pressureCurrent, data.gridSize);
projectPass(data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, pressure, data.gridSize);
setBoundsPass(data.velocityX0Buffer, 1, data.gridSize);
setBoundsPass(data.velocityY0Buffer, 2, data.gridSize);
setBoundsPass(data.velocityZ0Buffer, 3, data.gridSize);
setBoundsPass(data.velocityX0Buffer, 1, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityY0Buffer, 2, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityZ0Buffer, 3, data.gridSize, data.planeBuffer, data.numPlanes);
graphics->endDebugRegion();
graphics->beginDebugRegion("Advect");
@@ -384,28 +381,26 @@ void SimulationComputePass::render() {
}
// Set boundary conditions after advection/forces so the divergence sees correct wall values
setBoundsPass(data.velocityXBuffer, 1, data.gridSize);
setBoundsPass(data.velocityYBuffer, 2, data.gridSize);
setBoundsPass(data.velocityZBuffer, 3, data.gridSize);
setBoundsPass(data.velocityXBuffer, 1, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityYBuffer, 2, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityZBuffer, 3, data.gridSize, data.planeBuffer, data.numPlanes);
// Re-project after advection/forces to keep the final velocity field divergence-free.
Gfx::PShaderBuffer advectedDivergence = data.velocityXNextBuffer;
Gfx::PShaderBuffer advectedPressureCurrent = data.velocityYNextBuffer;
Gfx::PShaderBuffer advectedPressureNext = data.velocityZNextBuffer;
divergencePass(advectedDivergence, advectedPressureCurrent, data.velocityXBuffer, data.velocityYBuffer, data.velocityZBuffer, data.gridSize);
setBoundsPass(advectedDivergence, 0, data.gridSize);
setBoundsPass(advectedPressureCurrent, 0, data.gridSize);
Gfx::PShaderBuffer advectedPressure = data.velocityYNextBuffer;
divergencePass(advectedDivergence, advectedPressure, data.velocityXBuffer, data.velocityYBuffer, data.velocityZBuffer, data.gridSize);
setBoundsPass(advectedDivergence, 0, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(advectedPressure, 0, data.gridSize, data.planeBuffer, data.numPlanes);
for (int iteration = 0; iteration < pressureSolveIterations; ++iteration) {
linearSolvePass(advectedPressureNext, advectedPressureCurrent, advectedDivergence, 1, 6, data.gridSize);
setBoundsPass(advectedPressureNext, 0, data.gridSize);
SWAP(advectedPressureCurrent, advectedPressureNext);
linearSolvePass(advectedPressure, advectedDivergence, 1, 6, pressureSORomega, data.gridSize);
setBoundsPass(advectedPressure, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
projectPass(data.velocityXBuffer, data.velocityYBuffer, data.velocityZBuffer, advectedPressureCurrent, data.gridSize);
projectPass(data.velocityXBuffer, data.velocityYBuffer, data.velocityZBuffer, advectedPressure, data.gridSize);
setBoundsPass(data.velocityXBuffer, 1, data.gridSize);
setBoundsPass(data.velocityYBuffer, 2, data.gridSize);
setBoundsPass(data.velocityZBuffer, 3, data.gridSize);
setBoundsPass(data.velocityXBuffer, 1, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityYBuffer, 2, data.gridSize, data.planeBuffer, data.numPlanes);
setBoundsPass(data.velocityZBuffer, 3, data.gridSize, data.planeBuffer, data.numPlanes);
graphics->endDebugRegion();
// swap velocity buffers
SWAP(data.velocityX0Buffer, data.velocityXBuffer);
@@ -415,7 +410,6 @@ void SimulationComputePass::render() {
// diffuse density
graphics->beginDebugRegion("DiffuseDensity");
Gfx::PShaderBuffer densityCurrent = data.densityBuffer;
Gfx::PShaderBuffer densityNext = data.scratchBuffer;
Gfx::PShaderBuffer density0 = data.density0Buffer;
graphics->copyBuffer(density0, densityCurrent);
densityCurrent->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
@@ -423,24 +417,23 @@ void SimulationComputePass::render() {
for (int iteration = 0; iteration < densityDiffuseIterations; ++iteration) {
float a = data.dt * data.diffusion * (data.gridSize.x - 2) * (data.gridSize.x - 2);
float c = 1 + 6 * a;
linearSolvePass(densityNext, densityCurrent, density0, a, c, data.gridSize);
setBoundsPass(densityNext, 0, data.gridSize);
SWAP(densityCurrent, densityNext);
linearSolvePass(densityCurrent, density0, a, c, diffusionSORomega, data.gridSize);
setBoundsPass(densityCurrent, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
graphics->endDebugRegion();
graphics->beginDebugRegion("AdvectDensity");
advectPass(data.density0Buffer, densityCurrent, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, data.dt,
data.gridSize);
setBoundsPass(data.density0Buffer, 0, data.gridSize);
setBoundsPass(data.density0Buffer, 0, data.gridSize, data.planeBuffer, data.numPlanes);
graphics->endDebugRegion();
graphics->beginDebugRegion("AdvectLevelSet");
advectPass(data.phiBuffer, data.phi0Buffer, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, data.dt, data.gridSize);
setBoundsPass(data.phiBuffer, 0, data.gridSize);
setBoundsPass(data.phiBuffer, 0, data.gridSize, data.planeBuffer, data.numPlanes);
// Reinitialize level set every N frames to restore |grad phi| = 1
if (data.frameCounter % reinitInterval == 0) {
reinitializeLevelSetPass(data.phiBuffer, data.phi0Buffer, data.gridSize);
reinitializeLevelSetPass(data.phiBuffer, data.phi0Buffer, data.scratchBuffer, data.gridSize, data.planeBuffer, data.numPlanes);
}
++data.frameCounter;
@@ -467,65 +460,46 @@ UVector SimulationComputePass::getDispatchSize(const UVector& gridSize, const UV
};
}
void SimulationComputePass::setBoundsPass(Gfx::PShaderBuffer grid, int b, UVector gridSize) {
void SimulationComputePass::setBoundsPass(Gfx::PShaderBuffer grid, int b, UVector gridSize, Gfx::PShaderBuffer planeBuffer, uint32 numPlanes) {
Gfx::ODescriptorSet bounds = setBounds.descriptorLayout->allocateDescriptorSet();
bounds->updateConstants("b", 0, &b);
bounds->updateConstants("numPlanes", 0, &numPlanes);
bounds->updateBuffer("planes", 0, planeBuffer);
bounds->updateBuffer("grid", 0, grid);
bounds->writeChanges();
Gfx::OComputeCommand boundsCommand = graphics->createComputeCommand();
boundsCommand->bindPipeline(setBounds.pipeline);
boundsCommand->bindDescriptor(bounds);
boundsCommand->bindDescriptor(gridSizeSet);
// Dispatch covers max(gridSize.x, gridSize.y) x max(gridSize.y, gridSize.z) to handle all faces on non-cubic grids
UVector faceDispatchGrid = {std::max(gridSize.x, gridSize.y), std::max(gridSize.y, gridSize.z), 1};
auto dispatchSize = getDispatchSize(faceDispatchGrid, setBounds.threadGroupSize);
boundsCommand->dispatch(dispatchSize.x, dispatchSize.y, 1);
auto dispatchSize = getDispatchSize(gridSize, setBounds.threadGroupSize);
boundsCommand->dispatch(dispatchSize.x, dispatchSize.y, dispatchSize.z);
graphics->executeCommands(std::move(boundsCommand));
grid->pipelineBarrier(Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
Gfx::OComputeCommand edgesCommand = graphics->createComputeCommand();
edgesCommand->bindPipeline(setBounds.pipelineEdges);
edgesCommand->bindDescriptor(bounds);
edgesCommand->bindDescriptor(gridSizeSet);
// Dispatch covers max dimension to handle all 12 edges on non-cubic grids; edge shader uses numthreads(128,1,1)
uint32_t maxDim = std::max({gridSize.x, gridSize.y, gridSize.z});
edgesCommand->dispatch((maxDim + 127) / 128, 1, 1);
graphics->executeCommands(std::move(edgesCommand));
grid->pipelineBarrier(Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
Gfx::OComputeCommand cornersCommand = graphics->createComputeCommand();
cornersCommand->bindPipeline(setBounds.pipelineCorners);
cornersCommand->bindDescriptor(bounds);
cornersCommand->bindDescriptor(gridSizeSet);
cornersCommand->dispatch(1, 1, 1);
graphics->executeCommands(std::move(cornersCommand));
grid->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
}
void SimulationComputePass::linearSolvePass(Gfx::PShaderBuffer next, Gfx::PShaderBuffer current, Gfx::PShaderBuffer grid0, float a, float c,
void SimulationComputePass::linearSolvePass(Gfx::PShaderBuffer grid, Gfx::PShaderBuffer grid0, float a, float c, float omega,
UVector gridSize) {
Gfx::ODescriptorSet solve = linearSolve.descriptorLayout->allocateDescriptorSet();
solve->updateBuffer("next", 0, next);
solve->updateBuffer("current", 0, current);
solve->updateBuffer("grid0", 0, grid0);
solve->updateConstants("a", 0, &a);
solve->updateConstants("c", 0, &c);
solve->writeChanges();
Gfx::OComputeCommand solveCommand = graphics->createComputeCommand();
solveCommand->bindPipeline(linearSolve.pipeline);
solveCommand->bindDescriptor(solve);
solveCommand->bindDescriptor(gridSizeSet);
solveCommand->dispatch(getDispatchSize(gridSize, linearSolve.threadGroupSize));
graphics->executeCommands(std::move(solveCommand));
for (int color = 0; color < 2; ++color) {
Gfx::ODescriptorSet solve = linearSolve.descriptorLayout->allocateDescriptorSet();
solve->updateBuffer("grid", 0, grid);
solve->updateBuffer("grid0", 0, grid0);
solve->updateConstants("a", 0, &a);
solve->updateConstants("c", 0, &c);
solve->updateConstants("omega", 0, &omega);
solve->updateConstants("color", 0, &color);
solve->writeChanges();
Gfx::OComputeCommand solveCommand = graphics->createComputeCommand();
solveCommand->bindPipeline(linearSolve.pipeline);
solveCommand->bindDescriptor(solve);
solveCommand->bindDescriptor(gridSizeSet);
solveCommand->dispatch(getDispatchSize(gridSize, linearSolve.threadGroupSize));
graphics->executeCommands(std::move(solveCommand));
next->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
grid->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
}
}
void SimulationComputePass::divergencePass(Seele::Gfx::PShaderBuffer divergence, Seele::Gfx::PShaderBuffer pressure,
Seele::Gfx::PShaderBuffer velocityX, Seele::Gfx::PShaderBuffer velocityY,
@@ -617,11 +591,15 @@ void SimulationComputePass::applyForcesPass(Gfx::PShaderBuffer velocityX, Gfx::P
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
}
void SimulationComputePass::reinitializeLevelSetPass(Seele::Gfx::PShaderBuffer phi, Seele::Gfx::PShaderBuffer phi0, UVector gridSize) {
void SimulationComputePass::reinitializeLevelSetPass(Seele::Gfx::PShaderBuffer phi, Seele::Gfx::PShaderBuffer phi0, Seele::Gfx::PShaderBuffer phiSign, UVector gridSize, Gfx::PShaderBuffer planeBuffer, uint32 numPlanes) {
graphics->beginDebugRegion("ReinitializeLevelSet");
// Snapshot current phi into phi0 for the sign function
// Snapshot current phi into phiSign for the sign function (preserved across all iterations)
phi->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_TRANSFER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
graphics->copyBuffer(phi, phiSign);
phiSign->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
// Also copy phi into phi0 as the "previous iteration" input for iteration 0
graphics->copyBuffer(phi, phi0);
phi0->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
@@ -633,7 +611,8 @@ void SimulationComputePass::reinitializeLevelSetPass(Seele::Gfx::PShaderBuffer p
Gfx::ODescriptorSet reinitSet = reinitialize.descriptorLayout->allocateDescriptorSet();
reinitSet->updateConstants("dtau", 0, &dtau);
reinitSet->updateBuffer("phi", 0, phi);
reinitSet->updateBuffer("phi0", 0, phi0);
reinitSet->updateBuffer("phiPrev", 0, phi0);
reinitSet->updateBuffer("phiSign", 0, phiSign);
reinitSet->writeChanges();
Gfx::OComputeCommand reinitCommand = graphics->createComputeCommand();
reinitCommand->bindPipeline(reinitialize.pipeline);
@@ -644,8 +623,15 @@ void SimulationComputePass::reinitializeLevelSetPass(Seele::Gfx::PShaderBuffer p
phi->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
setBoundsPass(phi, 0, gridSize);
SWAP(phi, phi0);
setBoundsPass(phi, 0, gridSize, planeBuffer, numPlanes);
// Copy result to phi0 for the next iteration's gradient reads
phi->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_TRANSFER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
graphics->copyBuffer(phi, phi0);
phi0->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
phi->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT | Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
}
graphics->endDebugRegion();
}
@@ -20,25 +20,21 @@ class SimulationComputePass : public RenderPass {
private:
UVector getDispatchSize(const UVector& gridSize, const UVector& threadGroupSize);
void setBoundsPass(Gfx::PShaderBuffer buffer, int b, UVector gridSize);
void linearSolvePass(Gfx::PShaderBuffer next, Seele::Gfx::PShaderBuffer current, Seele::Gfx::PShaderBuffer grid0, float a, float c, UVector gridSize);
void setBoundsPass(Gfx::PShaderBuffer buffer, int b, UVector gridSize, Gfx::PShaderBuffer planeBuffer, uint32 numPlanes);
void linearSolvePass(Gfx::PShaderBuffer grid, Seele::Gfx::PShaderBuffer grid0, float a, float c, float omega, UVector gridSize);
void divergencePass(Gfx::PShaderBuffer divergence, Seele::Gfx::PShaderBuffer pressure, Seele::Gfx::PShaderBuffer velocityX,
Seele::Gfx::PShaderBuffer velocityY, Seele::Gfx::PShaderBuffer velocityZ, UVector gridSize);
void projectPass(Gfx::PShaderBuffer velocityX, Seele::Gfx::PShaderBuffer velocityY, Seele::Gfx::PShaderBuffer velocityZ,
Seele::Gfx::PShaderBuffer pressure, UVector gridSize);
void advectPass(Gfx::PShaderBuffer quantity, Seele::Gfx::PShaderBuffer quantity0, Seele::Gfx::PShaderBuffer velocityX,
Seele::Gfx::PShaderBuffer velocityY, Seele::Gfx::PShaderBuffer velocityZ, float dt, UVector gridSize);
void reinitializeLevelSetPass(Gfx::PShaderBuffer phi, Seele::Gfx::PShaderBuffer phi0, UVector gridSize);
void reinitializeLevelSetPass(Gfx::PShaderBuffer phi, Seele::Gfx::PShaderBuffer phi0, Seele::Gfx::PShaderBuffer phiSign, UVector gridSize, Gfx::PShaderBuffer planeBuffer, uint32 numPlanes);
struct SetBounds {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
Gfx::OComputeShader shaderEdges;
Gfx::PComputePipeline pipelineEdges;
Gfx::OComputeShader shaderCorners;
Gfx::PComputePipeline pipelineCorners;
constexpr static UVector threadGroupSize = {32, 8, 1};
constexpr static UVector threadGroupSize = {4, 4, 4};
} setBounds;
struct LinearSolve {
Gfx::OPipelineLayout pipelineLayout;
@@ -85,6 +81,10 @@ class SimulationComputePass : public RenderPass {
void applyForcesPass(Gfx::PShaderBuffer velocityX, Gfx::PShaderBuffer velocityY, Gfx::PShaderBuffer velocityZ, Vector gravity, float dt, UVector gridSize);
Gfx::ODescriptorLayout gridSizeLayout;
Gfx::ODescriptorSet gridSizeSet;
struct BoundaryPlane {
Vector normal;
float d;
};
PScene scene;
};
DEFINE_REF(SimulationComputePass)
+72 -10
View File
@@ -722,6 +722,11 @@ PDestructionManager Graphics::getDestructionManager() { return destructionManage
Array<const char*> Graphics::getRequiredExtensions() {
Array<const char*> extensions;
const auto availableExtensions = enumerateInstanceExtensions();
auto supportsExtension = [&](const char* extensionName) {
return hasInstanceExtension(availableExtensions, extensionName);
};
unsigned int glfwExtensionCount = 0;
const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
@@ -741,22 +746,41 @@ Array<const char*> Graphics::getRequiredExtensions() {
#if defined(GLFW_PLATFORM_WAYLAND) && defined(GLFW_PLATFORM_X11)
const int platform = glfwGetPlatform();
if (platform == GLFW_PLATFORM_WAYLAND) {
extensions.addUnique("VK_KHR_wayland_surface");
if (supportsExtension("VK_KHR_wayland_surface")) {
extensions.addUnique("VK_KHR_wayland_surface");
}
} else if (platform == GLFW_PLATFORM_X11) {
extensions.addUnique("VK_KHR_xlib_surface");
extensions.addUnique("VK_KHR_xcb_surface");
if (supportsExtension("VK_KHR_xlib_surface")) {
extensions.addUnique("VK_KHR_xlib_surface");
}
if (supportsExtension("VK_KHR_xcb_surface")) {
extensions.addUnique("VK_KHR_xcb_surface");
}
} else {
extensions.addUnique("VK_KHR_wayland_surface");
extensions.addUnique("VK_KHR_xlib_surface");
extensions.addUnique("VK_KHR_xcb_surface");
if (supportsExtension("VK_KHR_wayland_surface")) {
extensions.addUnique("VK_KHR_wayland_surface");
}
if (supportsExtension("VK_KHR_xlib_surface")) {
extensions.addUnique("VK_KHR_xlib_surface");
}
if (supportsExtension("VK_KHR_xcb_surface")) {
extensions.addUnique("VK_KHR_xcb_surface");
}
}
#else
bool isWayland = std::getenv("WAYLAND_DISPLAY") != nullptr;
if (isWayland) {
if (isWayland && supportsExtension("VK_KHR_wayland_surface")) {
extensions.addUnique("VK_KHR_wayland_surface");
} else {
extensions.addUnique("VK_KHR_xlib_surface");
extensions.addUnique("VK_KHR_xcb_surface");
if (supportsExtension("VK_KHR_xlib_surface")) {
extensions.addUnique("VK_KHR_xlib_surface");
}
if (supportsExtension("VK_KHR_xcb_surface")) {
extensions.addUnique("VK_KHR_xcb_surface");
}
if (extensions.size() == 1 && supportsExtension("VK_KHR_wayland_surface")) {
extensions.addUnique("VK_KHR_wayland_surface");
}
}
#endif
} else {
@@ -769,7 +793,39 @@ Array<const char*> Graphics::getRequiredExtensions() {
#ifdef ENABLE_VALIDATION
extensions.addUnique(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#endif
return extensions;
Array<const char*> validatedExtensions;
for (uint32 i = 0; i < extensions.size(); ++i) {
const char* extensionName = extensions[i];
if (supportsExtension(extensionName)) {
validatedExtensions.addUnique(extensionName);
} else {
std::cerr << "Warning: Vulkan instance extension not exposed by loader, skipping: " << extensionName << std::endl;
}
}
auto hasValidatedExtension = [&](const char* extensionName) {
for (uint32 i = 0; i < validatedExtensions.size(); ++i) {
if (std::strcmp(validatedExtensions[i], extensionName) == 0) {
return true;
}
}
return false;
};
// VK_KHR_surface is required for GLFW window surface creation.
if (!hasValidatedExtension(VK_KHR_SURFACE_EXTENSION_NAME)) {
throw std::runtime_error("Missing required Vulkan instance extension VK_KHR_surface");
}
const bool hasWayland = hasValidatedExtension("VK_KHR_wayland_surface");
const bool hasXlib = hasValidatedExtension("VK_KHR_xlib_surface");
const bool hasXcb = hasValidatedExtension("VK_KHR_xcb_surface");
if (!hasWayland && !hasXlib && !hasXcb) {
throw std::runtime_error("Missing required Vulkan WSI instance extensions (need one of VK_KHR_wayland_surface, VK_KHR_xlib_surface, VK_KHR_xcb_surface)");
}
return validatedExtensions;
}
void Graphics::initInstance(GraphicsInitializer initInfo) {
@@ -804,6 +860,12 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
.enabledExtensionCount = (uint32)extensions.size(),
.ppEnabledExtensionNames = extensions.data(),
};
std::cout << "Enabled Extensions: ";
for(uint i = 0; i < extensions.size(); ++i)
{
std::cout << extensions[i] << " ";
}
std::cout << std::endl;
VK_CHECK(vkCreateInstance(&info, nullptr, &instance));
}
+24
View File
@@ -210,6 +210,30 @@ void FluidScene::updateFluidData(uint32 entityID, const Component::Transform& tr
data.gridSize = grid.gridSize;
data.gravity = grid.gravity;
data.enableGravity = grid.enableGravity;
// Build plane list: 6 cube walls + user-defined extra planes
struct GPUPlane { Vector normal; float d; };
Array<GPUPlane> planes;
// Cube wall planes (inward normal, solid when dot(n,p)+d < 0)
planes.add({{1, 0, 0}, -0.5f});
planes.add({{-1, 0, 0}, static_cast<float>(grid.gridSize.x) - 1.5f});
planes.add({{0, 1, 0}, -0.5f});
planes.add({{0, -1, 0}, static_cast<float>(grid.gridSize.y) - 1.5f});
planes.add({{0, 0, 1}, -0.5f});
planes.add({{0, 0, -1}, static_cast<float>(grid.gridSize.z) - 1.5f});
for (const auto& ep : grid.extraPlanes) {
planes.add({ep.normal, ep.d});
}
data.numPlanes = static_cast<uint32>(planes.size());
data.planeBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = static_cast<uint64>(planes.size() * sizeof(GPUPlane)),
.data = reinterpret_cast<uint8*>(planes.data()),
},
.numElements = static_cast<uint64>(planes.size()),
.name = "PlaneBuffer",
});
}
void FluidScene::addSource() {
+2
View File
@@ -43,6 +43,8 @@ class FluidScene {
Gfx::OShaderBuffer normalBuffer;
Gfx::OShaderBuffer indexBuffer;
Gfx::OShaderBuffer surfaceCountBuffer;
Gfx::OShaderBuffer planeBuffer;
uint32 numPlanes = 0;
uint32 frameCounter = 0;
constexpr uint getVertexBufferSize() {
// Max 5 triangles per cell, 3 vertices per triangle