diff --git a/.vscode/launch.json b/.vscode/launch.json index 1c92f67..855a476 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -31,39 +31,16 @@ "request": "launch", "program": "${workspaceRoot}/build/Editor", "args": [], - "environment": [ - { - "name": "ASAN_OPTIONS", - "value": "detect_leaks=0", - }, - { - "name": "DISABLE_VK_LAYER_VALVE_steam_overlay_1", - "value": "1", - }, - { - "name": "DISABLE_VK_LAYER_VALVE_steam_fossilize_1", - "value": "1", - }, - { - "name": "MANGOHUD", - "value": "0", - } - ], + "env": { + "ASAN_OPTIONS": "detect_leaks=0", + "DISABLE_VK_LAYER_VALVE_steam_overlay_1": "1", + "DISABLE_VK_LAYER_VALVE_steam_fossilize_1": "1", + "MANGOHUD": "0" + }, "cwd": "${workspaceRoot}/build", - "MIMode": "gdb", - "miDebuggerPath": "/usr/bin/gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - }, - { - "description": "Import Seele pretty-printers", - "text": "python exec(open('${workspaceFolder}/Seele/gdb/Seele.py').read())", - "ignoreFailures": true - } - ], + "initCommands": [ + "command script import ${workspaceFolder}/Seele/gdb/Seele_lldb.py" + ] }, ] } \ No newline at end of file diff --git a/res/shaders/fluid/LinearSolve.slang b/res/shaders/fluid/LinearSolve.slang index 7cd4f56..6983a55 100644 --- a/res/shaders/fluid/LinearSolve.slang +++ b/res/shaders/fluid/LinearSolve.slang @@ -2,14 +2,14 @@ import FluidGridData; struct Params { - // read-write - FluidGridData next; - // read-only - FluidGridData current; - // read-only + // read-write (in-place update) + FluidGridData grid; + // read-only (right-hand side) FluidGridData grid0; float a; float c; + float omega; + int color; }; ParameterBlock params; @@ -17,16 +17,22 @@ ParameterBlock params; [numthreads(32, 8, 1)] void linearSolve(uint3 dispatchThreadID : SV_DispatchThreadID) { - FluidGridData next = params.next; - FluidGridData current = params.current; + FluidGridData grid = params.grid; FluidGridData grid0 = params.grid0; float a = params.a; float cRecip = 1.0f / params.c; + float omega = params.omega; + int color = params.color; int x = dispatchThreadID.x + 1; int y = dispatchThreadID.y + 1; int z = dispatchThreadID.z + 1; if(x >= gridParams.gridSize.x - 1 || y >= gridParams.gridSize.y - 1 || z >= gridParams.gridSize.z - 1) return; - next[x, y, z] = (grid0[x, y, z] + a * (current[x + 1, y, z] + current[x - 1, y, z] + current[x, y + 1, z] + current[x, y - 1, z] + current[x, y, z + 1] + current[x, y, z - 1])) * cRecip; + // Red-Black Gauss-Seidel: only update cells matching the current color + if((x + y + z) % 2 != color) return; + + float oldVal = grid[x, y, z]; + float gs = (grid0[x, y, z] + a * (grid[x + 1, y, z] + grid[x - 1, y, z] + grid[x, y + 1, z] + grid[x, y - 1, z] + grid[x, y, z + 1] + grid[x, y, z - 1])) * cRecip; + grid[x, y, z] = lerp(oldVal, gs, omega); } \ No newline at end of file diff --git a/res/shaders/fluid/SetBound.slang b/res/shaders/fluid/SetBound.slang index 1120265..394a02f 100644 --- a/res/shaders/fluid/SetBound.slang +++ b/res/shaders/fluid/SetBound.slang @@ -1,93 +1,81 @@ import FluidGridData; +struct BoundaryPlane +{ + float3 normal; // inward normal (points from solid toward fluid) + float d; // plane offset: dot(normal, cellPos) + d < 0 means solid +}; + struct Params { int b; + uint numPlanes; + StructuredBuffer planes; // read-write FluidGridData grid; }; ParameterBlock params; +// Find the interior (fluid) neighbor of a solid cell by stepping along the +// dominant axis of the plane inward normal. +int3 getInteriorNeighbor(int3 pos, float3 normal) +{ + float3 absN = abs(normal); + int3 step; + if (absN.x >= absN.y && absN.x >= absN.z) + step = int3(normal.x > 0 ? 1 : -1, 0, 0); + else if (absN.y >= absN.z) + step = int3(0, normal.y > 0 ? 1 : -1, 0); + else + step = int3(0, 0, normal.z > 0 ? 1 : -1); + + return clamp(pos + step, int3(0), int3(gridParams.gridSize) - 1); +} + +// Compute the boundary value scaling factor for velocity component b. +// For b=0 (scalar): factor = 1 (Neumann / copy). +// For b=1,2,3 (velocity x,y,z): reflects the normal component so that +// velocity normal to the plane is negated while tangential parts are preserved. +float getBoundaryFactor(int b, float3 normal) +{ + if (b == 0) return 1.0f; + float nComp = (b == 1) ? normal.x : ((b == 2) ? normal.y : normal.z); + return 1.0f - 2.0f * nComp * nComp; +} + [shader("compute")] -[numthreads(32, 8, 1)] +[numthreads(4, 4, 4)] void setBound(uint3 dispatchThreadID : SV_DispatchThreadID) { + int3 pos = int3(dispatchThreadID); + if (any(uint3(pos) >= gridParams.gridSize)) + return; + int b = params.b; FluidGridData grid = params.grid; - int i = dispatchThreadID.x + 1; - int j = dispatchThreadID.y + 1; + float3 cellPos = float3(pos); - // X-faces: indices range over (gridSize.y, gridSize.z) - if(i < gridParams.gridSize.y - 1 && j < gridParams.gridSize.z - 1) + int numAffecting = 0; + float valueSum = 0.0f; + + for (uint p = 0; p < params.numPlanes; p++) { - grid[0, i, j] = b == 1 ? -grid[1, i, j] : grid[1, i, j]; - grid[gridParams.gridSize.x - 1, i, j] = b == 1 ? -grid[gridParams.gridSize.x - 2, i, j] : grid[gridParams.gridSize.x - 2, i, j]; + BoundaryPlane plane = params.planes[p]; + float dist = dot(plane.normal, cellPos) + plane.d; + + if (dist < 0.0f) // cell is on the solid side + { + int3 neighbor = getInteriorNeighbor(pos, plane.normal); + float neighborVal = grid[uint3(neighbor)]; + float factor = getBoundaryFactor(b, plane.normal); + + valueSum += factor * neighborVal; + numAffecting++; + } } - // Y-faces: indices range over (gridSize.x, gridSize.z) - if(i < gridParams.gridSize.x - 1 && j < gridParams.gridSize.z - 1) + if (numAffecting > 0) { - grid[i, 0, j] = b == 2 ? -grid[i, 1, j] : grid[i, 1, j]; - grid[i, gridParams.gridSize.y - 1, j] = b == 2 ? -grid[i, gridParams.gridSize.y - 2, j] : grid[i, gridParams.gridSize.y - 2, j]; + grid[uint3(pos)] = valueSum / float(numAffecting); } - - // Z-faces: indices range over (gridSize.x, gridSize.y) - if(i < gridParams.gridSize.x - 1 && j < gridParams.gridSize.y - 1) - { - grid[i, j, 0] = b == 3 ? -grid[i, j, 1] : grid[i, j, 1]; - grid[i, j, gridParams.gridSize.z - 1] = b == 3 ? -grid[i, j, gridParams.gridSize.z - 2] : grid[i, j, gridParams.gridSize.z - 2]; - } -} - -[shader("compute")] -[numthreads(128, 1, 1)] -void setBoundEdges(uint3 dispatchThreadID : SV_DispatchThreadID) -{ - FluidGridData grid = params.grid; - int x = dispatchThreadID.x + 1; - - // X-axis edges: x ranges [1, gridSize.x-2] - if(x < gridParams.gridSize.x - 1) - { - grid[x, 0, 0] = 0.5f * (grid[x, 1, 0] + grid[x, 0, 1]); - grid[x, gridParams.gridSize.y - 1, 0] = 0.5f * (grid[x, gridParams.gridSize.y - 2, 0] + grid[x, gridParams.gridSize.y - 1, 1]); - grid[x, 0, gridParams.gridSize.z - 1] = 0.5f * (grid[x, 1, gridParams.gridSize.z - 1] + grid[x, 0, gridParams.gridSize.z - 2]); - grid[x, gridParams.gridSize.y - 1, gridParams.gridSize.z - 1] = 0.5f * (grid[x, gridParams.gridSize.y - 2, gridParams.gridSize.z - 1] + grid[x, gridParams.gridSize.y - 1, gridParams.gridSize.z - 2]); - } - - // Y-axis edges: x ranges [1, gridSize.y-2] - if(x < gridParams.gridSize.y - 1) - { - grid[0, x, 0] = 0.5f * (grid[1, x, 0] + grid[0, x, 1]); - grid[0, x, gridParams.gridSize.z - 1] = 0.5f * (grid[1, x, gridParams.gridSize.z - 1] + grid[0, x, gridParams.gridSize.z - 2]); - grid[gridParams.gridSize.x - 1, x, 0] = 0.5f * (grid[gridParams.gridSize.x - 2, x, 0] + grid[gridParams.gridSize.x - 1, x, 1]); - grid[gridParams.gridSize.x - 1, x, gridParams.gridSize.z - 1] = 0.5f * (grid[gridParams.gridSize.x - 2, x, gridParams.gridSize.z - 1] + grid[gridParams.gridSize.x - 1, x, gridParams.gridSize.z - 2]); - } - - // Z-axis edges: x ranges [1, gridSize.z-2] - if(x < gridParams.gridSize.z - 1) - { - grid[0, 0, x] = 0.5f * (grid[1, 0, x] + grid[0, 1, x]); - grid[0, gridParams.gridSize.y - 1, x] = 0.5f * (grid[1, gridParams.gridSize.y - 1, x] + grid[0, gridParams.gridSize.y - 2, x]); - grid[gridParams.gridSize.x - 1, 0, x] = 0.5f * (grid[gridParams.gridSize.x - 2, 0, x] + grid[gridParams.gridSize.x - 1, 1, x]); - grid[gridParams.gridSize.x - 1, gridParams.gridSize.y - 1, x] = 0.5f * (grid[gridParams.gridSize.x - 2, gridParams.gridSize.y - 1, x] + grid[gridParams.gridSize.x - 1, gridParams.gridSize.y - 2, x]); - } -} - -[shader("compute")] -[numthreads(8, 1, 1)] -void setBoundCorners(uint3 dispatchThreadID : SV_DispatchThreadID) -{ - FluidGridData grid = params.grid; - uint threadIdx = dispatchThreadID.x; - - uint x = ((threadIdx & 1) == 0) ? 0 : (gridParams.gridSize.x - 1); - uint y = ((threadIdx & 2) == 0) ? 0 : (gridParams.gridSize.y - 1); - uint z = ((threadIdx & 4) == 0) ? 0 : (gridParams.gridSize.z - 1); - - uint nx = (x == 0) ? 1 : (gridParams.gridSize.x - 2); - uint ny = (y == 0) ? 1 : (gridParams.gridSize.y - 2); - uint nz = (z == 0) ? 1 : (gridParams.gridSize.z - 2); - - grid[x, y, z] = (1.0f / 3.0f) * (grid[nx, y, z] + grid[x, ny, z] + grid[x, y, nz]); } \ No newline at end of file diff --git a/res/shaders/fluid/SignedDistance.slang b/res/shaders/fluid/SignedDistance.slang index cbc847a..42d6776 100644 --- a/res/shaders/fluid/SignedDistance.slang +++ b/res/shaders/fluid/SignedDistance.slang @@ -3,10 +3,12 @@ import FluidGridData; struct Params { float dtau; - // read-write: phi being reinitialized + // read-write: phi being updated (output) FluidGridData phi; - // read-only: snapshot of phi before reinitialization (for sign) - FluidGridData phi0; + // read-only: phi from previous iteration (for gradient computation) + FluidGridData phiPrev; + // read-only: phi snapshot from before reinitialization (for sign function) + FluidGridData phiSign; }; ParameterBlock params; @@ -24,24 +26,24 @@ void reinitialize(uint3 dispatchThreadID : SV_DispatchThreadID) uint y = dispatchThreadID.y + 1; uint z = dispatchThreadID.z + 1; FluidGridData phi = params.phi; - FluidGridData phi0 = params.phi0; + FluidGridData phiPrev = params.phiPrev; + FluidGridData phiSign = params.phiSign; if(x >= gridParams.gridSize.x - 1 || y >= gridParams.gridSize.y - 1 || z >= gridParams.gridSize.z - 1) return; float dtau = params.dtau; float dx = 1.0f; - // Current value and original sign - float p = phi0[x, y, z]; - float s = smoothSign(phi0[x, y, z], dx); + // Current value from previous iteration, sign from original snapshot + float p = phiPrev[x, y, z]; + float s = smoothSign(phiSign[x, y, z], dx); - // Upwind finite differences for |grad phi| — read from snapshot to avoid race - // Forward and backward differences - float dxp = phi0[x + 1, y, z] - p; - float dxm = p - phi0[x - 1, y, z]; - float dyp = phi0[x, y + 1, z] - p; - float dym = p - phi0[x, y - 1, z]; - float dzp = phi0[x, y, z + 1] - p; - float dzm = p - phi0[x, y, z - 1]; + // Upwind finite differences for |grad phi| from previous iteration + float dxp = phiPrev[x + 1, y, z] - p; + float dxm = p - phiPrev[x - 1, y, z]; + float dyp = phiPrev[x, y + 1, z] - p; + float dym = p - phiPrev[x, y - 1, z]; + float dzp = phiPrev[x, y, z + 1] - p; + float dzm = p - phiPrev[x, y, z - 1]; // Godunov upwind scheme float gradPhi; diff --git a/src/Engine/Component/FluidGrid.h b/src/Engine/Component/FluidGrid.h index 7ba19d1..113bc03 100644 --- a/src/Engine/Component/FluidGrid.h +++ b/src/Engine/Component/FluidGrid.h @@ -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 extraPlanes; }; } // namespace Component } // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/SimulationComputePass.cpp b/src/Engine/Graphics/RenderPass/SimulationComputePass.cpp index 8661e33..799dcad 100644 --- a/src/Engine/Graphics/RenderPass/SimulationComputePass.cpp +++ b/src/Engine/Graphics/RenderPass/SimulationComputePass.cpp @@ -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(); } diff --git a/src/Engine/Graphics/RenderPass/SimulationComputePass.h b/src/Engine/Graphics/RenderPass/SimulationComputePass.h index 4c0d0a8..8a0d549 100644 --- a/src/Engine/Graphics/RenderPass/SimulationComputePass.h +++ b/src/Engine/Graphics/RenderPass/SimulationComputePass.h @@ -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) diff --git a/src/Engine/Graphics/Vulkan/Graphics.cpp b/src/Engine/Graphics/Vulkan/Graphics.cpp index dcd7f0d..b2d00c1 100644 --- a/src/Engine/Graphics/Vulkan/Graphics.cpp +++ b/src/Engine/Graphics/Vulkan/Graphics.cpp @@ -722,6 +722,11 @@ PDestructionManager Graphics::getDestructionManager() { return destructionManage Array Graphics::getRequiredExtensions() { Array 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 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 Graphics::getRequiredExtensions() { #ifdef ENABLE_VALIDATION extensions.addUnique(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); #endif - return extensions; + + Array 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)); } diff --git a/src/Engine/Scene/FluidScene.cpp b/src/Engine/Scene/FluidScene.cpp index d8475ab..43d7f58 100644 --- a/src/Engine/Scene/FluidScene.cpp +++ b/src/Engine/Scene/FluidScene.cpp @@ -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 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(grid.gridSize.x) - 1.5f}); + planes.add({{0, 1, 0}, -0.5f}); + planes.add({{0, -1, 0}, static_cast(grid.gridSize.y) - 1.5f}); + planes.add({{0, 0, 1}, -0.5f}); + planes.add({{0, 0, -1}, static_cast(grid.gridSize.z) - 1.5f}); + for (const auto& ep : grid.extraPlanes) { + planes.add({ep.normal, ep.d}); + } + data.numPlanes = static_cast(planes.size()); + data.planeBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ + .sourceData = + { + .size = static_cast(planes.size() * sizeof(GPUPlane)), + .data = reinterpret_cast(planes.data()), + }, + .numElements = static_cast(planes.size()), + .name = "PlaneBuffer", + }); } void FluidScene::addSource() { diff --git a/src/Engine/Scene/FluidScene.h b/src/Engine/Scene/FluidScene.h index 22a91a8..a93adb4 100644 --- a/src/Engine/Scene/FluidScene.h +++ b/src/Engine/Scene/FluidScene.h @@ -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 diff --git a/vcpkg.json b/vcpkg.json index 68f0d3f..12f2707 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -1,5 +1,21 @@ { "dependencies": [ + { + "name": "vulkan-loader", + "features": [ + "xlib", + "xcb", + "wayland" + ] + }, + { + "name": "vulkan-loader", + "features": [ + "xlib", + "xcb", + "wayland" + ] + }, "vulkan", { "name": "vulkan-memory-allocator"