Trying to add water

This commit is contained in:
Dynamitos
2024-08-13 22:44:04 +02:00
parent 97244e87c1
commit 252a241208
43 changed files with 1408 additions and 219 deletions
+53
View File
@@ -0,0 +1,53 @@
struct WaterPayload
{
float2 offset;
float extent;
float height;
};
struct WaterVertex
{
float4 position_CS : SV_Position;
float3 position_WS : POSITION0;
float2 texCoord : TEXCOORD0;
float depth : DEPTH;
};
struct MaterialParams
{
float3 sunDirection;
float displacementDepthAttenuation;
float foamSubtract0;
float foamSubtract1;
float foamSubtract2;
float foamSubtract3;
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;
float3 foamColor;
Texture2DArray displacementTextures;
Texture2DArray<float2> slopeTextures;
TextureCube environmentMap;
SamplerState sampler;
StructuredBuffer<WaterPayload> tiles;
};
layout(set = 1)
ParameterBlock<MaterialParams> pWaterMaterial;
+329
View File
@@ -0,0 +1,329 @@
const static float PI = 3.1415926535897932f;
// FFT and JONSWAP Implementation largely referenced from https://github.com/gasgiant/FFT-Ocean/
struct SpectrumParameters {
float scale;
float angle;
float spreadBlend;
float swell;
float alpha;
float peakOmega;
float gamma;
float shortWavesFade;
};
struct ComputeParams
{
float frameTime, deltaTime, gravity, repeatTime, depth, lowCutoff, highCutoff;
int seed;
float2 lambda;
uint N, lengthScale0, lengthScale1, lengthScale2, lengthScale3;
float foamBias, foamDecayRate, foamAdd, foamThreshold;
RWTexture2DArray<float4> spectrumTextures, initialSpectrumTextures, displacementTextures;
RWTexture2DArray<float2> slopeTexture;
RWTexture2D<half> boyancyData;
StructuredBuffer<SpectrumParameters> spectrums;
SamplerState linear_repeat_sampler;
};
layout(set = 0)
ParameterBlock<ComputeParams> pParams;
float2 ComplexMult(float2 a, float2 b) {
return float2(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
}
float2 EulerFormula(float x) {
return float2(cos(x), sin(x));
}
float hash(uint n) {
// integer hash copied from Hugo Elias
n = (n << 13U) ^ n;
n = n * (n * n * 15731U + 0x789221U) + (0x1376312589U);
return float(n & uint(0x7fffffffU)) / float(0x7fffffff);
}
float2 UniformToGaussian(float u1, float u2) {
float R = sqrt(-2.0f * log(u1));
float theta = 2.0f * PI * u2;
return float2(R * cos(theta), R * sin(theta));
}
float Dispersion(float kMag) {
return sqrt(pParams.gravity * kMag * tanh(min(kMag * pParams.depth, 20)));
}
float DispersionDerivative(float kMag) {
float th = tanh(min(kMag * pParams.depth, 20));
float ch = cosh(kMag * pParams.depth);
return pParams.gravity * (pParams.depth * kMag / ch / ch + th) / Dispersion(kMag) / 2.0f;
}
float NormalizationFactor(float s) {
float s2 = s * s;
float s3 = s2 * s;
float s4 = s3 * s;
if (s < 5) return -0.000564f * s4 + 0.00776f * s3 - 0.044f * s2 + 0.192f * s + 0.163f;
else return -4.80e-08f * s4 + 1.07e-05f * s3 - 9.53e-04f * s2 + 5.90e-02f * s + 3.93e-01f;
}
float DonelanBannerBeta(float x) {
if (x < 0.95f) return 2.61f * pow(abs(x), 1.3f);
if (x < 1.6f) return 2.28f * pow(abs(x), -1.3f);
float p = -0.4f + 0.8393f * exp(-0.567f * log(x * x));
return pow(10.0f, p);
}
float DonelanBanner(float theta, float omega, float peakOmega) {
float beta = DonelanBannerBeta(omega / peakOmega);
float sech = 1.0f / cosh(beta * theta);
return beta / 2.0f / tanh(beta * 3.1416f) * sech * sech;
}
float Cosine2s(float theta, float s) {
return NormalizationFactor(s) * pow(abs(cos(0.5f * theta)), 2.0f * s);
}
float SpreadPower(float omega, float peakOmega) {
if (omega > peakOmega)
return 9.77f * pow(abs(omega / peakOmega), -2.5f);
else
return 6.97f * pow(abs(omega / peakOmega), 5.0f);
}
float DirectionSpectrum(float theta, float omega, SpectrumParameters spectrum) {
float s = SpreadPower(omega, spectrum.peakOmega) + 16 * tanh(min(omega / spectrum.peakOmega, 20)) * spectrum.swell * spectrum.swell;
return lerp(2.0f / 3.1415f * cos(theta) * cos(theta), Cosine2s(theta - spectrum.angle, s), spectrum.spreadBlend);
}
float TMACorrection(float omega) {
float omegaH = omega * sqrt(pParams.depth / pParams.gravity);
if (omegaH <= 1.0f)
return 0.5f * omegaH * omegaH;
if (omegaH < 2.0f)
return 1.0f - 0.5f * (2.0f - omegaH) * (2.0f - omegaH);
return 1.0f;
}
float JONSWAP(float omega, SpectrumParameters spectrum) {
float sigma = (omega <= spectrum.peakOmega) ? 0.07f : 0.09f;
float r = exp(-(omega - spectrum.peakOmega) * (omega - spectrum.peakOmega) / 2.0f / sigma / sigma / spectrum.peakOmega / spectrum.peakOmega);
float oneOverOmega = 1.0f / omega;
float peakOmegaOverOmega = spectrum.peakOmega / omega;
return spectrum.scale * TMACorrection(omega) * spectrum.alpha * pParams.gravity * pParams.gravity
* oneOverOmega * oneOverOmega * oneOverOmega * oneOverOmega * oneOverOmega
* exp(-1.25f * peakOmegaOverOmega * peakOmegaOverOmega * peakOmegaOverOmega * peakOmegaOverOmega)
* pow(abs(spectrum.gamma), r);
}
float ShortWavesFade(float kLength, SpectrumParameters spectrum) {
return exp(-spectrum.shortWavesFade * spectrum.shortWavesFade * kLength * kLength);
}
[numthreads(8,8,1)]
void CS_InitializeSpectrum(uint3 id : SV_DISPATCHTHREADID) {
uint seed = id.x + pParams.N * id.y + pParams.N;
seed += pParams.seed;
float lengthScales[4] = { pParams.lengthScale0, pParams.lengthScale1, pParams.lengthScale2, pParams.lengthScale3 };
for (uint i = 0; i < 4; ++i) {
float halfN = pParams.N / 2.0f;
float deltaK = 2.0f * PI / lengthScales[i];
float2 K = (id.xy - halfN) * deltaK;
float kLength = length(K);
seed += i + uint(hash(seed) * 10);
float4 uniformRandSamples = float4(hash(seed), hash(seed * 2), hash(seed * 3), hash(seed * 4));
float2 gauss1 = float2(1, 1);UniformToGaussian(uniformRandSamples.x, uniformRandSamples.y);
float2 gauss2 = float2(1, 1);UniformToGaussian(uniformRandSamples.z, uniformRandSamples.w);
if (pParams.lowCutoff <= kLength && kLength <= pParams.highCutoff) {
float kAngle = atan2(K.y, K.x);
float omega = Dispersion(kLength);
float dOmegadk = DispersionDerivative(kLength);
float spectrum = JONSWAP(omega, pParams.spectrums[i * 2]) * DirectionSpectrum(kAngle, omega, pParams.spectrums[i * 2]) * ShortWavesFade(kLength, pParams.spectrums[i * 2]);
if (pParams.spectrums[i * 2 + 1].scale > 0)
spectrum += JONSWAP(omega, pParams.spectrums[i * 2 + 1]) * DirectionSpectrum(kAngle, omega, pParams.spectrums[i * 2 + 1]) * ShortWavesFade(kLength, pParams.spectrums[i * 2 + 1]);
pParams.initialSpectrumTextures[uint3(id.xy, i)] = float4(float2(gauss2.x, gauss1.y) * sqrt(2 * spectrum * abs(dOmegadk) / kLength * deltaK * deltaK), 0.0f, 0.0f);
} else {
pParams.initialSpectrumTextures[uint3(id.xy, i)] = 0.0f;
}
}
}
[numthreads(8,8,1)]
void CS_PackSpectrumConjugate(uint3 id : SV_DISPATCHTHREADID) {
for (uint i = 0; i < 4; ++i) {
float2 h0 = pParams.initialSpectrumTextures[uint3(id.xy, i)].rg;
float2 h0conj = pParams.initialSpectrumTextures[uint3((pParams.N - id.x ) % pParams.N, (pParams.N - id.y) % pParams.N, i)].rg;
pParams.initialSpectrumTextures[uint3(id.xy, i)] = float4(h0, h0conj.x, -h0conj.y);
}
}
[numthreads(8, 8, 1)]
void CS_UpdateSpectrumForFFT(uint3 id : SV_DISPATCHTHREADID) {
float lengthScales[4] = { pParams.lengthScale0, pParams.lengthScale1, pParams.lengthScale2, pParams.lengthScale3 };
for (int i = 0; i < 4; ++i) {
float4 initialSignal = pParams.initialSpectrumTextures[uint3(id.xy, i)];
float2 h0 = initialSignal.xy;
float2 h0conj = initialSignal.zw;
float halfN = pParams.N / 2.0f;
float2 K = (id.xy - halfN) * 2.0f * PI / lengthScales[i];
float kMag = length(K);
float kMagRcp = rcp(kMag);
if (kMag < 0.0001f) {
kMagRcp = 1.0f;
}
float w_0 = 2.0f * PI / pParams.repeatTime;
float dispersion = floor(sqrt(pParams.gravity * kMag) / w_0) * w_0 * pParams.frameTime;
float2 exponent = EulerFormula(dispersion);
float2 htilde = ComplexMult(h0, exponent) + ComplexMult(h0conj, float2(exponent.x, -exponent.y));
float2 ih = float2(-htilde.y, htilde.x);
float2 displacementX = ih * K.x * kMagRcp;
float2 displacementY = htilde;
float2 displacementZ = ih * K.y * kMagRcp;
float2 displacementX_dx = -htilde * K.x * K.x * kMagRcp;
float2 displacementY_dx = ih * K.x;
float2 displacementZ_dx = -htilde * K.x * K.y * kMagRcp;
float2 displacementY_dz = ih * K.y;
float2 displacementZ_dz = -htilde * K.y * K.y * kMagRcp;
float2 htildeDisplacementX = float2(displacementX.x - displacementZ.y, displacementX.y + displacementZ.x);
float2 htildeDisplacementZ = float2(displacementY.x - displacementZ_dx.y, displacementY.y + displacementZ_dx.x);
float2 htildeSlopeX = float2(displacementY_dx.x - displacementY_dz.y, displacementY_dx.y + displacementY_dz.x);
float2 htildeSlopeZ = float2(displacementX_dx.x - displacementZ_dz.y, displacementX_dx.y + displacementZ_dz.x);
pParams.spectrumTextures[uint3(id.xy, i * 2)] = float4(htildeDisplacementX, htildeDisplacementZ);
pParams.spectrumTextures[uint3(id.xy, i * 2 + 1)] = float4(htildeSlopeX, htildeSlopeZ);
}
}
float2 ComplexExp(float2 a) {
return float2(cos(a.y), sin(a.y) * exp(a.x));
}
float4 ComputeTwiddleFactorAndInputIndices(uint2 id) {
uint b = pParams.N >> (id.x + 1);
float2 mult = 2 * PI * float2(0.0f, 1.0f) / pParams.N;
uint i = (2 * b * (id.y / b) + id.y % b) % pParams.N;
float2 twiddle = ComplexExp(-mult * ((id.y / b) * b));
return float4(twiddle, i, i + b);
}
#define SIZE 1024
#define LOG_SIZE 10
groupshared float4 fftGroupBuffer[2][SIZE];
void ButterflyValues(uint step, uint index, out uint2 indices, out float2 twiddle) {
const float twoPi = 6.28318530718;
uint b = SIZE >> (step + 1);
uint w = b * (index / b);
uint i = (w + index) % SIZE;
sincos(-twoPi / SIZE * w, twiddle.y, twiddle.x);
//This is what makes it the inverse FFT
twiddle.y = -twiddle.y;
indices = uint2(i, i + b);
}
float4 FFT(uint threadIndex, float4 input) {
fftGroupBuffer[0][threadIndex] = input;
GroupMemoryBarrierWithGroupSync();
bool flag = false;
[unroll]
for (uint step = 0; step < LOG_SIZE; ++step) {
uint2 inputsIndices;
float2 twiddle;
ButterflyValues(step, threadIndex, inputsIndices, twiddle);
float4 v = fftGroupBuffer[uint(flag)][inputsIndices.y];
fftGroupBuffer[uint(flag)][threadIndex] = fftGroupBuffer[uint(flag)][inputsIndices.x] + float4(ComplexMult(twiddle, v.xy), ComplexMult(twiddle, v.zw));
flag = !flag;
GroupMemoryBarrierWithGroupSync();
}
return fftGroupBuffer[uint(flag)][threadIndex];
}
[numthreads(SIZE, 1, 1)]
void CS_HorizontalFFT(uint3 id : SV_DISPATCHTHREADID) {
for (int i = 0; i < 8; ++i) {
pParams.spectrumTextures[uint3(id.xy, i)] = FFT(id.x, pParams.spectrumTextures[uint3(id.xy, i)]);
}
}
[numthreads(SIZE, 1, 1)]
void CS_VerticalFFT(uint3 id : SV_DISPATCHTHREADID) {
for (int i = 0; i < 8; ++i) {
pParams.spectrumTextures[uint3(id.yx, i)] = FFT(id.x, pParams.spectrumTextures[uint3(id.yx, i)]);
}
}
float4 Permute(float4 data, float3 id) {
return data * (1.0f - 2.0f * ((id.x + id.y) % 2));
}
[numthreads(8, 8, 1)]
void CS_AssembleMaps(uint3 id : SV_DISPATCHTHREADID) {
for (int i = 0; i < 4; ++i) {
float4 htildeDisplacement = Permute(pParams.spectrumTextures[uint3(id.xy, i * 2)], id);
float4 htildeSlope = Permute(pParams.spectrumTextures[uint3(id.xy, i * 2 + 1)], id);
float2 dxdz = htildeDisplacement.rg;
float2 dydxz = htildeDisplacement.ba;
float2 dyxdyz = htildeSlope.rg;
float2 dxxdzz = htildeSlope.ba;
float jacobian = (1.0f + pParams.lambda.x * dxxdzz.x) * (1.0f + pParams.lambda.y * dxxdzz.y) - pParams.lambda.x * pParams.lambda.y * dydxz.y * dydxz.y;
float3 displacement = float3(pParams.lambda.x * dxdz.x, dydxz.x, pParams.lambda.y * dxdz.y);
float2 slopes = dyxdyz.xy / (1 + abs(dxxdzz * pParams.lambda));
float covariance = slopes.x * slopes.y;
float foam = pParams.displacementTextures[uint3(id.xy, i)].a;
foam *= exp(-pParams.foamDecayRate);
foam = saturate(foam);
float biasedJacobian = max(0.0f, -(jacobian - pParams.foamBias));
if (biasedJacobian > pParams.foamThreshold)
foam += pParams.foamAdd * biasedJacobian;
pParams.displacementTextures[uint3(id.xy, i)] = float4(displacement, foam);
pParams.slopeTexture[uint3(id.xy, i)] = float2(slopes);
if (i == 0) {
pParams.boyancyData[id.xy] = half(displacement.y);
}
}
}
+69
View File
@@ -0,0 +1,69 @@
import Common;
import Scene;
import WaterCommon;
import MaterialParameter;
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 uint 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];
float2 worldTransformed = params.offset + (params.extent * base);
float3 worldPos = float3(worldTransformed.x, params.height, worldTransformed.y);
float3 displacement1 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz, 0)).xyz;
float3 displacement2 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz, 1)).xyz;
float3 displacement3 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz, 2)).xyz;
float3 displacement4 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(worldPos.xz, 3)).xyz;
float3 displacement = displacement1 + displacement2 + displacement3 + displacement4;
float4 clipPos = mul(vp, float4(worldPos, 1));
float depth = 1 - clamp(1 / (clipPos.z / clipPos.w), 0, 1);
displacement = lerp(0, displacement, pow(saturate(depth), pWaterMaterial.displacementDepthAttenuation));
worldPos += displacement;
WaterVertex v;
v.position_WS = worldPos;
v.position_CS = clipPos;// mul(vp, float4(worldPos, 1));
v.texCoord = worldPos.xz;
v.depth = clipPos.z / clipPos.w;
vertices[i] = v;
}
}
+103
View File
@@ -0,0 +1,103 @@
import Common;
import WaterCommon;
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
return 0.02f + (1 - 0.02f) * (pow(1 - clamp(dot(normal, viewDir), 0, 1), 5.0f));
}
float SmithMaskingBeckmann(float3 H, float3 S, float roughness) {
float hdots = max(0.001f, clamp(dot(H, S), 0, 1));
float a = hdots / (roughness * sqrt(1 - hdots * hdots));
float a2 = a * a;
return a < 1.6f ? (1.0f - 1.259f * a + 0.396f * a2) / (3.535f * a + 2.181 * a2) : 0.0f;
}
float Beckmann(float ndoth, float roughness) {
float exp_arg = (ndoth * ndoth - 1) / (roughness * roughness * ndoth * ndoth);
return exp(exp_arg) / (PI * roughness * roughness * ndoth * ndoth * ndoth * ndoth);
}
[shader("pixel")]
float4 main(WaterVertex vert) : SV_TARGET {
float3 lightDir = -normalize(pWaterMaterial.sunDirection);
float3 viewDir = normalize(pViewParams.cameraPos_WS.xyz - vert.position_WS);
float3 halfwayDir = normalize(lightDir + viewDir);
float depth = vert.depth;
float LdotH = clamp(dot(lightDir, halfwayDir), 0, 1);
float VdotH = clamp(dot(viewDir, halfwayDir), 0, 1);
float4 displacementFoam1 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 0));
displacementFoam1.a += pWaterMaterial.foamSubtract0;
float4 displacementFoam2 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 0));
displacementFoam2.a += pWaterMaterial.foamSubtract1;
float4 displacementFoam3 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 0));
displacementFoam3.a += pWaterMaterial.foamSubtract2;
float4 displacementFoam4 = pWaterMaterial.displacementTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 0));
displacementFoam4.a += pWaterMaterial.foamSubtract3;
float4 displacementFoam = displacementFoam1 + displacementFoam2 + displacementFoam3 + displacementFoam4;
float2 slopes1 = pWaterMaterial.slopeTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 0));
float2 slopes2 = pWaterMaterial.slopeTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 1));
float2 slopes3 = pWaterMaterial.slopeTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 2));
float2 slopes4 = pWaterMaterial.slopeTextures.Sample(pWaterMaterial.sampler, float3(vert.texCoord, 3));
float2 slopes = slopes1 + slopes2 + slopes3 + slopes4;
slopes *= pWaterMaterial.normalStrength;
float foam = lerp(0.0f, saturate(displacementFoam.a), pow(depth, pWaterMaterial.foamDepthAttenuation));
float3 macroNormal = float3(0, 1, 0);
float3 mesoNormal = normalize(float3(-slopes.x, 1.0f, -slopes.y));
mesoNormal = normalize(lerp(float3(0, 1, 0), mesoNormal, pow(saturate(depth), pWaterMaterial.normalDepthAttenuation)));
mesoNormal = normalize(mesoNormal);
float NdotL = clamp(dot(mesoNormal, lightDir), 0, 1);
float a = pWaterMaterial.roughness + foam * pWaterMaterial.foamRoughnessModifier;
float ndoth = max(0.0001f, dot(mesoNormal, halfwayDir));
float viewMask = SmithMaskingBeckmann(halfwayDir, viewDir, a);
float lightMask = SmithMaskingBeckmann(halfwayDir, lightDir, a);
float G = rcp(1 + viewMask + lightMask);
float eta = 1.33f;
float R = ((eta - 1) * (eta - 1)) / ((eta + 1) * (eta + 1));
float thetaV = acos(viewDir.y);
float numerator = pow(1 - dot(mesoNormal, viewDir), 5 * exp(-2.69 * a));
float F = R + (1 - R) * numerator / (1.0f + 22.7f * pow(a, 1.5f));
F = saturate(F);
float3 specular = pWaterMaterial.sunIrradiance * F * G * Beckmann(ndoth, a);
specular /= 4.0f * max(0.001f, clamp(dot(macroNormal, lightDir), 0, 1));
specular *= clamp(dot(mesoNormal, lightDir), 0, 1);
float3 envReflection = pWaterMaterial.environmentMap.Sample(pWaterMaterial.sampler, reflect(-viewDir, mesoNormal)).rgb;
envReflection *= pWaterMaterial.environmentLightStrength;
float H = max(0.0f, displacementFoam.y) * pWaterMaterial.heightModifier;
float3 scatterColor = pWaterMaterial.scatterColor;
float3 bubbleColor = pWaterMaterial.bubbleColor;
float bubbleDensity = pWaterMaterial.bubbleDensity;
float k1 = pWaterMaterial.wavePeakScatterStrength * H * pow(clamp(dot(lightDir, -viewDir), 0, 1), 4.0f) * pow(0.5f - 0.5f * dot(lightDir, mesoNormal), 3.0f);
float k2 = pWaterMaterial.scatterStrength * pow(clamp(dot(viewDir, mesoNormal), 0, 1), 2.0f);
float k3 = pWaterMaterial.scatterShadowStrength * NdotL;
float k4 = bubbleDensity;
float3 scatter = (k1 + k2) * scatterColor * pWaterMaterial.sunIrradiance * rcp(1 + lightMask);
scatter += k3 * scatterColor * pWaterMaterial.sunIrradiance + k4 * bubbleColor * pWaterMaterial.sunIrradiance;
float3 output = (1 - F) * scatter + specular + F * envReflection;
output = max(0.0f, output);
output = lerp(output, pWaterMaterial.foamColor, saturate(foam));
return float4(output, 1.0f);
}
+15
View File
@@ -0,0 +1,15 @@
import Common;
import Scene;
import WaterCommon;
groupshared WaterPayload p;
[numthreads(1, 1, 1)]
[shader("amplification")]
void taskMain(
uint threadID: SV_GroupThreadID,
uint groupID: SV_GroupID, )
{
p = pWaterMaterial.tiles[groupID];
DispatchMesh(1, 1, 1, p);
}
+2
View File
@@ -1,6 +1,8 @@
const static float PI = 3.1415926535897932f;
const static uint BLOCK_SIZE = 32;
static const uint64_t MAX_TEXCOORDS = 8;
static const uint32_t TASK_GROUP_SIZE = 32;
static const uint32_t MESH_GROUP_SIZE = 32;
struct ViewParameter
{
-2
View File
@@ -22,8 +22,6 @@ struct MeshData
static const uint32_t MAX_VERTICES = 256;
static const uint32_t MAX_PRIMITIVES = 256;
static const uint32_t TASK_GROUP_SIZE = 32;
static const uint32_t MESH_GROUP_SIZE = 32;
static const uint32_t MAX_MESHLETS_PER_INSTANCE = 2048;
struct InstanceData