Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

3.2 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    ProbeConstants.hpp
 * @brief   The indirect light where ONE draw stands, when a grid was baked for it.
 *
 * @details Per DRAW and rewritten immediately before it, the way @ref SkinConstants
 *          and @ref MorphConstants are. A draw and not an object: a bake's rectangle
 *          belongs to an object, because one mesh placed twice occupies two patches,
 *          but irradiance is a function of WHERE, and a batch of instances has only
 *          one position to speak of. So a draw that stands in a volume is its own.
 *
 *          Nine coefficients and not four: this is the same shape
 *          LightConstants::envIrradiance carries, evaluated by the same expression.
 *          An environment is that field held constant; a volume is it sampled.
 */
#pragma once

#include "../../core/Types.hpp"

#include <glm/glm.hpp>

namespace esengine {

/** @brief Indexed UBO binding point for the per-draw ProbeConstants block. */
inline constexpr u32 PROBE_CONSTANTS_BINDING = 7;

/** @brief GLSL block name; must match the injected Lit header + Shader::compile lookup. */
inline constexpr const char* PROBE_CONSTANTS_BLOCK = "ProbeConstants";

/**
 * @brief How many instances of one merged draw carry their own irradiance.
 *
 * @details It rides the draw's uniform block, one run of ten vec4 per instance,
 *          so 100 runs is 16000 bytes — inside the 16KB a WebGL2 uniform block is
 *          guaranteed. A run reaching this many stops merging rather than drawing
 *          someone else's light.
 */
inline constexpr u32 PROBE_MAX_INSTANCES = 100;

/// Texels one instance's indirect light occupies in that block.
inline constexpr u32 PROBE_TEXELS = 10;

/** @brief The indirect light one instance stands in — both halves of it. */
struct ProbeConstants {
    /// SH9, rgb in xyz. `[0].w` is 1 where this draw stands in a volume — a flag
    /// and not the absence of coefficients, since zeroes ARE an answer here: a
    /// probe in the dark.
    glm::vec4 irradiance[9]{};
    /// x = which column of the frame's reflection atlas this instance reflects.
    /// 0 is the environment, so an instance inside no probe needs nothing written
    /// here and a zeroed run is already right. y = the first coefficient of the sky's
    /// visibility (the other eight ride `irradiance[1..8].w`), z = 1 when there is one
    /// (a grid baked with sky occlusion): the shaders' skyOcclusion().
    glm::vec4 reflection{0.0f};
};

static_assert(sizeof(ProbeConstants) == 16 * PROBE_TEXELS,
              "ProbeConstants must be a tight array of vec4 to match std140");

/**
 * @brief Bytes the block is declared with: every run, then one vec4 that is not data.
 *
 * @details ANGLE on D3D11 turns a block whose only member is a large array into a
 *          StructuredBuffer placed after the stage's texture registers. A lit material
 *          under a reflection samples sixteen, so it lands out of range and Chrome's
 *          GPU process aborts. A second member keeps the block a constant buffer.
 */
inline constexpr u32 PROBE_BLOCK_BYTES =
    static_cast<u32>(sizeof(ProbeConstants)) * PROBE_MAX_INSTANCES + 16;

}  // namespace esengine