diff --git a/res/shaders/WaterMesh.slang b/res/shaders/TerrainPass.slang similarity index 79% rename from res/shaders/WaterMesh.slang rename to res/shaders/TerrainPass.slang index 69fda0b..0c8f4a3 100644 --- a/res/shaders/WaterMesh.slang +++ b/res/shaders/TerrainPass.slang @@ -1,8 +1,78 @@ import Common; +import Bounding; import Scene; -import WaterCommon; import MaterialParameter; +struct TerrainPayload +{ + float3 offset; + float extent; + uint numMeshes; +}; + +struct TerrainTile +{ + int2 location; + float extent; + float height; +}; + +struct TerrainVertex +{ + float4 position_CS : SV_Position; + float3 position_WS : POSITION0; + float2 texCoord : TEXCOORD0; + float depth : DEPTH; + uint lod : BONEINDEX; +}; + +struct TerrainData +{ + StructuredBuffer tiles; + Texture2D displacement; + SamplerState sampler; +}; +ParameterBlock pTerrainData; + +[numthreads(1, 1, 1)] +[shader("amplification")] +void taskMain( + uint threadID: SV_GroupThreadID, + uint3 groupID: SV_GroupID + ) { + TerrainTile tile = pTerrainData.tiles[groupID.x]; + GroupMemoryBarrierWithGroupSync(); + AABB bounding; + bounding.minCorner = float3(tile.location.x, tile.height, tile.location.y) * tile.extent; + bounding.maxCorner = float3(tile.location.x + 1, tile.height, tile.location.y + 1) * tile.extent; + float3 median = (bounding.minCorner + bounding.maxCorner) / 2; + float distance = distance(median, pViewParams.cameraPos_WS.xyz); + float tileDistance = distance / tile.extent; + uint numMeshes = groupID.y + 1; + + if(numMeshes == 4 && tileDistance > 2) + { + return; + } + if(numMeshes == 3 && (tileDistance > 4 || tileDistance < 1)) + { + return; + } + if(numMeshes == 2 && (tileDistance > 8 || tileDistance < 3)) + { + return; + } + if(numMeshes == 1 && tileDistance < 7) + { + return; + } + TerrainPayload payload; + payload.offset = bounding.minCorner; + payload.extent = tile.extent / numMeshes; + payload.numMeshes = numMeshes; + DispatchMesh(numMeshes, numMeshes, 1, payload); +} + const static uint64_t TILE_VERTS = 144; const static uint64_t TILE_PRIMS = 242; @@ -30,8 +100,8 @@ uint3(53, 42, 54), uint3(42, 43, 54), uint3(54, 43, 55), uint3(43, 44, 55), uint void meshMain( in uint threadID: SV_GroupThreadID, in uint3 groupID: SV_GroupID, - in payload WaterPayload params, - out vertices WaterVertex vertices[TILE_VERTS], + in payload TerrainPayload params, + out vertices TerrainVertex vertices[TILE_VERTS], out indices uint3 indices[TILE_PRIMS], ) { float4x4 vp = mul(pViewParams.c.projectionMatrix, pViewParams.c.viewMatrix); @@ -77,23 +147,23 @@ void meshMain( } - float3 displacement1 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile1, 0)).xyz * pWaterMaterial.contributeDisplacement1; - float3 displacement2 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile2, 1)).xyz * pWaterMaterial.contributeDisplacement2; - float3 displacement3 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile3, 2)).xyz * pWaterMaterial.contributeDisplacement3; - float3 displacement4 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile4, 3)).xyz * pWaterMaterial.contributeDisplacement4; - float3 displacement = displacement1 + displacement2 + displacement3 + displacement4; +// float3 displacement1 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile1, 0)).xyz * pWaterMaterial.contributeDisplacement1; +// float3 displacement2 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile2, 1)).xyz * pWaterMaterial.contributeDisplacement2; +// float3 displacement3 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile3, 2)).xyz * pWaterMaterial.contributeDisplacement3; +// float3 displacement4 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile4, 3)).xyz * pWaterMaterial.contributeDisplacement4; + float displacement = pTerrainData.displacement.Sample(pTerrainData.sampler, worldPos.xz); float4 clipPos = mul(vp, float4(worldPos, 1)); float ndcDepth = clipPos.z / clipPos.w; float depth = 1 - (-ndcDepth + 1.0f) / 2.0f; - displacement = lerp(0.0f, displacement, pow(saturate(depth), pWaterMaterial.displacementDepthAttenuation)); +// displacement = lerp(0.0f, displacement, pow(saturate(depth), pTerrainData.displacementDepthAttenuation)); worldPos += displacement; worldPos.y += lodDisplacement; - WaterVertex v; + TerrainVertex v; v.position_WS = worldPos; v.position_CS = mul(vp, float4(worldPos, 1)); v.texCoord = worldPos.xz; @@ -101,4 +171,10 @@ void meshMain( v.lod = params.numMeshes; vertices[i] = v; } +} + +[shader("pixel")] +float4 fragmentMain(TerrainVertex vertex) +{ + return float4(0, 1, 0, 1); } \ No newline at end of file diff --git a/res/shaders/WaterCommon.slang b/res/shaders/WaterCommon.slang deleted file mode 100644 index a07cd2b..0000000 --- a/res/shaders/WaterCommon.slang +++ /dev/null @@ -1,72 +0,0 @@ -struct WaterPayload -{ - float3 offset; - float extent; - uint numMeshes; -}; - -struct WaterTile -{ - int2 location; - float extent; - float height; -} - -struct WaterVertex -{ - float4 position_CS : SV_Position; - float3 position_WS : POSITION0; - float2 texCoord : TEXCOORD0; - float depth : DEPTH; - uint lod : BONEINDEX; -}; - -struct MaterialParams -{ - float3 sunDirection; - float displacementDepthAttenuation; - - float foamSubtract0; - float foamSubtract1; - float foamSubtract2; - float foamSubtract3; - - float tile1; - float tile2; - float tile3; - float tile4; - - float normalStrength; - float foamDepthAttenuation; - float normalDepthAttenuation; - float roughness; - - float3 sunIrradiance; - float foamRoughnessModifier; - - float3 scatterColor; - float environmentLightStrength; - - float3 bubbleColor; - float heightModifier; - - float bubbleDensity; - float wavePeakScatterStrength; - float scatterStrength; - float scatterShadowStrength; - - uint contributeDisplacement1; - uint contributeDisplacement2; - uint contributeDisplacement3; - uint contributeDisplacement4; - - float3 foamColor; - - Texture2DArray displacementTextures; - Texture2DArray slopeTextures; - TextureCube environmentMap; - SamplerState sampler; - StructuredBuffer tiles; -}; -layout(set = 1) -ParameterBlock pWaterMaterial; diff --git a/res/shaders/WaterPass.slang b/res/shaders/WaterPass.slang index 549de04..1a8e787 100644 --- a/res/shaders/WaterPass.slang +++ b/res/shaders/WaterPass.slang @@ -1,5 +1,118 @@ import Common; -import WaterCommon; +import Bounding; +import Scene; + +struct WaterPayload +{ + float3 offset; + float extent; + uint numMeshes; +}; + +struct WaterTile +{ + int2 location; + float extent; + float height; +} + +struct WaterVertex +{ + float4 position_CS : SV_Position; + float3 position_WS : POSITION0; + float2 texCoord : TEXCOORD0; + float depth : DEPTH; + uint lod : BONEINDEX; +}; + +struct MaterialParams +{ + float3 sunDirection; + float displacementDepthAttenuation; + + float foamSubtract0; + float foamSubtract1; + float foamSubtract2; + float foamSubtract3; + + float tile1; + float tile2; + float tile3; + float tile4; + + float normalStrength; + float foamDepthAttenuation; + float normalDepthAttenuation; + float roughness; + + float3 sunIrradiance; + float foamRoughnessModifier; + + float3 scatterColor; + float environmentLightStrength; + + float3 bubbleColor; + float heightModifier; + + float bubbleDensity; + float wavePeakScatterStrength; + float scatterStrength; + float scatterShadowStrength; + + uint contributeDisplacement1; + uint contributeDisplacement2; + uint contributeDisplacement3; + uint contributeDisplacement4; + + float3 foamColor; + + Texture2DArray displacementTextures; + Texture2DArray slopeTextures; + TextureCube environmentMap; + SamplerState sampler; + StructuredBuffer tiles; +}; +layout(set = 1) +ParameterBlock pWaterMaterial; + +[numthreads(1, 1, 1)] +[shader("amplification")] +void taskMain( + uint threadID: SV_GroupThreadID, + uint3 groupID: SV_GroupID + ) { + WaterTile tile = pWaterMaterial.tiles[groupID.x]; + GroupMemoryBarrierWithGroupSync(); + AABB bounding; + bounding.minCorner = float3(tile.location.x, tile.height, tile.location.y) * tile.extent; + bounding.maxCorner = float3(tile.location.x + 1, tile.height, tile.location.y + 1) * tile.extent; + float3 median = (bounding.minCorner + bounding.maxCorner) / 2; + float distance = distance(median, pViewParams.cameraPos_WS.xyz); + float tileDistance = distance / tile.extent; + uint numMeshes = groupID.y + 1; + + if(numMeshes == 4 && tileDistance > 2) + { + return; + } + if(numMeshes == 3 && (tileDistance > 4 || tileDistance < 1)) + { + return; + } + if(numMeshes == 2 && (tileDistance > 8 || tileDistance < 3)) + { + return; + } + if(numMeshes == 1 && tileDistance < 7) + { + return; + } + WaterPayload payload; + payload.offset = bounding.minCorner; + payload.extent = tile.extent / numMeshes; + payload.numMeshes = numMeshes; + DispatchMesh(numMeshes, numMeshes, 1, payload); +} float SchlickFresnel(float3 normal, float3 viewDir) { // 0.02f comes from the reflectivity bias of water kinda idk it's from a paper somewhere i'm not gonna link it tho lmaooo @@ -20,6 +133,107 @@ float Beckmann(float ndoth, float roughness) { return exp(exp_arg) / (PI * roughness * roughness * ndoth * ndoth * ndoth * ndoth); } + +const static uint64_t TILE_VERTS = 144; +const static uint64_t TILE_PRIMS = 242; + +const static float2 VERT[TILE_VERTS] = {float2(0.0f / 11, 0.0f / 11), float2(0.0f / 11, 1.0f / 11), float2(0.0f / 11, 2.0f / 11), float2(0.0f / 11, 3.0f / 11), float2(0.0f / 11, 4.0f / 11), float2(0.0f / 11, 5.0f / 11), float2(0.0f / 11, +6.0f / 11), float2(0.0f / 11, 7.0f / 11), float2(0.0f / 11, 8.0f / 11), float2(0.0f / 11, 9.0f / 11), float2(0.0f / 11, 10.0f / 11), float2(0.0f / 11, 11.0f / 11), float2(1.0f / 11, 0.0f / 11), float2(1.0f / 11, 1.0f / 11), float2(1.0f / 11, 2.0f / 11), float2(1.0f / 11, 3.0f / 11), float2(1.0f / +11, 4.0f / 11), float2(1.0f / 11, 5.0f / 11), float2(1.0f / 11, 6.0f / 11), float2(1.0f / 11, 7.0f / 11), float2(1.0f / 11, 8.0f / 11), float2(1.0f / 11, 9.0f / 11), float2(1.0f / 11, 10.0f / 11), float2(1.0f / 11, 11.0f / 11), float2(2.0f / 11, 0.0f / 11), float2(2.0f / 11, 1.0f / 11), float2(2.0f / 11, 2.0f / 11), float2(2.0f / 11, 3.0f / 11), float2(2.0f / 11, 4.0f / 11), float2(2.0f / 11, 5.0f / 11), float2(2.0f / 11, 6.0f / 11), float2(2.0f / 11, 7.0f / 11), float2(2.0f / 11, 8.0f / 11), float2(2.0f / 11, 9.0f / 11), float2(2.0f / 11, 10.0f / 11), float2(2.0f / 11, 11.0f / 11), float2(3.0f / 11, 0.0f / 11), float2(3.0f / 11, 1.0f / 11), float2(3.0f / 11, 2.0f / 11), float2(3.0f / 11, 3.0f / 11), float2(3.0f / 11, 4.0f / 11), float2(3.0f / 11, 5.0f / 11), float2(3.0f / 11, 6.0f / 11), float2(3.0f / 11, 7.0f / 11), float2(3.0f / 11, 8.0f / 11), float2(3.0f / 11, 9.0f / 11), float2(3.0f / 11, 10.0f / 11), float2(3.0f / 11, 11.0f / 11), float2(4.0f / 11, 0.0f / 11), float2(4.0f +/ 11, 1.0f / 11), float2(4.0f / 11, 2.0f / 11), float2(4.0f / 11, 3.0f / 11), float2(4.0f / 11, 4.0f / 11), float2(4.0f / 11, 5.0f / 11), float2(4.0f / 11, 6.0f / 11), float2(4.0f / 11, 7.0f / 11), float2(4.0f / 11, 8.0f / 11), float2(4.0f / 11, 9.0f / 11), float2(4.0f / 11, 10.0f / 11), float2(4.0f / 11, 11.0f / 11), float2(5.0f / 11, 0.0f / 11), float2(5.0f / 11, 1.0f / 11), float2(5.0f / 11, +2.0f / 11), float2(5.0f / 11, 3.0f / 11), float2(5.0f / 11, 4.0f / 11), float2(5.0f / 11, 5.0f / 11), float2(5.0f / 11, 6.0f / 11), float2(5.0f / 11, 7.0f / 11), float2(5.0f / 11, 8.0f / 11), float2(5.0f / 11, 9.0f / 11), float2(5.0f / 11, 10.0f / 11), float2(5.0f / 11, 11.0f / 11), float2(6.0f / +11, 0.0f / 11), float2(6.0f / 11, 1.0f / 11), float2(6.0f / 11, 2.0f / 11), float2(6.0f / 11, 3.0f / 11), float2(6.0f / 11, 4.0f / 11), float2(6.0f / 11, 5.0f / 11), float2(6.0f / 11, 6.0f / 11), float2(6.0f / 11, 7.0f / 11), float2(6.0f / 11, 8.0f / 11), float2(6.0f / 11, 9.0f / 11), float2(6.0f +/ 11, 10.0f / 11), float2(6.0f / 11, 11.0f / 11), float2(7.0f / 11, 0.0f / 11), float2(7.0f / 11, 1.0f / 11), float2(7.0f / 11, 2.0f / 11), float2(7.0f / 11, 3.0f / 11), float2(7.0f / 11, 4.0f / 11), float2(7.0f / 11, 5.0f / 11), float2(7.0f / 11, 6.0f / 11), float2(7.0f / 11, 7.0f / 11), float2(7.0f / 11, 8.0f / 11), float2(7.0f / 11, 9.0f / 11), float2(7.0f / 11, 10.0f / 11), float2(7.0f / 11, 11.0f / 11), float2(8.0f / 11, 0.0f / 11), float2(8.0f / 11, 1.0f / 11), float2(8.0f / 11, 2.0f / 11), float2(8.0f / 11, 3.0f / 11), float2(8.0f / 11, 4.0f / 11), float2(8.0f / 11, 5.0f / 11), float2(8.0f / 11, 6.0f / 11), float2(8.0f / 11, 7.0f / 11), float2(8.0f / 11, 8.0f / 11), float2(8.0f / +11, 9.0f / 11), float2(8.0f / 11, 10.0f / 11), float2(8.0f / 11, 11.0f / 11), float2(9.0f / 11, 0.0f / 11), float2(9.0f / 11, 1.0f / 11), float2(9.0f / 11, 2.0f / 11), float2(9.0f / 11, 3.0f / 11), float2(9.0f / 11, 4.0f / 11), float2(9.0f / 11, 5.0f / 11), float2(9.0f / 11, 6.0f / 11), float2(9.0f / 11, 7.0f / 11), float2(9.0f / 11, 8.0f / 11), float2(9.0f / 11, 9.0f / 11), float2(9.0f / 11, 10.0f / 11), float2(9.0f / 11, 11.0f / 11), float2(10.0f / 11, 0.0f / 11), float2(10.0f / 11, 1.0f / 11), float2(10.0f / 11, 2.0f / 11), float2(10.0f / 11, 3.0f / 11), float2(10.0f / 11, 4.0f / 11), float2(10.0f / 11, 5.0f / 11), float2(10.0f / 11, 6.0f / 11), float2(10.0f / 11, 7.0f / 11), float2(10.0f / 11, 8.0f / 11), float2(10.0f / 11, 9.0f / 11), float2(10.0f / 11, 10.0f / 11), float2(10.0f / 11, 11.0f / 11), float2(11.0f / 11, 0.0f / 11), float2(11.0f / 11, 1.0f / 11), float2(11.0f / 11, 2.0f / 11), float2(11.0f / 11, 3.0f / 11), float2(11.0f / 11, 4.0f / 11), float2(11.0f / 11, 5.0f / 11), float2(11.0f / 11, 6.0f / 11), float2(11.0f / 11, 7.0f / 11), float2(11.0f / 11, 8.0f / 11), float2(11.0f / 11, 9.0f / 11), float2(11.0f / 11, 10.0f / 11), float2(11.0f / 11, 11.0f / 11)}; + +const static uint3 IND[TILE_PRIMS] = {uint3(0, 1, 12), uint3(12, 1, 13), uint3(1, 2, 13), uint3(13, 2, 14), uint3(2, 3, 14), uint3(14, 3, 15), uint3(3, 4, 15), uint3(15, 4, 16), uint3(4, 5, 16), uint3(16, 5, 17), uint3(5, 6, 17), uint3(17, 6, 18), uint3(6, 7, 18), uint3(18, 7, 19), uint3(7, 8, 19), uint3(19, 8, 20), uint3(8, 9, 20), uint3(20, 9, 21), uint3(9, 10, 21), uint3(21, 10, 22), uint3(10, 11, 22), uint3(22, 11, 23), uint3(12, 13, 24), uint3(24, 13, 25), uint3(13, 14, 25), uint3(25, 14, 26), uint3(14, +15, 26), uint3(26, 15, 27), uint3(15, 16, 27), uint3(27, 16, 28), uint3(16, 17, 28), uint3(28, 17, 29), uint3(17, 18, 29), uint3(29, 18, 30), uint3(18, 19, 30), uint3(30, 19, 31), uint3(19, 20, 31), uint3(31, 20, 32), uint3(20, 21, 32), uint3(32, 21, 33), uint3(21, 22, 33), uint3(33, 22, 34), uint3(22, 23, 34), uint3(34, 23, 35), uint3(24, 25, 36), uint3(36, 25, 37), uint3(25, 26, +37), uint3(37, 26, 38), uint3(26, 27, 38), uint3(38, 27, 39), uint3(27, 28, 39), uint3(39, 28, 40), uint3(28, 29, 40), uint3(40, 29, 41), uint3(29, 30, 41), uint3(41, 30, 42), uint3(30, 31, 42), uint3(42, 31, 43), uint3(31, 32, 43), uint3(43, 32, 44), uint3(32, 33, 44), uint3(44, 33, 45), uint3(33, 34, 45), uint3(45, 34, 46), uint3(34, 35, 46), uint3(46, 35, 47), uint3(36, 37, 48), uint3(48, 37, 49), uint3(37, 38, 49), uint3(49, 38, 50), uint3(38, 39, 50), uint3(50, 39, 51), uint3(39, 40, 51), uint3(51, 40, 52), uint3(40, 41, 52), uint3(52, 41, 53), uint3(41, 42, 53), +uint3(53, 42, 54), uint3(42, 43, 54), uint3(54, 43, 55), uint3(43, 44, 55), uint3(55, 44, 56), uint3(44, 45, 56), uint3(56, 45, 57), uint3(45, 46, 57), uint3(57, 46, 58), uint3(46, 47, 58), uint3(58, 47, 59), uint3(48, 49, 60), uint3(60, 49, 61), uint3(49, 50, 61), uint3(61, 50, 62), uint3(50, 51, 62), uint3(62, 51, 63), uint3(51, 52, 63), uint3(63, 52, 64), uint3(52, 53, 64), uint3(64, 53, 65), uint3(53, 54, 65), uint3(65, 54, 66), uint3(54, 55, 66), uint3(66, 55, 67), uint3(55, 56, 67), uint3(67, 56, 68), uint3(56, 57, 68), uint3(68, 57, 69), uint3(57, 58, 69), uint3(69, 58, 70), uint3(58, 59, 70), uint3(70, 59, 71), uint3(60, 61, 72), uint3(72, 61, 73), uint3(61, 62, 73), uint3(73, 62, 74), uint3(62, 63, 74), uint3(74, 63, 75), uint3(63, 64, 75), uint3(75, 64, 76), uint3(64, 65, 76), uint3(76, 65, 77), uint3(65, 66, 77), uint3(77, 66, 78), uint3(66, 67, 78), uint3(78, 67, 79), uint3(67, 68, 79), uint3(79, 68, 80), uint3(68, 69, 80), uint3(80, 69, 81), uint3(69, 70, 81), uint3(81, 70, 82), uint3(70, 71, 82), uint3(82, 71, 83), uint3(72, +73, 84), uint3(84, 73, 85), uint3(73, 74, 85), uint3(85, 74, 86), uint3(74, 75, 86), uint3(86, 75, 87), uint3(75, 76, 87), uint3(87, 76, 88), uint3(76, 77, 88), uint3(88, 77, 89), uint3(77, 78, 89), uint3(89, 78, 90), uint3(78, 79, 90), uint3(90, 79, 91), uint3(79, 80, 91), uint3(91, 80, 92), uint3(80, 81, 92), uint3(92, 81, 93), uint3(81, 82, 93), uint3(93, 82, 94), uint3(82, 83, +94), uint3(94, 83, 95), uint3(84, 85, 96), uint3(96, 85, 97), uint3(85, 86, 97), uint3(97, 86, 98), uint3(86, 87, 98), uint3(98, 87, 99), uint3(87, 88, 99), uint3(99, 88, 100), uint3(88, 89, 100), uint3(100, 89, 101), uint3(89, 90, 101), uint3(101, 90, 102), uint3(90, 91, 102), uint3(102, 91, 103), uint3(91, 92, 103), uint3(103, 92, 104), uint3(92, 93, 104), uint3(104, 93, 105), uint3(93, 94, 105), uint3(105, 94, 106), uint3(94, 95, 106), uint3(106, 95, 107), uint3(96, 97, 108), uint3(108, 97, 109), uint3(97, 98, 109), uint3(109, 98, 110), uint3(98, 99, 110), uint3(110, 99, 111), uint3(99, 100, 111), uint3(111, 100, 112), uint3(100, 101, 112), uint3(112, 101, 113), uint3(101, 102, 113), uint3(113, 102, 114), uint3(102, 103, 114), uint3(114, 103, 115), uint3(103, 104, 115), uint3(115, 104, 116), uint3(104, 105, 116), uint3(116, 105, 117), uint3(105, 106, 117), uint3(117, 106, 118), uint3(106, 107, 118), uint3(118, 107, 119), uint3(108, 109, 120), uint3(120, 109, 121), uint3(109, 110, 121), uint3(121, 110, 122), uint3(110, 111, 122), uint3(122, 111, +123), uint3(111, 112, 123), uint3(123, 112, 124), uint3(112, 113, 124), uint3(124, 113, 125), uint3(113, 114, 125), uint3(125, 114, 126), uint3(114, 115, 126), uint3(126, 115, 127), uint3(115, 116, 127), uint3(127, 116, 128), uint3(116, 117, 128), uint3(128, 117, 129), uint3(117, 118, 129), uint3(129, 118, 130), uint3(118, 119, 130), uint3(130, 119, 131), uint3(120, 121, 132), uint3(132, 121, 133), uint3(121, 122, 133), uint3(133, 122, 134), uint3(122, 123, 134), uint3(134, 123, 135), uint3(123, 124, 135), uint3(135, 124, 136), uint3(124, 125, 136), uint3(136, 125, 137), uint3(125, 126, 137), uint3(137, 126, 138), uint3(126, 127, 138), uint3(138, 127, 139), uint3(127, 128, 139), uint3(139, 128, 140), uint3(128, 129, 140), uint3(140, 129, 141), uint3(129, 130, 141), uint3(141, 130, 142), uint3(130, 131, 142), uint3(142, 131, 143)}; + + +[numthreads(MESH_GROUP_SIZE, 1, 1)] +[outputtopology("triangle")] +[shader("mesh")] +void meshMain( + in uint threadID: SV_GroupThreadID, + in uint3 groupID: SV_GroupID, + in payload WaterPayload params, + out vertices WaterVertex vertices[TILE_VERTS], + out indices uint3 indices[TILE_PRIMS], +) { + float4x4 vp = mul(pViewParams.projectionMatrix, pViewParams.viewMatrix); + SetMeshOutputCounts(TILE_VERTS, TILE_PRIMS); + + for(uint i = threadID; i < TILE_PRIMS; i += MESH_GROUP_SIZE) + { + indices[i] = IND[i]; + } + for(uint i = threadID; i < TILE_VERTS; i += MESH_GROUP_SIZE) + { + float2 base = VERT[i]; + float3 objectPos = float3(base.x, 0, base.y); + float3 worldPos = params.offset + objectPos * params.extent + float3(groupID.x * params.extent, 0, groupID.y * params.extent); + + float lodDisplacement = 0; + float3 camPos = pViewParams.cameraPos_WS.xyz; + float cameraDistance = distance(worldPos, camPos); + float threshold = 0; + if(params.numMeshes == 3) + { + threshold = 10; + } + if(params.numMeshes == 2) + { + threshold = 30; + } + if(params.numMeshes == 1) + { + threshold = 70; + } + + if(params.numMeshes < 4) + { + if(cameraDistance > threshold) + { + lodDisplacement = clamp(exp(-(1 / (cameraDistance + threshold))), 0, 1) - 1; + } + else + { + lodDisplacement = -1; + } + } + + + float3 displacement1 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile1, 0)).xyz * pWaterMaterial.contributeDisplacement1; + float3 displacement2 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile2, 1)).xyz * pWaterMaterial.contributeDisplacement2; + float3 displacement3 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile3, 2)).xyz * pWaterMaterial.contributeDisplacement3; + float3 displacement4 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz * pWaterMaterial.tile4, 3)).xyz * pWaterMaterial.contributeDisplacement4; + float3 displacement = displacement1 + displacement2 + displacement3 + displacement4; + + float4 clipPos = mul(vp, float4(worldPos, 1)); + float ndcDepth = clipPos.z / clipPos.w; + + float depth = 1 - (-ndcDepth + 1.0f) / 2.0f; + + displacement = lerp(0.0f, displacement, pow(saturate(depth), pWaterMaterial.displacementDepthAttenuation)); + + worldPos += displacement; + worldPos.y += lodDisplacement; + + WaterVertex v; + v.position_WS = worldPos; + v.position_CS = mul(vp, float4(worldPos, 1)); + v.texCoord = worldPos.xz; + v.depth = depth; + v.lod = params.numMeshes; + vertices[i] = v; + } +} + [shader("pixel")] float4 fragmentMain(WaterVertex vert) : SV_TARGET { float3 lightDir = -normalize(pWaterMaterial.sunDirection); diff --git a/res/shaders/WaterTask.slang b/res/shaders/WaterTask.slang deleted file mode 100644 index 6726bb4..0000000 --- a/res/shaders/WaterTask.slang +++ /dev/null @@ -1,43 +0,0 @@ -import Bounding; -import Common; -import Scene; -import WaterCommon; - -[numthreads(1, 1, 1)] -[shader("amplification")] -void taskMain( - uint threadID: SV_GroupThreadID, - uint3 groupID: SV_GroupID - ) { - WaterTile tile = pWaterMaterial.tiles[groupID.x]; - GroupMemoryBarrierWithGroupSync(); - AABB bounding; - bounding.minCorner = float3(tile.location.x, tile.height, tile.location.y) * tile.extent; - bounding.maxCorner = float3(tile.location.x + 1, tile.height, tile.location.y + 1) * tile.extent; - float3 median = (bounding.minCorner + bounding.maxCorner) / 2; - float distance = distance(median, pViewParams.c.cameraPos_WS.xyz); - float tileDistance = distance / tile.extent; - uint numMeshes = groupID.y + 1; - - if(numMeshes == 4 && tileDistance > 2) - { - return; - } - if(numMeshes == 3 && (tileDistance > 4 || tileDistance < 1)) - { - return; - } - if(numMeshes == 2 && (tileDistance > 8 || tileDistance < 3)) - { - return; - } - if(numMeshes == 1 && tileDistance < 7) - { - return; - } - WaterPayload payload; - payload.offset = bounding.minCorner; - payload.extent = tile.extent / numMeshes; - payload.numMeshes = numMeshes; - DispatchMesh(numMeshes, numMeshes, 1, payload); -} diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index d543af0..ee25828 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -54,7 +54,12 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path& } else if (std::filesystem::exists(meshDirectory / texPath)) { texPath = meshDirectory / texPath; } else { - texPath = (meshDirectory / texPath).replace_extension("png"); + if (tex->mFilename.length == 0) { + texPath = (meshDirectory / fmt::format("Texture{0}", i)); + } else { + texPath = (meshDirectory / texPath); + } + texPath = texPath.replace_extension(tex->achFormatHint); if (tex->mHeight == 0) { std::cout << "Dumping texture " << texPath << std::endl; // already compressed, just dump it to the disk @@ -219,18 +224,17 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array& expressions.back()->key = colorExtract; expressions.back()->inputs["target"].source = sampleKey; - if (alpha != nullptr) - { + if (alpha != nullptr) { std::string alphaExtract = fmt::format("{0}Alpha{1}", paramKey, index); expressions.add(new SwizzleExpression({3, -1, -1, -1})); expressions.back()->key = alphaExtract; expressions.back()->inputs["target"].source = sampleKey; - //std::string alphaMul = fmt::format("{0}AlphaMul{1}", paramKey, index); - //expressions.add(new MulExpression()); - //expressions.back()->key = alphaMul; - //expressions.back()->inputs["lhs"].source = *alpha; - //expressions.back()->inputs["rhs"].source = alphaExtract; + // std::string alphaMul = fmt::format("{0}AlphaMul{1}", paramKey, index); + // expressions.add(new MulExpression()); + // expressions.back()->key = alphaMul; + // expressions.back()->inputs["lhs"].source = *alpha; + // expressions.back()->inputs["rhs"].source = alphaExtract; *alpha = alphaExtract; } @@ -561,9 +565,8 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset) { meshAsset->setStatus(Asset::Status::Loading); Assimp::Importer importer; importer.ReadFile(args.filePath.string().c_str(), - (uint32)(aiProcess_FlipUVs | aiProcess_Triangulate | aiProcess_SortByPType | - aiProcess_GenBoundingBoxes | aiProcess_GenSmoothNormals | - aiProcess_GenUVCoords | aiProcess_FindDegenerates)); + (uint32)(aiProcess_FlipUVs | aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_GenBoundingBoxes | + aiProcess_GenSmoothNormals | aiProcess_GenUVCoords | aiProcess_FindDegenerates)); const aiScene* scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace); std::cout << importer.GetErrorString() << std::endl; @@ -593,6 +596,6 @@ void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset) { meshAsset->physicsMesh = std::move(collider); AssetRegistry::saveAsset(meshAsset, MeshAsset::IDENTIFIER, meshAsset->getFolderPath(), meshAsset->getName()); - + meshAsset->setStatus(Asset::Status::Ready); } diff --git a/src/Editor/Window/PlayView.cpp b/src/Editor/Window/PlayView.cpp index ed82e86..600451d 100644 --- a/src/Editor/Window/PlayView.cpp +++ b/src/Editor/Window/PlayView.cpp @@ -45,6 +45,4 @@ void PlayView::keyCallback(KeyCode code, InputAction action, KeyModifier modifie std::cout << cam.getCameraPosition() << std::endl; std::cout << tra.getRotation() << std::endl; } - if (code == KeyCode::KEY_R && action == InputAction::RELEASE) { - } } diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index d5c1b8a..eb8e8a3 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -77,6 +77,14 @@ int main() { // .filePath = sourcePath / "import/models/city-suburbs/source/city-suburbs.obj", // .importPath = "suburbs", //}); + //AssetImporter::importMesh(MeshImportArgs{ + // .filePath = sourcePath / "import/models/minecraft-medieval-city.fbx", + // .importPath = "minecraft", + //}); + //AssetImporter::importMesh(MeshImportArgs{ + // .filePath = sourcePath / "import/models/Volvo/Volvo.fbx", + // .importPath = "Volvo", + //}); getThreadPool().waitIdle(); vd->commitMeshes(); WindowCreateInfo mainWindowInfo = { diff --git a/src/Engine/Component/CMakeLists.txt b/src/Engine/Component/CMakeLists.txt index 6e03346..751508d 100644 --- a/src/Engine/Component/CMakeLists.txt +++ b/src/Engine/Component/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources(Engine ShapeBase.cpp Skybox.h SphereCollider.h + TerrainTile.h Transform.h Transform.cpp WaterTile.h) @@ -37,5 +38,6 @@ target_sources(Engine ShapeBase.h Skybox.h SphereCollider.h + TerrainTile.h Transform.h WaterTile.h) \ No newline at end of file diff --git a/src/Engine/Component/TerrainTile.h b/src/Engine/Component/TerrainTile.h new file mode 100644 index 0000000..67a931a --- /dev/null +++ b/src/Engine/Component/TerrainTile.h @@ -0,0 +1,13 @@ +#pragma once +#include "Math/Vector.h" +#include "MinimalEngine.h" + +namespace Seele { +namespace Component { +struct TerrainTile { + IVector2 location; + float height; + constexpr static float DIMENSIONS = 10; +}; +} // namespace Component +} // namespace Seele \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 8ce8096..31a3a6c 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -27,6 +27,7 @@ void Seele::addDebugVertices(Array verts) { gDebugVertices.addAll(v BasePass::BasePass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) { //waterRenderer = new WaterRenderer(graphics, scene, viewParamsLayout); + terrainRenderer = new TerrainRenderer(graphics, scene, viewParamsLayout); basePassLayout = graphics->createPipelineLayout("BasePassLayout"); basePassLayout->addDescriptorLayout(viewParamsLayout); @@ -99,6 +100,7 @@ void BasePass::beginFrame(const Component::Camera& cam) { transparentCulling = lightCullingLayout->allocateDescriptorSet(); //waterRenderer->beginFrame(); + terrainRenderer->beginFrame(); // Debug vertices { @@ -247,7 +249,8 @@ void BasePass::render() { } } - //commands.add(waterRenderer->render(viewParamsSet)); + // commands.add(waterRenderer->render(viewParamsSet)); + commands.add(terrainRenderer->render(viewParamsSet)); // Skybox { @@ -471,6 +474,7 @@ void BasePass::createRenderPass() { tLightGrid = resources->requestTexture("LIGHTCULLING_TLIGHTGRID"); //waterRenderer->setViewport(viewport, renderPass); + terrainRenderer->setViewport(viewport, renderPass); // Debug rendering { diff --git a/src/Engine/Graphics/RenderPass/BasePass.h b/src/Engine/Graphics/RenderPass/BasePass.h index df99ad5..67c414f 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.h +++ b/src/Engine/Graphics/RenderPass/BasePass.h @@ -2,6 +2,7 @@ #include "MinimalEngine.h" #include "RenderPass.h" #include "WaterRenderer.h" +#include "TerrainRenderer.h" namespace Seele { DECLARE_REF(CameraActor) @@ -50,6 +51,7 @@ class BasePass : public RenderPass { Gfx::PShaderBuffer cullingBuffer; OWaterRenderer waterRenderer; + OTerrainRenderer terrainRenderer; // Debug rendering Gfx::OVertexInput debugVertexInput; diff --git a/src/Engine/Graphics/RenderPass/CMakeLists.txt b/src/Engine/Graphics/RenderPass/CMakeLists.txt index 34904c2..c5916c1 100644 --- a/src/Engine/Graphics/RenderPass/CMakeLists.txt +++ b/src/Engine/Graphics/RenderPass/CMakeLists.txt @@ -15,6 +15,8 @@ target_sources(Engine RenderGraphResources.cpp RenderPass.h RenderPass.cpp + TerrainRenderer.h + TerrainRenderer.cpp TextPass.h TextPass.cpp UIPass.h @@ -35,6 +37,7 @@ target_sources(Engine RenderGraph.h RenderGraphResources.h RenderPass.h + TerrainRenderer.h TextPass.h UIPass.h VisibilityPass.h) \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp b/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp new file mode 100644 index 0000000..aa2621f --- /dev/null +++ b/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp @@ -0,0 +1,134 @@ +#include "TerrainRenderer.h" +#include "Asset/AssetRegistry.h" +#include "Component/TerrainTile.h" +#include "Graphics/Graphics.h" +#include "Graphics/Shader.h" + +using namespace Seele; + +TerrainRenderer::TerrainRenderer(Gfx::PGraphics graphics, PScene scene, Gfx::PDescriptorLayout viewParamsLayout) + : graphics(graphics), scene(scene) { + tilesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{ + .dynamic = true, + .name = "TilesBuffer", + }); + float displacementData = 0; + displacementMap = graphics->createTexture2D(TextureCreateInfo{ + .sourceData = + { + .size = sizeof(float), + .data = (uint8*)&displacementData, + }, + .format = Gfx::SE_FORMAT_R32_SFLOAT, + .width = 1, + .height = 1, + .depth = 1, + .name = "TerrainDisplacement", + }); + sampler = graphics->createSampler(SamplerCreateInfo{}); + layout = graphics->createDescriptorLayout("pTerrainData"); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 0, + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 1, + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 2, + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER, + }); + layout->create(); + pipelineLayout = graphics->createPipelineLayout("TerrainLayout"); + pipelineLayout->addDescriptorLayout(viewParamsLayout); + pipelineLayout->addDescriptorLayout(layout); + ShaderCompilationInfo s{ + .name = "TerrainShaders", + .modules = {"TerrainPass"}, + .entryPoints = + { + {"taskMain", "TerrainPass"}, + {"meshMain", "TerrainPass"}, + {"fragmentMain", "TerrainPass"}, + }, + .rootSignature = pipelineLayout, + .dumpIntermediate = false, + }; + graphics->beginShaderCompilation(s); + task = graphics->createTaskShader({0}); + mesh = graphics->createMeshShader({1}); + frag = graphics->createFragmentShader({2}); + pipelineLayout->create(); +} + +TerrainRenderer::~TerrainRenderer() {} + +void TerrainRenderer::beginFrame() { + struct TerrainTile { + IVector2 offset; + float extent; + float height; + }; + Array payloads; + scene->view([&](Component::TerrainTile& tile) { + payloads.add(TerrainTile{ + .offset = IVector2(tile.location), + .extent = Component::TerrainTile::DIMENSIONS, + .height = tile.height, + }); + }); + if (payloads.size() == 0) + return; + tilesBuffer->rotateBuffer(sizeof(TerrainTile) * payloads.size()); + tilesBuffer->updateContents(0, sizeof(TerrainTile) * payloads.size(), payloads.data()); + tilesBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, + Gfx::SE_PIPELINE_STAGE_TASK_SHADER_BIT_EXT); + + set = layout->allocateDescriptorSet(); + set->updateBuffer(0, tilesBuffer); + set->updateTexture(1, 0, Gfx::PTexture2D(displacementMap)); + set->updateSampler(2, sampler); + set->writeChanges(); +} + +Gfx::ORenderCommand TerrainRenderer::render(Gfx::PDescriptorSet viewParamsSet) { + Gfx::ORenderCommand command = graphics->createRenderCommand("TerrainRender"); + command->setViewport(viewport); + command->bindPipeline(pipeline); + command->bindDescriptor({viewParamsSet, set}); + command->drawMesh(400, 4, 1); + return command; +} + +void TerrainRenderer::setViewport(Gfx::PViewport _viewport, Gfx::PRenderPass renderPass) { + viewport = _viewport; + + Gfx::MeshPipelineCreateInfo pipelineInfo = { + .taskShader = task, + .meshShader = mesh, + .fragmentShader = frag, + .renderPass = renderPass, + .pipelineLayout = pipelineLayout, + .multisampleState = + { + .samples = viewport->getSamples(), + }, + .rasterizationState = + { + //.polygonMode = Gfx::SE_POLYGON_MODE_LINE, + .cullMode = Gfx::SE_CULL_MODE_BACK_BIT, + }, + .colorBlend = + { + .attachmentCount = 1, + .blendAttachments = + { + Gfx::ColorBlendState::BlendAttachment{ + .blendEnable = false, + }, + }, + }, + }; + pipeline = graphics->createGraphicsPipeline(std::move(pipelineInfo)); +} \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/TerrainRenderer.h b/src/Engine/Graphics/RenderPass/TerrainRenderer.h new file mode 100644 index 0000000..dcbbbc0 --- /dev/null +++ b/src/Engine/Graphics/RenderPass/TerrainRenderer.h @@ -0,0 +1,29 @@ +#pragma once +#include "RenderPass.h" + +namespace Seele { +class TerrainRenderer { + public: + TerrainRenderer(Gfx::PGraphics graphics, PScene scene, Gfx::PDescriptorLayout viewParamsLayout); + ~TerrainRenderer(); + void beginFrame(); + Gfx::ORenderCommand render(Gfx::PDescriptorSet viewParamsSet); + void setViewport(Gfx::PViewport viewport, Gfx::PRenderPass renderPass); + + private: + Gfx::PGraphics graphics; + PScene scene; + Gfx::ODescriptorLayout layout; + Gfx::PDescriptorSet set; + Gfx::OPipelineLayout pipelineLayout; + Gfx::OTaskShader task; + Gfx::OMeshShader mesh; + Gfx::OFragmentShader frag; + Gfx::PGraphicsPipeline pipeline; + Gfx::PViewport viewport; + Gfx::OShaderBuffer tilesBuffer; + Gfx::OTexture2D displacementMap; + Gfx::OSampler sampler; +}; +DEFINE_REF(TerrainRenderer); +} \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/WaterRenderer.cpp b/src/Engine/Graphics/RenderPass/WaterRenderer.cpp index 6e8f33c..ac0c41f 100644 --- a/src/Engine/Graphics/RenderPass/WaterRenderer.cpp +++ b/src/Engine/Graphics/RenderPass/WaterRenderer.cpp @@ -243,15 +243,15 @@ WaterRenderer::WaterRenderer(Gfx::PGraphics graphics, PScene scene, Gfx::PDescri ShaderCompilationInfo createInfo = { .name = "WaterTiles", - .modules = {"WaterTask", "WaterMesh", "WaterPass"}, + .modules = {"WaterPass"}, .entryPoints = { - {"taskMain", "WaterTask"}, - {"meshMain", "WaterMesh"}, + {"taskMain", "WaterPass"}, + {"meshMain", "WaterPass"}, {"fragmentMain", "WaterPass"}, }, .rootSignature = waterLayout, - .dumpIntermediate = true, + .dumpIntermediate = false, }; graphics->beginShaderCompilation(createInfo); waterTask = graphics->createTaskShader({0}); @@ -353,7 +353,7 @@ void WaterRenderer::beginFrame() { Array payloads; scene->view([&](Component::WaterTile& tile) { payloads.add(WaterTile{ - .offset = Vector2(tile.location), + .offset = IVector2(tile.location), .extent = Component::WaterTile::DIMENSIONS, .height = tile.height, }); diff --git a/src/Engine/Graphics/slang-compile.cpp b/src/Engine/Graphics/slang-compile.cpp index 495b29d..e7a5af7 100644 --- a/src/Engine/Graphics/slang-compile.cpp +++ b/src/Engine/Graphics/slang-compile.cpp @@ -42,7 +42,7 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg option[1].value.intValue0 = 1; option[2].name = slang::CompilerOptionName::DebugInformation; option[2].value.kind = slang::CompilerOptionValueKind::Int; - option[2].value.intValue0 = SLANG_DEBUG_INFO_LEVEL_STANDARD; + option[2].value.intValue0 = SLANG_DEBUG_INFO_LEVEL_NONE; option[3].name = slang::CompilerOptionName::DebugInformationFormat; option[3].value.kind = slang::CompilerOptionValueKind::Int; option[3].value.intValue0 = SLANG_DEBUG_INFO_FORMAT_PDB;