载入中...
搜索中...
未找到
OceanRenderer.cpp
浏览该文件的文档.
2
4#include "fluids/shaders/ocean_frag_spv.inc"
5#include "fluids/shaders/ocean_gpu_vert_spv.inc"
6#include "fluids/shaders/ocean_vert_spv.inc"
7#include "graphics/Graphics.h"
8#include "graphics/Mesh.h"
9#include "graphics/Shader.h"
10#include "graphics/Texture.h"
11
12#include <algorithm>
13#include <cmath>
14#include <exception>
15
16namespace eve::fluids {
18 if (!graphics || lifetime.expired()) return;
19 if (mesh && graphics->releaseMesh(mesh)) delete mesh;
20 if (shader && graphics->releaseShader(shader)) delete shader;
21 for (auto* texture : atlas)
23 for (auto* texture : hullMask)
25 for (auto* texture : wake)
27}
28namespace {
29Diagnostic failure(DiagnosticCode code, const char* message) {
30 return Diagnostic::error(code, message, "fluids.ocean.render");
31}
32} // namespace
33OceanRenderer::OceanRenderer() : impl_(std::make_unique<Impl>()) {}
39 const auto lifetime = graphics_.resourceLifetime();
40 if (lifetime.expired())
42 failure(DiagnosticCode::StaleHandle, "Graphics provider has retired"));
43 if (graphics_.getBackendName() != "vulkan" && graphics_.getBackendName() != "webgpu")
45 failure(DiagnosticCode::Unsupported, "Ocean renderer requires Vulkan or WebGPU"));
48 failure(DiagnosticCode::InvalidArgument, "Invalid ocean render backend"));
49 if (backend == OceanRenderBackend::GpuResident && graphics_.getBackendName() != "vulkan")
51 failure(DiagnosticCode::Unsupported, "Resident ocean rendering requires Vulkan"));
52 OceanMeshBuilder builder;
53 builder.settings = mesh;
54 auto topology = builder.build();
55 if (!topology) return Result<std::unique_ptr<OceanRenderer>>::failure(topology.status());
56 std::unique_ptr<OceanRenderer> renderer(new OceanRenderer);
57 auto& state = *renderer->impl_;
58 state.graphics = &graphics_;
59 state.lifetime = lifetime;
60 state.topology = std::move(topology.value());
61 state.backend = backend;
62 try {
63 if (graphics_.getBackendName() == "webgpu")
64 state.shader = graphics_.newMeshShaderFromWgsl(shaders::oceanVertex, shaders::oceanFragment);
65 else
66 state.shader = graphics_.newMeshShaderFromSpv(
69 : std::vector<uint32_t>(ocean_vert_spv, ocean_vert_spv + ocean_vert_spv_count),
70 std::vector<uint32_t>(ocean_frag_spv, ocean_frag_spv + ocean_frag_spv_count));
71 if (!state.shader)
73 failure(DiagnosticCode::Unsupported, "Ocean mesh shader unavailable"));
74 auto configured = graphics_.configureMeshShaderSurface(*state.shader, graphics::BlendMode::Opaque, true, true);
75 if (!configured) return Result<std::unique_ptr<OceanRenderer>>::failure(configured.status());
77 state.shader->declareMatrix("oceanBands");
78 state.shader->declareMatrix("oceanFrame");
79 }
80 const auto count = state.topology.parameterOffsets.size();
81 std::vector<float> positions(count * 3, 0.0F), normals(count * 3, 0.0F), uv(count * 2, 0.0F);
82 for (std::size_t i = 0; i < count; ++i) {
83 positions[i * 3] = static_cast<float>(state.topology.parameterOffsets[i].x);
84 positions[i * 3 + 2] = static_cast<float>(state.topology.parameterOffsets[i].y);
85 normals[i * 3 + 1] = 1.0F;
86 }
87 state.mesh =
88 graphics_.newMeshFromArrays(positions.data(), normals.data(), uv.data(), static_cast<int>(count),
89 state.topology.indices.data(), static_cast<int>(state.topology.indices.size()));
90 if (!state.mesh)
92 failure(DiagnosticCode::Unsupported, "Ocean mesh allocation unavailable"));
93 } catch (const std::exception& error) {
95 }
96 return Result<std::unique_ptr<OceanRenderer>>::success(std::move(renderer));
97}
99 auto& state = *impl_;
101 return Result<void>::failure(failure(DiagnosticCode::Conflict, "Renderer expects a resident GPU projection"));
102 if (state.lifetime.expired())
103 return Result<void>::failure(failure(DiagnosticCode::StaleHandle, "Graphics retired"));
104 const auto count = state.topology.parameterOffsets.size();
105 auto finite = [](float value) { return std::isfinite(value); };
106 if (frame.positions.size() != count * 3 || frame.normals.size() != count * 3 || frame.surface.size() != count * 2 ||
107 !std::isfinite(frame.time) || !std::isfinite(frame.origin.x) || !std::isfinite(frame.origin.y) ||
108 !std::isfinite(frame.origin.z) || !std::all_of(frame.positions.begin(), frame.positions.end(), finite) ||
109 !std::all_of(frame.normals.begin(), frame.normals.end(), finite) ||
110 !std::all_of(frame.surface.begin(), frame.surface.end(), finite))
111 return Result<void>::failure(failure(DiagnosticCode::InvalidArgument, "Invalid ocean vertex frame"));
112 for (std::size_t i = 0; i < count; ++i) {
113 const glm::vec3 normal(frame.normals[i * 3], frame.normals[i * 3 + 1], frame.normals[i * 3 + 2]);
114 const float length = glm::length(normal);
115 if (length < 0.99F || length > 1.01F || frame.surface[i * 2] < 0.0F || frame.surface[i * 2] > 1.0F)
116 return Result<void>::failure(failure(DiagnosticCode::InvalidArgument, "Ocean normals or foam invalid"));
117 }
118 if (!state.graphics->updateMeshVertices(state.mesh, frame.positions.data(), frame.normals.data(),
119 frame.surface.data(), static_cast<int>(count), nullptr, 0))
120 return Result<void>::failure(failure(DiagnosticCode::Unsupported, "Ocean mesh upload rejected"));
121 state.origin = frame.origin;
122 state.published = true;
123 return Result<void>::success();
124}
126 auto& state = *impl_;
127 if (state.lifetime.expired())
128 return Result<void>::failure(failure(DiagnosticCode::StaleHandle, "Graphics retired"));
129 if (!state.published) return Result<void>::failure(failure(DiagnosticCode::Conflict, "Ocean frame not published"));
130 if (state.hullPublished && state.origin != state.hullOrigin)
131 return Result<void>::failure(failure(DiagnosticCode::Conflict, "Hull mask and ocean render origins differ"));
132 state.graphics->setMesh3DNormalTexture(state.hullPublished ? state.hullMask[state.hullFront] : nullptr);
133 auto* atlas = state.backend == OceanRenderBackend::GpuResident ? state.atlas[state.front] : nullptr;
135 state.graphics->setMesh3DHeightTexture(state.wake[state.front]);
136 state.graphics->drawMeshShader(state.mesh, glm::mat4(1.0F), atlas, glm::vec4(1.0F), state.shader);
137 if (state.backend == OceanRenderBackend::GpuResident) state.graphics->setMesh3DHeightTexture(nullptr);
138 state.graphics->setMesh3DNormalTexture(nullptr);
139 return Result<void>::success();
140}
141} // namespace eve::fluids
float uv
std::string message
DiagnosticCode code
std::vector< float > normals
std::vector< float > positions
float length
std::weak_ptr< const void > lifetime
Texture * normal
OceanMeshSettings mesh
std::shared_ptr< OceanRenderer > renderer
OceanRenderBackend backend
ssq::Object frame
ssq::Object water
bool finite
std::string error
Definition Package.cpp:174
Texture * texture
Topology topology
Object::Ptr value
int count
A structured explanation of a failed, degraded, or noteworthy result.
Definition Diagnostic.h:94
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
Owning topology preparation; settings are copied at build, with no retained references.
Definition OceanMesh.h:36
EVENGINE_API_DOMAINS Result< OceanMeshData > build() const
Build positively oriented triangles on XZ, graded geometrically from near water to horizon.
Definition OceanMesh.cpp:6
OceanMeshSettings settings
Definition OceanMesh.h:38
Prepare CPU reference vertices from the same water view used by rigid bodies. Borrows mesh and water ...
Definition OceanMesh.h:64
EVENGINE_API_DOMAINS Result< std::unique_ptr< OceanRenderer > > build() const
Allocate shader and fixed topology, publishing only a complete renderer.
Graphics projection of an ocean; never owns or advances simulation state. Render-thread only,...
EVENGINE_API_DOMAINS OceanMeshFrameBuilder frameBuilder(const OceanWaterView &water) const
Prepare a cheap borrowed CPU projection builder; set center/origin before building.
EVENGINE_API_DOMAINS Result< void > draw()
Draw the published frame in the current 3D pass; camera/scene must use its render origin.
EVENGINE_API_DOMAINS ~OceanRenderer()
EVENGINE_API_DOMAINS Result< void > publish(const OceanMeshFrame &frame)
Publish a prepared owning vertex frame; input is borrowed only for this call.
Synchronous borrowed composition of the spectral sea and an optional local wake. Both owners must out...
virtual bool releaseMesh(Mesh *mesh)
Eagerly releases a mesh created by this Graphics.
Definition Graphics.h:935
virtual bool releaseTexture(Texture *texture)
Eagerly releases a texture created by this Graphics.
Definition Graphics.h:732
virtual bool releaseShader(Shader *shader)
Eagerly releases a shader created by this Graphics.
Definition Graphics.h:1976
constexpr char oceanVertex[]
Definition OceanWgsl.h:4
constexpr char oceanFragment[]
Definition OceanWgsl.h:22
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owning CPU reference projection, ready for a mesh upload; simulation time and origin identify the fra...
Definition OceanMesh.h:49
std::weak_ptr< const void > lifetime
std::array< graphics::Texture *, 2 > wake
std::array< graphics::Texture *, 2 > hullMask
std::array< graphics::Texture *, 2 > atlas