载入中...
搜索中...
未找到
OceanSprayRenderer.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <array>
4#include <cmath>
5#include <exception>
7#include "fluids/shaders/ocean_spray_frag_spv.inc"
8#include "fluids/shaders/ocean_spray_vert_spv.inc"
9#include "graphics/Graphics.h"
10#include "graphics/Mesh.h"
11#include "graphics/Shader.h"
12
13namespace eve::fluids {
16 std::weak_ptr<const void> lifetime;
17 std::array<graphics::Mesh*, 2> mesh{};
19 unsigned front = 0;
20 bool published = false, empty = true;
22 if (!graphics || lifetime.expired()) return;
23 for (auto* m : mesh)
24 if (m && graphics->releaseMesh(m)) delete m;
25 if (shader && graphics->releaseShader(shader)) delete shader;
26 }
27};
28namespace {
29Diagnostic failure(DiagnosticCode code, const char* text) {
30 return Diagnostic::error(code, text, "fluids.ocean.spray.render");
31}
32} // namespace
33OceanSprayRenderer::OceanSprayRenderer() : impl_(std::make_unique<Impl>()) {}
36 auto lifetime = graphics_.resourceLifetime();
37 if (lifetime.expired())
39 failure(DiagnosticCode::StaleHandle, "Graphics retired"));
40 const auto backend = graphics_.getBackendName();
41 if (backend != "vulkan" && backend != "webgpu")
43 failure(DiagnosticCode::Unsupported, "Spray requires Vulkan or WebGPU"));
44 std::unique_ptr<OceanSprayRenderer> result(new OceanSprayRenderer);
45 auto& state = *result->impl_;
46 state.graphics = &graphics_;
47 state.lifetime = lifetime;
48 try {
49 if (backend == "webgpu")
50 state.shader = graphics_.newMeshShaderFromWgsl(shaders::sprayVertex, shaders::sprayFragment);
51 else
52 state.shader = graphics_.newMeshShaderFromSpv(
55 if (!state.shader)
57 failure(DiagnosticCode::Unsupported, "Spray shader unavailable"));
58 auto configured = graphics_.configureMeshShaderSurface(*state.shader, graphics::BlendMode::Alpha, false, true);
59 if (!configured) return Result<std::unique_ptr<OceanSprayRenderer>>::failure(configured.status());
60 const float positions[9] = {0, 0, 0, 1, 0, 0, 0, 1, 0}, attributes[9] = {}, uv[6] = {};
61 const std::uint32_t indices[3] = {0, 1, 2};
62 for (auto& mesh : state.mesh) {
63 mesh = graphics_.newMeshFromArrays(positions, attributes, uv, 3, indices, 3);
64 if (!mesh)
66 failure(DiagnosticCode::Unsupported, "Spray mesh unavailable"));
67 }
68 } catch (const std::exception& error) {
70 }
71 return Result<std::unique_ptr<OceanSprayRenderer>>::success(std::move(result));
72}
74 auto& s = *impl_;
75 if (s.lifetime.expired()) return Result<void>::failure(failure(DiagnosticCode::StaleHandle, "Graphics retired"));
76 const auto vertices = frame.positions.size() / 3;
77 auto finite = [](float v) { return std::isfinite(v); };
78 if (frame.positions.size() % 12 || vertices > 16384 * 4 || frame.attributes.size() != frame.positions.size() ||
79 frame.uv.size() != vertices * 2 || frame.indices.size() != vertices / 4 * 6 || !std::isfinite(frame.time) ||
80 !std::all_of(frame.positions.begin(), frame.positions.end(), finite) ||
81 !std::all_of(frame.attributes.begin(), frame.attributes.end(), finite) ||
82 !std::all_of(frame.uv.begin(), frame.uv.end(), finite))
83 return Result<void>::failure(failure(DiagnosticCode::InvalidArgument, "Invalid spray batch"));
84 for (std::size_t i = 0; i < vertices; ++i)
85 if (frame.attributes[i * 3] < 0 || frame.attributes[i * 3] > 1 || frame.attributes[i * 3 + 1] != 0 ||
86 frame.attributes[i * 3 + 2] != 0)
87 return Result<void>::failure(failure(DiagnosticCode::InvalidArgument, "Invalid spray coverage"));
88 for (auto index : frame.indices)
89 if (index >= vertices)
90 return Result<void>::failure(failure(DiagnosticCode::InvalidArgument, "Invalid spray index"));
91 if (vertices == 0) {
92 s.empty = true;
93 s.published = true;
94 return Result<void>::success();
95 }
96 const auto next = 1 - s.front;
97 if (!s.graphics->updateMeshVertices(s.mesh[next], frame.positions.data(), frame.attributes.data(), frame.uv.data(),
98 static_cast<int>(vertices), frame.indices.data(),
99 static_cast<int>(frame.indices.size())))
100 return Result<void>::failure(failure(DiagnosticCode::Unsupported, "Spray upload rejected"));
101 s.front = next;
102 s.empty = false;
103 s.published = true;
104 return Result<void>::success();
105}
107 auto& s = *impl_;
108 if (s.lifetime.expired()) return Result<void>::failure(failure(DiagnosticCode::StaleHandle, "Graphics retired"));
109 if (!s.published) return Result<void>::failure(failure(DiagnosticCode::Conflict, "Spray batch unpublished"));
110 if (!s.empty)
111 s.graphics->drawMeshShader(s.mesh[s.front], glm::mat4(1.0F), nullptr, graphics::Color(1.0F), s.shader);
112 return Result<void>::success();
113}
114} // namespace eve::fluids
double m[16]
const std::string & s
float uv
DiagnosticCode code
std::vector< std::uint32_t > indices
std::vector< float > positions
float v
std::weak_ptr< const void > lifetime
Poco::JSON::Object::Ptr result
OceanMeshSettings mesh
OceanRenderBackend backend
ssq::Object frame
bool finite
std::string error
Definition Package.cpp:174
std::map< std::string, double > attributes
std::string text
std::array< double, 2 > vertices[3]
uint32_t index
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
EVENGINE_API_DOMAINS Result< std::unique_ptr< OceanSprayRenderer > > build() const
Create shaders and double-buffered meshes; retain no builder reference. @cost Pipeline creation and s...
Render-thread-only spray projection; never advances or owns simulation authority. Graphics resources ...
EVENGINE_API_DOMAINS ~OceanSprayRenderer()
EVENGINE_API_DOMAINS Result< void > draw()
Draw one transparent, depth-tested batch after opaque water and hull geometry.
EVENGINE_API_DOMAINS Result< void > publish(const OceanSprayMeshFrame &frame)
Upload validated geometry to the inactive mesh, then publish it atomically.
virtual bool releaseMesh(Mesh *mesh)
Eagerly releases a mesh created by this Graphics.
Definition Graphics.h:935
virtual bool releaseShader(Shader *shader)
Eagerly releases a shader created by this Graphics.
Definition Graphics.h:1976
Custom GPU program.
Definition Shader.h:41
constexpr char sprayFragment[]
constexpr char sprayVertex[]
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
eve::Color Color
RGBA color used by every graphics draw call. Lives inside eve::graphics so including a graphics heade...
Definition Color.h:13
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Owning batch; attributes store (coverage,0,0) in the mesh normal attribute.
std::array< graphics::Mesh *, 2 > mesh