载入中...
搜索中...
未找到
FluidSurfaceRenderer.cpp
浏览该文件的文档.
113void FluidSurfaceRenderer::setCamera(const glm::vec3& eye, const glm::vec3& target, const glm::vec3& up,
134Result<void> FluidSurfaceRenderer::configureProjection(bool orthographic, float verticalHalfSize) {
137 DiagnosticCode::InvalidArgument, "Invalid orthographic vertical half size", "fluids.surface.projection"));
163void FluidSurfaceRenderer::render(const std::vector<glm::vec3>& positions, float particleRadius) {
176void FluidSurfaceRenderer::renderInternal(const std::vector<glm::vec3>& positions, float particleRadius) {
243 if (!keepColorResident) seq_->recordDownload(bufColor_, stColor_, uint64_t(pixels) * sizeof(uint32_t));
265 depth_[size_t(i)] = key == kEmptyKey ? 1e30f : nearZ + (float(key) / kKeyScale) * (farZ - nearZ);
279Result<void> FluidSurfaceRenderer::copyToTexture(graphics::Graphics* graphics, graphics::Texture* texture) {
281 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "Missing graphics or texture",
285 graphics->updateTextureFromResidentRgba8(texture, bufColor_->residentView(), params_.width, params_.height);
291 Diagnostic::error(DiagnosticCode::Failed, "Fluid texture upload failed", "fluids.surface.copyToTexture"));
295Result<void> FluidSurfaceRenderer::renderVolumeColorToTexture(const VolumeFluid& sim, graphics::Graphics* graphics,
299 return Result<void>::failure(Diagnostic::error(DiagnosticCode::InvalidArgument, "Missing graphics or texture",
360 gpgpu::GpuBuffer* finalDepth = std::max(0, params_.smoothIterations) % 2 == 0 ? bufDepthA_ : bufDepthB_;
383 colorAccum = gpgpu_->newBuffer(params_.width * params_.height * 5 * int(sizeof(uint32_t)), "storage");
411 if (!shAnisotropicSplat_ || !bufAnisotropicParts_) throw std::runtime_error("anisotropy GPU resource");
443 glm::mat3(radii.x * radii.x, 0.f, 0.f, 0.f, radii.y * radii.y, 0.f, 0.f, 0.f, radii.z * radii.z) *
446 const glm::vec3 jx(focal * invZ, 0.f, params_.orthographic ? 0.f : focal * center.x * invZ * invZ);
447 const glm::vec3 jy(0.f, -focal * invZ, params_.orthographic ? 0.f : -focal * center.y * invZ * invZ);
462 const float conditionalVariance = std::max(covariance[2][2] - (slopeX * covZx + slopeY * covZy), 1e-10f);
464 const float eigenMax = .5f * (trace + std::sqrt(std::max(0.f, (s00 - s11) * (s00 - s11) + 4.f * s01 * s01)));
467 const float sx = (.5f + .5f * center.x / (divisor * projectionScale * params_.aspect)) * float(params_.width);
493 seq_->recordUpload(bufParts_, gpuParticles_.data(), uint64_t(gpuParticles_.size()) * sizeof(float));
496Result<void> FluidSurfaceRenderer::occludeWithSceneDepth(std::span<const float> sceneDepth, float depthBias) {
502 if (sceneDepth.size() != pixels || !std::isfinite(depthBias) || depthBias < 0.f || depthBias > 1.f)
520Result<void> FluidSurfaceRenderer::compositeSceneRefraction(std::span<const uint8_t> sceneColor, float distortion,
529 if (sceneColor.size() != pixels * 4u || !std::isfinite(distortion) || distortion < 0.f || distortion > 64.f ||
556 const float composited = float(sceneColor[rgba + channel]) * (1.f - coverage) + absorbed * coverage;
565Result<void> FluidSurfaceRenderer::configureSurface(float thicknessScale, float thicknessCutoff, float depthFalloff,
569 !std::isfinite(depthFalloff) || depthFalloff <= 0.f || depthFalloff > 1.f || smoothIterations < 0 ||
572 DiagnosticCode::InvalidArgument, "Invalid fluid surface controls", "fluids.surface.configureSurface"));
587Result<void> FluidSurfaceRenderer::configureRendererSettings(const FluidRendererSettings& settings) {
592 if (!blend(settings.blendSource) || !blend(settings.blendDestination) || !blend(settings.particleBlendSource) ||
593 !blend(settings.particleBlendDestination) || !finiteRange(settings.thicknessCutoff, .01f, 5.f) ||
597 !finiteRange(settings.metalness, 0.f, 1.f) || !finiteRange(settings.ambientMultiplier, 0.f, 6.f) ||
600 settings.refractionDownsample < 1 || settings.refractionDownsample > 4 || settings.foamDownsample < 1 ||
603 DiagnosticCode::InvalidArgument, "Invalid Fluid3D renderer settings", "fluids.surface.rendererSettings"));
662Result<void> FluidSurfaceRenderer::configureMaterial(bool lighting, float smoothness, float metalness,
664 const auto unit = [](float value) { return std::isfinite(value) && value >= 0.f && value <= 1.f; };
665 if (!unit(smoothness) || !unit(metalness) || !std::isfinite(ambientMultiplier) || ambientMultiplier < 0.f ||
666 ambientMultiplier > 6.f || !unit(reflection) || !std::isfinite(opacity) || opacity < 0.f || opacity > 30.f)
668 DiagnosticCode::InvalidArgument, "Invalid fluid material controls", "fluids.surface.configureMaterial"));
698Result<void> FluidSurfaceRenderer::configureColors(const glm::vec3& baseColor, const glm::vec3& reflectionColor) {
724 DiagnosticCode::InvalidArgument, "Surface downsample must be in [1,4]", "fluids.surface.downsample"));
794 reduced.height = std::max(8, (params_.height + thicknessDownsample_ - 1) / thicknessDownsample_);
813 customShading_ = thicknessCutoff_ != 0.f || !lighting_ || smoothness_ != .8f || metalness_ != 0.f ||
831 lighting_ ? std::clamp(ambientMultiplier_ + (1.f - std::min(ambientMultiplier_, 1.f)) * diff, 0.f, 6.f)
836 const float spec = lighting_ ? std::pow(std::max(glm::dot(n, hv), 0.f), exponent) * smoothness_ * .56f : 0.f;
839 const glm::vec3 shaded = baseColor_ * lit + reflected * fresnel * reflectionStrength + glm::vec3(spec);
840 for (size_t c = 0; c < 3u; ++c) color_[rgba + c] = uint8_t(255.f * std::clamp(shaded[int(c)], 0.f, 1.f));
850 ? glm::orthoRH(-params_.orthographicSize * params_.aspect, params_.orthographicSize * params_.aspect,
852 : glm::perspectiveRH(glm::radians(params_.fovYDeg), params_.aspect, params_.nearZ, params_.farZ);
861 params_.orthographic ? params_.orthographicSize : std::tan(glm::radians(params_.fovYDeg) * .5f));
896 params_.orthographic ? params_.orthographicSize : std::tan(glm::radians(params_.fovYDeg) * .5f);
900 ? glm::orthoRH(-tanHalf * params_.aspect, tanHalf * params_.aspect, -tanHalf, tanHalf, nearZ, farZ)
912 const int x0 = std::max(int(std::floor(sx - bound)), 0), x1 = std::min(int(std::ceil(sx + bound)), W - 1);
913 const int y0 = std::max(int(std::floor(sy - bound)), 0), y1 = std::min(int(std::ceil(sy + bound)), H - 1);
932 if (glm::any(glm::lessThan(ndc, glm::vec3(-1.f))) || glm::any(glm::greaterThan(ndc, glm::vec3(1.f))))
998 const std::vector<float>& smooth = (params_.smoothIterations % 2 == 0) ? depth_ : depthScratch_;
1012 return glm::vec3((u * 2.f - 1.f) * params_.aspect * tanHalf * scale, (1.f - v * 2.f) * tanHalf * scale,
1020 const auto derivative = [&](glm::vec3 forward, glm::vec3 backward, bool forwardValid, bool backwardValid) {
#define EV_PROFILE_MODULE(module, name)
Profile the enclosing scope, tagged with a module for grouping.
Definition Profile.h:158
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Definition Diagnostic.cpp:21
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
GPU-backed surface fluid simulator (falls back to the CPU solver).
Definition Fluids.h:45
Result< void > configureSurfaceEnabled(bool enabled)
Enables or disables Fluid3D's generateSurface policy for renderConfiguredVolume.
Definition FluidSurfaceRenderer.cpp:582
Result< void > configureThicknessDownsample(int factor)
Sets the independently reconstructed liquid-thickness target divisor.
Definition FluidSurfaceRenderer.cpp:731
Result< void > configureSurfaceDownsample(int factor)
Configures Fluid3D-style surface target downsampling.
Definition FluidSurfaceRenderer.cpp:721
Result< void > occludeWithSceneDepth(std::span< const float > sceneDepth, float depthBias=0.001f)
Removes fluid and diffuse color hidden by borrowed linear scene depth.
Definition FluidSurfaceRenderer.cpp:496
void render(const std::vector< glm::vec3 > &positions, float particleRadius)
Reconstruct a frame from particle positions.
Definition FluidSurfaceRenderer.cpp:163
Result< void > configureSurfaceBlurRadius(float radius)
Sets Fluid3D's world-space surface blur radius for subsequent reconstruction.
Definition FluidSurfaceRenderer.cpp:680
Result< void > configureSurface(float thicknessScale, float thicknessCutoff, float depthFalloff, int smoothIterations)
Atomically configures thickness reconstruction and surface smoothing.
Definition FluidSurfaceRenderer.cpp:565
Result< void > configureAnisotropy(bool enabled)
Enables oriented-ellipsoid projection for volume-fluid particles.
Definition FluidSurfaceRenderer.cpp:714
Result< void > copyToTexture(graphics::Graphics *graphics, graphics::Texture *texture)
Present the current RGBA8 result into an existing graphics texture.
Definition FluidSurfaceRenderer.cpp:279
Result< void > configureReflection(bool enabled)
Enables or disables the configured reflection contribution without losing its coefficient.
Definition FluidSurfaceRenderer.cpp:690
Result< void > configureMaterial(bool lighting, float smoothness, float metalness, float ambientMultiplier, float reflection, float opacity)
Atomically configures lighting and material response for subsequent liquid frames.
Definition FluidSurfaceRenderer.cpp:662
void renderFrom(FluidSimulator *sim)
Script-friendly wrapper: render(*sim) with a null check.
Definition FluidSurfaceRenderer.cpp:129
Result< void > configureColors(const glm::vec3 &baseColor, const glm::vec3 &reflectionColor)
Sets the linear RGB base and reflection colors atomically.
Definition FluidSurfaceRenderer.cpp:698
Result< void > configureProjection(bool orthographic, float verticalHalfSize=1.f)
Selects perspective or orthographic reconstruction for subsequent frames.
Definition FluidSurfaceRenderer.cpp:134
Result< void > renderVolumeColorToTexture(const VolumeFluid &sim, graphics::Graphics *graphics, graphics::Texture *texture)
Reconstruct and present a uniform-color volume without host color readback when supported.
Definition FluidSurfaceRenderer.cpp:295
void setCamera(const glm::vec3 &eye, const glm::vec3 &target, const glm::vec3 &up, float fovYDeg)
Update the camera.
Definition FluidSurfaceRenderer.cpp:113
Result< void > configureRendererSettings(const FluidRendererSettings &settings)
Validates and atomically applies every Fluid3DRendererSettings field.
Definition FluidSurfaceRenderer.cpp:587
FluidSurfaceRenderer(const FluidSurfaceParams ¶ms, bool preferGpu)
Fluid surface renderer.
Definition FluidSurfaceRenderer.cpp:48
void writePpm(const std::string &path) const
Write the color buffer as a PPM image (debug artifact).
Definition FluidSurfaceRenderer.cpp:1093
FluidRendererSettings rendererSettings() const noexcept
Returns an owning snapshot of the current Fluid3D renderer settings without allocation.
Definition FluidSurfaceRenderer.cpp:634
Result< void > prepare()
Prepares the preferred reconstruction backend before frame timing begins.
Definition FluidSurfaceRenderer.cpp:144
void renderVolumeColorOnly(const VolumeFluid &sim)
Reconstructs only the RGBA output for display, skipping auxiliary GPU readback when color is uniform.
Definition VolumeFluidSurface.cpp:16
Result< void > compositeSceneRefraction(std::span< const uint8_t > sceneColor, float distortion=8.f, float absorption=3.f)
Refracts a matching opaque RGBA8 scene through the current liquid surface.
Definition FluidSurfaceRenderer.cpp:520
CPU position-based free-volume fluid with a bounded spatial grid. @ownership Owns all particle state;...
Definition VolumeFluid.h:517
virtual void bindBuffer(int binding, GpuBuffer *buffer)=0
Bind a storage buffer to set=0 binding. binding in [0, kMaxBindings).
ComputeShader * newShader(const std::string &source)
Compatibility-only raw-owning shader factory (Vulkan: GLSL; WebGPU: WGSL). Vulkan delegates to the ch...
Definition Gpgpu.cpp:324
bool isAvailable() const
True when the active Graphics backend can run compute (device initialized).
Definition Gpgpu.cpp:284
GpuBuffer * newBuffer(int byteSize, const std::string &usage="storage")
Allocate a GPU buffer. usage: "storage" (SSBO) | "staging" (host transfer) | "storage_vertex" (Vulkan...
Definition Gpgpu.cpp:372
Sequence * newSequence()
Create a Kompute-style command Sequence: record buffer transfers and compute dispatches into one comm...
Definition Gpgpu.cpp:384
virtual void downloadBytes(void *dst, uint64_t nbytes, uint64_t srcOffset=0) const =0
Downloads bytes.
virtual GpuResidentBufferView residentView() const
Return a transient non-owning native view for same-device rendering. Unsupported/test buffers return ...
Definition GpuBuffer.h:85
void recordUpload(GpuBuffer *dst, const void *src, uint64_t nbytes, uint64_t dstOffset=0)
Record upload.
Definition Sequence.cpp:77
void recordDownload(GpuBuffer *src, GpuBuffer *staging, uint64_t nbytes, uint64_t srcOffset=0)
Record download.
Definition Sequence.cpp:82
void recordDispatch(ComputeShader *shader, int groupsX, int groupsY=1, int groupsZ=1)
Record dispatch.
Definition Sequence.cpp:87
Definition Graphics.h:134
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
const char * kSsfColorSplat
Accumulate isotropic particle colors using the CPU tint pass' disc weights. @lifetime The returned po...
Definition FluidSsfKernels.h:164
const char * kSsfSmooth
One bilateral smoothing pass (read depthA, write depthB).
Definition FluidSsfKernels.h:271
const char * kSsfShade
Water/mud shading from depth + normal + thickness.
Definition FluidSsfKernels.h:421
const char * kSsfClear
Reset depth (0xFFFFFFFF), thickness, normal and color buffers.
Definition FluidSsfKernels.h:37
const char * kSsfSplat
Splat particles into depth + thickness (atomic min / add).
Definition FluidSsfKernels.h:67
FluidBlendFactor
Unity/Fluid3D blend factors used by fluid render settings.
Definition FluidSurfaceRenderer.h:46
const char * kSsfNormal
Reconstruct view-space normals from the smoothed depth.
Definition FluidSsfKernels.h:336
const char * kSsfColorClear
Clear lazily-created fixed-point multicolor accumulation buffers. @lifetime The returned pointer rema...
Definition FluidSsfKernels.h:142
constexpr auto kSsfAnisotropicSplat
Definition FluidSsfKernels.h:223
@ InvalidArgument
@ PreconditionViolation
@ Unsupported
@ Failed
@ Vulkan
Definition Container.h:602
Complete owning mirror of Fluid3DRendererSettings with package defaults.
Definition FluidSurfaceRenderer.h:82
Camera + reconstruction tuning for the SSF pipeline.
Definition FluidSurfaceRenderer.h:61
float particleRadius
Definition FluidSurfaceRenderer.h:73
int smoothIterations
Definition FluidSurfaceRenderer.h:74
float thicknessScale
Definition FluidSurfaceRenderer.h:76
float orthographicSize
Definition FluidSurfaceRenderer.h:67