4#ifdef ES_ENABLE_POSTPROCESS
12RenderFrame::ReceiverMaps RenderFrame::declareCausticReceivers(
ecs::Registry& registry,
const MaterialBake& bake,
13 const glm::vec4& sun,
usize index) {
16 const glm::dvec4 window(bake.a), levels(bake.b);
17 const double span = window.z, level = levels.z, reach = levels.w;
19 if (span <= 0.0 || reach <= 0.0)
return maps;
21 const double block = span / 4.0, extent = span + block;
22 const auto corner = [span, block, extent](
double x) {
23 return std::floor((x + span * 0.5) / block + 0.5) * block - extent * 0.5;
25 const double x = corner(window.x), z = corner(window.y);
26 maps.window = glm::vec4((window.x - x) / extent, (window.y - z) / extent, span / extent, 0);
27 const double top = std::max(levels.x + levels.y, level) + 0.01 * reach;
28 const double height = top - level + reach;
29 const glm::dvec3 ray = -glm::refract(-glm::dvec3(sun), glm::dvec3(0, 1, 0), 1.0 / 1.333);
30 const glm::dvec2 slope(ray.x / ray.y, ray.z / ray.y);
32 glm::dmat4 matrix(1.0);
33 matrix[0][0] = 2.0 / extent;
34 matrix[1][0] = -2.0 * slope.x / extent;
35 matrix[3][0] = 2.0 * (origin.x - x + slope.x * (level - origin.y)) / extent - 1.0;
36 matrix[1][1] = 2.0 * slope.y / extent;
37 matrix[2][1] = -2.0 / extent;
38 matrix[3][1] = 1.0 - 2.0 * (origin.z - z + slope.y * (level - origin.y)) / extent;
39 matrix[1][2] = -1.0 / height;
41 matrix[3][2] = (top - origin.y) / height;
42 const glm::mat4 projection(matrix);
45 frustum.extractFromMatrix(projection);
46 auto frame = makeContext();
47 frame.view_projection = projection;
49 frame.causticReceivers =
true;
52 for (
auto& plugin : plugins_) {
54 if (plugin->castsShadows()) plugin->castInputs(frame, registry, unused);
56 const u32 side = bake.w + bake.w / 4;
57 for (
u32 group = 0; group < 2; ++group) {
58 const bool moving = group == 1;
59 const usize slot = index * 2 + group;
60 if (slot >= receiver_captures_.size()) receiver_captures_.resize(slot + 1);
61 if (!receiver_captures_[slot]) {
64 receiver_captures_[slot] = std::move(record);
66 auto& capture = *receiver_captures_[slot];
67 capture.bakeTarget = bake.receiver;
68 capture.pool.beginFrame();
69 capture.draws.clear();
70 RenderCollectContext collect{registry, frustum, clip_state_, capture.pool, capture.draws, frame,
computeCameraView(projection)};
71 collect.screen_ui = screen_domain_;
72 collect.ui_tree_layers = ui_tree_layers_;
73 collect.lod = {view_id_, &lod_view_state_,
nullptr,
true};
75 for (
auto& plugin : plugins_)
if (plugin->castsShadows()) plugin->collect(collect);
76 capture.draws.finalize(capture.pool);
77 if (!capture.draws.commandCount()) { capture.valid =
false;
continue; }
79 if (!capture.target || capture.target->getWidth() != side) {
81 spec.width = spec.height = side;
82 spec.linearFilter =
false;
83 spec.depthStencil =
true;
85 capture.valid =
false;
87 if (!capture.target)
continue;
88 const auto depth = graph_.importTarget(capture.target->handle(), side, side,
false, capture.target->getColorAttachment());
89 if (moving) { maps.moving = depth; maps.window.w = 1; }
90 else maps.fixed = depth;
92 bool trusted = !moving;
97 if (trusted) key = capture.draws.contentHashOfRecords(key, capture.pool, trusted);
99 const u64 revision = device_.trackedTextureRevision(texture);
100 trusted &= revision != 0;
103 const MaterialStore& materials = context_.materials();
104 for (
u32 i = 0; trusted && i < capture.draws.commandCount(); ++i) {
105 const DrawCommand& draw = capture.draws.command(i);
106 if (!draw.material_id)
continue;
107 const auto* material = materials.find(draw.material_id);
108 const auto* layout = material ? materials.layoutFor(material->shaderRef) :
nullptr;
109 if (!layout) { trusted =
false;
break; }
111 if (materials.castsCutoutShadow(draw.material_id) && draw.texture_count) {
114 for (
const auto& binding : material->textures) {
115 if (
static_cast<i32>(binding.unit) != layout->depthInputUnit)
continue;
116 const auto* texture = resource_manager_.getTexture(binding.texture);
117 if (texture) trackTexture(texture->handle());
118 else trusted =
false;
121 if (trusted && capture.valid && capture.key == key)
continue;
127 capture.pool.upload();
128 context_.prepareFrameConstants(capture.constants, projection);
130 pass.name = moving ?
"water-caustic-moving-receivers" :
"water-caustic-fixed-receivers";
132 pass.clear = pass.clearDepth =
true;
133 pass.clearColor[3] = 0.0f;
134 pass.discardDepth =
true;
135 pass.execute = [
this, &capture, trusted, key](
const rg::PassContext&) {
137 context_.bindFrameConstants(capture.constants);
138 context_.lights().uploadAndBind();
139 capture.draws.execute(device_, capture.pool, context_.materials(), context_.getWhiteTextureId(),
140 nullptr, context_.perDrawBlocks());
141 context_.bindFrameConstants(context_.frameUbo());
142 device_.invalidatePipelineCache();
143 capture.valid = trusted && capture.draws.skippedForProgram() == 0;
146 graph_.addPass(std::move(pass));
Per-frame CPU scope timing (ES_PROFILE_SCOPE) read by the editor profiler.
#define ES_PROFILE_SCOPE(name)
Frame resources and the shared scene rendering contract.
The point the frame is drawn relative to: a world far from zero loses precision in single floats,...
static Unique< Framebuffer > create(GfxDevice &device, const FramebufferSpec &spec)
Creates a framebuffer from specification.
static constexpr GfxContent transient()
static u32 frameConstantsSize()
Bytes a buffer passed to useFrameConstants must hold.
Central container for all entities and components.
constexpr u64 combine(u64 h, u64 v)
constexpr u64 kFnv64Offset
u64 combineWords(u64 h, const void *data, usize bytes)
constexpr Unique< T > makeUnique(Args &&... args)
Creates a Unique pointer with the given arguments.
std::int32_t i32
Signed 32-bit integer.
std::uint64_t u64
Unsigned 64-bit integer.
std::size_t usize
Size type for indexing and sizes.
std::uint32_t u32
Unsigned 32-bit integer.
glm::dvec3 & renderOrigin()
The origin, in world units. A double, as the world positions it is taken from are far from zero.
CameraView computeCameraView(const glm::mat4 &viewProjection)