Compare commits

...
14 Commits
111 changed files with 4608 additions and 711 deletions

No files matched your search

+10 -16
View File
@@ -27,26 +27,20 @@
},
{
"name": "Editor (Linux)",
"type": "cppdbg",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/build/Editor",
"args": [],
"environment": [
{
"name": "ASAN_OPTIONS",
"value": "alloc_dealloc_mismatch=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
}
],
"initCommands": [
"command script import ${workspaceFolder}/Seele/gdb/Seele_lldb.py"
]
},
]
}
+15 -19
View File
@@ -54,12 +54,22 @@ if(WIN32)
endif()
add_library(Engine SHARED "")
set_target_properties(Engine PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)
target_include_directories(Engine PRIVATE src/Engine)
target_link_libraries(Engine PUBLIC Vulkan::Vulkan)
target_link_libraries(Engine PUBLIC GPUOpen::VulkanMemoryAllocator)
if(NOT APPLE)
target_link_libraries(Engine PUBLIC slang::slang-glslang)
# slang-glslang is a MODULE library (plugin) - copy it instead of linking
add_custom_command(TARGET Engine POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_FILE:slang::slang-glslang>
$<TARGET_FILE_DIR:Engine>
)
endif()
target_link_libraries(Engine PUBLIC EnTT::EnTT)
target_link_libraries(Engine PUBLIC glfw)
@@ -121,31 +131,17 @@ if(APPLE)
target_compile_options(Engine PUBLIC -Wno-gnu-anonymous-struct -Wno-nested-anon-types)
target_compile_options(Editor PUBLIC -Wno-gnu-anonymous-struct -Wno-nested-anon-types)
endif()
target_compile_options(Engine PUBLIC "$<$<CONFIG:DEBUG>:-DENABLE_VALIDATION>")
target_compile_options(Engine PUBLIC "$<$<CONFIG:DEBUG>:-DSEELE_DEBUG>")
add_subdirectory(src/)
#add_subdirectory(tests/)
if(WIN32)
add_custom_target(dll_copy ALL
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:Engine> $<TARGET_FILE_DIR:Editor>
COMMAND_EXPAND_LISTS
DEPENDS Editor)
elseif(APPLE)
add_custom_target(dll_copy ALL
COMMAND ${CMAKE_COMMAND} -E true
COMMAND_EXPAND_LISTS
DEPENDS Editor)
else()
add_custom_target(dll_copy ALL
COMMAND ${CMAKE_COMMAND} -E true)
add_custom_command(TARGET Engine POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_RUNTIME_DLLS:Engine> $<TARGET_FILE_DIR:Editor>)
endif()
add_custom_target(SeeleEngine ALL
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/res $<TARGET_FILE_DIR:Engine>
COMMAND_EXPAND_LISTS
DEPENDS Editor dll_copy)
add_custom_command(TARGET Engine POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/res $<TARGET_FILE_DIR:Engine>)
install(
TARGETS
-2
View File
@@ -1,2 +0,0 @@
(?<enum>[A-Z_]*) = [0-9]*,
case SE_${enum}:\nreturn VK_${enum};
-4
View File
@@ -1,4 +0,0 @@
// Hint files help the Visual Studio IDE interpret Visual C++ identifiers
// such as names of functions and macros.
// For more information see https://go.microsoft.com/fwlink/?linkid=865984
#define DECLARE_REF(x)
+1 -1
+83 -2
View File
@@ -2,6 +2,25 @@ import sys
sys.path.insert(0, '/usr/share/gdb/python/')
import gdb.printing
def _base_cast(val):
for field in val.type.strip_typedefs().fields():
if getattr(field, 'is_base_class', False):
return val.cast(field.type)
return None
def _member(val, name):
"""Find a member by name on a value, searching base classes if needed."""
current = val
while current is not None:
t = current.type.strip_typedefs()
for field in t.fields():
if field.name == name:
return current[name]
current = _base_cast(current)
raise gdb.error(f"Member '{name}' not found on {val.type}")
class ArrayPrinter:
def __init__(self, val):
self.val = val
@@ -13,8 +32,8 @@ class ArrayPrinter:
data = self.val['_data']
size = int(self.val['arraySize'])
allocated = int(self.val['allocated'])
#yield f"[size]", size
#yield f"[allocated]", allocated
yield f"[size]", size
yield f"[allocated]", allocated
for i in range(size):
elem_val = (data + i).dereference()
yield f"[{i}]", elem_val
@@ -22,6 +41,66 @@ class ArrayPrinter:
def display_hint(self):
return "array"
class ListPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
size = int(_member(self.val, '_size'))
return f"List(size={size})"
def children(self):
size = int(_member(self.val, '_size'))
root = _member(self.val, 'root')
tail = _member(self.val, 'tail')
yield "[size]", size
node = root
idx = 0
# Bound traversal by logical size to avoid walking corrupted links forever.
while idx < size and node != tail:
yield f"[{idx}]", node['data']
node = node['next']
idx += 1
def display_hint(self):
return "array"
class MapPrinter:
def __init__(self, val):
self.val = val
def to_string(self):
size = int(_member(self.val, '_size'))
return f"Map(size={size})"
def children(self):
size = int(_member(self.val, '_size'))
root = _member(self.val, 'root')
stack = []
node = root
idx = 0
# In-order traversal of Tree nodes yields map entries sorted by key.
while (node or stack) and idx < size:
while node:
stack.append(node)
node = node['leftChild']
node = stack.pop()
pair = node['data']
yield f"[{idx}].key", pair['key']
yield f"[{idx}].value", pair['value']
idx += 1
node = node['rightChild']
def display_hint(self):
return "map"
class VectorPrinter:
def __init__(self, val):
self.val = val
@@ -53,6 +132,8 @@ class VectorPrinter:
def build_pretty_printer():
pp = gdb.printing.RegexpCollectionPrettyPrinter("Seele")
pp.add_printer("Array", r'^Seele::Array<.*>$', ArrayPrinter)
pp.add_printer("List", r'^Seele::List<.*>$', ListPrinter)
pp.add_printer("Map", r'^Seele::Map<.*>$', MapPrinter)
pp.add_printer("Vector", r"^glm::(detail::)?t?vec(\d)?<[^<>]*>$", VectorPrinter)
return pp
+37
View File
@@ -31,6 +31,39 @@ class ArraySyntheticProvider:
def has_children(self):
return True
def List_SummaryProvider(valobj, internal_dict):
size = valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0)
return f"List(size={size})"
class ListSyntheticProvider:
def __init__(self, valobj, internal_dict):
self.valobj = valobj
self.update()
def num_children(self):
return self.size
def get_child_index(self, name):
try:
return int(name.lstrip('[').rstrip(']'))
except:
return -1
def get_child_at_index(self, index):
if index < 0 or index >= self.size:
return None
current = self.root
for i in range(index):
current = current.GetChildMemberWithName('next')
return current.GetChildMemberWithName('data')
def update(self):
self.root = self.valobj.GetChildMemberWithName('root')
self.size = self.valobj.GetChildMemberWithName('_size').GetValueAsUnsigned(0)
def has_children(self):
return True
def Vector_SummaryProvider(valobj, internal_dict):
# Get the template parameters to determine vector length
type_name = valobj.GetTypeName()
@@ -56,6 +89,10 @@ def __lldb_init_module(debugger, internal_dict):
debugger.HandleCommand('type summary add -F Seele_lldb.Array_SummaryProvider -x "^Seele::Array<.*>$"')
debugger.HandleCommand('type synthetic add -l Seele_lldb.ArraySyntheticProvider -x "^Seele::Array<.*>$"')
# Register List formatter
debugger.HandleCommand('type summary add -F Seele_lldb.List_SummaryProvider -x "^Seele::List<.*>$"')
debugger.HandleCommand('type synthetic add -l Seele_lldb.ListSyntheticProvider -x "^Seele::List<.*>$"')
# Register Vector formatter
debugger.HandleCommand('type summary add -F Seele_lldb.Vector_SummaryProvider -x "^glm::(detail::)?t?vec[234]?<.*>$"')
+35
View File
@@ -0,0 +1,35 @@
import FluidGridData;
struct Params
{
// read-write: phi to add source to
FluidGridData<float> phi;
FluidGridData<float> density;
};
ParameterBlock<Params> params;
[shader("compute")]
[numthreads(32, 8, 1)]
void addSource(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> phi = params.phi;
FluidGridData<float> density = params.density;
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;
// Add a spherical source and union it into the existing level set.
float3 center = float3(gridParams.gridSize) * 0.5f;
float radius = min(gridParams.gridSize.x, min(gridParams.gridSize.y, gridParams.gridSize.z)) * 0.25f;
float3 pos = float3(x, y, z);
float dist = length(pos - center);
float sourcePhi = dist - radius;
phi[x, y, z] = min(phi[x, y, z], sourcePhi);
if (sourcePhi < 0.0f)
{
density[x, y, z] += 0.5f;
density[x, y, z] = min(density[x, y, z], 1.0f);
}
}
+55
View File
@@ -0,0 +1,55 @@
import FluidGridData;
struct Params
{
// read-write
FluidGridData<float> density;
// read-only
FluidGridData<float> density0;
// read-only
FluidGridData<float> velocityX;
// read-only
FluidGridData<float> velocityY;
// read-only
FluidGridData<float> velocityZ;
float dt;
};
ParameterBlock<Params> params;
[shader("compute")]
[numthreads(32, 8, 1)]
void advect(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> density = params.density;
FluidGridData<float> density0 = params.density0;
FluidGridData<float> velocityX = params.velocityX;
FluidGridData<float> velocityY = params.velocityY;
FluidGridData<float> velocityZ = params.velocityZ;
float dt = params.dt;
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;
float dt0x = dt * (gridParams.gridSize.x - 2);
float dt0y = dt * (gridParams.gridSize.y - 2);
float dt0z = dt * (gridParams.gridSize.z - 2);
float3 pos = float3(x - dt0x * velocityX[x, y, z], y - dt0y * velocityY[x, y, z], z - dt0z * velocityZ[x, y, z]);
pos.x = clamp(pos.x, 0.5f, gridParams.gridSize.x - 1.5f);
pos.y = clamp(pos.y, 0.5f, gridParams.gridSize.y - 1.5f);
pos.z = clamp(pos.z, 0.5f, gridParams.gridSize.z - 1.5f);
int3 i0 = int3(pos);
int3 i1 = i0 + int3(1, 1, 1);
float3 s1 = pos - i0;
float3 s0 = 1 - s1;
density[x, y, z] =
s0.x * (s0.y * (s0.z * density0[i0.x, i0.y, i0.z] + s1.z * density0[i0.x, i0.y, i1.z]) +
s1.y * (s0.z * density0[i0.x, i1.y, i0.z] + s1.z * density0[i0.x, i1.y, i1.z])) +
s1.x * (s0.y * (s0.z * density0[i1.x, i0.y, i0.z] + s1.z * density0[i1.x, i0.y, i1.z]) +
s1.y * (s0.z * density0[i1.x, i1.y, i0.z] + s1.z * density0[i1.x, i1.y, i1.z]));
}
+31
View File
@@ -0,0 +1,31 @@
import FluidGridData;
struct Params
{
FluidGridData<float> velocityX;
FluidGridData<float> velocityY;
FluidGridData<float> velocityZ;
float3 force;
float dt;
};
ParameterBlock<Params> params;
[shader("compute")]
[numthreads(32, 8, 1)]
void applyForces(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> velocityX = params.velocityX;
FluidGridData<float> velocityY = params.velocityY;
FluidGridData<float> velocityZ = params.velocityZ;
float3 force = params.force;
float dt = params.dt;
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;
velocityX[x, y, z] += force.x * dt;
velocityY[x, y, z] += force.y * dt;
velocityZ[x, y, z] += force.z * dt;
}
+34
View File
@@ -0,0 +1,34 @@
import FluidGridData;
struct Params {
// read-only
FluidGridData<float> velocityX;
// read-only
FluidGridData<float> velocityY;
// read-only
FluidGridData<float> velocityZ;
// read-write
FluidGridData<float> divergence;
// read-write
FluidGridData<float> pressure;
};
ParameterBlock<Params> params;
[shader("compute")]
[numthreads(32, 8, 1)]
void computeDivergence(uint3 dispatchThreadID: SV_DispatchThreadID) {
FluidGridData<float> velocityX = params.velocityX;
FluidGridData<float> velocityY = params.velocityY;
FluidGridData<float> velocityZ = params.velocityZ;
FluidGridData<float> divergence = params.divergence;
FluidGridData<float> pressure = params.pressure;
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;
divergence[x, y, z] = -0.5f * (velocityX[x + 1, y, z] - velocityX[x - 1, y, z] + velocityY[x, y + 1, z] - velocityY[x, y - 1, z] + velocityZ[x, y, z + 1] - velocityZ[x, y, z - 1]);
pressure[x, y, z] = 0;
}
+47
View File
@@ -0,0 +1,47 @@
import FluidGridData;
struct Parameter
{
float iso;
FluidGridData<float> density;
RWStructuredBuffer<float3> vertices;
RWStructuredBuffer<uint> indices;
RWStructuredBuffer<uint> surfaceCount;
RWStructuredBuffer<uint> vertexCount;
};
ParameterBlock<Parameter> params;
float getDensity(uint x, uint y, uint z)
{
return params.density[x + gridParams.gridSize.x * y + gridParams.gridSize.x * gridParams.gridSize.y * z];
}
[shader("compute")]
[numthreads(32, 8, 1)]
void main(uint3 dispatchThreadID : SV_DispatchThreadID)
{
uint x = dispatchThreadID.x+1;
uint y = dispatchThreadID.y+1;
uint z = dispatchThreadID.z+1;
FluidGridData<float> density = params.density;
if(x >= gridParams.gridSize.x-3 || y >= gridParams.gridSize.y-3 || z >= gridParams.gridSize.z-3) return;
float phi[8] = {
getDensity(x, y, z),
getDensity(x + 1, y, z),
getDensity(x, y + 1, z),
getDensity(x + 1, y + 1, z),
getDensity(x, y, z + 1),
getDensity(x + 1, y, z + 1),
getDensity(x, y + 1, z + 1),
getDensity(x + 1, y + 1, z + 1)
};
uint cubeIndex = 0;
for (uint i = 0; i < 8; ++i) {
if (phi[i] < params.iso) {
cubeIndex |= (1 << i);
}
}
if(cubeIndex == 0 || cubeIndex == 255) return;
}
+19
View File
@@ -0,0 +1,19 @@
struct GridParams
{
uint3 gridSize;
};
ParameterBlock<GridParams> gridParams;
struct FluidGridData<T>
{
RWStructuredBuffer<T> dataGrid;
__subscript(uint3 index) -> T
{
get { return dataGrid[index.x + index.y * gridParams.gridSize.x + index.z * gridParams.gridSize.x * gridParams.gridSize.y]; }
set { dataGrid[index.x + index.y * gridParams.gridSize.x + index.z * gridParams.gridSize.x * gridParams.gridSize.y] = newValue; }
}
__subscript(uint x, uint y, uint z) -> T
{
get { return dataGrid[x + y * gridParams.gridSize.x + z * gridParams.gridSize.x * gridParams.gridSize.y]; }
set { dataGrid[x + y * gridParams.gridSize.x + z * gridParams.gridSize.x * gridParams.gridSize.y] = newValue; }
}
};
+38
View File
@@ -0,0 +1,38 @@
import FluidGridData;
struct Params
{
// read-write (in-place update)
FluidGridData<float> grid;
// read-only (right-hand side)
FluidGridData<float> grid0;
float a;
float c;
float omega;
int color;
};
ParameterBlock<Params> params;
[shader("compute")]
[numthreads(32, 8, 1)]
void linearSolve(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> grid = params.grid;
FluidGridData<float> 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;
// 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);
}
+205
View File
@@ -0,0 +1,205 @@
import FluidGridData;
import MarchingCubesData;
struct Params
{
RWStructuredBuffer<float> vertexBuffer;
RWStructuredBuffer<float> normalBuffer;
RWStructuredBuffer<uint> indexBuffer;
RWStructuredBuffer<uint> indicesCount;
FluidGridData<float> levelSet;
};
ParameterBlock<Params> params;
const static float isoLevel = 0.0f;
const static float3 cellSize = 1.0f / gridParams.gridSize;
float3 vertexInterp(float isoLevel, float3 p1, float3 p2, float valp1, float valp2)
{
if (abs(isoLevel - valp1) < 0.00001)
return p1;
if (abs(isoLevel - valp2) < 0.00001)
return p2;
if (abs(valp1 - valp2) < 0.00001)
return p1;
float mu = (isoLevel - valp1) / (valp2 - valp1);
return p1 + mu * (p2 - p1);
}
float3 gridPos(uint x, uint y, uint z)
{
return float3(x, y, z) * cellSize;
}
// TODO: parameterize
const static uint32_t LOCAL_THREADGROUP_SIZE = 16 * 8 * 1; // Must match numthreads
const static uint32_t MAX_TRIS_PER_THREAD = 5; // Max triangles per cube configuration
const static uint32_t MAX_VERTS_PER_GROUP = LOCAL_THREADGROUP_SIZE * MAX_TRIS_PER_THREAD * 3;
groupshared float3 localVertices[MAX_VERTS_PER_GROUP];
groupshared float3 localNormals[MAX_VERTS_PER_GROUP];
groupshared uint localTriCounts[LOCAL_THREADGROUP_SIZE]; // per-thread triangle counts
groupshared uint localOffsets[LOCAL_THREADGROUP_SIZE]; // per-thread exclusive offsets (in vertices)
groupshared uint localBaseOffset; // global base offset for this workgroup
// Hillis-Steele inclusive scan on localTriCounts, result in localOffsets
// Then convert to exclusive scan and allocate global space
void prefixSumAndAllocate(uint groupIndex)
{
localOffsets[groupIndex] = localTriCounts[groupIndex];
GroupMemoryBarrierWithGroupSync();
for (uint offset = 1; offset < LOCAL_THREADGROUP_SIZE; offset *= 2)
{
uint temp = 0;
if (groupIndex >= offset)
temp = localOffsets[groupIndex - offset];
GroupMemoryBarrierWithGroupSync();
localOffsets[groupIndex] += temp;
GroupMemoryBarrierWithGroupSync();
}
// localOffsets is now an inclusive prefix sum
// Last thread has the total — allocate global space
if (groupIndex == LOCAL_THREADGROUP_SIZE - 1)
{
uint totalVerts = localOffsets[groupIndex] * 3;
InterlockedAdd(params.indicesCount[0], totalVerts, localBaseOffset);
}
GroupMemoryBarrierWithGroupSync();
// Convert inclusive to exclusive: shift right, first element = 0
uint inclusive = localOffsets[groupIndex];
GroupMemoryBarrierWithGroupSync();
localOffsets[groupIndex] = (groupIndex == 0) ? 0 : localOffsets[groupIndex - 1];
GroupMemoryBarrierWithGroupSync();
}
[shader("compute")]
[numthreads(16, 8, 1)]
void marchingCubes(uint3 dispatchThreadID : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex)
{
uint x = dispatchThreadID.x + 1;
uint y = dispatchThreadID.y + 1;
uint z = dispatchThreadID.z + 1;
FluidGridData<float> levelSet = params.levelSet;
// Don't early-return — all threads must participate in barriers.
// Use a flag to skip work for out-of-bounds threads.
bool valid = (x < gridParams.gridSize.x - 2 && y < gridParams.gridSize.y - 2 && z < gridParams.gridSize.z - 2);
float val[8];
uint cubeIndex = 0;
uint triCount = 0;
if (valid)
{
val[0] = levelSet[x, y, z];
val[1] = levelSet[x + 1, y, z];
val[2] = levelSet[x + 1, y + 1, z];
val[3] = levelSet[x, y + 1, z];
val[4] = levelSet[x, y, z + 1];
val[5] = levelSet[x + 1, y, z + 1];
val[6] = levelSet[x + 1, y + 1, z + 1];
val[7] = levelSet[x, y + 1, z + 1];
if (val[0] < isoLevel) cubeIndex |= 1;
if (val[1] < isoLevel) cubeIndex |= 2;
if (val[2] < isoLevel) cubeIndex |= 4;
if (val[3] < isoLevel) cubeIndex |= 8;
if (val[4] < isoLevel) cubeIndex |= 16;
if (val[5] < isoLevel) cubeIndex |= 32;
if (val[6] < isoLevel) cubeIndex |= 64;
if (val[7] < isoLevel) cubeIndex |= 128;
for (int i = 0; triTable[cubeIndex][i] != -1; i += 3)
triCount++;
}
// Prefix sum to compute per-thread offsets and allocate global space
localTriCounts[groupIndex] = triCount;
GroupMemoryBarrierWithGroupSync();
prefixSumAndAllocate(groupIndex);
// localOffsets[groupIndex] = exclusive sum of triCounts before this thread
// localBaseOffset = global vertex offset for this workgroup
if (valid && triCount > 0)
{
float3 pos[8];
pos[0] = gridPos(x, y, z);
pos[1] = gridPos(x + 1, y, z);
pos[2] = gridPos(x + 1, y + 1, z);
pos[3] = gridPos(x, y + 1, z);
pos[4] = gridPos(x, y, z + 1);
pos[5] = gridPos(x + 1, y, z + 1);
pos[6] = gridPos(x + 1, y + 1, z + 1);
pos[7] = gridPos(x, y + 1, z + 1);
float3 vertList[12];
if ((edgeTable[cubeIndex] & 1) != 0)
vertList[0] = vertexInterp(isoLevel, pos[0], pos[1], val[0], val[1]);
if ((edgeTable[cubeIndex] & 2) != 0)
vertList[1] = vertexInterp(isoLevel, pos[1], pos[2], val[1], val[2]);
if ((edgeTable[cubeIndex] & 4) != 0)
vertList[2] = vertexInterp(isoLevel, pos[2], pos[3], val[2], val[3]);
if ((edgeTable[cubeIndex] & 8) != 0)
vertList[3] = vertexInterp(isoLevel, pos[3], pos[0], val[3], val[0]);
if ((edgeTable[cubeIndex] & 16) != 0)
vertList[4] = vertexInterp(isoLevel, pos[4], pos[5], val[4], val[5]);
if ((edgeTable[cubeIndex] & 32) != 0)
vertList[5] = vertexInterp(isoLevel, pos[5], pos[6], val[5], val[6]);
if ((edgeTable[cubeIndex] & 64) != 0)
vertList[6] = vertexInterp(isoLevel, pos[6], pos[7], val[6], val[7]);
if ((edgeTable[cubeIndex] & 128) != 0)
vertList[7] = vertexInterp(isoLevel, pos[7], pos[4], val[7], val[4]);
if ((edgeTable[cubeIndex] & 256) != 0)
vertList[8] = vertexInterp(isoLevel, pos[0], pos[4], val[0], val[4]);
if ((edgeTable[cubeIndex] & 512) != 0)
vertList[9] = vertexInterp(isoLevel, pos[1], pos[5], val[1], val[5]);
if ((edgeTable[cubeIndex] & 1024) != 0)
vertList[10] = vertexInterp(isoLevel, pos[2], pos[6], val[2], val[6]);
if ((edgeTable[cubeIndex] & 2048) != 0)
vertList[11] = vertexInterp(isoLevel, pos[3], pos[7], val[3], val[7]);
// Write triangles into shared memory
uint localOff = localOffsets[groupIndex] * 3; // in vertices
for (int i = 0; triTable[cubeIndex][i] != -1; i += 3)
{
float3 v0 = vertList[triTable[cubeIndex][i]];
float3 v1 = vertList[triTable[cubeIndex][i + 1]];
float3 v2 = vertList[triTable[cubeIndex][i + 2]];
float3 n = normalize(cross(v2 - v0, v1 - v0));
localVertices[localOff] = v0;
localVertices[localOff + 1] = v1;
localVertices[localOff + 2] = v2;
localNormals[localOff] = n;
localNormals[localOff + 1] = n;
localNormals[localOff + 2] = n;
localOff += 3;
}
}
GroupMemoryBarrierWithGroupSync();
// Copy from shared memory to global buffers
// Each thread copies a chunk of the workgroup's output
uint totalGroupVerts = localTriCounts[LOCAL_THREADGROUP_SIZE - 1] > 0
? (localOffsets[LOCAL_THREADGROUP_SIZE - 1] + localTriCounts[LOCAL_THREADGROUP_SIZE - 1]) * 3
: localOffsets[LOCAL_THREADGROUP_SIZE - 1] * 3;
for (uint i = groupIndex; i < totalGroupVerts; i += LOCAL_THREADGROUP_SIZE)
{
uint globalIdx = localBaseOffset + i;
float3 v = localVertices[i];
float3 n = localNormals[i];
params.vertexBuffer[globalIdx * 3 + 0] = v.x;
params.vertexBuffer[globalIdx * 3 + 1] = v.y;
params.vertexBuffer[globalIdx * 3 + 2] = v.z;
params.normalBuffer[globalIdx * 3 + 0] = n.x;
params.normalBuffer[globalIdx * 3 + 1] = n.y;
params.normalBuffer[globalIdx * 3 + 2] = n.z;
params.indexBuffer[globalIdx] = globalIdx;
}
}
+299
View File
@@ -0,0 +1,299 @@
// Marching Cubes edge table: for each cube configuration (256), a 12-bit mask
// indicating which edges are intersected by the isosurface.
// clang-format off
static const uint16_t edgeTable[256] = {
0x000, 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,
0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
0x190, 0x099, 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,
0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
0x230, 0x339, 0x033, 0x13a, 0x636, 0x73f, 0x435, 0x53c,
0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
0x3a0, 0x2a9, 0x1a3, 0x0aa, 0x7a6, 0x6af, 0x5a5, 0x4ac,
0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
0x460, 0x569, 0x663, 0x76a, 0x066, 0x16f, 0x265, 0x36c,
0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0x0ff, 0x3f5, 0x2fc,
0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x055, 0x15c,
0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0x0cc,
0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,
0x0cc, 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,
0x15c, 0x055, 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,
0x2fc, 0x3f5, 0x0ff, 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,
0x36c, 0x265, 0x16f, 0x066, 0x76a, 0x663, 0x569, 0x460,
0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
0x4ac, 0x5a5, 0x6af, 0x7a6, 0x0aa, 0x1a3, 0x2a9, 0x3a0,
0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,
0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x033, 0x339, 0x230,
0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,
0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x099, 0x190,
0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,
0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x000,
};
// Marching Cubes triangle table: for each cube configuration, up to 5
// triangles specified as sequences of edge indices, terminated by -1.
static const int8_t triTable[256][16] = {
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,8,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,1,9,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,8,3,9,8,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,2,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,8,3,1,2,10,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{9,2,10,0,2,9,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{2,8,3,2,10,8,10,9,8,-1,-1,-1,-1,-1,-1,-1},
{3,11,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,11,2,8,11,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,9,0,2,3,11,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,11,2,1,9,11,9,8,11,-1,-1,-1,-1,-1,-1,-1},
{3,10,1,11,10,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,10,1,0,8,10,8,11,10,-1,-1,-1,-1,-1,-1,-1},
{3,9,0,3,11,9,11,10,9,-1,-1,-1,-1,-1,-1,-1},
{9,8,10,10,8,11,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,7,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,3,0,7,3,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,1,9,8,4,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,1,9,4,7,1,7,3,1,-1,-1,-1,-1,-1,-1,-1},
{1,2,10,8,4,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{3,4,7,3,0,4,1,2,10,-1,-1,-1,-1,-1,-1,-1},
{9,2,10,9,0,2,8,4,7,-1,-1,-1,-1,-1,-1,-1},
{2,10,9,2,9,7,2,7,3,7,9,4,-1,-1,-1,-1},
{8,4,7,3,11,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{11,4,7,11,2,4,2,0,4,-1,-1,-1,-1,-1,-1,-1},
{9,0,1,8,4,7,2,3,11,-1,-1,-1,-1,-1,-1,-1},
{4,7,11,9,4,11,9,11,2,9,2,1,-1,-1,-1,-1},
{3,10,1,3,11,10,7,8,4,-1,-1,-1,-1,-1,-1,-1},
{1,11,10,1,4,11,1,0,4,7,11,4,-1,-1,-1,-1},
{4,7,8,9,0,11,9,11,10,11,0,3,-1,-1,-1,-1},
{4,7,11,4,11,9,9,11,10,-1,-1,-1,-1,-1,-1,-1},
{9,5,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{9,5,4,0,8,3,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,5,4,1,5,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{8,5,4,8,3,5,3,1,5,-1,-1,-1,-1,-1,-1,-1},
{1,2,10,9,5,4,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{3,0,8,1,2,10,4,9,5,-1,-1,-1,-1,-1,-1,-1},
{5,2,10,5,4,2,4,0,2,-1,-1,-1,-1,-1,-1,-1},
{2,10,5,3,2,5,3,5,4,3,4,8,-1,-1,-1,-1},
{9,5,4,2,3,11,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,11,2,0,8,11,4,9,5,-1,-1,-1,-1,-1,-1,-1},
{0,5,4,0,1,5,2,3,11,-1,-1,-1,-1,-1,-1,-1},
{2,1,5,2,5,8,2,8,11,4,8,5,-1,-1,-1,-1},
{10,3,11,10,1,3,9,5,4,-1,-1,-1,-1,-1,-1,-1},
{4,9,5,0,8,1,8,10,1,8,11,10,-1,-1,-1,-1},
{5,4,0,5,0,11,5,11,10,11,0,3,-1,-1,-1,-1},
{5,4,8,5,8,10,10,8,11,-1,-1,-1,-1,-1,-1,-1},
{9,7,8,5,7,9,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{9,3,0,9,5,3,5,7,3,-1,-1,-1,-1,-1,-1,-1},
{0,7,8,0,1,7,1,5,7,-1,-1,-1,-1,-1,-1,-1},
{1,5,3,3,5,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{9,7,8,9,5,7,10,1,2,-1,-1,-1,-1,-1,-1,-1},
{10,1,2,9,5,0,5,3,0,5,7,3,-1,-1,-1,-1},
{8,0,2,8,2,5,8,5,7,10,5,2,-1,-1,-1,-1},
{2,10,5,2,5,3,3,5,7,-1,-1,-1,-1,-1,-1,-1},
{7,9,5,7,8,9,3,11,2,-1,-1,-1,-1,-1,-1,-1},
{9,5,7,9,7,2,9,2,0,2,7,11,-1,-1,-1,-1},
{2,3,11,0,1,8,1,7,8,1,5,7,-1,-1,-1,-1},
{11,2,1,11,1,7,7,1,5,-1,-1,-1,-1,-1,-1,-1},
{9,5,8,8,5,7,10,1,3,10,3,11,-1,-1,-1,-1},
{5,7,0,5,0,9,7,11,0,1,0,10,11,10,0,-1},
{11,10,0,11,0,3,10,5,0,8,0,7,5,7,0,-1},
{11,10,5,7,11,5,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{10,6,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,8,3,5,10,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{9,0,1,5,10,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,8,3,1,9,8,5,10,6,-1,-1,-1,-1,-1,-1},
{1,6,5,2,6,1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,6,5,1,2,6,3,0,8,-1,-1,-1,-1,-1,-1},
{9,6,5,9,0,6,0,2,6,-1,-1,-1,-1,-1,-1},
{5,9,8,5,8,2,5,2,6,3,2,8,-1,-1,-1,-1},
{2,3,11,10,6,5,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{11,0,8,11,2,0,10,6,5,-1,-1,-1,-1,-1,-1},
{0,1,9,2,3,11,5,10,6,-1,-1,-1,-1,-1,-1},
{5,10,6,1,9,2,9,11,2,9,8,11,-1,-1,-1,-1},
{6,3,11,6,5,3,5,1,3,-1,-1,-1,-1,-1,-1,-1},
{0,8,11,0,11,5,0,5,1,5,11,6,-1,-1,-1,-1},
{3,11,6,0,3,6,0,6,5,0,5,9,-1,-1,-1,-1},
{6,5,9,6,9,11,11,9,8,-1,-1,-1,-1,-1,-1,-1},
{5,10,6,4,7,8,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,3,0,4,7,3,6,5,10,-1,-1,-1,-1,-1,-1,-1},
{1,9,0,5,10,6,8,4,7,-1,-1,-1,-1,-1,-1,-1},
{10,6,5,1,9,7,1,7,3,7,9,4,-1,-1,-1,-1},
{6,1,2,6,5,1,4,7,8,-1,-1,-1,-1,-1,-1,-1},
{1,2,5,5,2,6,3,0,4,3,4,7,-1,-1,-1,-1},
{8,4,7,9,0,5,0,6,5,0,2,6,-1,-1,-1,-1},
{7,3,9,7,9,4,3,2,9,5,9,6,2,6,9,-1},
{3,11,2,7,8,4,10,6,5,-1,-1,-1,-1,-1,-1,-1},
{5,10,6,4,7,2,4,2,0,2,7,11,-1,-1,-1,-1},
{0,1,9,4,7,8,2,3,11,5,10,6,-1,-1,-1,-1},
{9,2,1,9,11,2,9,4,11,7,11,4,5,10,6,-1},
{8,4,7,3,11,5,3,5,1,5,11,6,-1,-1,-1,-1},
{5,1,11,5,11,6,1,0,11,7,11,4,0,4,11,-1},
{0,5,9,0,6,5,0,3,6,11,6,3,8,4,7,-1},
{6,5,9,6,9,11,4,7,9,7,11,9,-1,-1,-1,-1},
{10,4,9,6,4,10,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,10,6,4,9,10,0,8,3,-1,-1,-1,-1,-1,-1},
{10,0,1,10,6,0,6,4,0,-1,-1,-1,-1,-1,-1},
{8,3,1,8,1,6,8,6,4,6,1,10,-1,-1,-1,-1},
{1,4,9,1,2,4,2,6,4,-1,-1,-1,-1,-1,-1,-1},
{3,0,8,1,2,9,2,4,9,2,6,4,-1,-1,-1,-1},
{0,2,4,4,2,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{8,3,2,8,2,4,4,2,6,-1,-1,-1,-1,-1,-1,-1},
{10,4,9,10,6,4,11,2,3,-1,-1,-1,-1,-1,-1},
{0,8,2,2,8,11,4,9,10,4,10,6,-1,-1,-1,-1},
{3,11,2,0,1,6,0,6,4,6,1,10,-1,-1,-1,-1},
{6,4,1,6,1,10,4,8,1,2,1,11,8,11,1,-1},
{9,6,4,9,3,6,9,1,3,11,6,3,-1,-1,-1,-1},
{8,11,1,8,1,0,11,6,1,9,1,4,6,4,1,-1},
{3,11,6,3,6,0,0,6,4,-1,-1,-1,-1,-1,-1,-1},
{6,4,8,11,6,8,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{7,10,6,7,8,10,8,9,10,-1,-1,-1,-1,-1,-1,-1},
{0,7,3,0,10,7,0,9,10,6,7,10,-1,-1,-1,-1},
{10,6,7,1,10,7,1,7,8,1,8,0,-1,-1,-1,-1},
{10,6,7,10,7,1,1,7,3,-1,-1,-1,-1,-1,-1,-1},
{1,2,6,1,6,8,1,8,9,8,6,7,-1,-1,-1,-1},
{2,6,9,2,9,1,6,7,9,0,9,3,7,3,9,-1},
{7,8,0,7,0,6,6,0,2,-1,-1,-1,-1,-1,-1,-1},
{7,3,2,6,7,2,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{2,3,11,10,6,8,10,8,9,8,6,7,-1,-1,-1,-1},
{2,0,7,2,7,11,0,9,7,6,7,10,9,10,7,-1},
{1,8,0,1,7,8,1,10,7,6,7,10,2,3,11,-1},
{11,2,1,11,1,7,10,6,1,6,7,1,-1,-1,-1,-1},
{8,9,6,8,6,7,9,1,6,11,6,3,1,3,6,-1},
{0,9,1,11,6,7,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{7,8,0,7,0,6,3,11,0,11,6,0,-1,-1,-1,-1},
{7,11,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{7,6,11,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{3,0,8,11,7,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,1,9,11,7,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{8,1,9,8,3,1,11,7,6,-1,-1,-1,-1,-1,-1},
{10,1,2,6,11,7,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,2,10,3,0,8,6,11,7,-1,-1,-1,-1,-1,-1},
{2,9,0,2,10,9,6,11,7,-1,-1,-1,-1,-1,-1},
{6,11,7,2,10,3,10,8,3,10,9,8,-1,-1,-1,-1},
{7,2,3,6,2,7,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{7,0,8,7,6,0,6,2,0,-1,-1,-1,-1,-1,-1,-1},
{2,7,6,2,3,7,0,1,9,-1,-1,-1,-1,-1,-1,-1},
{1,6,2,1,8,6,1,9,8,8,7,6,-1,-1,-1,-1},
{10,7,6,10,1,7,1,3,7,-1,-1,-1,-1,-1,-1,-1},
{10,7,6,1,7,10,1,8,7,1,0,8,-1,-1,-1,-1},
{0,3,7,0,7,10,0,10,9,6,10,7,-1,-1,-1,-1},
{7,6,10,7,10,8,8,10,9,-1,-1,-1,-1,-1,-1,-1},
{6,8,4,11,8,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{3,6,11,3,0,6,0,4,6,-1,-1,-1,-1,-1,-1,-1},
{8,6,11,8,4,6,9,0,1,-1,-1,-1,-1,-1,-1,-1},
{9,4,6,9,6,3,9,3,1,11,3,6,-1,-1,-1,-1},
{6,8,4,6,11,8,2,10,1,-1,-1,-1,-1,-1,-1,-1},
{1,2,10,3,0,11,0,6,11,0,4,6,-1,-1,-1,-1},
{4,11,8,4,6,11,0,2,9,2,10,9,-1,-1,-1,-1},
{10,9,3,10,3,2,9,4,3,11,3,6,4,6,3,-1},
{8,2,3,8,4,2,4,6,2,-1,-1,-1,-1,-1,-1,-1},
{0,4,2,4,6,2,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,9,0,2,3,4,2,4,6,4,3,8,-1,-1,-1,-1},
{1,9,4,1,4,2,2,4,6,-1,-1,-1,-1,-1,-1,-1},
{8,1,3,8,6,1,8,4,6,6,10,1,-1,-1,-1,-1},
{10,1,0,10,0,6,6,0,4,-1,-1,-1,-1,-1,-1,-1},
{4,6,3,4,3,8,6,10,3,0,3,9,10,9,3,-1},
{10,9,4,6,10,4,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,9,5,7,6,11,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,8,3,4,9,5,11,7,6,-1,-1,-1,-1,-1,-1,-1},
{5,0,1,5,4,0,7,6,11,-1,-1,-1,-1,-1,-1,-1},
{11,7,6,8,3,4,3,5,4,3,1,5,-1,-1,-1,-1},
{9,5,4,10,1,2,7,6,11,-1,-1,-1,-1,-1,-1,-1},
{6,11,7,1,2,10,0,8,3,4,9,5,-1,-1,-1,-1},
{7,6,11,5,4,10,4,2,10,4,0,2,-1,-1,-1,-1},
{3,4,8,3,5,4,3,2,5,10,5,2,11,7,6,-1},
{7,2,3,7,6,2,5,4,9,-1,-1,-1,-1,-1,-1,-1},
{9,5,4,0,8,6,0,6,2,6,8,7,-1,-1,-1,-1},
{3,6,2,3,7,6,1,5,0,5,4,0,-1,-1,-1,-1},
{6,2,8,6,8,7,2,1,8,4,8,5,1,5,8,-1},
{9,5,4,10,1,6,1,7,6,1,3,7,-1,-1,-1,-1},
{1,6,10,1,7,6,1,0,7,8,7,0,9,5,4,-1},
{4,0,10,4,10,5,0,3,10,6,10,7,3,7,10,-1},
{7,6,10,7,10,8,5,4,10,4,8,10,-1,-1,-1,-1},
{6,9,5,6,11,9,11,8,9,-1,-1,-1,-1,-1,-1,-1},
{3,6,11,0,6,3,0,5,6,0,9,5,-1,-1,-1,-1},
{0,11,8,0,5,11,0,1,5,5,6,11,-1,-1,-1,-1},
{6,11,3,6,3,5,5,3,1,-1,-1,-1,-1,-1,-1,-1},
{1,2,10,9,5,11,9,11,8,11,5,6,-1,-1,-1,-1},
{0,11,3,0,6,11,0,9,6,5,6,9,1,2,10,-1},
{11,8,5,11,5,6,8,0,5,10,5,2,0,2,5,-1},
{6,11,3,6,3,5,2,10,3,10,5,3,-1,-1,-1,-1},
{5,8,9,5,2,8,5,6,2,3,8,2,-1,-1,-1,-1},
{9,5,6,9,6,0,0,6,2,-1,-1,-1,-1,-1,-1,-1},
{1,5,8,1,8,0,5,6,8,3,8,2,6,2,8,-1},
{1,5,6,2,1,6,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,3,6,1,6,10,3,8,6,5,6,9,8,9,6,-1},
{10,1,0,10,0,6,9,5,0,5,6,0,-1,-1,-1,-1},
{0,3,8,5,6,10,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{10,5,6,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{11,5,10,7,5,11,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{11,5,10,11,7,5,8,3,0,-1,-1,-1,-1,-1,-1},
{5,11,7,5,10,11,1,9,0,-1,-1,-1,-1,-1,-1},
{10,7,5,10,11,7,9,8,1,8,3,1,-1,-1,-1,-1},
{11,1,2,11,7,1,7,5,1,-1,-1,-1,-1,-1,-1},
{0,8,3,1,2,7,1,7,5,7,2,11,-1,-1,-1,-1},
{9,7,5,9,2,7,9,0,2,2,11,7,-1,-1,-1,-1},
{7,5,2,7,2,11,5,9,2,3,2,8,9,8,2,-1},
{2,5,10,2,3,5,3,7,5,-1,-1,-1,-1,-1,-1,-1},
{8,2,0,8,5,2,8,7,5,10,2,5,-1,-1,-1,-1},
{9,0,1,5,10,3,5,3,7,3,10,2,-1,-1,-1,-1},
{9,8,2,9,2,1,8,7,2,10,2,5,7,5,2,-1},
{1,3,5,3,7,5,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,8,7,0,7,1,1,7,5,-1,-1,-1,-1,-1,-1,-1},
{9,0,3,9,3,5,5,3,7,-1,-1,-1,-1,-1,-1,-1},
{9,8,7,5,9,7,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{5,8,4,5,10,8,10,11,8,-1,-1,-1,-1,-1,-1,-1},
{5,0,4,5,11,0,5,10,11,11,3,0,-1,-1,-1,-1},
{0,1,9,8,4,10,8,10,11,10,4,5,-1,-1,-1,-1},
{10,11,4,10,4,5,11,3,4,9,4,1,3,1,4,-1},
{2,5,1,2,8,5,2,11,8,4,5,8,-1,-1,-1,-1},
{0,4,11,0,11,3,4,5,11,2,11,1,5,1,11,-1},
{0,2,5,0,5,9,2,11,5,4,5,8,11,8,5,-1},
{9,4,5,2,11,3,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{2,5,10,3,5,2,3,4,5,3,8,4,-1,-1,-1,-1},
{5,10,2,5,2,4,4,2,0,-1,-1,-1,-1,-1,-1,-1},
{3,10,2,3,5,10,3,8,5,4,5,8,0,1,9,-1},
{5,10,2,5,2,4,1,9,2,9,4,2,-1,-1,-1,-1},
{8,4,5,8,5,3,3,5,1,-1,-1,-1,-1,-1,-1,-1},
{0,4,5,1,0,5,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{8,4,5,8,5,3,9,0,5,0,3,5,-1,-1,-1,-1},
{9,4,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,11,7,4,9,11,9,10,11,-1,-1,-1,-1,-1,-1},
{0,8,3,4,9,7,9,11,7,9,10,11,-1,-1,-1,-1},
{1,10,11,1,11,4,1,4,0,7,4,11,-1,-1,-1,-1},
{3,1,4,3,4,8,1,10,4,7,4,11,10,11,4,-1},
{4,11,7,9,11,4,9,2,11,9,1,2,-1,-1,-1,-1},
{9,7,4,9,11,7,9,1,11,2,11,1,0,8,3,-1},
{11,7,4,11,4,2,2,4,0,-1,-1,-1,-1,-1,-1,-1},
{11,7,4,11,4,2,8,3,4,3,2,4,-1,-1,-1,-1},
{2,9,10,2,7,9,2,3,7,7,4,9,-1,-1,-1,-1},
{9,10,7,9,7,4,10,2,7,8,7,0,2,0,7,-1},
{3,7,10,3,10,2,7,4,10,1,10,0,4,0,10,-1},
{1,10,2,8,7,4,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,9,1,4,1,7,7,1,3,-1,-1,-1,-1,-1,-1,-1},
{4,9,1,4,1,7,0,8,1,8,7,1,-1,-1,-1,-1},
{4,0,3,7,4,3,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{4,8,7,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{9,10,8,10,11,8,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{3,0,9,3,9,11,11,9,10,-1,-1,-1,-1,-1,-1,-1},
{0,1,10,0,10,8,8,10,11,-1,-1,-1,-1,-1,-1,-1},
{3,1,10,11,3,10,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,2,11,1,11,9,9,11,8,-1,-1,-1,-1,-1,-1,-1},
{3,0,9,3,9,11,1,2,9,2,11,9,-1,-1,-1,-1},
{0,2,11,8,0,11,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{3,2,11,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{2,3,8,2,8,10,10,8,9,-1,-1,-1,-1,-1,-1,-1},
{9,10,2,0,9,2,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{2,3,8,2,8,10,0,1,8,1,10,8,-1,-1,-1,-1},
{1,10,2,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{1,3,8,9,1,8,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,9,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{0,3,8,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
};
// clang-format on
+68
View File
@@ -0,0 +1,68 @@
import FluidGridData;
// Diagnostics-only: sums the interior of several fields so we can watch
// "mass" over time on the CPU. Not part of the simulation.
struct Params
{
// read-only
FluidGridData<float> density;
FluidGridData<float> phi;
FluidGridData<float> velocityX;
FluidGridData<float> velocityY;
FluidGridData<float> velocityZ;
// read-write: [0]=density sum, [1]=phi sum, [2]=|velocity| sum
RWStructuredBuffer<float> out;
};
ParameterBlock<Params> params;
// Reduce the 32x8 thread group via shared memory, then one atomic add per group.
groupshared float laneD[256];
groupshared float laneP[256];
groupshared float laneV[256];
// One thread group per z-slice; each thread covers a 32x8 tile of the x/y plane.
[numthreads(32, 8, 1)]
[shader("compute")]
void massProbe(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> density = params.density;
FluidGridData<float> phi = params.phi;
FluidGridData<float> velocityX = params.velocityX;
FluidGridData<float> velocityY = params.velocityY;
FluidGridData<float> velocityZ = params.velocityZ;
RWStructuredBuffer<float> out = params.out;
// Interior only: skip the 1-cell boundary shell (matches the other passes).
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;
float dSum = density[x, y, z];
float pSum = phi[x, y, z];
float vSum = sqrt(velocityX[x, y, z] * velocityX[x, y, z] +
velocityY[x, y, z] * velocityY[x, y, z] +
velocityZ[x, y, z] * velocityZ[x, y, z]);
uint lane = dispatchThreadID.x + dispatchThreadID.y * 32; // 0..255
laneD[lane] = dSum;
laneP[lane] = pSum;
laneV[lane] = vSum;
GroupMemoryBarrierWithGroupSync();
// Lane 0 of the group sums all 256 lanes and atomically adds to the global total.
if (lane == 0)
{
float td = 0.0f, tp = 0.0f, tv = 0.0f;
for (uint i = 0; i < 256; ++i)
{
td += laneD[i];
tp += laneP[i];
tv += laneV[i];
}
InterlockedAdd(out[0], td);
InterlockedAdd(out[1], tp);
InterlockedAdd(out[2], tv);
}
}
+33
View File
@@ -0,0 +1,33 @@
import FluidGridData;
struct Params
{
// read-write
FluidGridData<float> velocityX;
// read-write
FluidGridData<float> velocityY;
// read-write
FluidGridData<float> velocityZ;
// read-only
FluidGridData<float> pressure;
};
ParameterBlock<Params> params;
[shader("compute")]
[numthreads(32, 8, 1)]
void project(uint3 dispatchThreadID : SV_DispatchThreadID)
{
FluidGridData<float> velocityX = params.velocityX;
FluidGridData<float> velocityY = params.velocityY;
FluidGridData<float> velocityZ = params.velocityZ;
FluidGridData<float> pressure = params.pressure;
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;
velocityX[x, y, z] -= 0.5f * (pressure[x + 1, y, z] - pressure[x - 1, y, z]);
velocityY[x, y, z] -= 0.5f * (pressure[x, y + 1, z] - pressure[x, y - 1, z]);
velocityZ[x, y, z] -= 0.5f * (pressure[x, y, z + 1] - pressure[x, y, z - 1]);
}
+62
View File
@@ -0,0 +1,62 @@
import Common;
import FluidGridData;
import LightEnv;
import MaterialParameter;
struct Params
{
float4x4 transform;
StructuredBuffer<float> vertexBuffer;
StructuredBuffer<float> normalBuffer;
StructuredBuffer<uint> indexBuffer;
};
ParameterBlock<Params> params;
struct VertexOut
{
float3 position_WS : POSITION;
float4 position_CS : SV_Position;
float3 normal : NORMAL;
};
[shader("vertex")]
VertexOut vertexMain(uint vertexId : SV_VertexID)
{
VertexOut output;
uint index = params.indexBuffer[vertexId];
float3 vertex = float3(params.vertexBuffer[index * 3 + 0],
params.vertexBuffer[index * 3 + 1],
params.vertexBuffer[index * 3 + 2]);
float3 normal = float3(params.normalBuffer[index * 3 + 0],
params.normalBuffer[index * 3 + 1],
params.normalBuffer[index * 3 + 2]);
vertex = mul(params.transform, float4(vertex, 1)).xyz;
normal = mul((float3x3)params.transform, normal);
output.position_WS = vertex;
output.position_CS = mul(pViewParams.viewProjectionMatrix, float4(vertex, 1));
output.normal = normal;
return output;
}
[shader("pixel")]
float4 fragmentMain(VertexOut input) : SV_Target
{
BlinnPhong brdf;
brdf.baseColor = float3(0, 0, 1);
brdf.alpha = 1;
brdf.specularColor = float3(1, 1, 1);
brdf.normal = input.normal;
brdf.shininess = 32;
brdf.ambient = float3(0.1, 0.1, 0.1);
brdf.emissive = float3(0, 0, 0);
float3 result = float3(0, 0, 0);
float3 viewDir = normalize(pViewParams.cameraPosition_WS.xyz - input.position_WS);
for(int i = 0; i < pLightEnv.numDirectionalLights; i++)
{
LightingParameter lightParams;
lightParams.position_WS = input.position_WS;
lightParams.viewDir_WS = viewDir;
result += pLightEnv.directionalLights[i].illuminate(lightParams, brdf);
}
return float4(result, 1);
}
+89
View File
@@ -0,0 +1,89 @@
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<BoundaryPlane> planes;
// read-write
FluidGridData<float> grid;
};
ParameterBlock<Params> 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(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<float> grid = params.grid;
float3 cellPos = float3(pos);
int numAffecting = 0;
float valueSum = 0.0f;
for (uint p = 0; p < params.numPlanes; p++)
{
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++;
}
}
if (numAffecting > 0)
{
// For all quantities (velocity components AND scalars: pressure,
// divergence, density) the boundary cell takes the (factor-scaled)
// interior neighbor value. For scalars the factor is 1 (Neumann / copy),
// which is the correct boundary condition for the pressure Poisson solve
// (zero normal gradient) and for the divergence. Do NOT zero the scalar
// boundary: zeroing the pressure shell breaks the Poisson Neumann
// condition, corrupts the projection, and injects energy into the
// velocity field (observed as |vel|_sum runaway).
grid[uint3(pos)] = valueSum / float(numAffecting);
}
}
+67
View File
@@ -0,0 +1,67 @@
import FluidGridData;
struct Params
{
float dtau;
// read-write: phi being updated (output)
FluidGridData<float> phi;
// read-only: phi from previous iteration (for gradient computation)
FluidGridData<float> phiPrev;
// read-only: phi snapshot from before reinitialization (for sign function)
FluidGridData<float> phiSign;
};
ParameterBlock<Params> params;
// Smooth sign function to avoid discontinuity at zero
float smoothSign(float x, float dx)
{
return x / sqrt(x * x + dx * dx);
}
[shader("compute")]
[numthreads(32, 8, 1)]
void reinitialize(uint3 dispatchThreadID : SV_DispatchThreadID)
{
uint x = dispatchThreadID.x + 1;
uint y = dispatchThreadID.y + 1;
uint z = dispatchThreadID.z + 1;
FluidGridData<float> phi = params.phi;
FluidGridData<float> phiPrev = params.phiPrev;
FluidGridData<float> 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 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| 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;
if (s > 0)
{
float ax = max(max(dxm, 0.0f), -min(dxp, 0.0f));
float ay = max(max(dym, 0.0f), -min(dyp, 0.0f));
float az = max(max(dzm, 0.0f), -min(dzp, 0.0f));
gradPhi = sqrt(ax * ax + ay * ay + az * az) / dx;
}
else
{
float ax = max(-min(dxm, 0.0f), max(dxp, 0.0f));
float ay = max(-min(dym, 0.0f), max(dyp, 0.0f));
float az = max(-min(dzm, 0.0f), max(dzp, 0.0f));
gradPhi = sqrt(ax * ax + ay * ay + az * az) / dx;
}
// Update: push |grad phi| toward 1
phi[x, y, z] = p - dtau * s * (gradPhi - 1.0f);
}
-1
View File
@@ -21,7 +21,6 @@ struct ViewParameter
uint frameIndex;
float time;
};
layout(set=0)
ParameterBlock<ViewParameter> pViewParams;
float4 worldToModel(float4x4 inverseTransform, float4 world)
-1
View File
@@ -71,7 +71,6 @@ struct LightEnv
Texture2D brdfLUT;
SamplerState lutSampler;
};
layout(set=3)
ParameterBlock<LightEnv> pLightEnv;
interface IBRDF
-1
View File
@@ -14,7 +14,6 @@ struct MaterialResources
SamplerState samplerArray[512];
StructuredBuffer<float> floatArray;
};
layout(set=4)
ParameterBlock<MaterialResources> pResources;
Texture2D getMaterialTextureParameter(uint index)
+15 -14
View File
@@ -45,20 +45,6 @@ struct MeshletCullingInfo
}
};
struct DrawCallOffsets
{
uint instanceOffset;
uint textureOffset;
uint samplerOffset;
uint floatOffset;
};
#ifdef RAY_TRACING
layout(shaderRecordEXT)
#else
layout(push_constant)
#endif
ConstantBuffer<DrawCallOffsets> pOffsets;
struct Scene
{
StructuredBuffer<float> positions;
@@ -73,6 +59,21 @@ struct Scene
};
ParameterBlock<Scene> pScene;
struct DrawCallOffsets
{
uint instanceOffset;
uint textureOffset;
uint samplerOffset;
uint floatOffset;
};
#ifdef RAY_TRACING
// Shader record buffers are accessed via a different mechanism and don't use descriptor set bindings
layout(set = 15, shaderRecordEXT)
#else
layout(push_constant)
#endif
ConstantBuffer<DrawCallOffsets> pOffsets;
uint32_t encodePrimitive(uint32_t meshletId)
{
return meshletId;
@@ -34,5 +34,4 @@ struct StaticMeshVertexData
StructuredBuffer<uint16_t> color;
StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS];
};
layout(set = 1)
ParameterBlock<StaticMeshVertexData> pVertexData;
+2 -2
View File
@@ -1,12 +1,12 @@
import Common;
import MaterialParameter;
import LightEnv;
import Scene;
import RayTracingData;
import VertexData;
import Material;
import StaticMeshVertexData;
import MATERIAL_FILE_NAME;
import RayTracingData;
import Scene;
// simplification: all BLAS only have 1 geometry
@@ -9,7 +9,6 @@ struct RayTracingParams
TextureCube<float4> skyBox;
SamplerState skyBoxSampler;
};
layout(set=5)
ParameterBlock<RayTracingParams> pRayTracingParams;
struct RayPayload
-2
View File
@@ -78,5 +78,3 @@ void PlayView::render() {
GameView::render();
renderTimestamp->write(Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT, "RenderEnd");
}
void PlayView::keyCallback(KeyCode code, InputAction action, KeyModifier modifier) { GameView::keyCallback(code, action, modifier); }
-2
View File
@@ -12,8 +12,6 @@ class PlayView : public GameView {
virtual void prepareRender() override;
virtual void render() override;
virtual void keyCallback(Seele::KeyCode code, Seele::InputAction action, Seele::KeyModifier modifier) override;
private:
std::thread queryThread;
-1
View File
@@ -93,7 +93,6 @@ EnvironmentLoader::EnvironmentLoader(Gfx::PGraphics graphics) : graphics(graphic
{"precomputeBRDF", "EnvironmentMapping"},
},
.rootSignature = cubePipelineLayout,
.dumpIntermediate = true,
});
cubePipelineLayout->create();
+1 -3
View File
@@ -6,7 +6,6 @@
#include "Graphics/Mesh.h"
#include "Graphics/Shader.h"
#include "Graphics/StaticMeshVertexData.h"
#include "ThreadPool.h"
#include <Asset/MaterialLoader.h>
#include <Asset/TextureLoader.h>
#include <assimp/Importer.hpp>
@@ -14,10 +13,9 @@
#include <assimp/material.h>
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <fmt/core.h>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <set>
#include <stb_image_write.h>
using namespace Seele;
+1 -1
View File
@@ -114,7 +114,7 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) {
.uastcFlags = KTX_PACK_UASTC_LEVEL_DEFAULT,
.uastcRDO = true,
};
KTX_ASSERT(ktxTexture2_CompressBasisEx(kTexture, &basisParams));
// KTX_ASSERT(ktxTexture2_CompressBasis(kTexture, 0));
//KTX_ASSERT(ktxTexture2_DeflateZstd(kTexture, 10));
char writer[100];
+4 -3
View File
@@ -8,6 +8,7 @@
using namespace Seele;
using namespace Seele::Editor;
using namespace Seele::UI;
InspectorView::InspectorView(Gfx::PGraphics graphics, PWindow window, const ViewportCreateInfo& createInfo)
: View(graphics, window, createInfo, "InspectorView"),
@@ -15,7 +16,7 @@ InspectorView::InspectorView(Gfx::PGraphics graphics, PWindow window, const View
new UI::Div<M_8>(UI::Attributes{}, {
new UI::Div<BG_Red, Inline>({},
{
new UI::Text<Font_Arial, Text_9XL>("Other Test T"),
new UI::Text<Font_Arial, Text_8XL>("Other Test T"),
}),
new UI::Text<Font_Arial>("Test fasdf agll;sdfgkj ahsf"),
}))) {
@@ -38,11 +39,11 @@ void InspectorView::render() { renderGraph.render(Component::Camera(), Component
void InspectorView::applyArea(URect ) {}
void InspectorView::keyCallback(KeyCode, InputAction, KeyModifier) {}
void InspectorView::keyCallback(KeyCode, InputAction, KeyModifierFlags) {}
void InspectorView::mouseMoveCallback(double, double) {}
void InspectorView::mouseButtonCallback(MouseButton, InputAction, KeyModifier) {}
void InspectorView::mouseButtonCallback(MouseButton, InputAction, KeyModifierFlags) {}
void InspectorView::scrollCallback(double, double) {}
+3 -2
View File
@@ -3,6 +3,7 @@
#include "Graphics/RenderPass/UIPass.h"
#include "Window/View.h"
#include "UI/System.h"
#include "UI/Style/Fonts.h"
namespace Seele {
DECLARE_REF(Actor)
@@ -26,9 +27,9 @@ class InspectorView : public View {
RenderGraph renderGraph;
Component::Camera cam;
virtual void keyCallback(KeyCode code, InputAction action, KeyModifier modifier) override;
virtual void keyCallback(KeyCode code, InputAction action, KeyModifierFlags modifier) override;
virtual void mouseMoveCallback(double xPos, double yPos) override;
virtual void mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier) override;
virtual void mouseButtonCallback(MouseButton button, InputAction action, KeyModifierFlags modifier) override;
virtual void scrollCallback(double xOffset, double yOffset) override;
virtual void fileCallback(int count, const char** paths) override;
};
+2 -1
View File
@@ -2,6 +2,7 @@
#include "Graphics/Enums.h"
#include "MinimalEngine.h"
#include "Window/Window.h"
#include <iostream>
using namespace Seele;
using namespace Seele::Editor;
@@ -21,7 +22,7 @@ void PlayView::prepareRender() { GameView::prepareRender(); }
void PlayView::render() { GameView::render(); }
void PlayView::keyCallback(KeyCode code, InputAction action, KeyModifier modifier) {
void PlayView::keyCallback(KeyCode code, InputAction action, KeyModifierFlags modifier) {
GameView::keyCallback(code, action, modifier);
if (code == KeyCode::KEY_P && action == InputAction::RELEASE) {
getGlobals().usePositionOnly = !getGlobals().usePositionOnly;
+1 -1
View File
@@ -14,7 +14,7 @@ class PlayView : public GameView {
virtual void prepareRender() override;
virtual void render() override;
virtual void keyCallback(Seele::KeyCode code, Seele::InputAction action, Seele::KeyModifier modifier) override;
virtual void keyCallback(Seele::KeyCode code, Seele::InputAction action, Seele::KeyModifierFlags modifier) override;
private:
};
+2 -2
View File
@@ -42,11 +42,11 @@ void SceneView::prepareRender() {}
void SceneView::render() { renderGraph.render(viewportCamera, Component::Transform()); }
void SceneView::keyCallback(KeyCode code, InputAction action, KeyModifier) { cameraSystem.keyCallback(code, action); }
void SceneView::keyCallback(KeyCode code, InputAction action, KeyModifierFlags) { cameraSystem.keyCallback(code, action); }
void SceneView::mouseMoveCallback(double xPos, double yPos) { cameraSystem.mouseMoveCallback(xPos, yPos); }
void SceneView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier) {
void SceneView::mouseButtonCallback(MouseButton button, InputAction action, KeyModifierFlags) {
cameraSystem.mouseButtonCallback(button, action);
}
+2 -2
View File
@@ -29,9 +29,9 @@ class SceneView : public View {
ViewportControl cameraSystem;
virtual void keyCallback(KeyCode code, InputAction action, KeyModifier modifier) override;
virtual void keyCallback(KeyCode code, InputAction action, KeyModifierFlags modifier) override;
virtual void mouseMoveCallback(double xPos, double yPos) override;
virtual void mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier) override;
virtual void mouseButtonCallback(MouseButton button, InputAction action, KeyModifierFlags modifier) override;
virtual void scrollCallback(double xOffset, double yOffset) override;
virtual void fileCallback(int count, const char** paths) override;
};
+30 -34
View File
@@ -12,6 +12,7 @@
#include "Graphics/StaticMeshVertexData.h"
#include "Window/PlayView.h"
#include "Window/WindowManager.h"
#include "Window/InspectorView.h"
#include <fmt/core.h>
using namespace Seele;
@@ -22,8 +23,8 @@ static Gfx::OGraphics graphics;
int main() {
std::string gameName = "MeshShadingDemo";
std::filesystem::path outputPath = fmt::format("/Users/dynamitos/{0}Game/", gameName);
std::filesystem::path sourcePath = fmt::format("/Users/dynamitos/{0}/", gameName);
std::filesystem::path outputPath = fmt::format("/home/dynamitos/{0}Game/", gameName);
std::filesystem::path sourcePath = fmt::format("/home/dynamitos/{0}/", gameName);
#ifdef WIN32
std::string libraryEnding = "dll";
#elif __APPLE__
@@ -31,7 +32,7 @@ int main() {
#else
std::string libraryEnding = "so";
#endif
std::filesystem::path binaryPath = sourcePath / "build/Debug" / fmt::format("{}.{}", gameName, libraryEnding);
std::filesystem::path binaryPath = sourcePath / "bin" / fmt::format("{}.{}", gameName, libraryEnding);
std::filesystem::path cmakePath = outputPath / "cmake";
if (true) {
graphics = new Vulkan::Graphics();
@@ -46,9 +47,9 @@ int main() {
AssetImporter::importFont(FontImportArgs{
.filePath = "./fonts/arial.ttf",
});
// AssetImporter::importEnvironmentMap(EnvironmentImportArgs{
// .filePath = sourcePath / "import" / "textures" / "newport_loft.hdr",
// });
AssetImporter::importEnvironmentMap(EnvironmentImportArgs{
.filePath = sourcePath / "import" / "textures" / "newport_loft.hdr",
});
// AssetImporter::importTexture(TextureImportArgs{
// .filePath = sourcePath / "import" / "textures" / "grass_block_side.png",
// .importPath = "",
@@ -57,19 +58,15 @@ int main() {
// .filePath = sourcePath / "import" / "models" / "main1_sponza" / "sponza.gltf",
// .importPath = "sponza",
//});
// AssetImporter::importMesh(MeshImportArgs{
// .filePath = sourcePath / "import" / "models" / "rttest.gltf",
// .importPath = "rttest",
// });
AssetImporter::importMesh(MeshImportArgs{
.filePath = sourcePath / "import" / "models" / "rttest.gltf",
.importPath = "rttest",
});
AssetImporter::importMesh(MeshImportArgs{
.filePath = sourcePath / "import" / "models" / "cube.fbx",
.importPath = "",
});
AssetImporter::importMesh(MeshImportArgs{
.filePath = sourcePath / "import" / "models" / "rttest.gltf",
.importPath = "rttest",
});
getThreadPool().waitIdle();
vd->commitMeshes();
@@ -80,26 +77,25 @@ int main() {
.preferredFormat = Gfx::SE_FORMAT_B8G8R8A8_SRGB,
};
auto window = windowManager->addWindow(graphics, mainWindowInfo);
ViewportCreateInfo sceneViewInfo = {
.dimensions =
{
.size = {1920, 1080},
.offset = {0, 0},
},
};
OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath);
sceneView->setFocused();
// OInspectorView inspectorView = new Editor::InspectorView(graphics, window,
// ViewportCreateInfo{
// .dimensions =
// {
// .size = {1920, 1080},
// .offset = {0, 0},
// },
// .fieldOfView = 0,
// .numSamples = Gfx::SE_SAMPLE_COUNT_1_BIT,
// });
//
// ViewportCreateInfo sceneViewInfo = {
// .dimensions =
// {
// .size = {1920, 1080},
// .offset = {0, 0},
// },
// };
// OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath);
// sceneView->setFocused();
OInspectorView inspectorView = new Editor::InspectorView(graphics, window,
ViewportCreateInfo{
.dimensions =
{
.size = {1920, 1080},
.offset = {0, 0},
},
});
inspectorView->setFocused();
window->show();
while (windowManager->isActive() && getGlobals().running) {
windowManager->render();
+1
View File
@@ -3,6 +3,7 @@
#include "Graphics/Texture.h"
#include "Window/WindowManager.h"
#include "ktx.h"
#include <iostream>
using namespace Seele;
+1 -1
View File
@@ -5,7 +5,7 @@ target_sources(Engine
Game.h
MinimalEngine.h
ThreadPool.h
ThreadPool.cpp "../../tests/Engine/UI/Element.cpp")
ThreadPool.cpp)
target_sources(Engine
PUBLIC FILE_SET HEADERS
+2
View File
@@ -6,6 +6,7 @@ target_sources(Engine
Collider.cpp
Component.h
DirectionalLight.h
FluidGrid.h
KeyboardInput.h
Mesh.h
MeshCollider.h
@@ -29,6 +30,7 @@ target_sources(Engine
Collider.h
Component.h
DirectionalLight.h
FluidGrid.h
KeyboardInput.h
MeshCollider.h
Mesh.h
+22
View File
@@ -0,0 +1,22 @@
#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;
float viscosity = 0.001f;
float diffusion = 0.0001f;
UVector gridSize = {128, 128, 128};
Vector gravity = {0.0f, -9.81f, 0.0f};
bool enableGravity = true;
Array<BoundaryPlane> extraPlanes;
};
} // namespace Component
} // namespace Seele
+2
View File
@@ -21,6 +21,7 @@ class RenderCommand {
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) = 0;
virtual void drawIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) = 0;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance) = 0;
virtual void drawIndexedIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) = 0;
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) = 0;
virtual void drawMeshIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) = 0;
virtual void traceRays(uint32 width, uint32 height, uint32 depth) = 0;
@@ -36,6 +37,7 @@ class ComputeCommand {
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) = 0;
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) = 0;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) = 0;
virtual void dispatch(UVector threadGroupSize) = 0;
virtual void dispatchIndirect(Gfx::PShaderBuffer buffer, uint32 offset) = 0;
std::string name;
};
+1 -1
View File
@@ -59,7 +59,7 @@ class DescriptorSet {
DescriptorSet(PDescriptorLayout layout);
virtual ~DescriptorSet();
virtual void writeChanges() = 0;
virtual void updateConstants(const std::string& name, uint32 offset, void* data) = 0;
virtual void updateConstants(const std::string& name, uint32 offset, const void* data) = 0;
virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PShaderBuffer shaderBuffer) = 0;
virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PVertexBuffer vertexBuffer) = 0;
virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PIndexBuffer indexBuffer) = 0;
+1 -1
View File
@@ -4,7 +4,7 @@
#include "MinimalEngine.h"
#include "RenderTarget.h"
#include "Resources.h"
#define ENABLE_VALIDATION
namespace Seele {
namespace Gfx {
+67 -71
View File
@@ -1,5 +1,6 @@
#include "BasePass.h"
#include "Asset/AssetRegistry.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Component/Camera.h"
#include "Graphics/Command.h"
#include "Graphics/Descriptor.h"
@@ -12,10 +13,9 @@
#include "Math/Matrix.h"
#include "Math/Vector.h"
#include "MinimalEngine.h"
#include "ShadowPass.h"
#include "Scene/Scene.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Scene/LightEnvironment.h"
#include "Scene/Scene.h"
#include "ShadowPass.h"
using namespace Seele;
@@ -59,10 +59,8 @@ BasePass::BasePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics)
.name = SHADOWSAMPLER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
});
shadowMappingLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = CASCADE_SPLIT_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER
});
shadowMappingLayout->addDescriptorBinding(
Gfx::DescriptorBinding{.name = CASCADE_SPLIT_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER});
shadowMappingLayout->create();
basePassLayout->addDescriptorLayout(lightCullingLayout);
@@ -116,6 +114,58 @@ BasePass::BasePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics)
.minLod = 0.0f,
.borderColor = Gfx::SE_BORDER_COLOR_FLOAT_OPAQUE_WHITE,
});
debugPipelineLayout = graphics->createPipelineLayout("DebugPassLayout");
debugPipelineLayout->addDescriptorLayout(viewParamsLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "DebugVertex",
.modules = {"Debug"},
.entryPoints =
{
{"vertexMain", "Debug"},
{"fragmentMain", "Debug"},
},
.rootSignature = debugPipelineLayout,
});
debugVertexShader = graphics->createVertexShader({0});
debugFragmentShader = graphics->createFragmentShader({1});
debugPipelineLayout->create();
skyboxDataLayout = graphics->createDescriptorLayout("pSkyboxData");
skyboxDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.name = "transformMatrix", .uniformLength = sizeof(Matrix4)});
skyboxDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.name = "fogBlend", .uniformLength = sizeof(Vector4)});
skyboxDataLayout->create();
textureLayout = graphics->createDescriptorLayout("pSkyboxTextures");
textureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = SKYBOXDAY_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
textureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = SKYBOXNIGHT_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
textureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = SKYBOXSAMPLER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
});
textureLayout->create();
pipelineLayout = graphics->createPipelineLayout("SkyboxLayout");
pipelineLayout->addDescriptorLayout(viewParamsLayout);
pipelineLayout->addDescriptorLayout(skyboxDataLayout);
pipelineLayout->addDescriptorLayout(textureLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "SkyboxVertex",
.modules = {"Skybox"},
.entryPoints = {{"vertexMain", "Skybox"}, {"fragmentMain", "Skybox"}},
.rootSignature = pipelineLayout,
});
vertexShader = graphics->createVertexShader({0});
fragmentShader = graphics->createFragmentShader({1});
pipelineLayout->create();
}
BasePass::~BasePass() {}
@@ -176,7 +226,7 @@ void BasePass::render() {
transparentCulling->updateTexture(LIGHTGRID_NAME, 0, tLightGrid->getDefaultView());
opaqueCulling->writeChanges();
transparentCulling->writeChanges();
shadowMappingLayout->reset();
shadowMapping = shadowMappingLayout->allocateDescriptorSet();
for (uint32 i = 0; i < NUM_CASCADES; ++i) {
@@ -268,7 +318,8 @@ void BasePass::render() {
command->bindPipeline(graphics->createGraphicsPipeline(std::move(pipelineInfo)));
}
command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(),
scene->getLightEnvironment()->getDescriptorSet(), Material::getDescriptorSet(), shadowMapping, opaqueCulling});
scene->getLightEnvironment()->getDescriptorSet(), Material::getDescriptorSet(), shadowMapping,
opaqueCulling});
for (const auto& drawCall : materialData.instances) {
command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT |
Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
@@ -382,8 +433,8 @@ void BasePass::render() {
transparentCommand->bindPipeline(pipeline);
}
transparentCommand->bindDescriptor({viewParamsSet, t.vertexData->getVertexDataSet(), t.vertexData->getInstanceDataSet(),
scene->getLightEnvironment()->getDescriptorSet(), Material::getDescriptorSet(), shadowMapping,
transparentCulling});
scene->getLightEnvironment()->getDescriptorSet(), Material::getDescriptorSet(),
shadowMapping, transparentCulling});
transparentCommand->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT |
Gfx::SE_SHADER_STAGE_FRAGMENT_BIT,
0, sizeof(VertexData::DrawCallOffsets), &t.offsets);
@@ -536,24 +587,6 @@ void BasePass::createRenderPass() {
// Debug rendering
{
debugPipelineLayout = graphics->createPipelineLayout("DebugPassLayout");
debugPipelineLayout->addDescriptorLayout(viewParamsLayout);
ShaderCompilationInfo createInfo = {
.name = "DebugVertex",
.modules = {"Debug"},
.entryPoints =
{
{"vertexMain", "Debug"},
{"fragmentMain", "Debug"},
},
.rootSignature = debugPipelineLayout,
};
graphics->beginShaderCompilation(createInfo);
debugVertexShader = graphics->createVertexShader({0});
debugFragmentShader = graphics->createFragmentShader({1});
debugPipelineLayout->create();
VertexInputStateCreateInfo inputCreate = {
.bindings =
{
@@ -605,27 +638,6 @@ void BasePass::createRenderPass() {
}
// Skybox
{
skyboxDataLayout = graphics->createDescriptorLayout("pSkyboxData");
skyboxDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.name = "transformMatrix", .uniformLength = sizeof(Matrix4)});
skyboxDataLayout->addDescriptorBinding(Gfx::DescriptorBinding{.name = "fogBlend", .uniformLength = sizeof(Vector4)});
skyboxDataLayout->create();
textureLayout = graphics->createDescriptorLayout("pSkyboxTextures");
textureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = SKYBOXDAY_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
textureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = SKYBOXNIGHT_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
textureLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = SKYBOXSAMPLER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER,
});
textureLayout->create();
skyboxSampler = graphics->createSampler({
.magFilter = Gfx::SE_FILTER_LINEAR,
.minFilter = Gfx::SE_FILTER_LINEAR,
@@ -642,23 +654,6 @@ void BasePass::createRenderPass() {
skyboxData.fogColor = skybox.fogColor;
skyboxData.blendFactor = skybox.blendFactor;
pipelineLayout = graphics->createPipelineLayout("SkyboxLayout");
pipelineLayout->addDescriptorLayout(viewParamsLayout);
pipelineLayout->addDescriptorLayout(skyboxDataLayout);
pipelineLayout->addDescriptorLayout(textureLayout);
ShaderCompilationInfo createInfo = {
.name = "SkyboxVertex",
.modules = {"Skybox"},
.entryPoints = {{"vertexMain", "Skybox"}, {"fragmentMain", "Skybox"}},
.rootSignature = pipelineLayout,
};
graphics->beginShaderCompilation(createInfo);
vertexShader = graphics->createVertexShader({0});
fragmentShader = graphics->createFragmentShader({1});
pipelineLayout->create();
Gfx::LegacyPipelineCreateInfo gfxInfo = {
.topology = Gfx::SE_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
.vertexShader = vertexShader,
@@ -673,10 +668,11 @@ void BasePass::createRenderPass() {
{
.polygonMode = Gfx::SE_POLYGON_MODE_FILL,
},
.depthStencilState = {
.depthTestEnable = true,
.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER,
},
.depthStencilState =
{
.depthTestEnable = true,
.depthCompareOp = Gfx::SE_COMPARE_OP_GREATER,
},
.colorBlend =
{
.attachmentCount = 1,
@@ -2,6 +2,7 @@
#include "Graphics/Buffer.h"
#include "Graphics/Descriptor.h"
#include "Graphics/RenderPass/ShadowPass.h"
#include "Component/Skybox.h"
#include "MinimalEngine.h"
#include "RenderPass.h"
@@ -6,6 +6,8 @@ target_sources(Engine
CachedDepthPass.cpp
DepthCullingPass.h
DepthCullingPass.cpp
FluidRenderPass.h
FluidRenderPass.cpp
LightCullingPass.h
LightCullingPass.cpp
RayTracingPass.h
@@ -17,6 +19,10 @@ target_sources(Engine
RenderPass.cpp
ShadowPass.h
ShadowPass.cpp
SimulationComputePass.h
SimulationComputePass.cpp
SurfaceExtractPass.h
SurfaceExtractPass.cpp
#TerrainRenderer.h
#TerrainRenderer.cpp
ToneMappingPass.h
@@ -34,6 +40,7 @@ target_sources(Engine
BasePass.h
CachedDepthPass.h
DepthCullingPass.h
FluidRenderPass.h
LightCullingPass.h
ToneMappingPass.h
RayTracingPass.h
@@ -41,6 +48,8 @@ target_sources(Engine
RenderGraphResources.h
RenderPass.h
ShadowPass.h
SimulationComputePass.h
SurfaceExtractPass.h
#TerrainRenderer.h
UIPass.h
VisibilityPass.h)
@@ -37,6 +37,24 @@ DepthCullingPass::DepthCullingPass(Gfx::PGraphics graphics, PScene scene) : Rend
.size = sizeof(MipParam),
.name = "pMipParam"
});
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "DepthMipCompute",
.modules = {"DepthMipGen"},
.entryPoints = {{"sourceCopy", "DepthMipGen"}, {"reduceLevel", "DepthMipGen"}},
.rootSignature = depthComputeLayout,
});
depthSourceCopyShader = graphics->createComputeShader({0});
depthComputeLayout->create();
Gfx::ComputePipelineCreateInfo pipelineCreateInfo = {
.computeShader = depthSourceCopyShader,
.pipelineLayout = depthComputeLayout,
};
depthSourceCopy = graphics->createComputePipeline(pipelineCreateInfo);
depthReduceLevelShader = graphics->createComputeShader({1});
pipelineCreateInfo.computeShader = depthReduceLevelShader;
depthReduceLevel = graphics->createComputePipeline(pipelineCreateInfo);
if (graphics->supportMeshShading()) {
graphics->getShaderCompiler()->registerRenderPass("DepthPass", Gfx::PassConfig{
@@ -255,24 +273,6 @@ void DepthCullingPass::publishOutputs() {
.name = "DepthMipBuffer",
});
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "DepthMipCompute",
.modules = {"DepthMipGen"},
.entryPoints = {{"sourceCopy", "DepthMipGen"}, {"reduceLevel", "DepthMipGen"}},
.rootSignature = depthComputeLayout,
});
depthSourceCopyShader = graphics->createComputeShader({0});
depthComputeLayout->create();
Gfx::ComputePipelineCreateInfo pipelineCreateInfo = {
.computeShader = depthSourceCopyShader,
.pipelineLayout = depthComputeLayout,
};
depthSourceCopy = graphics->createComputePipeline(pipelineCreateInfo);
depthReduceLevelShader = graphics->createComputeShader({1});
pipelineCreateInfo.computeShader = depthReduceLevelShader;
depthReduceLevel = graphics->createComputePipeline(pipelineCreateInfo);
query = graphics->createPipelineStatisticsQuery("DepthPipelineStatistics");
resources->registerQueryOutput("DEPTH_QUERY", query);
@@ -0,0 +1,151 @@
#include "FluidRenderPass.h"
#include "Component/Transform.h"
#include "Graphics/Command.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Graphics.h"
#include "Graphics/Initializer.h"
#include "Graphics/RenderTarget.h"
#include "Graphics/Shader.h"
#include "Math/Matrix.h"
#include "Scene/LightEnvironment.h"
#include "Scene/Scene.h"
#include "Scene/FluidScene.h"
using namespace Seele;
FluidRenderPass::FluidRenderPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(scene) {
pipelineLayout = graphics->createPipelineLayout("FluidPipelineLayout");
descriptorLayout = graphics->createDescriptorLayout("params");
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "transform",
.uniformLength = sizeof(Matrix4),
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "vertexBuffer",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "normalBuffer",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "indexBuffer",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->create();
pipelineLayout->addDescriptorLayout(viewParamsLayout);
pipelineLayout->addDescriptorLayout(descriptorLayout);
pipelineLayout->addDescriptorLayout(scene->getLightEnvironment()->getDescriptorLayout());
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "Fluid",
.modules = {"Render"},
.entryPoints = {{"vertexMain", "Render"}, {"fragmentMain", "Render"}},
.rootSignature = pipelineLayout,
});
pipelineLayout->create();
vertexShader = graphics->createVertexShader({0});
fragmentShader = graphics->createFragmentShader({1});
}
FluidRenderPass::~FluidRenderPass() {}
void FluidRenderPass::beginFrame(const Seele::Component::Camera& camera, const Seele::Component::Transform& transform) {
updateViewParameters(camera, transform);
}
void FluidRenderPass::render() {
viewParamsSet = createViewParamsSet();
graphics->beginRenderPass(renderPass);
descriptorLayout->reset();
for (const auto& [id, data] : scene->getFluidScene()->getFluidDataMap()) {
descriptorSet = descriptorLayout->allocateDescriptorSet();
Matrix4 transformMatrix = data.transform.toMatrix();
descriptorSet->updateConstants("transform", 0, &transformMatrix);
descriptorSet->updateBuffer("vertexBuffer", 0, data.vertexBuffer);
descriptorSet->updateBuffer("normalBuffer", 0, data.normalBuffer);
descriptorSet->updateBuffer("indexBuffer", 0, data.indexBuffer);
descriptorSet->writeChanges();
Gfx::ORenderCommand renderCommand = graphics->createRenderCommand();
renderCommand->setViewport(viewport);
renderCommand->bindPipeline(pipeline);
renderCommand->bindDescriptor(viewParamsSet);
renderCommand->bindDescriptor(descriptorSet);
renderCommand->bindDescriptor(scene->getLightEnvironment()->getDescriptorSet());
renderCommand->drawIndirect(data.surfaceCountBuffer, 0, 1, 0);
graphics->executeCommands(std::move(renderCommand));
}
graphics->endRenderPass();
}
void FluidRenderPass::endFrame() {}
void FluidRenderPass::publishOutputs() {
}
void FluidRenderPass::createRenderPass() {
colorAttachment = resources->requestRenderTarget("BASEPASS_COLOR");
colorAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
colorAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
depthAttachment = resources->requestRenderTarget("BASEPASS_DEPTH");
depthAttachment.setLoadOp(Gfx::SE_ATTACHMENT_LOAD_OP_LOAD);
depthAttachment.setInitialLayout(Gfx::SE_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
Gfx::RenderTargetLayout layout = Gfx::RenderTargetLayout{
.colorAttachments = {colorAttachment},
.depthAttachment = depthAttachment,
};
Array<Gfx::SubPassDependency> dependency = {
{
.srcSubpass = ~0U,
.dstSubpass = 0,
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
},
{
.srcSubpass = 0,
.dstSubpass = ~0U,
.srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | Gfx::SE_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT |
Gfx::SE_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
.srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
.dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT |
Gfx::SE_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
},
};
renderPass = graphics->createRenderPass(layout, dependency, viewport->getRenderArea(), "FluidRenderPass");
pipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{
.vertexShader = vertexShader,
.fragmentShader = fragmentShader,
.renderPass = renderPass,
.pipelineLayout = pipelineLayout,
.multisampleState =
{
.samples = Gfx::SE_SAMPLE_COUNT_1_BIT,
},
.rasterizationState =
{
.cullMode = Gfx::SE_CULL_MODE_NONE,
},
.colorBlend =
{
.attachmentCount = 1,
.blendAttachments =
{
Gfx::ColorBlendState::BlendAttachment{
.blendEnable = true,
},
},
},
});
}
@@ -0,0 +1,38 @@
#pragma once
#include "Component/Transform.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Initializer.h"
#include "Graphics/RenderPass/RenderPass.h"
#include "MinimalEngine.h"
#include "Scene/Scene.h"
namespace Seele {
class FluidRenderPass : public RenderPass {
public:
FluidRenderPass(Gfx::PGraphics graphics, PScene scene);
virtual ~FluidRenderPass() override;
virtual void beginFrame(const Component::Camera& camera, const Component::Transform& transform) override;
virtual void render() override;
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
private:
Gfx::RenderTargetAttachment colorAttachment;
Gfx::RenderTargetAttachment depthAttachment;
Gfx::ODescriptorSet viewParamsSet;
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::ODescriptorSet descriptorSet;
Gfx::OVertexShader vertexShader;
Gfx::OFragmentShader fragmentShader;
Gfx::PGraphicsPipeline pipeline;
PScene scene;
};
DEFINE_REF(FluidRenderPass)
} // namespace Seele
@@ -8,10 +8,141 @@
#include "Math/Vector.h"
#include "RenderGraph.h"
#include "Scene/Scene.h"
#include "Scene/LightEnvironment.h"
using namespace Seele;
LightCullingPass::LightCullingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(scene) {}
LightCullingPass::LightCullingPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(scene) {
dispatchParamsLayout = graphics->createDescriptorLayout("pDispatchParams");
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "numThreadGroups",
.uniformLength = sizeof(UVector4),
});
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "numThreads",
.uniformLength = sizeof(UVector4),
});
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = FRUSTUMBUFFER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
dispatchParamsLayout->create();
frustumLayout = graphics->createPipelineLayout("FrustumLayout");
frustumLayout->addDescriptorLayout(viewParamsLayout);
frustumLayout->addDescriptorLayout(dispatchParamsLayout);
ShaderCompilationInfo createInfo = {
.name = "Frustum",
.modules = {"ComputeFrustums"},
.entryPoints = {{"computeFrustums", "ComputeFrustums"}},
.rootSignature = frustumLayout,
};
graphics->beginShaderCompilation(createInfo);
frustumShader = graphics->createComputeShader({0});
// Have to compile shader before finalizing layout as parameters get mapped later
frustumLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = frustumShader;
pipelineInfo.pipelineLayout = frustumLayout;
frustumPipeline = graphics->createComputePipeline(pipelineInfo);
cullingDescriptorLayout = graphics->createDescriptorLayout("pCullingParams");
// DepthTexture
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = DEPTHATTACHMENT_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
// o_lightIndexCounter
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = OLIGHTINDEXCOUNTER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
// t_lightIndexCounter
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = TLIGHTINDEXCOUNTER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
// o_lightIndexList
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = OLIGHTINDEXLIST_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
// t_lightIndexList
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = TLIGHTINDEXLIST_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
// o_lightGrid
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = OLIGHTGRID_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
// t_lightGrid
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = TLIGHTGRID_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
cullingDescriptorLayout->create();
lightEnv = scene->getLightEnvironment();
{
cullingLayout = graphics->createPipelineLayout("CullingLayout");
cullingLayout->addDescriptorLayout(viewParamsLayout);
cullingLayout->addDescriptorLayout(dispatchParamsLayout);
cullingLayout->addDescriptorLayout(cullingDescriptorLayout);
cullingLayout->addDescriptorLayout(lightEnv->getDescriptorLayout());
ShaderCompilationInfo createInfo = {
.name = "Culling",
.modules = {"LightCulling"},
.entryPoints = {{"cullLights", "LightCulling"}},
.rootSignature = cullingLayout,
};
graphics->beginShaderCompilation(createInfo);
cullingShader = graphics->createComputeShader({0});
cullingLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = cullingShader;
pipelineInfo.pipelineLayout = cullingLayout;
cullingPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
{
cullingEnableLayout = graphics->createPipelineLayout("CullingEnabledLayout");
cullingEnableLayout->addDescriptorLayout(viewParamsLayout);
cullingEnableLayout->addDescriptorLayout(dispatchParamsLayout);
cullingEnableLayout->addDescriptorLayout(cullingDescriptorLayout);
cullingEnableLayout->addDescriptorLayout(lightEnv->getDescriptorLayout());
ShaderCompilationInfo createInfo = {
.name = "Culling",
.modules = {"LightCulling"},
.entryPoints = {{"cullLights", "LightCulling"}},
.rootSignature = cullingEnableLayout,
};
createInfo.defines["LIGHT_CULLING"] = "1";
graphics->beginShaderCompilation(createInfo);
cullingEnabledShader = graphics->createComputeShader({0});
cullingEnableLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = cullingShader;
pipelineInfo.pipelineLayout = cullingEnableLayout;
cullingEnabledPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
}
LightCullingPass::~LightCullingPass() {}
@@ -91,86 +222,9 @@ void LightCullingPass::publishOutputs() {
dispatchParamsSet = dispatchParamsLayout->allocateDescriptorSet();
dispatchParamsSet->updateConstants("numThreadGroups", 0, &numThreadGroups);
dispatchParamsSet->updateConstants("numThreads", 0, &numThreads);
dispatchParamsSet->updateBuffer(FRUSTUMBUFFER_NAME, 0, frustumBuffer);
dispatchParamsSet->writeChanges();
cullingDescriptorLayout = graphics->createDescriptorLayout("pCullingParams");
// DepthTexture
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = DEPTHATTACHMENT_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
// o_lightIndexCounter
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = OLIGHTINDEXCOUNTER_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,});
// t_lightIndexCounter
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = TLIGHTINDEXCOUNTER_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,});
// o_lightIndexList
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = OLIGHTINDEXLIST_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,});
// t_lightIndexList
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = TLIGHTINDEXLIST_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,});
// o_lightGrid
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = OLIGHTGRID_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,});
// t_lightGrid
cullingDescriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = TLIGHTGRID_NAME, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_IMAGE, .access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,});
cullingDescriptorLayout->create();
lightEnv = scene->getLightEnvironment();
{
cullingLayout = graphics->createPipelineLayout("CullingLayout");
cullingLayout->addDescriptorLayout(viewParamsLayout);
cullingLayout->addDescriptorLayout(dispatchParamsLayout);
cullingLayout->addDescriptorLayout(cullingDescriptorLayout);
cullingLayout->addDescriptorLayout(lightEnv->getDescriptorLayout());
ShaderCompilationInfo createInfo = {
.name = "Culling",
.modules = {"LightCulling"},
.entryPoints = {{"cullLights", "LightCulling"}},
.rootSignature = cullingLayout,
};
graphics->beginShaderCompilation(createInfo);
cullingShader = graphics->createComputeShader({0});
cullingLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = cullingShader;
pipelineInfo.pipelineLayout = cullingLayout;
cullingPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
{
cullingEnableLayout = graphics->createPipelineLayout("CullingEnabledLayout");
cullingEnableLayout->addDescriptorLayout(viewParamsLayout);
cullingEnableLayout->addDescriptorLayout(dispatchParamsLayout);
cullingEnableLayout->addDescriptorLayout(cullingDescriptorLayout);
cullingEnableLayout->addDescriptorLayout(lightEnv->getDescriptorLayout());
ShaderCompilationInfo createInfo = {
.name = "Culling",
.modules = {"LightCulling"},
.entryPoints = {{"cullLights", "LightCulling"}},
.rootSignature = cullingEnableLayout,
};
createInfo.defines["LIGHT_CULLING"] = "1";
graphics->beginShaderCompilation(createInfo);
cullingEnabledShader = graphics->createComputeShader({0});
cullingEnableLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = cullingShader;
pipelineInfo.pipelineLayout = cullingEnableLayout;
cullingEnabledPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
uint32 counterReset = 0;
ShaderBufferCreateInfo structInfo = {
.sourceData =
@@ -188,8 +242,7 @@ void LightCullingPass::publishOutputs() {
structInfo = {
.sourceData =
{
.size = (uint32)sizeof(uint32) * numThreadGroups.x * numThreadGroups.y *
numThreadGroups.z * 8192,
.size = (uint32)sizeof(uint32) * numThreadGroups.x * numThreadGroups.y * numThreadGroups.z * 8192,
.data = nullptr,
.owner = Gfx::QueueType::COMPUTE,
},
@@ -237,41 +290,6 @@ void LightCullingPass::setupFrustums() {
updateViewParameters(Component::Camera(), Component::Transform());
viewParamsSet = createViewParamsSet();
dispatchParamsLayout = graphics->createDescriptorLayout("pDispatchParams");
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "numThreadGroups",
.uniformLength = sizeof(UVector4),
});
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "numThreads",
.uniformLength = sizeof(UVector4),
});
dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = FRUSTUMBUFFER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
dispatchParamsLayout->create();
frustumLayout = graphics->createPipelineLayout("FrustumLayout");
frustumLayout->addDescriptorLayout(viewParamsLayout);
frustumLayout->addDescriptorLayout(dispatchParamsLayout);
ShaderCompilationInfo createInfo = {
.name = "Frustum",
.modules = {"ComputeFrustums"},
.entryPoints = {{"computeFrustums", "ComputeFrustums"}},
.rootSignature = frustumLayout,
.dumpIntermediate = true,
};
graphics->beginShaderCompilation(createInfo);
frustumShader = graphics->createComputeShader({0});
// Have to compile shader before finalizing layout as parameters get mapped later
frustumLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = frustumShader;
pipelineInfo.pipelineLayout = frustumLayout;
frustumPipeline = graphics->createComputePipeline(pipelineInfo);
frustumBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
@@ -299,6 +317,6 @@ void LightCullingPass::setupFrustums() {
commands.add(std::move(command));
graphics->executeCommands(std::move(commands));
frustumBuffer->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);
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
graphics->endDebugRegion();
}
@@ -4,9 +4,11 @@
#include "Graphics/RayTracing.h"
#include "Graphics/Shader.h"
#include "Graphics/StaticMeshVertexData.h"
#include "RenderPass.h"
#include "Material/Material.h"
#include "Material/MaterialInstance.h"
#include "Scene/LightEnvironment.h"
#include "RenderPass.h"
#include <iostream>
using namespace Seele;
@@ -55,6 +57,18 @@ RayTracingPass::RayTracingPass(Gfx::PGraphics graphics, PScene scene) : RenderPa
.size = sizeof(SampleParams),
.name = "pSamps",
});
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "RayGenMiss",
.modules = {"RayGen", "AnyHit", "Miss"},
.entryPoints = {{"raygen", "RayGen"}, {"anyhit", "AnyHit"}, {"miss", "Miss"}},
.defines = {{"RAY_TRACING", "1"}},
.rootSignature = pipelineLayout,
});
rayGen = graphics->createRayGenShader({0});
anyhit = graphics->createAnyHitShader({1});
miss = graphics->createMissShader({2});
pipelineLayout->create();
graphics->getShaderCompiler()->registerRenderPass("RayTracing", Gfx::PassConfig{
.baseLayout = pipelineLayout,
.mainFile = "ClosestHit",
@@ -212,20 +226,9 @@ void RayTracingPass::publishOutputs() {
texture->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_NONE, Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
Gfx::SE_ACCESS_SHADER_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT | Gfx::SE_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR);
resources->registerRenderPassOutput("BASEPASS_COLOR", Gfx::RenderTargetAttachment(texture->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
ShaderCompilationInfo compileInfo = {
.name = "RayGenMiss",
.modules = {"RayGen", "AnyHit", "Miss"},
.entryPoints = {{"raygen", "RayGen"}, {"anyhit", "AnyHit"}, {"miss", "Miss"}},
.defines = {{"RAY_TRACING", "1"}},
.rootSignature = pipelineLayout,
};
graphics->beginShaderCompilation(compileInfo);
rayGen = graphics->createRayGenShader({0});
anyhit = graphics->createAnyHitShader({1});
miss = graphics->createMissShader({2});
pipelineLayout->create();
resources->registerRenderPassOutput("BASEPASS_COLOR",
Gfx::RenderTargetAttachment(texture->getDefaultView(), Gfx::SE_IMAGE_LAYOUT_UNDEFINED,
Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL));
}
void RayTracingPass::createRenderPass() {}
@@ -4,6 +4,7 @@
#include "Graphics/Initializer.h"
#include "Graphics/Shader.h"
#include "Math/Matrix.h"
#include "Scene/LightEnvironment.h"
#include <glm/ext/matrix_transform.hpp>
#include <glm/matrix.hpp>
@@ -0,0 +1,749 @@
#include "SimulationComputePass.h"
#include "Graphics/Buffer.h"
#include "Graphics/Command.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Graphics.h"
#include "Graphics/Initializer.h"
#include "Graphics/Shader.h"
#include "Math/Vector.h"
#include "Scene/FluidScene.h"
#include <algorithm>
#include <iostream>
using namespace Seele;
// Reinitialization is in pseudo-time; keep it gentle to avoid eroding the interface each frame.
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 = 100;
constexpr static int densityDiffuseIterations = 8;
constexpr static float pressureSORomega = 1.95f;
constexpr static float diffusionSORomega = 1.0f;
// Substep the semi-Lagrangian advection so the backtrace moves <~1 cell per
// step. A single full-dt advect at dt=0.1 moves many cells and smears the
// passive scalar (density) hard, which is the dominant source of mass loss.
constexpr static int advectionSubsteps = 8;
#define SWAP(a, b) \
{ \
auto temp = std::move(a); \
a = std::move(b); \
b = std::move(temp); \
}
SimulationComputePass::SimulationComputePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(scene) {
gridSizeLayout = graphics->createDescriptorLayout("gridParams");
gridSizeLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "gridSize",
.uniformLength = sizeof(UVector),
});
gridSizeLayout->create();
{
setBounds.pipelineLayout = graphics->createPipelineLayout("SetBoundsPipelineLayout");
setBounds.descriptorLayout = graphics->createDescriptorLayout("params");
setBounds.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.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,
});
setBounds.descriptorLayout->create();
setBounds.pipelineLayout->addDescriptorLayout(setBounds.descriptorLayout);
setBounds.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "SetBound",
.modules = {"SetBound"},
.entryPoints = {{"setBound", "SetBound"}},
.rootSignature = setBounds.pipelineLayout,
});
setBounds.pipelineLayout->create();
setBounds.shader = graphics->createComputeShader({0});
setBounds.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = setBounds.shader,
.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),
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "c",
.uniformLength = sizeof(float),
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "omega",
.uniformLength = sizeof(float),
});
linearSolve.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "color",
.uniformLength = sizeof(int),
});
linearSolve.descriptorLayout->create();
linearSolve.pipelineLayout->addDescriptorLayout(linearSolve.descriptorLayout);
linearSolve.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "LinearSolve",
.modules = {"LinearSolve"},
.entryPoints = {{"linearSolve", "LinearSolve"}},
.rootSignature = linearSolve.pipelineLayout,
});
linearSolve.pipelineLayout->create();
linearSolve.shader = graphics->createComputeShader({0});
linearSolve.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = linearSolve.shader,
.pipelineLayout = linearSolve.pipelineLayout,
});
}
{
computeDivergence.pipelineLayout = graphics->createPipelineLayout("ComputeDivergencePipelineLayout");
computeDivergence.descriptorLayout = graphics->createDescriptorLayout("params");
computeDivergence.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityX",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
computeDivergence.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityY",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
computeDivergence.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityZ",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
computeDivergence.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "divergence",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
computeDivergence.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "pressure",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
computeDivergence.descriptorLayout->create();
computeDivergence.pipelineLayout->addDescriptorLayout(computeDivergence.descriptorLayout);
computeDivergence.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "Divergence",
.modules = {"Divergence"},
.entryPoints = {{"computeDivergence", "Divergence"}},
.rootSignature = computeDivergence.pipelineLayout,
});
computeDivergence.pipelineLayout->create();
computeDivergence.shader = graphics->createComputeShader({0});
computeDivergence.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = computeDivergence.shader,
.pipelineLayout = computeDivergence.pipelineLayout,
});
}
{
project.pipelineLayout = graphics->createPipelineLayout("ProjectPipelineLayout");
project.descriptorLayout = graphics->createDescriptorLayout("params");
project.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityX",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
project.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityY",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
project.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityZ",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
project.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "pressure",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
project.descriptorLayout->create();
project.pipelineLayout->addDescriptorLayout(project.descriptorLayout);
project.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "Project",
.modules = {"Project"},
.entryPoints = {{"project", "Project"}},
.rootSignature = project.pipelineLayout,
});
project.pipelineLayout->create();
project.shader = graphics->createComputeShader({0});
project.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = project.shader,
.pipelineLayout = project.pipelineLayout,
});
}
{
advect.pipelineLayout = graphics->createPipelineLayout("AdvectPipelineLayout");
advect.descriptorLayout = graphics->createDescriptorLayout("params");
advect.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "density",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
advect.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "density0",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
advect.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityX",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
advect.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityY",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
advect.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityZ",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
advect.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "dt",
.uniformLength = sizeof(float),
});
advect.descriptorLayout->create();
advect.pipelineLayout->addDescriptorLayout(advect.descriptorLayout);
advect.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "Advect",
.modules = {"Advect"},
.entryPoints = {{"advect", "Advect"}},
.rootSignature = advect.pipelineLayout,
});
advect.pipelineLayout->create();
advect.shader = graphics->createComputeShader({0});
advect.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = advect.shader,
.pipelineLayout = advect.pipelineLayout,
});
}
{
reinitialize.pipelineLayout = graphics->createPipelineLayout("ReinitializePipelineLayout");
reinitialize.descriptorLayout = graphics->createDescriptorLayout("params");
reinitialize.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "dtau",
.uniformLength = sizeof(float),
});
reinitialize.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "phi",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
reinitialize.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.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();
reinitialize.pipelineLayout->addDescriptorLayout(reinitialize.descriptorLayout);
reinitialize.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "SignedDistance",
.modules = {"SignedDistance"},
.entryPoints = {{"reinitialize", "SignedDistance"}},
.rootSignature = reinitialize.pipelineLayout,
});
reinitialize.pipelineLayout->create();
reinitialize.shader = graphics->createComputeShader({0});
reinitialize.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = reinitialize.shader,
.pipelineLayout = reinitialize.pipelineLayout,
});
}
{
applyForces.pipelineLayout = graphics->createPipelineLayout("ApplyForcesPipelineLayout");
applyForces.descriptorLayout = graphics->createDescriptorLayout("params");
applyForces.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityX",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
applyForces.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityY",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
applyForces.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityZ",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
applyForces.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "force",
.uniformLength = sizeof(Vector),
});
applyForces.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "dt",
.uniformLength = sizeof(float),
});
applyForces.descriptorLayout->create();
applyForces.pipelineLayout->addDescriptorLayout(applyForces.descriptorLayout);
applyForces.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "ApplyForces",
.modules = {"ApplyForces"},
.entryPoints = {{"applyForces", "ApplyForces"}},
.rootSignature = applyForces.pipelineLayout,
});
applyForces.pipelineLayout->create();
applyForces.shader = graphics->createComputeShader({0});
applyForces.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = applyForces.shader,
.pipelineLayout = applyForces.pipelineLayout,
});
}
{
// Diagnostics-only mass probe: sums interior fields into a 3-float buffer.
massProbe.pipelineLayout = graphics->createPipelineLayout("MassProbePipelineLayout");
massProbe.descriptorLayout = graphics->createDescriptorLayout("params");
massProbe.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "density",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
massProbe.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "phi",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
massProbe.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityX",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
massProbe.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityY",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
massProbe.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "velocityZ",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
massProbe.descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "out",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
massProbe.descriptorLayout->create();
massProbe.pipelineLayout->addDescriptorLayout(massProbe.descriptorLayout);
massProbe.pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "MassProbe",
.modules = {"MassProbe"},
.entryPoints = {{"massProbe", "MassProbe"}},
.rootSignature = massProbe.pipelineLayout,
});
massProbe.pipelineLayout->create();
massProbe.shader = graphics->createComputeShader({0});
massProbe.pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = massProbe.shader,
.pipelineLayout = massProbe.pipelineLayout,
});
// 3 floats: [0]=density sum, [1]=phi sum, [2]=|velocity| sum.
probeOutBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = 3 * sizeof(float),
},
.name = "MassProbeOut",
});
}
}
SimulationComputePass::~SimulationComputePass() {}
void SimulationComputePass::beginFrame(const Seele::Component::Camera&, const Seele::Component::Transform&) {
setBounds.descriptorLayout->reset();
linearSolve.descriptorLayout->reset();
computeDivergence.descriptorLayout->reset();
project.descriptorLayout->reset();
advect.descriptorLayout->reset();
reinitialize.descriptorLayout->reset();
applyForces.descriptorLayout->reset();
gridSizeLayout->reset();
}
void SimulationComputePass::render() {
for (auto& [entityID, data] : scene->getFluidScene()->getFluidDataMap()) {
if (data.paused) {
continue;
}
gridSizeSet = gridSizeLayout->allocateDescriptorSet();
gridSizeSet->updateConstants("gridSize", 0, &data.gridSize);
gridSizeSet->writeChanges();
graphics->beginDebugRegion("Diffuse");
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.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.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);
SWAP(data.velocityY0Buffer, data.velocityYBuffer);
SWAP(data.velocityZ0Buffer, data.velocityZBuffer);
graphics->endDebugRegion();
graphics->beginDebugRegion("Project");
Gfx::PShaderBuffer divergence = data.velocityXBuffer;
Gfx::PShaderBuffer pressure = data.velocityYBuffer;
divergencePass(divergence, pressure, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, 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(pressure, divergence, 1, 6, pressureSORomega, data.gridSize);
setBoundsPass(pressure, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
projectPass(data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, pressure, 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");
// Substep ONLY the advection: each step uses dt/substeps so the
// backtrace displacement stays small, reducing the numerical (bilinear)
// dissipation of the semi-Lagrangian scheme. The pressure re-projection
// is a GLOBAL operation and must run exactly once per frame on the final
// velocity field — running it inside the substep loop over-corrects and
// injects energy into the velocity field (observed as |vel|_sum runaway).
const float subDt = data.dt / advectionSubsteps;
for (int sub = 0; sub < advectionSubsteps; ++sub) {
advectPass(data.velocityXBuffer, data.velocityX0Buffer, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, subDt,
data.gridSize);
advectPass(data.velocityYBuffer, data.velocityY0Buffer, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, subDt,
data.gridSize);
advectPass(data.velocityZBuffer, data.velocityZ0Buffer, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, subDt,
data.gridSize);
if (data.enableGravity) {
applyForcesPass(data.velocityXBuffer, data.velocityYBuffer, data.velocityZBuffer, data.gravity, subDt, data.gridSize);
}
// Set boundary conditions after advection/forces so the divergence sees correct wall values
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 ONCE per frame after all substeps to keep the final velocity
// field divergence-free.
Gfx::PShaderBuffer advectedDivergence = data.velocityXNextBuffer;
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(advectedPressure, advectedDivergence, 1, 6, pressureSORomega, data.gridSize);
setBoundsPass(advectedPressure, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
projectPass(data.velocityXBuffer, data.velocityYBuffer, data.velocityZBuffer, advectedPressure, 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);
SWAP(data.velocityY0Buffer, data.velocityYBuffer);
SWAP(data.velocityZ0Buffer, data.velocityZBuffer);
// diffuse density
graphics->beginDebugRegion("DiffuseDensity");
Gfx::PShaderBuffer densityCurrent = data.densityBuffer;
Gfx::PShaderBuffer density0 = data.density0Buffer;
graphics->copyBuffer(density0, densityCurrent);
densityCurrent->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);
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(densityCurrent, density0, a, c, diffusionSORomega, data.gridSize);
setBoundsPass(densityCurrent, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
graphics->endDebugRegion();
graphics->beginDebugRegion("AdvectDensity");
// Substep density advection with the same small subDt as the velocity
// advection so the passive scalar is not smeared by a large backtrace.
for (int sub = 0; sub < advectionSubsteps; ++sub) {
advectPass(data.density0Buffer, densityCurrent, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, subDt,
data.gridSize);
setBoundsPass(data.density0Buffer, 0, data.gridSize, data.planeBuffer, data.numPlanes);
}
graphics->endDebugRegion();
graphics->beginDebugRegion("AdvectLevelSet");
for (int sub = 0; sub < advectionSubsteps; ++sub) {
advectPass(data.phiBuffer, data.phi0Buffer, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, subDt, 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.scratchBuffer, data.gridSize, data.planeBuffer, data.numPlanes);
}
++data.frameCounter;
data.phiBuffer->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(data.phiBuffer, data.phi0Buffer);
data.phi0Buffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT);
graphics->endDebugRegion();
// Diagnostics: log interior field sums so we can see which quantity is decaying.
massProbePass(data.density0Buffer, data.phiBuffer, data.velocityX0Buffer, data.velocityY0Buffer, data.velocityZ0Buffer, data.gridSize);
}
}
void SimulationComputePass::endFrame() {}
void SimulationComputePass::publishOutputs() {}
void SimulationComputePass::createRenderPass() {}
UVector SimulationComputePass::getDispatchSize(const UVector& gridSize, const UVector& threadGroupSize) {
return {
(gridSize.x + threadGroupSize.x - 1) / threadGroupSize.x,
(gridSize.y + threadGroupSize.y - 1) / threadGroupSize.y,
(gridSize.z + threadGroupSize.z - 1) / threadGroupSize.z,
};
}
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);
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_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 grid, Gfx::PShaderBuffer grid0, float a, float c, float omega,
UVector gridSize) {
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));
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,
Seele::Gfx::PShaderBuffer velocityZ, UVector gridSize) {
Gfx::ODescriptorSet divergenceSet = computeDivergence.descriptorLayout->allocateDescriptorSet();
divergenceSet->updateBuffer("velocityX", 0, velocityX);
divergenceSet->updateBuffer("velocityY", 0, velocityY);
divergenceSet->updateBuffer("velocityZ", 0, velocityZ);
divergenceSet->updateBuffer("divergence", 0, divergence);
divergenceSet->updateBuffer("pressure", 0, pressure);
divergenceSet->writeChanges();
Gfx::OComputeCommand divergenceCommand = graphics->createComputeCommand();
divergenceCommand->bindPipeline(computeDivergence.pipeline);
divergenceCommand->bindDescriptor(divergenceSet);
divergenceCommand->bindDescriptor(gridSizeSet);
divergenceCommand->dispatch(getDispatchSize(gridSize, computeDivergence.threadGroupSize));
graphics->executeCommands(std::move(divergenceCommand));
// divergence is now in velocityXBuffer, pressure is in velocityYBuffer
divergence->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);
pressure->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::projectPass(Seele::Gfx::PShaderBuffer velocityX, Seele::Gfx::PShaderBuffer velocityY,
Seele::Gfx::PShaderBuffer velocityZ, Seele::Gfx::PShaderBuffer pressure, UVector gridSize) {
Gfx::ODescriptorSet projectSet = project.descriptorLayout->allocateDescriptorSet();
projectSet->updateBuffer("velocityX", 0, velocityX);
projectSet->updateBuffer("velocityY", 0, velocityY);
projectSet->updateBuffer("velocityZ", 0, velocityZ);
projectSet->updateBuffer("pressure", 0, pressure);
projectSet->writeChanges();
Gfx::OComputeCommand projectCommand = graphics->createComputeCommand();
projectCommand->bindPipeline(project.pipeline);
projectCommand->bindDescriptor(projectSet);
projectCommand->bindDescriptor(gridSizeSet);
projectCommand->dispatch(getDispatchSize(gridSize, project.threadGroupSize));
graphics->executeCommands(std::move(projectCommand));
velocityX->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);
velocityY->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);
velocityZ->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::advectPass(Gfx::PShaderBuffer quantity, Gfx::PShaderBuffer quantity0, Gfx::PShaderBuffer velocityX,
Gfx::PShaderBuffer velocityY, Gfx::PShaderBuffer velocityZ, float dt, UVector gridSize) {
Gfx::ODescriptorSet advectSet = this->advect.descriptorLayout->allocateDescriptorSet();
advectSet->updateBuffer("density", 0, quantity);
advectSet->updateBuffer("density0", 0, quantity0);
advectSet->updateBuffer("velocityX", 0, velocityX);
advectSet->updateBuffer("velocityY", 0, velocityY);
advectSet->updateBuffer("velocityZ", 0, velocityZ);
advectSet->updateConstants("dt", 0, &dt);
advectSet->writeChanges();
Gfx::OComputeCommand advectCommand = graphics->createComputeCommand();
advectCommand->bindPipeline(advect.pipeline);
advectCommand->bindDescriptor(advectSet);
advectCommand->bindDescriptor(gridSizeSet);
advectCommand->dispatch(getDispatchSize(gridSize, advect.threadGroupSize));
graphics->executeCommands(std::move(advectCommand));
quantity->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::applyForcesPass(Gfx::PShaderBuffer velocityX, Gfx::PShaderBuffer velocityY, Gfx::PShaderBuffer velocityZ, Vector gravity, float dt, UVector gridSize) {
Gfx::ODescriptorSet forcesSet = applyForces.descriptorLayout->allocateDescriptorSet();
forcesSet->updateBuffer("velocityX", 0, velocityX);
forcesSet->updateBuffer("velocityY", 0, velocityY);
forcesSet->updateBuffer("velocityZ", 0, velocityZ);
forcesSet->updateConstants("force", 0, &gravity);
forcesSet->updateConstants("dt", 0, &dt);
forcesSet->writeChanges();
Gfx::OComputeCommand forcesCommand = graphics->createComputeCommand();
forcesCommand->bindPipeline(applyForces.pipeline);
forcesCommand->bindDescriptor(forcesSet);
forcesCommand->bindDescriptor(gridSizeSet);
forcesCommand->dispatch(getDispatchSize(gridSize, applyForces.threadGroupSize));
graphics->executeCommands(std::move(forcesCommand));
velocityX->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);
velocityY->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);
velocityZ->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::massProbePass(Gfx::PShaderBuffer density, Gfx::PShaderBuffer phi, Gfx::PShaderBuffer velocityX, Gfx::PShaderBuffer velocityY,
Gfx::PShaderBuffer velocityZ, UVector gridSize) {
// Zero the 3-float accumulator before dispatching.
float zeros[3] = {0.0f, 0.0f, 0.0f};
probeOutBuffer->updateContents(0, 3 * sizeof(float), zeros);
probeOutBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_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);
Gfx::ODescriptorSet probeSet = massProbe.descriptorLayout->allocateDescriptorSet();
probeSet->updateBuffer("density", 0, density);
probeSet->updateBuffer("phi", 0, phi);
probeSet->updateBuffer("velocityX", 0, velocityX);
probeSet->updateBuffer("velocityY", 0, velocityY);
probeSet->updateBuffer("velocityZ", 0, velocityZ);
probeSet->updateBuffer("out", 0, probeOutBuffer);
probeSet->writeChanges();
Gfx::OComputeCommand probeCommand = graphics->createComputeCommand();
probeCommand->bindPipeline(massProbe.pipeline);
probeCommand->bindDescriptor(probeSet);
probeCommand->bindDescriptor(gridSizeSet);
probeCommand->dispatch(getDispatchSize(gridSize, massProbe.threadGroupSize));
graphics->executeCommands(std::move(probeCommand));
// Blocking readback (safe mid-frame: the Vulkan impl waits on a fence internally).
float results[3] = {0.0f, 0.0f, 0.0f};
probeOutBuffer->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);
probeOutBuffer->readContents(0, 3 * sizeof(float), results);
std::cout << "[MassProbe] frame=" << probeFrame
<< " density_sum=" << results[0]
<< " phi_sum=" << results[1]
<< " |vel|_sum=" << results[2] << std::endl;
++probeFrame;
}
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 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);
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);
float dtau = reinitDtau;
for (int i = 0; i < reinitIterations; ++i) {
Gfx::ODescriptorSet reinitSet = reinitialize.descriptorLayout->allocateDescriptorSet();
reinitSet->updateConstants("dtau", 0, &dtau);
reinitSet->updateBuffer("phi", 0, phi);
reinitSet->updateBuffer("phiPrev", 0, phi0);
reinitSet->updateBuffer("phiSign", 0, phiSign);
reinitSet->writeChanges();
Gfx::OComputeCommand reinitCommand = graphics->createComputeCommand();
reinitCommand->bindPipeline(reinitialize.pipeline);
reinitCommand->bindDescriptor(reinitSet);
reinitCommand->bindDescriptor(gridSizeSet);
reinitCommand->dispatch(getDispatchSize(gridSize, reinitialize.threadGroupSize));
graphics->executeCommands(std::move(reinitCommand));
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, 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();
}
@@ -0,0 +1,104 @@
#pragma once
#include "Graphics/Buffer.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Initializer.h"
#include "Graphics/RenderPass/RenderPass.h"
#include "Graphics/Resources.h"
#include "Math/Vector.h"
#include "Scene/Scene.h"
namespace Seele {
class SimulationComputePass : public RenderPass {
public:
SimulationComputePass(Gfx::PGraphics graphics, PScene scene);
virtual ~SimulationComputePass() override;
virtual void beginFrame(const Component::Camera& camera, const Seele::Component::Transform& transform) override;
virtual void render() override;
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
private:
UVector getDispatchSize(const UVector& gridSize, const UVector& threadGroupSize);
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, 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;
constexpr static UVector threadGroupSize = {4, 4, 4};
} setBounds;
struct LinearSolve {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} linearSolve;
struct ComputeDivergence {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} computeDivergence;
struct Project {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} project;
struct Advect {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} advect;
struct Reinitialize {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} reinitialize;
struct ApplyForces {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} applyForces;
void applyForcesPass(Gfx::PShaderBuffer velocityX, Gfx::PShaderBuffer velocityY, Gfx::PShaderBuffer velocityZ, Vector gravity, float dt, UVector gridSize);
// Diagnostics: sum interior fields and log to console. Not part of the simulation.
void massProbePass(Gfx::PShaderBuffer density, Gfx::PShaderBuffer phi, Gfx::PShaderBuffer velocityX, Gfx::PShaderBuffer velocityY,
Gfx::PShaderBuffer velocityZ, UVector gridSize);
struct MassProbe {
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
} massProbe;
Gfx::ODescriptorLayout gridSizeLayout;
Gfx::ODescriptorSet gridSizeSet;
// Diagnostics: 3 floats (density sum, phi sum, |velocity| sum), zeroed each frame.
Gfx::OShaderBuffer probeOutBuffer;
uint32 probeFrame = 0;
struct BoundaryPlane {
Vector normal;
float d;
};
PScene scene;
};
DEFINE_REF(SimulationComputePass)
} // namespace Seele
@@ -0,0 +1,107 @@
#include "SurfaceExtractPass.h"
#include "Graphics/Command.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Graphics.h"
#include "Graphics/Initializer.h"
#include "Graphics/Shader.h"
#include "Scene/FluidScene.h"
using namespace Seele;
SurfaceExtractPass::SurfaceExtractPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics), scene(scene) {
pipelineLayout = graphics->createPipelineLayout("SurfaceExtractPipelineLayout");
gridSizeLayout = graphics->createDescriptorLayout("gridParams");
gridSizeLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "gridSize",
.uniformLength = sizeof(UVector),
});
gridSizeLayout->create();
descriptorLayout = graphics->createDescriptorLayout("params");
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "vertexBuffer",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "normalBuffer",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "indexBuffer",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "surfaceCounts",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "levelSet",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->create();
pipelineLayout->addDescriptorLayout(descriptorLayout);
pipelineLayout->addDescriptorLayout(gridSizeLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "MarchingCubes",
.modules = {"MarchingCubes"},
.entryPoints = {{"marchingCubes", "MarchingCubes"}},
.rootSignature = pipelineLayout,
.dumpIntermediate = true,
});
pipelineLayout->create();
computeShader = graphics->createComputeShader({0});
pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = computeShader,
.pipelineLayout = pipelineLayout,
});
}
SurfaceExtractPass::~SurfaceExtractPass() {}
void SurfaceExtractPass::beginFrame(const Seele::Component::Camera&, const Seele::Component::Transform&) {}
void SurfaceExtractPass::render() {
for (auto& [entityID, data] : scene->getFluidScene()->getFluidDataMap()) {
Gfx::ODescriptorSet gridSizeSet = gridSizeLayout->allocateDescriptorSet();
gridSizeSet->updateConstants("gridSize", 0, &data.gridSize);
gridSizeSet->writeChanges();
data.vertexBuffer->rotateBuffer(data.getVertexBufferSize() * sizeof(float));
data.normalBuffer->rotateBuffer(data.getVertexBufferSize() * sizeof(float));
data.indexBuffer->rotateBuffer(data.getIndexBufferSize() * sizeof(uint32));
uint32 surfaceCountData[4] = {0, 1, 0, 0};
data.surfaceCountBuffer->updateContents(0, sizeof(surfaceCountData), surfaceCountData);
data.surfaceCountBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer("vertexBuffer", 0, data.vertexBuffer);
descriptorSet->updateBuffer("normalBuffer", 0, data.normalBuffer);
descriptorSet->updateBuffer("indexBuffer", 0, data.indexBuffer);
descriptorSet->updateBuffer("surfaceCounts", 0, data.surfaceCountBuffer);
descriptorSet->updateBuffer("levelSet", 0, data.phiBuffer);
descriptorSet->writeChanges();
Gfx::OComputeCommand cmd = graphics->createComputeCommand();
cmd->bindPipeline(pipeline);
cmd->bindDescriptor(descriptorSet);
cmd->bindDescriptor(gridSizeSet);
cmd->dispatch((data.gridSize + threadGroupSize - 1u) / threadGroupSize);
graphics->executeCommands(std::move(cmd));
data.vertexBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT);
data.normalBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT);
data.indexBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_VERTEX_SHADER_BIT);
data.surfaceCountBuffer->pipelineBarrier(Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
Gfx::SE_ACCESS_INDIRECT_COMMAND_READ_BIT, Gfx::SE_PIPELINE_STAGE_DRAW_INDIRECT_BIT);
}
}
void SurfaceExtractPass::endFrame() {}
void SurfaceExtractPass::publishOutputs() {}
void SurfaceExtractPass::createRenderPass() {}
@@ -0,0 +1,30 @@
#pragma once
#include "Graphics/Graphics.h"
#include "Graphics/RenderPass/RenderPass.h"
#include "Graphics/Resources.h"
#include "Scene/Scene.h"
namespace Seele {
class SurfaceExtractPass : public RenderPass {
public:
SurfaceExtractPass(Gfx::PGraphics graphics, PScene scene);
virtual ~SurfaceExtractPass() override;
virtual void beginFrame(const Component::Camera &camera, const Component::Transform &transform) override;
virtual void render() override;
virtual void endFrame() override;
virtual void publishOutputs() override;
virtual void createRenderPass() override;
private:
PScene scene;
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::ODescriptorLayout gridSizeLayout;
Gfx::ODescriptorSet descriptorSet;
Gfx::OComputeShader computeShader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {16, 8, 1};
};
DEFINE_REF(SurfaceExtractPass)
} // namespace Seele
+4 -3
View File
@@ -56,6 +56,10 @@ UIPass::~UIPass() {}
void UIPass::beginFrame(const Component::Camera& cam, const Component::Transform& transform) {
updateViewParameters(cam, transform);
viewParamsSet = createViewParamsSet();
// input first (hover transitions etc.), then style+layout, then gather render data
system->processInput();
system->beginFrame(UVector2(viewport->getWidth(), viewport->getHeight()));
renderElements = system->render();
glyphs.clear();
usedTextures.clear();
elements.clear();
@@ -253,7 +257,4 @@ void UIPass::createRenderPass() {
}},
},
});
system->style();
system->layout(UVector2(viewport->getWidth(), viewport->getHeight()));
renderElements = system->render();
}
+8
View File
@@ -19,6 +19,14 @@ class UIPass : public RenderPass {
virtual void publishOutputs() override;
virtual void createRenderPass() override;
// input: the owning view forwards its window callbacks here (window-relative coords)
void mouseMove(Vector2 position) { system->mouseMove(position); }
void mouseButton(MouseButton button, InputAction action, KeyModifierFlags modifier, Vector2 position) {
system->mouseButton(button, action, (KeyModifier)modifier, position);
}
void scroll(Vector2 delta) { system->scroll(delta); }
void key(KeyCode code, InputAction action, KeyModifierFlags modifier, char text = 0) { system->key(code, action, (KeyModifier)modifier, text); }
private:
struct GlyphData {
Vector2 bearing;
@@ -4,7 +4,39 @@
using namespace Seele;
VisibilityPass::VisibilityPass(Gfx::PGraphics graphics) : RenderPass(graphics) {}
VisibilityPass::VisibilityPass(Gfx::PGraphics graphics) : RenderPass(graphics) {
visibilityDescriptor = graphics->createDescriptorLayout("pVisibilityParams");
visibilityDescriptor->addDescriptorBinding(Gfx::DescriptorBinding{
.name = VISIBILITY_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
visibilityDescriptor->addDescriptorBinding(Gfx::DescriptorBinding{
.name = CULLINGBUFFER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
visibilityDescriptor->create();
visibilityLayout = graphics->createPipelineLayout("VisibilityLayout");
visibilityLayout->addDescriptorLayout(visibilityDescriptor);
visibilityLayout->addDescriptorLayout(viewParamsLayout);
ShaderCompilationInfo createInfo = {
.name = "Visibility",
.modules = {"VisibilityCompute"},
.entryPoints = {{"computeMain", "VisibilityCompute"}},
.rootSignature = visibilityLayout,
};
graphics->beginShaderCompilation(createInfo);
visibilityShader = graphics->createComputeShader({0});
visibilityLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = visibilityShader;
pipelineInfo.pipelineLayout = visibilityLayout;
visibilityPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
}
VisibilityPass::~VisibilityPass() {}
@@ -52,37 +84,6 @@ void VisibilityPass::publishOutputs() {
uint32_t viewportWidth = viewport->getWidth();
uint32_t viewportHeight = viewport->getHeight();
threadGroupSize = glm::ceil(glm::vec3(viewportWidth / (float)BLOCK_SIZE, viewportHeight / (float)BLOCK_SIZE, 1));
visibilityDescriptor = graphics->createDescriptorLayout("pVisibilityParams");
visibilityDescriptor->addDescriptorBinding(Gfx::DescriptorBinding{
.name = VISIBILITY_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE,
.access = Gfx::SE_DESCRIPTOR_ACCESS_SAMPLE_BIT,
});
visibilityDescriptor->addDescriptorBinding(Gfx::DescriptorBinding{
.name = CULLINGBUFFER_NAME,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.access = Gfx::SE_DESCRIPTOR_ACCESS_READ_BIT | Gfx::SE_DESCRIPTOR_ACCESS_WRITE_BIT,
});
visibilityDescriptor->create();
visibilityLayout = graphics->createPipelineLayout("VisibilityLayout");
visibilityLayout->addDescriptorLayout(visibilityDescriptor);
visibilityLayout->addDescriptorLayout(viewParamsLayout);
ShaderCompilationInfo createInfo = {
.name = "Visibility",
.modules = {"VisibilityCompute"},
.entryPoints = {{"computeMain", "VisibilityCompute"}},
.rootSignature = visibilityLayout,
};
graphics->beginShaderCompilation(createInfo);
visibilityShader = graphics->createComputeShader({0});
visibilityLayout->create();
Gfx::ComputePipelineCreateInfo pipelineInfo;
pipelineInfo.computeShader = visibilityShader;
pipelineInfo.pipelineLayout = visibilityLayout;
visibilityPipeline = graphics->createComputePipeline(std::move(pipelineInfo));
cullingBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.clearValue = 0xffffffff,
+1 -2
View File
@@ -3,7 +3,7 @@
#include "Graphics/Initializer.h"
#include "Material/Material.h"
#include "ThreadPool.h"
#include <fmt/core.h>
#include <fmt/format.h>
#include <mutex>
using namespace Seele;
@@ -134,7 +134,6 @@ void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipeline
}
// createInfo.typeParameter.add({Pair<const char*, const char*>("IVertexData", permutation.vertexDataName)});
createInfo.modules.add(permutation.vertexDataName);
createInfo.dumpIntermediate = true;
if (permutation.useMeshShading) {
if (permutation.hasTaskShader) {
+5 -4
View File
@@ -4,6 +4,7 @@
#include "Graphics/Enums.h"
#include <fmt/format.h>
#include <vk_mem_alloc.h>
#include <vulkan/vulkan_core.h>
using namespace Seele;
using namespace Seele::Vulkan;
@@ -192,7 +193,8 @@ void BufferAllocation::unmap() {
Buffer::Buffer(PGraphics graphics, uint64 size, VkBufferUsageFlags usage, Gfx::QueueType queueType, bool dynamic, std::string name,
bool createCleared, uint32 clearValue)
: graphics(graphics), currentBuffer(0), initialOwner(queueType),
usage(usage | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT),
usage(usage | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
(graphics->supportRayTracing() ? VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR : 0)),
dynamic(dynamic), createCleared(createCleared), name(name), clearValue(clearValue) {
if (size > 0) {
buffers.add(nullptr);
@@ -483,9 +485,8 @@ IndexBuffer::IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo& create
: Gfx::IndexBuffer(graphics->getFamilyMapping(), createInfo),
Vulkan::Buffer(graphics, createInfo.sourceData.size,
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | (graphics->supportRayTracing()
? VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR
: 0),
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
(graphics->supportRayTracing() ? VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR : 0),
createInfo.sourceData.owner, false, createInfo.name) {
getAlloc()->updateContents(createInfo.sourceData.offset, createInfo.sourceData.size, createInfo.sourceData.data);
// getAlloc()->pipelineBarrier(VK_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_INDEX_READ_BIT,
+18 -6
View File
@@ -268,7 +268,7 @@ void RenderCommand::bindDescriptor(Gfx::PDescriptorSet descriptor) {
}
}
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
descriptorSet->setHandle->constantsBuffer->bind();
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
@@ -299,7 +299,7 @@ void RenderCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptorS
}
}
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
descriptorSet->setHandle->constantsBuffer->bind();
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
sets[layout->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
@@ -351,6 +351,12 @@ void RenderCommand::drawIndexed(uint32 indexCount, uint32 instanceCount, int32 f
assert(threadId == std::this_thread::get_id());
vkCmdDrawIndexed(handle, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
}
void RenderCommand::drawIndexedIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) {
assert(threadId == std::this_thread::get_id());
vkCmdDrawIndexedIndirect(handle, buffer.cast<ShaderBuffer>()->getHandle(), offset, drawCount, stride);
}
void RenderCommand::drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) {
assert(threadId == std::this_thread::get_id());
if (groupX * groupY * groupZ == 0) // any dimension is 0
@@ -449,7 +455,7 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptor) {
}
}
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
descriptorSet->setHandle->constantsBuffer->bind();
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
@@ -476,7 +482,7 @@ void ComputeCommand::bindDescriptor(const Array<Gfx::PDescriptorSet>& descriptor
}
}
if (descriptorSet->setHandle->constantsBuffer != nullptr) {
descriptorSet->setHandle->bind();
descriptorSet->setHandle->constantsBuffer->bind();
boundResources.add(PBufferAllocation(descriptorSet->setHandle->constantsBuffer));
}
sets[pipeline->getPipelineLayout()->findParameter(descriptorSet->getName())] = descriptorSet->getHandle();
@@ -491,10 +497,16 @@ void ComputeCommand::pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset,
vkCmdPushConstants(handle, pipeline->getLayout(), stage, offset, size, data);
}
void ComputeCommand::dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) {
void ComputeCommand::dispatch(uint32 threadGroupCountX, uint32 threadGroupCountY, uint32 threadGroupCountZ) {
assert(threadId == std::this_thread::get_id());
vkCmdDispatch(handle, threadX, threadY, threadZ);
vkCmdDispatch(handle, threadGroupCountX, threadGroupCountY, threadGroupCountZ);
}
void ComputeCommand::dispatch(UVector threadGroupSize) {
assert(threadId == std::this_thread::get_id());
vkCmdDispatch(handle, threadGroupSize.x, threadGroupSize.y, threadGroupSize.z);
}
void ComputeCommand::dispatchIndirect(Gfx::PShaderBuffer buffer, uint32 offset) {
assert(threadId == std::this_thread::get_id());
vkCmdDispatchIndirect(handle, buffer.cast<ShaderBuffer>()->getHandle(), offset);
+2
View File
@@ -83,6 +83,7 @@ class RenderCommand : public Gfx::RenderCommand {
virtual void draw(uint32 vertexCount, uint32 instanceCount, int32 firstVertex, uint32 firstInstance) override;
virtual void drawIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) override;
virtual void drawIndexed(uint32 indexCount, uint32 instanceCount, int32 firstIndex, uint32 vertexOffset, uint32 firstInstance) override;
virtual void drawIndexedIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) override;
virtual void drawMesh(uint32 groupX, uint32 groupY, uint32 groupZ) override;
virtual void drawMeshIndirect(Gfx::PShaderBuffer buffer, uint64 offset, uint32 drawCount, uint32 stride) override;
virtual void traceRays(uint32 width, uint32 height, uint32 depth) override;
@@ -116,6 +117,7 @@ class ComputeCommand : public Gfx::ComputeCommand {
virtual void bindDescriptor(const Array<Gfx::PDescriptorSet>& sets) override;
virtual void pushConstants(Gfx::SeShaderStageFlags stage, uint32 offset, uint32 size, const void* data) override;
virtual void dispatch(uint32 threadX, uint32 threadY, uint32 threadZ) override;
virtual void dispatch(UVector threadGroupSize) override;
virtual void dispatchIndirect(Gfx::PShaderBuffer buffer, uint32 offset) override;
private:
+17 -2
View File
@@ -1,11 +1,26 @@
#include "Debug.h"
#include <iostream>
namespace {
const char* severityToString(VkDebugUtilsMessageSeverityFlagBitsEXT severity) {
if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) {
return "ERROR";
}
if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) {
return "WARNING";
}
if (severity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) {
return "INFO";
}
return "VERBOSE";
}
} // namespace
using namespace Seele::Vulkan;
VkBool32 Seele::Vulkan::debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT, VkDebugUtilsMessageTypeFlagsEXT,
VkBool32 Seele::Vulkan::debugCallback(VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageTypeFlagsEXT,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, void*) {
std::cerr << pCallbackData->pMessage << std::endl;
std::cerr << "[Vulkan " << severityToString(messageSeverity) << "] " << pCallbackData->pMessage << std::endl;
return VK_FALSE;
}
+22 -22
View File
@@ -175,7 +175,6 @@ Gfx::ODescriptorSet DescriptorPool::allocateDescriptorSet() {
};
vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo);
}
cachedHandles[setIndex]->isUsed = true;
// Found set, stop searching
return new DescriptorSet(graphics, this, cachedHandles[setIndex]);
}
@@ -191,13 +190,11 @@ void DescriptorPool::reset() {}
DescriptorSetHandle::DescriptorSetHandle(PGraphics graphics, const std::string& name) : CommandBoundResource(graphics, name) {}
DescriptorSetHandle::~DescriptorSetHandle() {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer));
}
DescriptorSetHandle::~DescriptorSetHandle() { graphics->getDestructionManager()->queueResourceForDestruction(std::move(constantsBuffer)); }
DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner, PDescriptorSetHandle setHandle)
: Gfx::DescriptorSet(owner->getLayout()), setHandle(setHandle),
graphics(graphics), owner(owner) {
: Gfx::DescriptorSet(owner->getLayout()), setHandle(setHandle), graphics(graphics), owner(owner) {
setHandle->isUsed = true;
boundResources.resize(owner->getLayout()->bindings.size());
for (uint32 i = 0; i < boundResources.size(); ++i) {
boundResources[i].resize(owner->getLayout()->bindings[i].descriptorCount);
@@ -208,12 +205,14 @@ DescriptorSet::DescriptorSet(PGraphics graphics, PDescriptorPool owner, PDescrip
constantData.resize(owner->getLayout()->constantsSize);
}
DescriptorSet::~DescriptorSet() {
setHandle->isUsed = false;
DescriptorSet::~DescriptorSet() {
if (setHandle != nullptr) {
setHandle->isUsed = false;
}
}
void DescriptorSet::updateConstants(const std::string& mappingName, uint32 offset, void* data) {
std::memcpy(constantData.data() + owner->getLayout()->mappings[mappingName].constantOffset, (char*)data + offset,
void DescriptorSet::updateConstants(const std::string& mappingName, uint32 offset, const void* data) {
std::memcpy(constantData.data() + owner->getLayout()->mappings[mappingName].constantOffset, (const char*)data + offset,
owner->getLayout()->mappings[mappingName].constantSize);
}
@@ -406,20 +405,21 @@ void DescriptorSet::writeChanges() {
if (setHandle->constantsBuffer != nullptr) {
graphics->getDestructionManager()->queueResourceForDestruction(std::move(setHandle->constantsBuffer));
}
setHandle->constantsBuffer = new BufferAllocation(graphics, owner->getLayout()->getName(),
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = constantData.size(),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
setHandle->constantsBuffer =
new BufferAllocation(graphics, owner->getLayout()->getName(),
VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.size = constantData.size(),
.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
},
VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
},
Gfx::QueueType::GRAPHICS);
setHandle->constantsBuffer->updateContents(0, constantData.size(), constantData.data());
setHandle->constantsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_ALL_COMMANDS_BIT);
Gfx::SE_ACCESS_UNIFORM_READ_BIT, Gfx::SE_PIPELINE_STAGE_ALL_COMMANDS_BIT);
bufferInfos.add(VkDescriptorBufferInfo{
.buffer = setHandle->constantsBuffer->buffer,
.offset = 0,
+6 -2
View File
@@ -68,8 +68,12 @@ class DescriptorSet : public Gfx::DescriptorSet {
public:
DescriptorSet(PGraphics graphics, PDescriptorPool owner, PDescriptorSetHandle setHandle);
virtual ~DescriptorSet();
DescriptorSet(const DescriptorSet&) = delete;
DescriptorSet(DescriptorSet&&) noexcept;
DescriptorSet& operator=(const DescriptorSet&) = delete;
DescriptorSet& operator=(DescriptorSet&&) noexcept;
virtual void writeChanges() override;
virtual void updateConstants(const std::string& name, uint32 offset, void* data) override;
virtual void updateConstants(const std::string& name, uint32 offset, const void* data) override;
virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PShaderBuffer shaderBuffer) override;
virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PVertexBuffer vertexBuffer) override;
virtual void updateBuffer(const std::string& name, uint32 index, Gfx::PIndexBuffer indexBuffer) override;
@@ -91,7 +95,7 @@ class DescriptorSet : public Gfx::DescriptorSet {
// would not work anyways, so casts should be safe
// Array<void*> cachedData;
Array<Array<PCommandBoundResource>> boundResources;
PDescriptorSetHandle setHandle;
PDescriptorSetHandle setHandle = nullptr;
PGraphics graphics;
PDescriptorPool owner;
friend class DescriptorPool;
+2 -1
View File
@@ -12,7 +12,8 @@
if (res == VK_ERROR_DEVICE_LOST) { \
std::this_thread::sleep_for(std::chrono::seconds(3)); \
} \
std::cout << "Fatal : VkResult is " << res << " in " << __FILE__ << " at line " << __LINE__ << std::endl; \
std::cerr << "Fatal: VkResult is " << res << " in " << __FILE__ << " at line " << __LINE__ << std::endl; \
std::cerr.flush(); \
abort(); \
} \
}
+178 -17
View File
@@ -16,8 +16,12 @@
#include "RenderPass.h"
#include "Shader.h"
#include "Window.h"
#include <GLFW/glfw3.h>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <vulkan/vulkan_wayland.h>
#include <vulkan/vulkan_core.h>
#define VMA_IMPLEMENTATION
@@ -26,6 +30,63 @@
using namespace Seele;
using namespace Seele::Vulkan;
namespace {
std::vector<VkExtensionProperties> enumerateInstanceExtensions() {
uint32_t extensionCount = 0;
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr));
std::vector<VkExtensionProperties> extensions(extensionCount);
if (extensionCount > 0) {
VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data()));
}
return extensions;
}
bool hasInstanceExtension(const std::vector<VkExtensionProperties>& extensions, const char* extensionName) {
for (const auto& extension : extensions) {
if (std::strcmp(extension.extensionName, extensionName) == 0) {
return true;
}
}
return false;
}
void applyCommonGlfwInitHints() {
#if defined(GLFW_WAYLAND_LIBDECOR) && defined(GLFW_WAYLAND_DISABLE_LIBDECOR)
glfwInitHint(GLFW_WAYLAND_LIBDECOR, GLFW_WAYLAND_DISABLE_LIBDECOR);
#endif
}
void initializeGlfwForVulkan() {
applyCommonGlfwInitHints();
if (!glfwInit()) {
throw std::runtime_error("Failed to initialize GLFW");
}
if (!glfwVulkanSupported()) {
throw std::runtime_error("GLFW: Vulkan not supported");
}
#if defined(GLFW_PLATFORM_WAYLAND) && defined(GLFW_PLATFORM_X11)
const auto availableExtensions = enumerateInstanceExtensions();
const bool hasWaylandSurface = hasInstanceExtension(availableExtensions, "VK_KHR_wayland_surface");
const bool hasXlibSurface = hasInstanceExtension(availableExtensions, "VK_KHR_xlib_surface");
const bool hasXcbSurface = hasInstanceExtension(availableExtensions, "VK_KHR_xcb_surface");
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND && !hasWaylandSurface && (hasXlibSurface || hasXcbSurface)) {
std::cerr << "Warning: Vulkan loader does not expose VK_KHR_wayland_surface on this run. Reinitializing GLFW on X11 backend." << std::endl;
glfwTerminate();
applyCommonGlfwInitHints();
glfwInitHint(GLFW_PLATFORM, GLFW_PLATFORM_X11);
if (!glfwInit()) {
throw std::runtime_error("Failed to reinitialize GLFW on X11 backend");
}
if (!glfwVulkanSupported()) {
throw std::runtime_error("GLFW: Vulkan not supported after switching to X11 backend");
}
}
#endif
}
}
thread_local PCommandPool Seele::Vulkan::Graphics::graphicsCommands = nullptr;
thread_local PCommandPool Seele::Vulkan::Graphics::computeCommands = nullptr;
thread_local PCommandPool Seele::Vulkan::Graphics::transferCommands = nullptr;
@@ -142,9 +203,7 @@ Graphics::~Graphics() {
void Graphics::init(GraphicsInitializer initInfo) {
initInstance(initInfo);
#ifdef ENABLE_VALIDATION
setupDebugCallback();
#endif
pickPhysicalDevice();
createDevice(initInfo);
VmaAllocatorCreateInfo createInfo = {
@@ -663,22 +722,115 @@ 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);
for (unsigned int i = 0; i < glfwExtensionCount; i++) {
extensions.add(glfwExtensions[i]);
}
#ifdef ENABLE_VALIDATION
extensions.add(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
if (glfwExtensions == nullptr || glfwExtensionCount == 0) {
const char* glfwError = nullptr;
glfwGetError(&glfwError);
std::string message = "GLFW did not report required Vulkan instance extensions";
if (glfwError != nullptr) {
message += ": ";
message += glfwError;
}
std::cerr << "Warning: " << message << ". Falling back to platform-specific surface extensions." << std::endl;
extensions.addUnique(VK_KHR_SURFACE_EXTENSION_NAME);
#if defined(GLFW_PLATFORM_WAYLAND) && defined(GLFW_PLATFORM_X11)
const int platform = glfwGetPlatform();
if (platform == GLFW_PLATFORM_WAYLAND) {
if (supportsExtension("VK_KHR_wayland_surface")) {
extensions.addUnique("VK_KHR_wayland_surface");
}
} else if (platform == GLFW_PLATFORM_X11) {
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 {
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 && supportsExtension("VK_KHR_wayland_surface")) {
extensions.addUnique("VK_KHR_wayland_surface");
} else {
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
return extensions;
} else {
// GLFW successfully reported extensions
for (unsigned int i = 0; i < glfwExtensionCount; i++) {
extensions.addUnique(glfwExtensions[i]);
}
}
#ifdef ENABLE_VALIDATION
extensions.addUnique(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#endif
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) {
glfwInit();
assert(glfwVulkanSupported());
initializeGlfwForVulkan();
VkApplicationInfo appInfo = {
.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = nullptr,
@@ -691,13 +843,12 @@ void Graphics::initInstance(GraphicsInitializer initInfo) {
Array<const char*> extensions = getRequiredExtensions();
for (uint32 i = 0; i < initInfo.instanceExtensions.size(); ++i) {
extensions.add(initInfo.instanceExtensions[i]);
extensions.addUnique(initInfo.instanceExtensions[i]);
}
#ifdef __APPLE__
extensions.add("VK_KHR_portability_enumeration");
extensions.addUnique("VK_KHR_portability_enumeration");
#endif
Array<const char*> layers = initInfo.layers;
// layers.add("VK_LAYER_KHRONOS_validation");
VkInstanceCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = nullptr,
@@ -710,6 +861,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));
}
@@ -754,11 +911,12 @@ void Graphics::pickPhysicalDevice() {
vkGetPhysicalDeviceProperties2(physicalDevice, &props.get());
features.get<VkPhysicalDeviceFeatures2>().features = {
.geometryShader = false,
// .robustBufferAccess = true,
.geometryShader = true,
.sampleRateShading = true,
.fillModeNonSolid = true,
.wideLines = false,
.pipelineStatisticsQuery = false,
.wideLines = true,
.pipelineStatisticsQuery = true,
.fragmentStoresAndAtomics = true,
.shaderInt64 = true,
.shaderInt16 = true,
@@ -766,12 +924,15 @@ void Graphics::pickPhysicalDevice() {
};
features.get<VkPhysicalDeviceVulkan11Features>().multiview = true;
features.get<VkPhysicalDeviceVulkan11Features>().storageBuffer16BitAccess = true;
features.get<VkPhysicalDeviceVulkan11Features>().shaderDrawParameters = true;
features.get<VkPhysicalDeviceVulkan11Features>().uniformAndStorageBuffer16BitAccess = true;
features.get<VkPhysicalDeviceVulkan12Features>().descriptorIndexing = true;
features.get<VkPhysicalDeviceVulkan12Features>().descriptorBindingPartiallyBound = true;
features.get<VkPhysicalDeviceVulkan12Features>().bufferDeviceAddress = true;
features.get<VkPhysicalDeviceVulkan12Features>().storageBuffer8BitAccess = true;
features.get<VkPhysicalDeviceVulkan12Features>().shaderInt8 = true;
features.get<VkPhysicalDeviceVulkan12Features>().uniformAndStorageBuffer8BitAccess = true;
rayTracingFeatures.get<VkPhysicalDeviceAccelerationStructureFeaturesKHR>().accelerationStructure = true;
-2
View File
@@ -1,8 +1,6 @@
#include "Resources.h"
#include "Command.h"
#include "Enums.h"
#include "Graphics.h"
#include "Window.h"
using namespace Seele;
using namespace Seele::Vulkan;
+144 -42
View File
@@ -3,11 +3,81 @@
#include "Enums.h"
#include "Graphics.h"
#include "Resources.h"
#include <vulkan/vulkan_core.h>
#define GLFW_EXPOSE_NATIVE_WAYLAND
#define GLFW_EXPOSE_NATIVE_X11
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
#include <X11/Xlib.h>
#include <sstream>
#include <vulkan/vulkan_wayland.h>
#include <vulkan/vulkan_xlib.h>
using namespace Seele;
using namespace Seele::Vulkan;
namespace {
VkResult createSurfaceFromNativeHandles(VkInstance instance, GLFWwindow* handle, VkSurfaceKHR* surface, std::string& reason) {
#if defined(GLFW_PLATFORM_WAYLAND)
if (glfwGetPlatform() == GLFW_PLATFORM_WAYLAND) {
auto createWaylandSurface = reinterpret_cast<PFN_vkCreateWaylandSurfaceKHR>(
vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR"));
if (createWaylandSurface == nullptr) {
reason = "vkCreateWaylandSurfaceKHR is unavailable on the Vulkan instance";
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
wl_display* display = glfwGetWaylandDisplay();
wl_surface* waylandSurface = glfwGetWaylandWindow(handle);
if (display == nullptr || waylandSurface == nullptr) {
reason = "GLFW did not provide valid Wayland native handles";
return VK_ERROR_INITIALIZATION_FAILED;
}
VkWaylandSurfaceCreateInfoKHR createInfo = {
.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR,
.pNext = nullptr,
.flags = 0,
.display = display,
.surface = waylandSurface,
};
return createWaylandSurface(instance, &createInfo, nullptr, surface);
}
#endif
#if defined(GLFW_PLATFORM_X11)
if (glfwGetPlatform() == GLFW_PLATFORM_X11) {
auto createXlibSurface = reinterpret_cast<PFN_vkCreateXlibSurfaceKHR>(
vkGetInstanceProcAddr(instance, "vkCreateXlibSurfaceKHR"));
if (createXlibSurface == nullptr) {
reason = "vkCreateXlibSurfaceKHR is unavailable on the Vulkan instance";
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
Display* display = glfwGetX11Display();
::Window x11Window = glfwGetX11Window(handle);
if (display == nullptr || x11Window == 0) {
reason = "GLFW did not provide valid X11 native handles";
return VK_ERROR_INITIALIZATION_FAILED;
}
VkXlibSurfaceCreateInfoKHR createInfo = {
.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR,
.pNext = nullptr,
.flags = 0,
.dpy = display,
.window = x11Window,
};
return createXlibSurface(instance, &createInfo, nullptr, surface);
}
#endif
reason = "Unsupported GLFW platform for manual Vulkan surface creation";
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
}
double currentFrameDelta = 0;
double Gfx::getCurrentFrameDelta() { return currentFrameDelta; }
@@ -21,46 +91,57 @@ void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier)
if (key == -1) {
return;
}
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->keyPress((KeyCode)key, (InputAction)action, (KeyModifier)modifier);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->keyPress(static_cast<Seele::KeyCode>(key), static_cast<InputAction>(action), modifier);
}
void glfwMouseMoveCallback(GLFWwindow* handle, double xpos, double ypos) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->mouseMove(xpos, ypos);
}
void glfwMouseButtonCallback(GLFWwindow* handle, int button, int action, int modifier) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->mouseButton((MouseButton)button, (InputAction)action, (KeyModifier)modifier);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->mouseButton(static_cast<MouseButton>(button), static_cast<InputAction>(action), static_cast<KeyModifierFlags>(modifier));
}
void glfwScrollCallback(GLFWwindow* handle, double xoffset, double yoffset) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->scroll(xoffset, yoffset);
}
void glfwFileCallback(GLFWwindow* handle, int count, const char** paths) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->fileDrop(count, paths);
}
void glfwCloseCallback(GLFWwindow* handle) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->close();
}
void glfwFramebufferResizeCallback(GLFWwindow* handle, int width, int height) {
Window* window = (Window*)glfwGetWindowUserPointer(handle);
auto* window = static_cast<Seele::Vulkan::Window*>(glfwGetWindowUserPointer(handle));
window->resize(width, height);
}
Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
Seele::Vulkan::Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
: graphics(graphics), preferences(createInfo), instance(graphics->getInstance()), swapchain(VK_NULL_HANDLE) {
glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &contentScaleX, &contentScaleY);
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
GLFWwindow* handle = glfwCreateWindow(createInfo.width / contentScaleX, createInfo.height / contentScaleY, createInfo.title, nullptr, nullptr);
if (handle == nullptr) {
const char* glfwError = nullptr;
glfwGetError(&glfwError);
std::string message = "Failed to create GLFW window";
if (glfwError != nullptr) {
message += ": ";
message += glfwError;
}
throw std::runtime_error(message);
}
windowHandle = handle;
glfwSetWindowUserPointer(handle, this);
@@ -73,7 +154,25 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
glfwSetFramebufferSizeCallback(handle, &glfwFramebufferResizeCallback);
// glfwSetWindowSizeCallback(handle, &glfwResizeCallback);
glfwCreateWindowSurface(instance, handle, nullptr, &surface);
VkResult surfaceResult = glfwCreateWindowSurface(instance, handle, nullptr, &surface);
if (surfaceResult != VK_SUCCESS || surface == VK_NULL_HANDLE) {
const char* glfwError = nullptr;
glfwGetError(&glfwError);
std::string nativeReason;
surfaceResult = createSurfaceFromNativeHandles(instance, handle, &surface, nativeReason);
if (surfaceResult != VK_SUCCESS || surface == VK_NULL_HANDLE) {
std::ostringstream oss;
oss << "Failed to create Vulkan surface via GLFW (VkResult=" << surfaceResult << ")";
if (glfwError != nullptr) {
oss << ": " << glfwError;
}
if (!nativeReason.empty()) {
oss << ". Native fallback failed: " << nativeReason;
}
throw std::runtime_error(oss.str());
}
}
querySurface();
chooseSwapSurfaceFormat();
@@ -85,17 +184,17 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo)
createSwapChain();
}
Window::~Window() {
Seele::Vulkan::Window::~Window() {
vkDestroySwapchainKHR(graphics->getDevice(), swapchain, nullptr);
vkDestroySurfaceKHR(instance, surface, nullptr);
glfwDestroyWindow(static_cast<GLFWwindow*>(windowHandle));
}
void Window::pollInput() { glfwPollEvents(); }
void Seele::Vulkan::Window::pollInput() { glfwPollEvents(); }
void Window::show() { glfwShowWindow(static_cast<GLFWwindow*>(windowHandle)); }
void Seele::Vulkan::Window::show() { glfwShowWindow(static_cast<GLFWwindow*>(windowHandle)); }
void Window::beginFrame() {
void Seele::Vulkan::Window::beginFrame() {
imageAvailableFences[currentSemaphoreIndex]->reset();
imageAvailableSemaphores[currentSemaphoreIndex]->resolveSignal();
imageAvailableSemaphores[currentSemaphoreIndex]->rotateSemaphore();
@@ -104,6 +203,9 @@ void Window::beginFrame() {
imageAvailableFences[currentSemaphoreIndex]->getHandle(), &currentImageIndex));
imageAvailableSemaphores[currentSemaphoreIndex]->encodeSignal();
imageAvailableFences[currentSemaphoreIndex]->submit();
// The image was reacquired, so the previous presentation using this image's
// renderDone semaphore has completed and it can be safely reused
renderingDoneSemaphores[currentImageIndex]->resolveSignal();
graphics->getGraphicsCommands()->getCommands()->waitForSemaphore(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
imageAvailableSemaphores[currentSemaphoreIndex]);
static double start = glfwGetTime();
@@ -113,13 +215,13 @@ void Window::beginFrame() {
start = end;
}
void Window::endFrame() {
void Seele::Vulkan::Window::endFrame() {
swapChainTextures[currentImageIndex]->changeLayout(Gfx::SE_IMAGE_LAYOUT_PRESENT_SRC_KHR, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_MEMORY_READ_BIT,
Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT);
renderingDoneSemaphores[currentSemaphoreIndex]->rotateSemaphore();
graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentSemaphoreIndex]);
VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentSemaphoreIndex]->getHandle();
renderingDoneSemaphores[currentImageIndex]->rotateSemaphore();
graphics->getGraphicsCommands()->submitCommands(renderingDoneSemaphores[currentImageIndex]);
VkSemaphore renderDoneHandle = renderingDoneSemaphores[currentImageIndex]->getHandle();
VkPresentInfoKHR presentInfo = {
.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.pNext = nullptr,
@@ -137,46 +239,45 @@ void Window::endFrame() {
} else {
VK_CHECK(r);
}
renderingDoneSemaphores[currentSemaphoreIndex]->resolveSignal();
renderingDoneSemaphores[currentImageIndex]->encodeSignal();
currentSemaphoreIndex = (currentSemaphoreIndex + 1) % Gfx::numFramesBuffered;
currentFrameIndex = currentSemaphoreIndex;
// graphics->waitDeviceIdle();
}
bool Window::shouldClose() const {
std::cout << "Checking if window should close: " << glfwWindowShouldClose((GLFWwindow*)windowHandle) << std::endl;
bool Seele::Vulkan::Window::shouldClose() const {
return glfwWindowShouldClose((GLFWwindow*)windowHandle);
}
Gfx::PTexture2D Window::getBackBuffer() const { return PTexture2D(swapChainTextures[currentImageIndex]); }
Gfx::PTexture2D Seele::Vulkan::Window::getBackBuffer() const { return PTexture2D(swapChainTextures[currentImageIndex]); }
void Window::setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) { keyCallback = callback; }
void Seele::Vulkan::Window::setKeyCallback(std::function<void(Seele::KeyCode, InputAction, KeyModifierFlags)> callback) { keyCallback = callback; }
void Window::setMouseMoveCallback(std::function<void(double, double)> callback) { mouseMoveCallback = callback; }
void Seele::Vulkan::Window::setMouseMoveCallback(std::function<void(double, double)> callback) { mouseMoveCallback = callback; }
void Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) { mouseButtonCallback = callback; }
void Seele::Vulkan::Window::setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifierFlags)> callback) { mouseButtonCallback = callback; }
void Window::setScrollCallback(std::function<void(double, double)> callback) { scrollCallback = callback; }
void Seele::Vulkan::Window::setScrollCallback(std::function<void(double, double)> callback) { scrollCallback = callback; }
void Window::setFileCallback(std::function<void(int, const char**)> callback) { fileCallback = callback; }
void Seele::Vulkan::Window::setFileCallback(std::function<void(int, const char**)> callback) { fileCallback = callback; }
void Window::setCloseCallback(std::function<void()> callback) { closeCallback = callback; }
void Seele::Vulkan::Window::setCloseCallback(std::function<void()> callback) { closeCallback = callback; }
void Window::setResizeCallback(std::function<void(uint32, uint32)> callback) { resizeCallback = callback; }
void Seele::Vulkan::Window::setResizeCallback(std::function<void(uint32, uint32)> callback) { resizeCallback = callback; }
void Window::keyPress(KeyCode code, InputAction action, KeyModifier modifier) { keyCallback(code, action, modifier); }
void Seele::Vulkan::Window::keyPress(Seele::KeyCode code, InputAction action, KeyModifierFlags modifier) { keyCallback(code, action, modifier); }
void Window::mouseMove(double x, double y) { mouseMoveCallback(x, y); }
void Seele::Vulkan::Window::mouseMove(double x, double y) { mouseMoveCallback(x, y); }
void Window::mouseButton(MouseButton button, InputAction action, KeyModifier modifier) { mouseButtonCallback(button, action, modifier); }
void Seele::Vulkan::Window::mouseButton(MouseButton button, InputAction action, KeyModifierFlags modifier) { mouseButtonCallback(button, action, modifier); }
void Window::scroll(double x, double y) { scrollCallback(x, y); }
void Seele::Vulkan::Window::scroll(double x, double y) { scrollCallback(x, y); }
void Window::fileDrop(int num, const char** files) { fileCallback(num, files); }
void Seele::Vulkan::Window::fileDrop(int num, const char** files) { fileCallback(num, files); }
void Window::close() { closeCallback(); }
void Seele::Vulkan::Window::close() { closeCallback(); }
void Window::resize(int width, int height) {
void Seele::Vulkan::Window::resize(int width, int height) {
if (width == 0 || height == 0) {
paused = true;
return;
@@ -193,7 +294,7 @@ void Window::resize(int width, int height) {
resizeCallback(width, height);
}
void Window::querySurface() {
void Seele::Vulkan::Window::querySurface() {
vkGetPhysicalDeviceSurfaceCapabilitiesKHR(graphics->getPhysicalDevice(), surface, &capabilities);
uint32 numFormats;
@@ -207,7 +308,7 @@ void Window::querySurface() {
vkGetPhysicalDeviceSurfacePresentModesKHR(graphics->getPhysicalDevice(), surface, &numPresentModes, supportedPresentModes.data());
}
void Window::chooseSwapSurfaceFormat() {
void Seele::Vulkan::Window::chooseSwapSurfaceFormat() {
for (const auto& supportedFormat : supportedFormats) {
if (supportedFormat.format == cast(preferences.preferredFormat)) {
format = supportedFormat;
@@ -223,7 +324,7 @@ void Window::chooseSwapSurfaceFormat() {
format = supportedFormats[0];
}
void Window::chooseSwapPresentMode() {
void Seele::Vulkan::Window::chooseSwapPresentMode() {
for (const auto& supportedPresentMode : supportedPresentModes) {
if (supportedPresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {
presentMode = supportedPresentMode;
@@ -233,7 +334,7 @@ void Window::chooseSwapPresentMode() {
presentMode = VK_PRESENT_MODE_FIFO_KHR;
}
void Window::chooseSwapExtent() {
void Seele::Vulkan::Window::chooseSwapExtent() {
if (capabilities.currentExtent.width != std::numeric_limits<uint32>::max()) {
extent = capabilities.currentExtent;
return;
@@ -249,7 +350,8 @@ void Window::chooseSwapExtent() {
extent.height = std::clamp(extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
}
void Window::createSwapChain() {
void Seele::Vulkan::Window::createSwapChain() {
graphics->waitDeviceIdle();
uint32 imageCount = Gfx::numFramesBuffered;
VkSwapchainCreateInfoKHR createInfo = {
.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR,
+6 -6
View File
@@ -17,17 +17,17 @@ class Window : public Gfx::Window {
virtual void endFrame() override;
virtual Gfx::PTexture2D getBackBuffer() const override;
virtual bool shouldClose() const override;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) override;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifierFlags)> callback) override;
virtual void setMouseMoveCallback(std::function<void(double, double)> callback) override;
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) override;
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifierFlags)> callback) override;
virtual void setScrollCallback(std::function<void(double, double)> callback) override;
virtual void setFileCallback(std::function<void(int, const char**)> callback) override;
virtual void setCloseCallback(std::function<void()> callback) override;
virtual void setResizeCallback(std::function<void(uint32, uint32)> callback) override;
void keyPress(KeyCode code, InputAction action, KeyModifier modifier);
void keyPress(KeyCode code, InputAction action, KeyModifierFlags modifier);
void mouseMove(double x, double y);
void mouseButton(MouseButton button, InputAction action, KeyModifier modifier);
void mouseButton(MouseButton button, InputAction action, KeyModifierFlags modifier);
void scroll(double x, double y);
void fileDrop(int num, const char** files);
void close();
@@ -61,9 +61,9 @@ class Window : public Gfx::Window {
uint32 currentImageIndex = 0;
uint32 currentSemaphoreIndex = 0;
std::function<void(KeyCode, InputAction, KeyModifier)> keyCallback;
std::function<void(KeyCode, InputAction, KeyModifierFlags)> keyCallback;
std::function<void(double, double)> mouseMoveCallback;
std::function<void(MouseButton, InputAction, KeyModifier)> mouseButtonCallback;
std::function<void(MouseButton, InputAction, KeyModifierFlags)> mouseButtonCallback;
std::function<void(double, double)> scrollCallback;
std::function<void(int, const char**)> fileCallback;
std::function<void()> closeCallback;
+2 -2
View File
@@ -15,9 +15,9 @@ class Window {
virtual void endFrame() = 0;
virtual bool shouldClose() const = 0;
virtual PTexture2D getBackBuffer() const = 0;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifier)> callback) = 0;
virtual void setKeyCallback(std::function<void(KeyCode, InputAction, KeyModifierFlags)> callback) = 0;
virtual void setMouseMoveCallback(std::function<void(double, double)> callback) = 0;
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifier)> callback) = 0;
virtual void setMouseButtonCallback(std::function<void(MouseButton, InputAction, KeyModifierFlags)> callback) = 0;
virtual void setScrollCallback(std::function<void(double, double)> callback) = 0;
virtual void setFileCallback(std::function<void(int, const char**)> callback) = 0;
virtual void setCloseCallback(std::function<void()> callback) = 0;
+94 -40
View File
@@ -1,7 +1,10 @@
#include "slang-compile.h"
#include "Containers/Array.h"
#include "Graphics/Descriptor.h"
#include <fmt/core.h>
#include <array>
#include <cstdlib>
#include <filesystem>
#include <fmt/format.h>
#include <iostream>
#include <slang.h>
@@ -27,37 +30,59 @@ thread_local Slang::ComPtr<slang::IComponentType> specializedComponent;
thread_local Slang::ComPtr<slang::ISession> session;
thread_local Array<std::string> entryPoints;
namespace {
void appendEnvPath(const char* varName, const std::filesystem::path& path) {
if (!std::filesystem::exists(path) || !std::filesystem::is_directory(path)) {
return;
}
const std::string newEntry = path.lexically_normal().string();
const char* currentRaw = std::getenv(varName);
std::string current = currentRaw ? currentRaw : "";
if (current.find(newEntry) != std::string::npos) {
return;
}
std::string updated = current.empty() ? newEntry : (newEntry + ":" + current);
setenv(varName, updated.c_str(), 1);
}
void configureSlangRuntimeEnv() {
std::filesystem::path exePath;
try {
exePath = std::filesystem::canonical("/proc/self/exe");
} catch (...) {
return;
}
const std::filesystem::path exeDir = exePath.parent_path();
const std::array<std::filesystem::path, 7> candidates = {
exeDir / "vcpkg_installed/x64-linux/lib",
exeDir / "vcpkg_installed/x64-linux/debug/lib",
exeDir / "vcpkg_installed/x64-linux/tools/shader-slang",
exeDir.parent_path() / "vcpkg_installed/x64-linux/lib",
exeDir.parent_path() / "vcpkg_installed/x64-linux/debug/lib",
exeDir.parent_path() / "vcpkg_installed/x64-linux/tools/shader-slang",
std::filesystem::current_path() / "build/vcpkg_installed/x64-linux/tools/shader-slang",
};
for (const auto& candidate : candidates) {
appendEnvPath("LD_LIBRARY_PATH", candidate);
appendEnvPath("PATH", candidate);
}
}
} // namespace
void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarget target, Gfx::PPipelineLayout layout) {
if (!globalSession) {
slang::createGlobalSession(globalSession.writeRef());
configureSlangRuntimeEnv();
SlangGlobalSessionDesc sessionDesc = {};
sessionDesc.enableGLSL = true;
slang::createGlobalSession(&sessionDesc, globalSession.writeRef());
}
slang::SessionDesc sessionDesc;
sessionDesc.flags = 0;
Array<slang::CompilerOptionEntry> option = {
{
.name = slang::CompilerOptionName::LineDirectiveMode,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = SLANG_LINE_DIRECTIVE_MODE_NONE,
},
},
{
.name = slang::CompilerOptionName::DebugInformation,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = SLANG_DEBUG_INFO_LEVEL_STANDARD,
},
},
{
.name = slang::CompilerOptionName::DebugInformationFormat,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = SLANG_DEBUG_INFO_FORMAT_PDB,
},
},
{
.name = slang::CompilerOptionName::DumpIntermediates,
.value =
@@ -66,10 +91,50 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
.intValue0 = info.dumpIntermediate,
},
},
{
.name = slang::CompilerOptionName::Optimization,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = SLANG_OPTIMIZATION_LEVEL_NONE,
},
},
{
.name = slang::CompilerOptionName::EmitSpirvMethod,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = SLANG_EMIT_SPIRV_DIRECTLY,
},
},
{
.name = slang::CompilerOptionName::PassThrough,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = SLANG_PASS_THROUGH_NONE,
},
},
{
.name = slang::CompilerOptionName::SkipSPIRVValidation,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = 1,
},
},
{
.name = slang::CompilerOptionName::SkipDownstreamLinking,
.value =
{
.kind = slang::CompilerOptionValueKind::Int,
.intValue0 = 1,
},
},
};
sessionDesc.compilerOptionEntries = 0;//option.data();
sessionDesc.compilerOptionEntryCount = 0;//(uint32)option.size();
sessionDesc.compilerOptionEntries = option.data();
sessionDesc.compilerOptionEntryCount = (uint32)option.size();
sessionDesc.defaultMatrixLayoutMode = SLANG_MATRIX_LAYOUT_COLUMN_MAJOR;
Array<slang::PreprocessorMacroDesc> macros;
for (const auto& [key, val] : info.defines) {
@@ -82,10 +147,9 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
sessionDesc.preprocessorMacros = macros.data();
slang::TargetDesc targetDesc = {};
targetDesc.format = target;
targetDesc.flags = SLANG_TARGET_FLAG_GENERATE_SPIRV_DIRECTLY;
sessionDesc.targetCount = 1;
sessionDesc.targets = &targetDesc;
StaticArray<const char*, 7> searchPaths = {"shaders/", "shaders/lib/", "shaders/raytracing/", "shaders/terrain", "shaders/game", "shaders/generated/", "Seele/shaders/lib/"};
StaticArray<const char*, 7> searchPaths = {"shaders/", "shaders/lib/", "shaders/raytracing/", "shaders/terrain", "shaders/game", "shaders/generated/", "shaders/fluid"};
sessionDesc.searchPaths = searchPaths.data();
sessionDesc.searchPathCount = searchPaths.size();
@@ -136,16 +200,6 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
layout->addMapping(param->getName(), param->getBindingIndex());
}
// workaround
if (info.name == "RayGenMiss")
{
layout->addMapping("pScene", 2);
layout->addMapping("pLightEnv", 3);
layout->addMapping("pResources", 4);
layout->addMapping("pRayTracingParams", 5);
}
//layout->addMapping("pVertexData", 1);
// layout->addMapping("pWaterMaterial", 1);
}
Pair<Slang::ComPtr<slang::IBlob>, std::string> Seele::generateShader(const ShaderCreateInfo& createInfo) {
+1 -2
View File
@@ -5,8 +5,7 @@
#include "Graphics/Graphics.h"
#include "Graphics/Resources.h"
#include "Material/Material.h"
#include <fmt/core.h>
#include <fstream>
#include <fmt/format.h>
using namespace Seele;
+1 -2
View File
@@ -1,6 +1,5 @@
#include "Vector.h"
#include "Serialization/ArchiveBuffer.h"
#include <fmt/core.h>
#include <fmt/format.h>
#include <nlohmann/json.hpp>
#include <regex>
#include <sstream>
+3
View File
@@ -1,6 +1,8 @@
target_sources(Engine
PRIVATE
EventManager.h
FluidScene.h
FluidScene.cpp
LightEnvironment.h
LightEnvironment.cpp
Util.h
@@ -11,6 +13,7 @@ target_sources(Engine
PUBLIC FILE_SET HEADERS
FILES
EventManager.h
FluidScene.h
LightEnvironment.h
Util.h
Scene.h)
+269
View File
@@ -0,0 +1,269 @@
#include "FluidScene.h"
#include "Graphics/Command.h"
#include "Graphics/Descriptor.h"
#include "Graphics/Enums.h"
#include "Graphics/Initializer.h"
using namespace Seele;
FluidScene::FluidScene(Gfx::PGraphics graphics) : graphics(graphics) {
pipelineLayout = graphics->createPipelineLayout("FluidScenePipelineLayout");
gridParamsLayout = graphics->createDescriptorLayout("gridParams");
gridParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "gridSize",
.uniformLength = sizeof(UVector),
});
gridParamsLayout->create();
descriptorLayout = graphics->createDescriptorLayout("params");
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "phi",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.name = "density",
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->create();
pipelineLayout->addDescriptorLayout(descriptorLayout);
pipelineLayout->addDescriptorLayout(gridParamsLayout);
graphics->beginShaderCompilation(ShaderCompilationInfo{
.name = "AddSource",
.modules = {"AddSource"},
.entryPoints = {{"addSource", "AddSource"}},
.rootSignature = pipelineLayout,
});
pipelineLayout->create();
shader = graphics->createComputeShader({0});
pipeline = graphics->createComputePipeline(Gfx::ComputePipelineCreateInfo{
.computeShader = shader,
.pipelineLayout = pipelineLayout,
});
}
FluidScene::~FluidScene() {}
void FluidScene::updateFluidData(uint32 entityID, const Component::Transform& transform, const Component::FluidGrid& grid) {
if (!fluidDataMap.contains(entityID)) {
auto& data = fluidDataMap[entityID];
uint64 totalCells = (uint64)grid.gridSize.x * grid.gridSize.y * grid.gridSize.z;
uint64 bufferSize = totalCells * sizeof(float);
// Array<float> initialDensity(bufferSize / sizeof(float));
// Array<float> initialVelocityX(bufferSize / sizeof(float));
// Array<float> initialVelocityY(bufferSize / sizeof(float));
// Array<float> initialVelocityZ(bufferSize / sizeof(float));
// Array<float> initialPhi(bufferSize / sizeof(float));
// // Initialize level set to positive (outside) everywhere
// float halfGridX = static_cast<float>(grid.gridSize.x) * 0.5f;
// float halfGridY = static_cast<float>(grid.gridSize.y) * 0.5f;
// float radius = static_cast<float>(grid.gridSize.x) * 0.2f;
// // Place sphere center in the lower portion of the grid
// float cx = halfGridX;
// float cy = halfGridY;
// float cz = static_cast<float>(grid.gridSize.z) * 0.3f;
// for (uint32 z = 1; z < grid.gridSize.z - 1; ++z) {
// for (uint32 y = 1; y < grid.gridSize.y - 1; ++y) {
// for (uint32 x = 1; x < grid.gridSize.x - 1; ++x) {
// uint32 idx = x + grid.gridSize.x * y + grid.gridSize.x * grid.gridSize.y * z;
// float dx = static_cast<float>(x) - cx;
// float dy = static_cast<float>(y) - cy;
// float dz = static_cast<float>(z) - cz;
// float dist = std::sqrt(dx * dx + dy * dy + dz * dz);
// initialPhi[idx] = dist - radius;
// if (dist < radius) {
// initialDensity[idx] = 1.0f;
// // Give an initial upward velocity to kick the simulation
// initialVelocityZ[idx] = 5.0f;
// }
// }
// }
// }
data.phiBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "PhiBuffer",
});
data.phi0Buffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
// .data = (uint8*)initialPhi.data(),
},
.name = "Phi0Buffer",
});
data.densityBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "DensityBuffer",
});
data.density0Buffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
// .data = (uint8*)initialDensity.data(),
},
.name = "Density0Buffer",
});
data.scratchBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "ScratchBuffer",
});
data.velocityXNextBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "VelocityXNextBuffer",
});
data.velocityYNextBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "VelocityYNextBuffer",
});
data.velocityZNextBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "VelocityZNextBuffer",
});
data.velocityXBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "VelocityXBuffer",
});
data.velocityYBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "VelocityYBuffer",
});
data.velocityZBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
},
.name = "VelocityZBuffer",
});
data.velocityX0Buffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
// .data = (uint8*)initialVelocityX.data(),
},
.name = "VelocityX0Buffer",
});
data.velocityY0Buffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
// .data = (uint8*)initialVelocityY.data(),
},
.name = "VelocityY0Buffer",
});
data.velocityZ0Buffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = bufferSize,
// .data = (uint8*)initialVelocityZ.data(),
},
.name = "VelocityZ0Buffer",
});
data.vertexBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.name = "VertexBuffer",
});
data.normalBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.name = "NormalBuffer",
});
data.indexBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.name = "IndexBuffer",
});
data.surfaceCountBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = 4 * sizeof(uint32),
},
.usage = Gfx::SE_BUFFER_USAGE_INDIRECT_BUFFER_BIT,
.name = "SurfaceCountBuffer",
});
}
auto& data = fluidDataMap[entityID];
data.paused = grid.paused;
data.transform = transform.transform;
data.dt = grid.dt;
data.viscosity = grid.viscosity;
data.diffusion = grid.diffusion;
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() {
for (auto& [entityID, data] : fluidDataMap) {
Gfx::ODescriptorSet gridParams = gridParamsLayout->allocateDescriptorSet();
gridParams->updateConstants("gridSize", 0, &data.gridSize);
gridParams->writeChanges();
Gfx::ODescriptorSet descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer("phi", 0, data.phi0Buffer);
descriptorSet->updateBuffer("density", 0, data.density0Buffer);
descriptorSet->writeChanges();
Gfx::OComputeCommand command = graphics->createComputeCommand();
command->bindPipeline(pipeline);
command->bindDescriptor(descriptorSet);
command->bindDescriptor(gridParams);
command->dispatch((data.gridSize + threadGroupSize - UVector{1, 1, 1}) / threadGroupSize);
graphics->executeCommands(std::move(command));
data.phi0Buffer->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);
data.density0Buffer->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);
// Keep render/extraction level set in sync with injected source immediately.
graphics->copyBuffer(data.phi0Buffer, data.phiBuffer);
data.phiBuffer->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);
data.phi0Buffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_READ_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
// Reinitialize soon after source injection to restore signed-distance quality.
data.frameCounter = 0;
}
}
+71
View File
@@ -0,0 +1,71 @@
#pragma once
#include "Component/FluidGrid.h"
#include "Component/Transform.h"
#include "Containers/Array.h"
#include "Containers/Map.h"
#include "Graphics/Buffer.h"
#include "Graphics/Graphics.h"
#include "Graphics/Resources.h"
#include "Graphics/Shader.h"
#include "Math/Transform.h"
#include "Math/Vector.h"
#include "MinimalEngine.h"
namespace Seele {
class FluidScene {
public:
FluidScene(Gfx::PGraphics graphics);
virtual ~FluidScene();
struct FluidData {
Math::Transform transform;
bool paused;
float viscosity;
float diffusion;
float dt = 0.01f;
UVector gridSize;
Vector gravity;
bool enableGravity;
Gfx::OShaderBuffer phiBuffer;
Gfx::OShaderBuffer phi0Buffer;
Gfx::OShaderBuffer densityBuffer;
Gfx::OShaderBuffer density0Buffer;
Gfx::OShaderBuffer scratchBuffer;
Gfx::OShaderBuffer velocityXNextBuffer;
Gfx::OShaderBuffer velocityYNextBuffer;
Gfx::OShaderBuffer velocityZNextBuffer;
Gfx::OShaderBuffer velocityXBuffer;
Gfx::OShaderBuffer velocityYBuffer;
Gfx::OShaderBuffer velocityZBuffer;
Gfx::OShaderBuffer velocityX0Buffer;
Gfx::OShaderBuffer velocityY0Buffer;
Gfx::OShaderBuffer velocityZ0Buffer;
Gfx::OShaderBuffer vertexBuffer;
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
return gridSize.x * gridSize.y * gridSize.z * 15;
}
constexpr uint getIndexBufferSize() { return gridSize.x * gridSize.y * gridSize.z * 15; }
};
void updateFluidData(uint32 entityID, const Component::Transform& transform, const Component::FluidGrid& grid);
const Map<uint32, FluidData>& getFluidDataMap() const { return fluidDataMap; }
void addSource();
private:
Gfx::OPipelineLayout pipelineLayout;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::ODescriptorLayout gridParamsLayout;
Gfx::OComputeShader shader;
Gfx::PComputePipeline pipeline;
constexpr static UVector threadGroupSize = {32, 8, 1};
Gfx::PGraphics graphics;
Map<uint32, FluidData> fluidDataMap;
};
DEFINE_REF(FluidScene)
} // namespace Seele
+1 -1
View File
@@ -1,7 +1,7 @@
#include "LightEnvironment.h"
#include "Asset/AssetRegistry.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Graphics/Descriptor.h"
#include "Asset/EnvironmentMapAsset.h"
#include "Graphics/Enums.h"
#include "Graphics/Graphics.h"
+4 -10
View File
@@ -1,22 +1,16 @@
#include "Scene.h"
#include "Actor/PointLightActor.h"
#include "Asset/MaterialAsset.h"
#include "Asset/TextureAsset.h"
#include "Component/DirectionalLight.h"
#include "Component/Mesh.h"
#include "Component/PointLight.h"
#include "Component/Transform.h"
#include "Graphics/Graphics.h"
#include "Graphics/Mesh.h"
#include "Graphics/StaticMeshVertexData.h"
#include "LightEnvironment.h"
#include "FluidScene.h"
using namespace Seele;
Scene::Scene(Gfx::PGraphics graphics)
: graphics(graphics), lightEnv(new LightEnvironment(graphics)), physics(registry) {}
: graphics(graphics), lightEnv(new LightEnvironment(graphics)), fluidScene(new FluidScene(graphics)), physics(registry) {}
Scene::~Scene() {}
void Scene::bakeLighting() {}
void Scene::update(float deltaTime) { physics.update(deltaTime); }
+4 -4
View File
@@ -1,16 +1,14 @@
#pragma once
#include "MinimalEngine.h"
#include "Component/Skybox.h"
#include "Graphics/Graphics.h"
#include "LightEnvironment.h"
#include "Physics/PhysicsSystem.h"
#include <entt/entt.hpp>
#include <iostream>
namespace Seele {
DECLARE_REF(Material)
DECLARE_REF(Entity)
DECLARE_REF(FluidScene)
DECLARE_REF(LightEnvironment)
class Scene {
public:
Scene(Gfx::PGraphics graphics);
@@ -33,12 +31,14 @@ class Scene {
template <typename Component> auto constructCallback() { return registry.on_construct<Component>(); }
template <typename Component> auto destroyCallback() { return registry.on_destroy<Component>(); }
PLightEnvironment getLightEnvironment() { return lightEnv; }
PFluidScene getFluidScene() { return fluidScene; }
Gfx::PGraphics getGraphics() const { return graphics; }
entt::registry registry;
private:
Gfx::PGraphics graphics;
OLightEnvironment lightEnv;
OFluidScene fluidScene;
PhysicsSystem physics;
Array<Gfx::OTexture2D> lightMaps;
};
+3
View File
@@ -5,6 +5,8 @@ target_sources(Engine
ComponentSystem.h
Executor.h
Executor.cpp
FluidUpdater.h
FluidUpdater.cpp
KeyboardInput.h
KeyboardInput.cpp
LightGather.h
@@ -21,6 +23,7 @@ target_sources(Engine
CameraUpdater.h
ComponentSystem.h
Executor.h
FluidUpdater.h
KeyboardInput.h
LightGather.h
MeshUpdater.h
+48
View File
@@ -0,0 +1,48 @@
#include "FluidUpdater.h"
#include "Graphics/DebugVertex.h"
#include "Scene/FluidScene.h"
using namespace Seele;
using namespace Seele::System;
FluidUpdater::FluidUpdater(PScene scene) : ComponentSystem(scene) {}
FluidUpdater::~FluidUpdater() {}
void FluidUpdater::update(entt::entity id, Component::FluidGrid &grid, Component::Transform &transform) {
auto fluidScene = scene->getFluidScene();
fluidScene->updateFluidData((uint32)id, transform, grid);
Vector corners[8];
corners[0] = transform.getPosition();
corners[1] = transform.getPosition() + Vector(transform.getScale().x, 0, 0);
corners[2] = transform.getPosition() + Vector(0, transform.getScale().y, 0);
corners[3] = transform.getPosition() + Vector(0, 0, transform.getScale().z);
corners[4] = transform.getPosition() + Vector(transform.getScale().x, transform.getScale().y, 0);
corners[5] = transform.getPosition() + Vector(transform.getScale().x, 0, transform.getScale().z);
corners[6] = transform.getPosition() + Vector(0, transform.getScale().y, transform.getScale().z);
corners[7] = transform.getPosition() + transform.getScale();
const Vector debugColor(1, 0, 0);
auto addEdge = [&](int a, int b) {
addDebugVertex(DebugVertex{.position = corners[a], .color = debugColor});
addDebugVertex(DebugVertex{.position = corners[b], .color = debugColor});
};
// Bottom face
addEdge(0, 1);
addEdge(1, 4);
addEdge(4, 2);
addEdge(2, 0);
// Top face
addEdge(3, 5);
addEdge(5, 7);
addEdge(7, 6);
addEdge(6, 3);
// Vertical edges
addEdge(0, 3);
addEdge(1, 5);
addEdge(2, 6);
addEdge(4, 7);
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "Component/FluidGrid.h"
#include "Component/Transform.h"
#include "ComponentSystem.h"
namespace Seele {
namespace System {
class FluidUpdater : public ComponentSystem<Component::FluidGrid, Component::Transform> {
public:
FluidUpdater(PScene scene);
virtual ~FluidUpdater();
virtual void update(entt::entity id, Component::FluidGrid& grid, Component::Transform& transform);
private:
};
} // namespace System
} // namespace Seele
+2 -2
View File
@@ -25,14 +25,14 @@ void KeyboardInput::update(Component::KeyboardInput& input) {
scrollY = 0;
}
void KeyboardInput::keyCallback(KeyCode code, InputAction action, KeyModifier) { keys[code] = action != InputAction::RELEASE; }
void KeyboardInput::keyCallback(KeyCode code, InputAction action, KeyModifierFlags) { keys[code] = action != InputAction::RELEASE; }
void KeyboardInput::mouseCallback(double x, double y) {
mouseX = (float)x;
mouseY = (float)y;
}
void KeyboardInput::mouseButtonCallback(MouseButton button, InputAction action, KeyModifier) {
void KeyboardInput::mouseButtonCallback(MouseButton button, InputAction action, KeyModifierFlags) {
if (button == MouseButton::MOUSE_BUTTON_1) {
mouse1 = action != InputAction::RELEASE;
}
+2 -2
View File
@@ -9,9 +9,9 @@ class KeyboardInput : public ComponentSystem<Component::KeyboardInput> {
KeyboardInput(PScene scene);
virtual ~KeyboardInput();
virtual void update(Component::KeyboardInput& input) override;
void keyCallback(KeyCode code, InputAction action, KeyModifier modifier);
void keyCallback(KeyCode code, InputAction action, KeyModifierFlags modifier);
void mouseCallback(double xPos, double yPos);
void mouseButtonCallback(MouseButton button, InputAction action, KeyModifier modifier);
void mouseButtonCallback(MouseButton button, InputAction action, KeyModifierFlags modifier);
void scrollCallback(double xScroll, double yScroll);
private:
+1
View File
@@ -1,4 +1,5 @@
#include "LightGather.h"
#include "Scene/LightEnvironment.h"
using namespace Seele;
using namespace Seele::System;
+28 -3
View File
@@ -1,11 +1,36 @@
#pragma once
#include "MinimalEngine.h"
#include "Graphics/Enums.h"
#include "Math/Vector.h"
#include <functional>
namespace Seele {
namespace UI {
// mouse coordinates are in element-local space when the event reaches the element
struct MouseEvent {
Vector2 position;
enum class Kind { Move, Down, Up, Scroll } kind;
Vector2 delta; // scroll
};
struct KeyEvent {
KeyCode code;
InputAction action;
bool shift;
bool ctrl;
bool alt;
char text; // for character input; 0 if none
};
struct Attributes {
std::function<void()> onClick;
bool focusable = false;
// input handlers; return true to consume the event (no further propagation)
std::function<bool(MouseEvent&)> onClick;
std::function<bool(MouseEvent&)> onHover; // hover-enter
std::function<bool(MouseEvent&)> onHoverLeave;
std::function<bool(MouseEvent&)> onScroll;
std::function<bool(KeyEvent&)> onKey;
std::function<bool(KeyEvent&)> onText;
std::function<bool()> onFocus;
std::function<bool()> onBlur;
};
} // namespace UI
} // namespace Seele
} // namespace Seele
+4 -2
View File
@@ -3,7 +3,8 @@ target_sources(Engine
Attributes.h
Element.h
Element.cpp
System.h
System.cpp
)
target_sources(Engine
@@ -11,7 +12,8 @@ target_sources(Engine
FILES
Attributes.h
Element.h
System.h
)
add_subdirectory(Style/)
add_subdirectory(Element/)
add_subdirectory(Element/)
+2 -11
View File
@@ -1,16 +1,7 @@
#include "Element.h"
#include "System.h"
#include "UI/Element.h"
#include "UI/System.h"
using namespace Seele;
using namespace Seele::UI;
Element::Element(Attributes attr, Array<Element*> _children) : attr(attr) {
for (auto c : _children) {
children.add(c);
}
for (auto& child : children) {
child->setParent(this);
}
}
Element::~Element() {}
+84 -19
View File
@@ -1,11 +1,16 @@
#pragma once
#include "Attributes.h"
#include "UI/Attributes.h"
#include "Containers/Array.h"
#include "MinimalEngine.h"
#include "Style/Style.h"
#include "UI/Style/Style.h"
#include "UI/Style/Theme.h"
#include <type_traits>
namespace Seele {
namespace UI {
DECLARE_REF(Element)
// a render quad. background quads use backgroundTexture/backgroundColor;
// text quads additionally carry a glyph (one UIRender per glyph, see Text::render)
struct UIRender {
Gfx::PTexture2D backgroundTexture;
Vector backgroundColor;
@@ -13,23 +18,75 @@ struct UIRender {
Vector2 dimensions;
uint32 z;
uint32 level;
PElement element; // back-ref for input hit-testing
// glyph-only (text): the glyph texture is backgroundTexture, this is the
// per-glyph rect in *atlas* space (0..1) so the pass can emit a glyph quad
bool isGlyph = false;
Vector2 uvOffset = Vector2(0);
Vector2 uvSize = Vector2(0);
};
DECLARE_REF(Element)
class Element {
public:
Element(Attributes attr, Array<Element*> children);
virtual ~Element();
void setParent(PElement p) { parent = p; }
// calculates the final style object taking into account inherited properties
virtual void applyStyle(Style parentStyle) = 0;
void calcStyle(Style parentStyle) {
applyStyle(parentStyle);
for (auto& child : children) {
child->calcStyle(style);
// the System walks the retained tree (style/layout/input/render) and needs to read
// the protected layout/children state; it is the only external consumer.
friend class System;
// children are raw, non-owning pointers (matches the pre-existing call sites, e.g.
// the InspectorView demo: new Div(Attr{}, { new A, new B })). the caller's `new`
// yields the pointer; ownership/lifetime for the demo is the caller's concern (the
// engine does not free them here).
explicit Element(Attributes attr) : attr(attr) {}
Element(Attributes attr, std::initializer_list<Element*> children_) : attr(attr) {
for (auto c : children_) {
if (c != nullptr) {
c->setParent(this);
children.add(c);
}
}
}
virtual ~Element();
void setParent(Element* p) { parent = p; }
// resolves the inherited style + this element's static template classes against the theme.
// only re-run for this element when style.dirty or the theme changed.
virtual void applyStyle(Style parentStyle, const Theme& theme) = 0;
void calcStyle(Style parentStyle, const Theme& theme) {
if (style.dirty || themeChanged) {
style.dirty = false;
themeChanged = false;
applyStyle(parentStyle, theme);
}
for (auto& child : children) {
child->calcStyle(style, theme);
}
}
// explicit mutation entry point: applies runtime overrides, then re-styles the dirty subtree
void markDirty() {
style.dirty = true;
if (parent) {
parent->markDirty();
}
}
// applies runtime style overrides (the data-driven mutation layer)
virtual void applyOverrides() {
// default: no-op; interactive elements override this to reflect hover/focus/etc.
}
// input; concrete signatures differ per element (see Button/Text),
// so the base takes a typed pointer
virtual bool onMouseEvent(MouseEvent* event) { (void)event; return false; }
virtual bool onKeyEvent(KeyEvent* event) { (void)event; return false; }
// interaction state hooks: the System calls these without knowing the concrete
// (templated) Button type. default no-ops; Button overrides. this keeps the
// input pipeline data-driven (on the Element base) and avoids dynamic_cast to a
// template type.
virtual void setHovered(bool hovered) { (void)hovered; }
virtual void setFocused(bool focused) { (void)focused; }
virtual bool isFocusable() const { return attr.focusable; }
// calculates the relative positions of the child elements for the applied layout style
virtual void layout(UVector2 parentSize) {
if (style.hidden) {
return;
}
if (style.maxWidthType == DimensionType::Auto) {
maxDimensions.x = (float)parentSize.x;
}
@@ -63,6 +120,9 @@ class Element {
Vector2 cursor = Vector2(style.paddingLeft, style.paddingTop);
uint32 lineHeight = 0;
for (auto& child : children) {
if (child->style.hidden) {
continue;
}
// inline elements flow left to right
if (child->style.outerDisplay == OuterDisplayType::Inline) {
// only static and relative elements actually reserve
@@ -110,15 +170,17 @@ class Element {
}
}
virtual Array<UIRender> render(Vector2 anchor, uint32 level) {
Array<UIRender> result = {
UIRender{
Array<UIRender> result;
if (!style.hidden && style.outerDisplay != OuterDisplayType::Hidden) {
result.add(UIRender{
.backgroundColor = style.backgroundColor,
.position = position + anchor,
.dimensions = dimensions,
.z = style.z,
.level = level,
},
};
.element = this,
});
}
for (auto& child : children) {
for (const auto& render : child->render(position + anchor, level+1)) {
result.add(render);
@@ -135,9 +197,12 @@ class Element {
protected:
Attributes attr;
PElement parent = nullptr;
Array<OElement> children;
Element* parent = nullptr;
// raw, non-owning (see the ctor comment); matches the pre-existing call sites
Array<Element*> children;
// set by the System when the theme changed; forces a one-time full re-style
bool themeChanged = false;
};
DEFINE_REF(Element)
} // namespace UI
} // namespace Seele
} // namespace Seele
+119
View File
@@ -0,0 +1,119 @@
#pragma once
#include "UI/Element/Div.h"
#include "UI/Style/Class.h"
namespace Seele {
namespace UI {
// an interactive block: hover/focus visuals are data (runtime state) toggled by
// the input pipeline, never template args. the user's classes set the *defaults*;
// the System layers the interaction state on top during style() (see System.h).
template<StyleClass... classes>
class Button : public Div<classes...> {
public:
// forward every Div construction form (no children / variadic / initializer list)
using Div<classes...>::Div;
// Button is a template with a DEPENDENT base (Div<classes...>), so unqualified
// access to inherited members is ill-formed; these using-declarations bring the
// specific members into Button's scope so the body below can use them.
using Div<classes...>::attr;
using Div<classes...>::style;
using Div<classes...>::dimensions;
using Div<classes...>::markDirty;
using Div<classes...>::setParent;
virtual ~Button() {}
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
Div<classes...>::applyStyle(parentStyle, theme);
// hover/focus/pressed overrides are applied by the System right after,
// keyed off this state (data, not template args)
}
virtual bool onMouseEvent(MouseEvent* event) override {
switch (event->kind) {
case MouseEvent::Kind::Move:
if (!hovered) {
hovered = true;
if (attr.onHover) {
attr.onHover(*event);
}
}
break;
case MouseEvent::Kind::Down:
pressed = true;
break;
case MouseEvent::Kind::Up:
if (pressed && hovered) {
pressed = false;
if (attr.onClick) {
attr.onClick(*event);
}
} else {
pressed = false;
}
break;
case MouseEvent::Kind::Scroll:
if (attr.onScroll) {
attr.onScroll(*event);
}
break;
}
markDirty();
return true;
}
virtual bool onKeyEvent(KeyEvent* event) override {
if (event->action == InputAction::PRESS) {
switch (event->code) {
case KeyCode::KEY_SPACE:
case KeyCode::KEY_ENTER:
if (focused && attr.onClick) {
MouseEvent click;
click.kind = MouseEvent::Kind::Down;
click.position = Vector2(dimensions.x / 2, dimensions.y / 2);
attr.onClick(click);
}
return true;
default:
break;
}
}
if (attr.onKey) {
return attr.onKey(*event);
}
return false;
}
// overrides of the Element base virtuals: this is how the System drives hover/focus
// data-drivenly without dynamic_cast to a concrete template type
void setHovered(bool hovered) override {
if (this->hovered == hovered) {
return;
}
this->hovered = hovered;
if (!hovered && attr.onHoverLeave) {
MouseEvent leave;
leave.kind = MouseEvent::Kind::Move;
attr.onHoverLeave(leave);
}
markDirty();
}
void setFocused(bool focused) override {
if (this->focused == focused) {
return;
}
this->focused = focused;
if (focused && attr.onFocus) {
attr.onFocus();
} else if (!focused && attr.onBlur) {
attr.onBlur();
}
markDirty();
}
bool isFocusable() const override { return attr.focusable; }
bool isHovered() const { return hovered; }
bool isFocused() const { return focused; }
bool isPressed() const { return pressed; }
private:
bool hovered = false;
bool focused = false;
bool pressed = false;
};
} // namespace UI
} // namespace Seele
+15 -3
View File
@@ -1,4 +1,16 @@
target_sources(Engine
PRIVATE
Div.h
Text.h)
PRIVATE
Div.h
Text.h
Button.h
Panel.h
)
target_sources(Engine
PUBLIC FILE_SET HEADERS
FILES
Div.h
Text.h
Button.h
Panel.h
)
+6 -4
View File
@@ -7,14 +7,16 @@ namespace UI {
template<StyleClass... classes>
class Div : public Element {
public:
Div(Attributes attr, Array<Element*> children) : Element(attr, std::move(children)) { }
// forward every Element construction form (no children / variadic / initializer list)
using Element::Element;
Div() = delete; // force an explicit style/attrs choice, no accidental empty divs
virtual ~Div() {}
virtual void applyStyle(Style parentStyle) override {
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
style = parentStyle;
style.outerDisplay = OuterDisplayType::Block;
(classes::apply(style), ...);
(classes::apply(style, theme), ...);
}
private:
};
} // namespace UI
} // namespace Seele
} // namespace Seele
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include "UI/Element/Div.h"
#include "UI/Style/Class.h"
namespace Seele {
namespace UI {
// a styled block container: defaults to the standard editor chrome
// (dark background, padding, text color); the user's template args override any of it
template<StyleClass... classes>
class Panel : public Div<classes...> {
public:
// forward every Div construction form (no children / variadic / initializer list)
using Div<classes...>::Div;
virtual ~Panel() {}
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
// start from the panel chrome, then let the user's classes win
style = parentStyle;
style.outerDisplay = OuterDisplayType::Block;
style.backgroundColor = theme.color(ColorToken::Gray900);
style.textColor = theme.color(ColorToken::White);
uint32 pad = theme.spacing(8);
style.paddingTop = pad;
style.paddingBottom = pad;
style.paddingLeft = pad;
style.paddingRight = pad;
(classes::apply(style, theme), ...);
}
private:
};
} // namespace UI
} // namespace Seele
+92 -22
View File
@@ -1,37 +1,107 @@
#pragma once
#include "Asset/AssetRegistry.h"
#include "Asset/FontAsset.h"
#include "UI/Element.h"
#include "UI/Style/Class.h"
#include <ranges>
#include <algorithm>
#include <string_view>
namespace Seele {
namespace UI {
template <StyleClass... classes> class Text : public Element {
// text: the string is data (mutable at runtime); fonts and sizing come from style
// classes. shaping happens in applyStyle (only when dirty); render() just emits quads.
template<StyleClass... classes>
class Text : public Element {
public:
Text(std::string text) : Element({}, {}), text(text) {}
virtual void applyStyle(Style parentStyle) {
// the string is data; Attributes is optional (defaults to no handlers).
// e.g. new Text<Font_Arial, Text_8XL>("hello") or new Text<>(Attributes{...}, "hi")
Text(std::string text, Attributes attr = Attributes{}) : Element(std::move(attr)), _text(std::move(text)) {}
virtual ~Text() {}
// runtime mutation: change the string, invalidate style+layout of this subtree
void setText(std::string text) {
if (text == _text) {
return;
}
_text = std::move(text);
markDirty();
}
const std::string& getText() const { return _text; }
virtual void applyStyle(Style parentStyle, const Theme& theme) override {
style = parentStyle;
style.outerDisplay = OuterDisplayType::Inline;
(classes::apply(style), ...);
}
virtual void layout(UVector2) override {
UVector2 cursor = UVector2(0);
dimensions = style.fontFamily->shapeText(text, style.fontSize, glyphRenders);
(classes::apply(style, theme), ...);
// resolve the font: a font class set style.fontName (a string literal can't
// be a template arg), so we look it up at style() time. invariant: the font
// must be imported before the first System::style() call (see System.h);
// fall back to the theme default if the name isn't found.
PFontAsset font = nullptr;
if (!style.fontName.empty()) {
try {
font = AssetRegistry::findFont("", std::string_view(style.fontName));
} catch (const std::exception&) {
font = nullptr; // unknown name -> fall back to the theme default
}
}
if (font == nullptr) {
font = theme.defaultFont;
}
if (font == nullptr) {
glyphs.clear();
return;
}
style.fontFamily = font;
UVector2 measured = font->shapeText(_text, (uint32)style.fontSize, glyphs);
// measure for auto sizing (shapeText already returns the bounds)
float w = (float)measured.x;
float h = (float)measured.y;
if (style.widthType == UI::DimensionType::Auto) {
style.width = w;
}
if (style.heightType == UI::DimensionType::Auto) {
style.height = h;
}
}
virtual Array<UIRender> render(Vector2 anchor, uint32 level) override {
return Array<UIRender>(std::from_range, glyphRenders | std::views::transform([=](const FontAsset::RenderGlyph& glyph) {
return UIRender{
.backgroundTexture = glyph.texture,
.position = Vector2(glyph.position) + anchor,
.dimensions = Vector2(glyph.dimensions),
.level = level,
};
}));
Array<UIRender> result;
if (style.hidden || style.outerDisplay == OuterDisplayType::Hidden) {
return result;
}
Vector2 pos = position + anchor;
// background quad (if any) — same shape as the base element quad
result.add(UIRender{
.backgroundTexture = nullptr,
.backgroundColor = style.backgroundColor,
.position = pos,
.dimensions = dimensions,
.z = style.z,
.level = level,
.element = this,
});
// text: one render quad per shaped glyph, carrying its atlas rect
if (style.fontFamily != nullptr) {
for (auto& g : glyphs) {
result.add(UIRender{
.backgroundTexture = g.texture,
.backgroundColor = style.textColor,
.position = pos + Vector2(g.position.x, g.position.y),
.dimensions = Vector2(g.dimensions.x, g.dimensions.y),
.z = style.z,
.level = level,
.element = this,
.isGlyph = true,
.uvOffset = Vector2(0),
.uvSize = Vector2(1),
});
}
}
return result;
}
// height = fontsize * 1.12
private:
std::string text;
Array<FontAsset::RenderGlyph> glyphRenders;
std::string _text;
Array<FontAsset::RenderGlyph> glyphs;
};
} // namespace UI
} // namespace Seele
} // namespace Seele
Loaded 100 of 111 files, more files were not shown because too many files have changed in this diff. Show more