载入中...
搜索中...
未找到
OceanGpuRenderFrame.cpp
浏览该文件的文档.
3#include "fluids/OceanWake.h"
4#include "graphics/Graphics.h"
5#include "graphics/Shader.h"
6#include "graphics/Texture.h"
7
8#include <bit>
9#include <cmath>
10#include <exception>
11
12namespace eve::fluids {
18 auto& state = *renderer_.impl_;
19 auto fail = [](DiagnosticCode code, const char* message) {
20 return Result<void>::failure(Diagnostic::error(code, message, "fluids.ocean.render"));
21 };
22 if (state.lifetime.expired()) return fail(DiagnosticCode::StaleHandle, "Graphics retired");
24 return fail(DiagnosticCode::Conflict, "Renderer expects a CPU reference projection");
25 auto finite = [](double value) { return std::isfinite(value) && std::abs(value) <= 1e9; };
26 if (!finite(center.x) || !finite(center.y) || !finite(origin.x) || !finite(origin.y) || !finite(origin.z))
27 return fail(DiagnosticCode::InvalidArgument, "Invalid ocean render coordinates");
28 auto atlas = spectrum_.atlas(token_);
29 if (!atlas) return Result<void>::failure(atlas.status());
30 const auto& view = atlas.value();
31 std::array<float, 32> parameters{};
32 for (unsigned i = 0; i < view.bandCount; ++i) {
33 const auto& band = view.bands[i];
34 parameters[i * 5] = static_cast<float>(band.resolution);
35 parameters[i * 5 + 1] = static_cast<float>(band.length);
36 parameters[i * 5 + 2] = static_cast<float>(band.firstRow);
37 parameters[i * 5 + 3] = static_cast<float>(std::remainder(center.x, band.length));
38 parameters[i * 5 + 4] = static_cast<float>(std::remainder(center.y, band.length));
39 }
40 parameters[20] = static_cast<float>(view.bandCount);
41 parameters[21] = static_cast<float>(center.x - origin.x);
42 parameters[22] = static_cast<float>(view.seaLevel - origin.y);
43 parameters[23] = static_cast<float>(center.y - origin.z);
44 unsigned wakeSize = 1;
45 std::vector<std::uint8_t> wakePixels(8, 0);
46 if (wake) {
47 const auto grid = wake->gridView();
48 if (std::abs(grid.time - token_.time()) > 1e-8)
49 return fail(DiagnosticCode::InvalidArgument, "Ocean GPU and wake timestamps differ");
50 wakeSize = grid.settings.resolution;
51 const auto count = wakeSize * wakeSize;
52 wakePixels.resize(count * 8);
53 for (unsigned i = 0; i < count * 2; ++i) {
54 const auto value = i < count ? grid.height[i] : grid.foam[i - count];
55 const auto bits = std::bit_cast<std::uint32_t>(static_cast<float>(value));
56 for (unsigned channel = 0; channel < 4; ++channel)
57 wakePixels[i * 4 + channel] = static_cast<std::uint8_t>((bits >> (8 * channel)) & 255U);
58 }
59 parameters[24] = static_cast<float>(wakeSize);
60 parameters[25] = static_cast<float>(grid.settings.length);
61 parameters[26] = static_cast<float>(grid.settings.absorptionWidth);
62 parameters[27] = static_cast<float>(center.x - grid.settings.center.x);
63 parameters[28] = static_cast<float>(center.y - grid.settings.center.y);
64 }
65 // Write inactive textures only. No partial upload can become the visible ocean frame.
66 const auto back = 1U - state.front;
67 try {
68 auto resize = [&](graphics::Texture*& texture, unsigned width, unsigned height) -> Result<void> {
69 if (texture && texture->getWidth() == static_cast<int>(width) &&
70 texture->getHeight() == static_cast<int>(height))
71 return Result<void>::success();
74 std::vector<std::uint8_t> zero(width * height * 4, 0);
75 auto* replacement =
76 state.graphics->newTexture(static_cast<int>(width), static_cast<int>(height), zero.data(), info);
77 if (!replacement) return fail(DiagnosticCode::Unsupported, "Ocean data texture unavailable");
78 if (texture && state.graphics->releaseTexture(texture)) delete texture;
79 texture = replacement;
80 return Result<void>::success();
81 };
82 auto resized = resize(state.atlas[back], view.width, view.height);
83 if (!resized) return resized;
84 resized = resize(state.wake[back], wakeSize, wakeSize * 2);
85 if (!resized) return resized;
86 auto copied = state.graphics->updateTextureFromResidentRgba8(
87 state.atlas[back], view.buffer, static_cast<int>(view.width), static_cast<int>(view.height));
88 if (!copied) return copied;
89 auto uploaded = state.graphics->updateTextureRegion(state.wake[back], 0, 0, static_cast<int>(wakeSize),
90 static_cast<int>(wakeSize * 2), wakePixels);
91 if (!uploaded) return uploaded;
92 auto sent = state.shader->sendFloatBlock(parameters);
93 if (!sent) return sent;
94 } catch (const std::exception& error) {
95 return fail(DiagnosticCode::Failed, error.what());
96 }
97 state.origin = origin;
98 state.front = back;
99 state.published = true;
100 return Result<void>::success();
101}
102} // namespace eve::fluids
std::string message
DiagnosticCode code
std::uint32_t height
std::uint32_t width
OceanGpuFrameToken token
OceanSprayView view
bool finite
std::string error
Definition Package.cpp:174
std::array< uint32_t, 9 > parameters
Texture * texture
Object::Ptr value
int count
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
Opaque revision of a completed GPU ocean; copying does not extend resource lifetime.
double time() const
Simulation timestamp of this completed projection.
Borrowed GPU rendering publication, with optional synchronous local wake upload. Does not retain inpu...
EVENGINE_API_DOMAINS Result< void > publish() const
Copy the completed resident atlas and optional wake to inactive textures, then publish atomically....
const OceanWake * wake
Borrow wake until publish returns; nullptr explicitly renders the open sea without a local wake.
Render-thread-owned GPU projection of immutable spectral coefficients, with no independent clock....
EVENGINE_API_DOMAINS Result< OceanGpuAtlasView > atlas(const OceanGpuFrameToken &token) const
Validate revision and borrow the complete rendering atlas for immediate same-device copying.
EVENGINE_API_DOMAINS OceanGpuRenderFrameBuilder gpuFrameBuilder(const OceanGpuSpectrum &spectrum, const OceanGpuFrameToken &token)
Prepare a borrowed GPU publication; owners must remain alive and unchanged through publish().
EVENGINE_API_DOMAINS OceanWakeGridView gridView() const
Borrow raw height/foam for immediate GPU upload; never retain across mutation or destruction.
GPU texture created via Graphics::newTexture. Owns GPU resources through an opaque backend handle.
Definition Texture.h:15
GLSL compute kernels for the GPU surface-flow solver.
Definition FluidTarget.h:12
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
Options for Graphics::newTexture / newCubemap. When generateMipmaps is true and sampler....
static TextureSampler nearest()
Nearest.
glm::uvec4 info