Water rendering doesnt work yet

This commit is contained in:
Dynamitos
2024-08-17 15:11:57 +02:00
parent 252a241208
commit 81090d78f0
12 changed files with 189 additions and 50 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ struct WaterTile
{
IVector2 location;
float height;
constexpr static float DIMENSIONS = 100;
constexpr static float DIMENSIONS = 10;
};
}
}
@@ -47,6 +47,7 @@ DepthCullingPass::DepthCullingPass(Gfx::PGraphics graphics, PScene scene) : Rend
.hasTaskShader = true,
.useMaterial = false,
.useVisibility = true,
.dumpIntermediates = true,
});
} else {
graphics->getShaderCompiler()->registerRenderPass("DepthPass", Gfx::PassConfig{
@@ -258,13 +258,92 @@ WaterRenderer::WaterRenderer(Gfx::PGraphics graphics, PScene scene, Gfx::PDescri
waterFragment = graphics->createFragmentShader({2});
waterLayout->create();
displaySpectrums[0] = {
.scale = 0.01f,
.windSpeed = 2,
.windDirection = 22,
.fetch = 100000,
.spreadBlend = 0.642,
.swell = 1,
.peakEnhancement = 1,
.shortWavesFade = 0.25f,
};
displaySpectrums[1] = {
.scale = 0.07f,
.windSpeed = 2,
.windDirection = 59,
.fetch = 1000,
.spreadBlend = 0,
.swell = 1,
.peakEnhancement = 1,
.shortWavesFade = 0.01f,
};
displaySpectrums[2] = {
.scale = 0.25f,
.windSpeed = 20,
.windDirection = 97,
.fetch = 100000000,
.spreadBlend = 0.14f,
.swell = 1,
.peakEnhancement = 1,
.shortWavesFade = 0.5f,
};
displaySpectrums[3] = {
.scale = 0.25f,
.windSpeed = 20,
.windDirection = 67,
.fetch = 1000000,
.spreadBlend = 0.47f,
.swell = 1,
.peakEnhancement = 1,
.shortWavesFade = 0.5f,
};
displaySpectrums[4] = {
.scale = 0.15f,
.windSpeed = 5,
.windDirection = 105,
.fetch = 1000000,
.spreadBlend = 0.2f,
.swell = 1,
.peakEnhancement = 1,
.shortWavesFade = 0.5f,
};
displaySpectrums[5] = {
.scale = 0.1f,
.windSpeed = 1,
.windDirection = 19,
.fetch = 10000,
.spreadBlend = 0.298f,
.swell = 0.695f,
.peakEnhancement = 1,
.shortWavesFade = 0.5f,
};
displaySpectrums[6] = {
.scale = 1.0f,
.windSpeed = 1,
.windDirection = 209,
.fetch = 200000,
.spreadBlend = 0.56f,
.swell = 1,
.peakEnhancement = 1,
.shortWavesFade = 0.0001f,
};
displaySpectrums[7] = {
.scale = 0.25f,
.windSpeed = 1,
.windDirection = 0,
.fetch = 1000,
.spreadBlend = 0,
.swell = 0,
.peakEnhancement = 1,
.shortWavesFade = 0.0001f,
};
}
WaterRenderer::~WaterRenderer() {}
void WaterRenderer::beginFrame() {
std::cout << "Test" << std::endl;
updateFFTDescriptor();
struct WaterPayload {
Vector2 offset;
float extent;
@@ -273,8 +352,8 @@ void WaterRenderer::beginFrame() {
Array<WaterPayload> payloads;
scene->view<Component::WaterTile>([&](Component::WaterTile& tile) {
payloads.add(WaterPayload{
.offset = Vector2(tile.location) * Component::WaterTile::DIMENSIONS,
.extent = Component::WaterTile::DIMENSIONS,
.offset = Vector2(tile.location),
.extent = 1,
.height = tile.height,
});
});
@@ -298,7 +377,7 @@ void WaterRenderer::beginFrame() {
boyancyData->changeLayout(Gfx::SE_IMAGE_LAYOUT_GENERAL, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_SHADER_STAGE_MESH_BIT_EXT,
Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
updateFFTDescriptor();
Gfx::OComputeCommand updateCommand = graphics->createComputeCommand("WaterUpdate");
updateCommand->bindPipeline(updateSpectrumForFFT);
updateCommand->bindDescriptor(computeSet);
@@ -334,6 +413,8 @@ void WaterRenderer::beginFrame() {
assembleCommand->dispatch(threadGroupsX, threadGroupsY, 1);
graphics->executeCommands(std::move(assembleCommand));
graphics->waitDeviceIdle();
// transition for mipmap gen
displacementTextures->changeLayout(
Gfx::SE_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, Gfx::SE_ACCESS_SHADER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
@@ -357,7 +438,6 @@ void WaterRenderer::beginFrame() {
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, Gfx::SE_SHADER_STAGE_MESH_BIT_EXT);
updateMaterialDescriptor();
graphics->waitDeviceIdle();
}
Gfx::ORenderCommand WaterRenderer::render(Gfx::PDescriptorSet viewParamsSet) {
@@ -443,12 +523,13 @@ void WaterRenderer::updateFFTDescriptor() {
spectrumBuffer->updateContents(0, sizeof(SpectrumParameters) * spectrums.size(), spectrums.data());
spectrumBuffer->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);
params.deltaTime = Gfx::getCurrentFrameDelta();
params.frameTime = Gfx::getCurrentFrameTime();
paramsBuffer->updateContents(0, sizeof(ComputeParams), &params);
paramsBuffer->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, Gfx::SE_ACCESS_UNIFORM_READ_BIT,
Gfx::SE_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
computeLayout->reset();
computeSet = computeLayout->allocateDescriptorSet();
computeSet->updateBuffer(0, paramsBuffer);
computeSet->updateTexture(1, 0, Gfx::PTexture2D(spectrumTextures));
@@ -462,7 +543,10 @@ void WaterRenderer::updateFFTDescriptor() {
}
void WaterRenderer::updateMaterialDescriptor() {
materialLayout->reset();
materialUniforms->updateContents(0, sizeof(MaterialParams), &materialParams);
materialUniforms->pipelineBarrier(Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT,
Gfx::SE_ACCESS_UNIFORM_READ_BIT,
Gfx::SE_PIPELINE_STAGE_MESH_SHADER_BIT_EXT | Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT);
materialSet = materialLayout->allocateDescriptorSet();
materialSet->updateBuffer(0, materialUniforms);
materialSet->updateTexture(1, Gfx::PTexture2D(displacementTextures));
+40 -21
View File
@@ -64,16 +64,25 @@ class WaterRenderer {
float depth = 20.0f;
float lowCutoff = 0.0001f;
float highCutoff = 9000.0f;
int seed = 0;
int seed = 1;
Vector2 lambda = Vector2(1, 1);
uint32 N = 1024;
uint32 lengthScale0 = 256;
uint32 lengthScale1 = 256;
uint32 lengthScale2 = 256;
uint32 lengthScale3 = 256;
float foamBias = -0.5f;
float foamDecayRate = 0.05f;
float foamAdd = 0.5f;
// Layer 1
uint32 lengthScale1 = 94;
// Layer 2
uint32 lengthScale2 = 128;
// Layer 3
uint32 lengthScale3 = 64;
// Layer 4
uint32 lengthScale4 = 32;
float foamBias = 0.85f;
float foamDecayRate = 0.0175f;
float foamAdd = 0.1f;
float foamThreshold = 0.0f;
} params;
float speed = 1.0f;
@@ -89,34 +98,44 @@ class WaterRenderer {
Gfx::OSampler linearRepeatSampler;
struct MaterialParams {
Vector sunDirection = Vector(0, -1, 0);
Vector sunDirection = Vector(-1.29f, -1.0f, 4.86f);
float displacementDepthAttenuation = 1;
float foamSubtract0 = 0;
float foamSubtract1 = 0;
float foamSubtract2 = 0;
float foamSubtract3 = 0;
float foamSubtract0 = 0.04f;
float foamSubtract1 = -0.04f;
float foamSubtract2 = -0.46f;
float foamSubtract3 = -0.38f;
float tile1 = 0.01f;
float tile2 = 3.0f;
float tile3 = 3.0f;
float tile4 = 0.13f;
float normalStrength = 1;
float foamDepthAttenuation = 1;
float normalDepthAttenuation = 1;
float roughness = 0.1f;
float roughness = 0.075f;
Vector sunIrradiance = Vector(1, 1, 1);
float foamRoughnessModifier = 1;
Vector sunIrradiance = Vector(1.0f, 0.694f, 0.32f);
float foamRoughnessModifier = 0;
Vector scatterColor = Vector(1, 1, 1);
float environmentLightStrength = 1;
Vector scatterColor = Vector(0.016f, 0.0735f, 0.16f);
float environmentLightStrength = 0.5f;
Vector bubbleColor = Vector(1, 1, 1);
Vector bubbleColor = Vector(0, 0.02f, 0.016f);
float heightModifier = 1;
float bubbleDensity = 1;
float wavePeakScatterStrength = 1;
float scatterStrength = 1;
float scatterShadowStrength = 1;
float scatterShadowStrength = 0.5f;
Vector foamColor = Vector(1, 1, 1);
uint32 contributeDisplacement1 = true;
uint32 contributeDisplacement2 = false;
uint32 contributeDisplacement3 = false;
uint32 contributeDisplacement4 = false;
Vector foamColor = Vector(0.6f, 0.5568f, 0.492f);
} materialParams;
// Water
Gfx::OUniformBuffer materialUniforms;
+5
View File
@@ -47,6 +47,7 @@ ShaderPermutation ShaderCompiler::getTemplate(std::string name) {
permutation.setTaskFile(pass.taskFile);
}
permutation.setVisibilityPass(pass.useVisibility);
permutation.setDumpIntermediates(pass.dumpIntermediates);
return permutation;
}
@@ -117,6 +118,10 @@ void ShaderCompiler::createShaders(ShaderPermutation permutation, Gfx::OPipeline
if (permutation.visibilityPass) {
createInfo.defines["VISIBILITY"] = "1";
}
if (permutation.dumpIntermediates)
{
createInfo.dumpIntermediate = true;
}
createInfo.typeParameter.add({Pair<const char*, const char*>("IVertexData", permutation.vertexDataName)});
createInfo.modules.add(permutation.vertexDataName);
+3
View File
@@ -100,6 +100,7 @@ struct ShaderPermutation {
uint8 depthCulling;
uint8 visibilityPass;
uint8 rayTracing;
uint8 dumpIntermediates;
// TODO: lightmapping etc
ShaderPermutation() { std::memset(this, 0, sizeof(ShaderPermutation)); }
void setTaskFile(std::string_view name) {
@@ -139,6 +140,7 @@ struct ShaderPermutation {
void setPositionOnly(bool enable) { positionOnly = enable; }
void setDepthCulling(bool enable) { depthCulling = enable; }
void setVisibilityPass(bool enable) { visibilityPass = enable; }
void setDumpIntermediates(bool enable) { dumpIntermediates = enable; }
};
// Hashed ShaderPermutation for fast lookup
struct PermutationId {
@@ -168,6 +170,7 @@ struct PassConfig {
bool useMaterial = false;
bool useVisibility = false;
bool rayTracing = false;
bool dumpIntermediates = false;
};
class ShaderCompiler {
public:
+17
View File
@@ -194,6 +194,14 @@ void RenderCommand::begin(PRenderPass renderPass, PFramebuffer framebuffer, VkQu
beginInfo.flags = VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT;
}
VK_CHECK(vkBeginCommandBuffer(handle, &beginInfo));
VkDebugUtilsObjectNameInfoEXT nameInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
.pNext = nullptr,
.objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
.objectHandle = (uint64)handle,
.pObjectName = name.c_str(),
};
vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo);
}
void RenderCommand::end() { VK_CHECK(vkEndCommandBuffer(handle)); }
@@ -374,6 +382,14 @@ void ComputeCommand::begin(VkQueryPipelineStatisticFlags pipelineFlags) {
.pInheritanceInfo = &inheritanceInfo,
};
VK_CHECK(vkBeginCommandBuffer(handle, &beginInfo));
VkDebugUtilsObjectNameInfoEXT nameInfo = {
.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT,
.pNext = nullptr,
.objectType = VK_OBJECT_TYPE_COMMAND_BUFFER,
.objectHandle = (uint64)handle,
.pObjectName = name.c_str(),
};
vkSetDebugUtilsObjectNameEXT(graphics->getDevice(), &nameInfo);
}
void ComputeCommand::end() {
@@ -399,6 +415,7 @@ void ComputeCommand::bindDescriptor(Gfx::PDescriptorSet descriptorSet, Array<uin
assert(threadId == std::this_thread::get_id());
auto descriptor = descriptorSet.cast<DescriptorSet>();
assert(descriptor->writeDescriptors.size() == 0);
descriptor->bind();
boundResources.add(descriptor.getHandle());
for (const auto& binding : descriptor->boundResources) {