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.

32.3 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    GfxEnums.hpp
 * @brief   Backend-agnostic graphics enumerations
 * @details Replaces raw GL enum constants in the GfxDevice interface so that
 *          upper-layer code has zero dependency on OpenGL headers.
 *
 * @author  ESEngine Team
 * @date    2026
 *
 * @copyright Copyright (c) 2026 ESEngine Team
 *            Licensed under the Apache License, Version 2.0.
 */
#pragma once

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

#include <string>

namespace esengine {

// =============================================================================
// Resource Handles
// =============================================================================

// Opaque, type-safe GPU resource handles. On the GL backend the value is the GL
// object id (0 is GL's null object, matching Invalid); other backends map the
// value to an internal resource table.

enum class BufferHandle : u32 { Invalid = 0 };
enum class TextureHandle : u32 { Invalid = 0 };
enum class ShaderHandle : u32 { Invalid = 0 };
/** @brief A compute program (GfxDevice::createCompute); only a device that supportsCompute() issues one. */
enum class ComputeHandle : u32 { Invalid = 0 };
/** @brief Handle 0 is the default framebuffer (backbuffer); it cannot be created or deleted. */
enum class FramebufferHandle : u32 { Default = 0 };
/** @brief An in-flight framebuffer readback (GfxDevice::requestReadback). */
enum class ReadbackHandle : u32 { Invalid = 0 };

/** @brief State of an in-flight readback. GL completes at request time (first poll
 *         reports Ready); WebGPU resolves when its staging-buffer map lands. */
enum class GfxReadbackStatus : u8 {
    Pending = 0,
    Ready = 1,
    Failed = 2,
};

// =============================================================================
// Texture Sampling
// =============================================================================

enum class TextureFilter : u8 {
    Nearest,  ///< No interpolation (pixelated look)
    Linear,   ///< Bilinear interpolation (smooth)
};

enum class TextureWrap : u8 {
    Repeat,          ///< Tile the texture
    ClampToEdge,     ///< Clamp to edge pixels
    MirroredRepeat,  ///< Tile with mirroring
};

// =============================================================================
// Buffer Usage
// =============================================================================

enum class GfxBufferUsage : u8 {
    Vertex,
    Index,
    Uniform,
    /// Read and written by compute shaders (`var<storage, read_write>`), and readable back. Needs a device that supportsCompute().
    Storage,
};

// =============================================================================
// Vertex Layout Id
// =============================================================================

/**
 * @brief Identifies a transient vertex stream / vertex format. Each layout has its own
 *        VBO+EBO+VAO in TransientBufferPool and is part of a pipeline's identity.
 */
enum class LayoutId : u8 {
    Batch           = 0,
    ParticleInstance = 1,  ///< Instanced: a static unit quad + a per-particle instance stream.
    Shape           = 2,
    /// Per-object transforms for GPU-resident meshes. Only the stream is the
    /// pool's; the layout belongs to the mesh, which is the one that knows what
    /// its own vertices look like.
    MeshInstance    = 3,
    /// The background quad: a world position and nothing else. Its own stream
    /// because the Batch one spends every sampler slot on a per-vertex merge, and
    /// the sky has to reach the environment atlas on the slot the shaders pin it to.
    Sky             = 4,
};

static constexpr u32 LAYOUT_COUNT = 5;

/// One per-object record for a resident mesh, in texels of the frame's RGBA32F
/// instance texture. FIXED at one shape for every variant, so no stride has to be
/// derived by the layout, the allocation and the packer separately.
/// @see MESH_INSTANCE_TEXEL_MODEL and the offsets below.
static constexpr u32 MESH_INSTANCE_TEXELS = 8;
/// The model matrix's three ROWS, translation in .w. Its fourth row is (0,0,0,1)
/// on every transform a Transform can state, so it is not sent.
static constexpr u32 MESH_INSTANCE_TEXEL_MODEL = 0;
static constexpr u32 MESH_INSTANCE_TEXEL_TINT = 3;
/// The normal matrix's three rows in .xyz. Per object because a non-uniform scale
/// makes the model matrix the wrong transform for a normal.
static constexpr u32 MESH_INSTANCE_TEXEL_NORMAL = 4;
/// The bake's atlas rectangle (xy scale, zw offset). Per object, because the same
/// mesh placed twice is lit twice and occupies two patches of one atlas.
static constexpr u32 MESH_INSTANCE_TEXEL_LIGHTMAP = 7;
/// Bytes one record occupies. RGBA32F: a world-space translation is what this
/// carries, and half precision loses whole units of it a thousand away.
static constexpr u32 MESH_INSTANCE_STRIDE = MESH_INSTANCE_TEXELS * 16;
/// Texels per row of the instance texture. A multiple of the record, so no
/// record straddles a row; 2048 is the smallest MAX_TEXTURE_SIZE WebGL2 grants.
static constexpr u32 MESH_INSTANCE_TEXTURE_WIDTH = 2048;
/// Records the texture holds. Beyond it the frame's tail draws unplaced, which
/// is visible; a texture the device refuses would draw nothing at all.
static constexpr u32 MESH_INSTANCE_MAX_RECORDS =
    MESH_INSTANCE_TEXTURE_WIDTH * MESH_INSTANCE_TEXTURE_WIDTH / MESH_INSTANCE_TEXELS;
/// The unit the frame's record texture binds on. Not one of the eight a draw's
/// own stream can claim, because it is FRAME state; and the TOP of what remains,
/// since a material's textures are handed units upward from unit 8.
static constexpr u32 MESH_INSTANCE_TEXTURE_UNIT = 15;
/// Bone matrices one skinned draw may carry; a mesh wanting more is drawn static. 256 mat4
/// is exactly the 16KB a WebGL2 uniform block is guaranteed, and a draw stages only the
/// matrices it has, so a small rig pays nothing for the room.
static constexpr u32 MESH_MAX_BONES = 256;
/// Morph targets one draw may blend AT ONCE; past it the heaviest are taken. The shader
/// loops over only the live ones, each a texture read or two per vertex, so an unused
/// budget costs nothing beyond the larger block. Matches the bones: a face runs dozens.
static constexpr u32 MESH_MAX_ACTIVE_MORPHS = 64;
/// Texels per row of a mesh's delta texture. 2048 is the smallest MAX_TEXTURE_SIZE
/// WebGL2 guarantees, so a square of them is the most any device here must hold.
static constexpr u32 MORPH_TEXTURE_WIDTH = 2048;
/// Deltas one mesh may carry: targets * vertices * (2 with normals, else 1).
/// Beyond it the geometry draws unmorphed — a texture the device refuses is a
/// mesh that does not draw at all.
static constexpr u32 MORPH_MAX_TEXELS = MORPH_TEXTURE_WIDTH * MORPH_TEXTURE_WIDTH;
static constexpr u32 MESH_VARIANTS = 4096;
/**
 * @brief What a mesh vertex channel MEANS, and the attribute location it binds to.
 * @details One vocabulary for the file format, the layout and the shaders: a
 *          channel's semantic IS its location. The first three match the 2D batch
 *          layout exactly, so one attribute vocabulary serves both vertex sources.
 *          Values are serialized in .esmesh — append only.
 */
enum class MeshChannel : u8 {
    Position  = 0,
    Color     = 1,
    TexCoord0 = 2,
    Normal    = 3,
    Tangent   = 4,
    Joints    = 5,
    Weights   = 6,
    /// A second UV set — what a bake is read through, laid out so no two of a
    /// model's surfaces overlap.
    TexCoord1 = 7,
};

/**
 * @brief Whether the mesh shaders READ a channel, as opposed to the file carrying it.
 * @details An attribute nothing reads spends a slot and a fetch per vertex, so a channel kept for its information
 *          stays out of the LAYOUT. Every channel the format names is read now; the predicate stays so the next one can
 *          wait for its reader. check-mesh-shader-inputs holds this both ways.
 */
constexpr bool meshShaderReads(MeshChannel c) {
    (void)c;
    return true;
}

/**
 * @brief How a channel's components are stored, as the file spells it.
 * @details The asset layer's vocabulary rather than the device's: these are the
 *          codes written into a .esmesh, and one of them (UInt16) exists because
 *          a joint index is neither a float nor a byte. Append only — serialized.
 */
enum class MeshChannelType : u8 {
    Float32 = 0,
    UNorm8  = 1,
    UInt16  = 2,
};

// =============================================================================
// Data Type (vertex attributes, index type, pixel data type)
// =============================================================================

enum class GfxDataType : u8 {
    Float,
    Int,
    UnsignedByte,
    UnsignedShort,
    UnsignedInt,
};

// =============================================================================
// Vertex Layout (part of a pipeline's identity; see PipelineDesc)
// =============================================================================

// 16 because that is what GLES3 and WebGPU both guarantee, and because a mesh
// carrying its own channels plus a per-instance transform passes 8 as soon as it
// has normals — the limit was the batch layouts' size, not the hardware's.
static constexpr u32 MAX_VERTEX_ATTRIBUTES = 16;
static constexpr u32 MAX_VERTEX_BUFFER_SLOTS = 2;

struct GfxVertexAttribute {
    u32 location = 0;
    u8 components = 4;  ///< 1..4
    GfxDataType type = GfxDataType::Float;
    bool normalized = false;
    u32 offset = 0;  ///< Byte offset within one element of its buffer slot.
    u8 bufferSlot = 0;
};

/**
 * @brief Describes how vertex-buffer bytes map to shader attributes.
 * @details Registered once (createVertexLayout) and referenced by pipelines;
 *          the buffers themselves bind per draw via setVertexBuffer. A slot with
 *          `instanceStep` advances per instance (attribute divisor 1).
 */
struct VertexLayoutDesc {
    GfxVertexAttribute attributes[MAX_VERTEX_ATTRIBUTES] = {};
    u32 attributeCount = 0;
    u32 strides[MAX_VERTEX_BUFFER_SLOTS] = {};
    bool instanceStep[MAX_VERTEX_BUFFER_SLOTS] = {};
};

enum class VertexLayoutHandle : u32 { Invalid = 0 };

// =============================================================================
// Stencil Function
// =============================================================================

enum class GfxStencilFunc : u8 {
    Never,
    Less,
    Equal,
    LEqual,
    Greater,
    NotEqual,
    GEqual,
    Always,
};

// =============================================================================
// Stencil Operation
// =============================================================================

enum class GfxStencilOp : u8 {
    Keep,
    Zero,
    Replace,
    Incr,
    Decr,
    Invert,
    IncrWrap,
    DecrWrap,
};

// =============================================================================
// Pixel Format (texture internal format + transfer format)
// =============================================================================

enum class GfxPixelFormat : u8 {
    RGB8,
    RGBA8,
    /// sRGB-encoded storage: hardware EOTF on sample, OETF on write, and
    /// blending in linear space — the 8-bit backbone of the linear pipeline.
    SRGB8_ALPHA8,
    /// Half-float HDR target; gate render-target use on
    /// GfxDevice::supportsFloatTargets (EXT_color_buffer_float on WebGL2).
    RGBA16F,
    /// Full-float storage, for values a half cannot hold — a world-space
    /// translation is one. Fetched by texel index only: filtering full floats is
    /// an optional device feature.
    RGBA32F,
    DepthComponent24,
    Depth24Stencil8,
};

/** @brief Bytes one pixel occupies in the CALLER's upload buffer — the single
 *         source both backends size a read of caller-owned pixels with. A
 *         backend's own texel size may differ (RGB8 widens to RGBA8 on WebGPU).
 *         Depth formats are attachment-only and never uploaded. */
inline u32 gfxBytesPerPixel(GfxPixelFormat fmt) {
    switch (fmt) {
    case GfxPixelFormat::RGB8:    return 3;
    case GfxPixelFormat::RGBA16F: return 8;
    case GfxPixelFormat::RGBA32F: return 16;
    default:                      return 4;
    }
}

// =============================================================================
// Compressed Texture Format (GPU-compressed internal formats)
// =============================================================================

/**
 * @brief GPU-compressed texture internal formats for createCompressedTexture.
 *
 * @details Decoded textures stay compressed in VRAM (4–8× smaller than RGBA8),
 *          the key constraint on mobile. Tiering:
 *          - **ETC2/EAC** — core in the WebGL2 / GLES3 spec; available wherever
 *            WebGL2 is, no extension. The safe baseline.
 *          - **ASTC** — `WEBGL_compressed_texture_astc` (iOS A8+, most modern
 *            Android); best quality/size. Query before use.
 *          - **S3TC/DXT** — `WEBGL_compressed_texture_s3tc` (desktop GPUs).
 *
 *          Always gate ASTC/S3TC behind GfxDevice::supportsCompressedFormat and
 *          fall back to the uncompressed RGBA8 path.
 */
enum class GfxCompressedFormat : u8 {
    ETC2_RGB8,         ///< GL_COMPRESSED_RGB8_ETC2 (core)
    ETC2_RGBA8,        ///< GL_COMPRESSED_RGBA8_ETC2_EAC (core)
    ASTC_4x4,          ///< GL_COMPRESSED_RGBA_ASTC_4x4_KHR (extension)
    ASTC_8x8,          ///< GL_COMPRESSED_RGBA_ASTC_8x8_KHR (extension)
    S3TC_DXT1,         ///< GL_COMPRESSED_RGBA_S3TC_DXT1_EXT (extension)
    S3TC_DXT5,         ///< GL_COMPRESSED_RGBA_S3TC_DXT5_EXT (extension)
    ETC2_RGBA8_SRGB,   ///< GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC (core)
    ASTC_4x4_SRGB,     ///< GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR (extension)
    S3TC_DXT5_SRGB,    ///< GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT (extension)
};

/** @brief Block footprint of a compressed format, the single source both backends
 *         use to size each mip level's upload. BC1/ETC2_RGB are 4x4x8B, the RGBA
 *         block formats 4x4x16B, ASTC's block matches its name. */
struct GfxBlockInfo {
    u32 blockWidth;
    u32 blockHeight;
    u32 bytesPerBlock;
};
inline GfxBlockInfo gfxCompressedBlockInfo(GfxCompressedFormat fmt) {
    switch (fmt) {
    case GfxCompressedFormat::S3TC_DXT1:      return {4, 4, 8};   // BC1
    case GfxCompressedFormat::ETC2_RGB8:      return {4, 4, 8};
    case GfxCompressedFormat::ASTC_8x8:       return {8, 8, 16};
    case GfxCompressedFormat::S3TC_DXT5:
    case GfxCompressedFormat::S3TC_DXT5_SRGB:
    case GfxCompressedFormat::ETC2_RGBA8:
    case GfxCompressedFormat::ETC2_RGBA8_SRGB:
    case GfxCompressedFormat::ASTC_4x4:
    case GfxCompressedFormat::ASTC_4x4_SRGB:
    default:                                  return {4, 4, 16};
    }
}

/** @brief How many mip levels of a @p width x @p height image are WHOLE blocks,
 *         counting from the base — the levels a compressed copy may write, since a
 *         copy whose size is not a multiple of the block is refused. 72x72 has two
 *         (72, 36; 18 stops it) and 70x70 has none, which is what makes an image
 *         uncompressible rather than merely short of mips. */
inline u32 gfxWholeBlockLevels(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels) {
    const GfxBlockInfo bi = gfxCompressedBlockInfo(fmt);
    u32 levels = 0;
    for (u32 level = 0; level < (mipLevels ? mipLevels : 1); ++level) {
        const u32 lw = (width >> level) ? (width >> level) : 1u;
        const u32 lh = (height >> level) ? (height >> level) : 1u;
        if ((lw % bi.blockWidth) != 0 || (lh % bi.blockHeight) != 0) break;
        ++levels;
    }
    return levels;
}

/** @brief Total bytes of a full mip pyramid (levels 0..mipLevels-1) for a
 *         base-size compressed texture — the block-aligned sum both the transcoder
 *         (packing) and the device (uploading) agree on. */
inline u32 gfxCompressedPyramidBytes(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels) {
    const GfxBlockInfo bi = gfxCompressedBlockInfo(fmt);
    u32 total = 0;
    for (u32 level = 0; level < (mipLevels ? mipLevels : 1); ++level) {
        const u32 lw = (width >> level) ? (width >> level) : 1u;
        const u32 lh = (height >> level) ? (height >> level) : 1u;
        total += ((lw + bi.blockWidth - 1) / bi.blockWidth)
               * ((lh + bi.blockHeight - 1) / bi.blockHeight) * bi.bytesPerBlock;
    }
    return total;
}

// =============================================================================
// Resource Descriptors
// =============================================================================

/**
 * @brief Immutable creation parameters of a GPU buffer.
 * @details `size` is the buffer's capacity; updateBuffer must stay within it.
 *          Growing is an explicit resizeBuffer (contents discarded), keeping the
 *          handle stable so vertex-layout associations survive.
 */
struct BufferDesc {
    GfxBufferUsage usage = GfxBufferUsage::Vertex;
    u32 size = 0;
    bool dynamic = false;
};

/**
 * @brief Immutable creation parameters of a 2D texture or 2D texture array.
 * @details `flipY` applies to the initial pixel upload only (WebGL upload state;
 *          native backends flip CPU-side at load time).
 */
struct TextureDesc {
    u32 width = 1;
    u32 height = 1;
    /// Samples per pixel. Above 1 the attachment is MULTISAMPLED and cannot be
    /// sampled by a shader — it is only ever drawn into and resolved from. Ask
    /// GfxDevice::maxSamples() first; a backend that answers 1 wants 1.
    u32 samples = 1;
    GfxPixelFormat format = GfxPixelFormat::RGBA8;
    TextureFilter minFilter = TextureFilter::Linear;
    TextureFilter magFilter = TextureFilter::Linear;
    TextureWrap wrapS = TextureWrap::ClampToEdge;
    TextureWrap wrapT = TextureWrap::ClampToEdge;
    bool mipmaps = false;
    bool flipY = false;
    /// Writable from a compute shader as a storage texture. RGBA8, RGBA16F and RGBA32F only, and only on a device
    /// that supportsCompute(); a texture asked for this elsewhere is created without it.
    bool storage = false;
    /// Above 1 the texture is a 2D ARRAY of this many layers, sampled through `sampler2DArray` /
    /// `texture_2d_array`. Its pixels are the layers back to back, each width * height texels. Colour, single-sample
    /// and sampled only: never an attachment, a storage target or compressed, and `flipY` does not apply.
    u32 layers = 1;
};

/**
 * @brief Attachments of an offscreen render target. The attach point of
 *        `depthStencil` follows its texture's pixel format.
 */
struct FramebufferDesc {
    TextureHandle color0 = TextureHandle::Invalid;
    TextureHandle depthStencil = TextureHandle::Invalid;
    /// Single-sample attachments a multisampled target resolves into. Set them
    /// and the device resolves whenever the target is left, so nothing above the
    /// RHI learns the target was multisampled.
    TextureHandle resolveColor0 = TextureHandle::Invalid;
    TextureHandle resolveDepthStencil = TextureHandle::Invalid;
    /// A second colour attachment, drawn in the same pass as the first: what the surface IS rather than
    /// how it looks (normal, roughness, metalness) — see `PipelineDesc::gbuffer`. Invalid for a target
    /// that carries none, which is nearly all of them.
    TextureHandle color1 = TextureHandle::Invalid;
    /// Its single-sample twin, when the target is multisampled.
    TextureHandle resolveColor1 = TextureHandle::Invalid;
    /// A third, beside the second whenever it is there: the surface's diffuse colour, for light an effect adds
    /// after the frame (bounce light), which must be coloured by what it lands on. See `PipelineDesc::albedo`.
    TextureHandle color2 = TextureHandle::Invalid;
    TextureHandle resolveColor2 = TextureHandle::Invalid;
    /// Pixel size, required only when resolving: a blit needs a rectangle and an
    /// attachment handle carries no dimensions.
    u32 width = 0;
    u32 height = 0;
};

/**
 * @brief One render pass: a target, which attachments to clear on entry, and the
 *        clear values — full load-op semantics, no sticky device clear state.
 * @details Clears ignore write masks a prior pipeline left restrictive (the masks
 *          are forced open). An optional clear region scopes the clear to a
 *          viewport rectangle (clearW == 0 = full target): the multi-camera flow
 *          clears each camera's region on the shared default target. A WebGPU
 *          backend maps the full-target case to a real load-op and emulates the
 *          scoped case (first-pass load-op or a scissored clear-quad).
 */
struct RenderPassDesc {
    FramebufferHandle target = FramebufferHandle::Default;
    bool clearColor = false;
    bool clearDepth = false;
    bool clearStencil = false;
    f32 clearColorValue[4] = {0.0f, 0.0f, 0.0f, 1.0f};
    i32 clearStencilValue = 0;
    i32 clearX = 0, clearY = 0;   ///< Clear region origin (pixels).
    u32 clearW = 0, clearH = 0;   ///< Clear region size; 0 = the whole target.
    /**
     * What this pass leaves in the target's multisampled attachments is not drawn on or read again once it has been resolved: nothing later
     * continues on this target. A tile-based GPU keeps a pass's samples in memory on the chip and only writes them out if asked to; this says
     * it need not, which is most of the memory traffic of a multisampled frame on a phone. Ignored where the target is not multisampled.
     */
    bool discardMultisampled = false;
    /** The pass's depth attachment was there to test against and nothing reads it afterwards (a shadow map keeps its depth in colour). */
    bool discardDepth = false;
};

// =============================================================================
// Backend Queries (diagnostics / capabilities)
// =============================================================================

/** @brief Backend identification strings. */
enum class GfxStringName : u8 {
    Version,
    Renderer,
    Vendor,
    ShadingLanguageVersion,
};

/** @brief Backend integer capabilities/limits. */
enum class GfxIntParam : u8 {
    MaxTextureSize,
    MaxTextureImageUnits,
    MaxVertexAttribs,
};

// =============================================================================
// Device Loss
// =============================================================================

/**
 * @brief Whether the device can still be drawn to.
 *
 * @details Every backend can lose its device: WebGL fires `webglcontextlost`,
 *          WebGPU resolves its lost future, a native driver resets under us. The
 *          states make "lost" a value the renderer branches on, not a condition
 *          it discovers by drawing into a dead device.
 */
enum class GfxDeviceStatus : u8 {
    Live,        ///< Normal operation.
    Lost,        ///< Every handle issued before is dead; submission has stopped.
    Recovering,  ///< A replacement device is being built; old handles stay dead.
    Dead,        ///< Unrecoverable — the renderer will not draw again this run.
};

/** @brief Why the device was lost. The report leads with this. */
enum class GfxDeviceLostReason : u8 {
    Unknown,
    ContextLost,   ///< The host took the context away (web `webglcontextlost`, surface teardown).
    OutOfMemory,   ///< An allocation the backend could not satisfy.
    Reset,         ///< Driver/GPU reset — a hang watchdog (TDR) or a peer process's fault.
    Removed,       ///< The adapter itself is gone: driver update, eGPU unplugged.
    Destroyed,     ///< We destroyed it. A loss report that is not a failure.
    Validation,    ///< The backend rejected our commands fatally.
    Internal,      ///< The backend failed inside itself and said no more than that.
};

/**
 * @brief What a device's clip volume does with depth.
 *
 * @details The engine builds every projection for [-1, 1], which its frustum
 *          extraction and its published frustum-corner API also speak. A device
 *          keeping [0, 1] is handed the same projection in its own terms by
 *          RenderContext::updateCameraConstants, the only place that happens.
 */
enum class ClipDepthRange : u8 {
    MinusOneToOne,  ///< OpenGL / OpenGL ES / WebGL.
    ZeroToOne,      ///< WebGPU, and every native API behind it.
};

/**
 * @brief Everything a human needs to know about one device loss.
 *
 * @details Assembled once, at the moment of loss, and then immutable — a report
 *          that says only "the context was lost" is what this type exists to
 *          replace.
 */
/**
 * @brief Who a device IS, captured at init() and true whether or not it still lives.
 *
 * @details A lost backend cannot be asked who it was (glGetString returns null
 *          once the context is gone), so this is read while the device works.
 *          Its own type because a device has an identity for its whole life,
 *          while a loss report exists only after one.
 */
struct GfxDeviceIdentity {
    std::string backend;   ///< "WebGL2", "WebGPU".
    std::string vendor;
    std::string renderer;
    std::string version;   ///< Driver / API version string.

    bool known() const { return !backend.empty(); }
};

struct GfxDeviceLostInfo {
    GfxDeviceLostReason reason = GfxDeviceLostReason::Unknown;

    /** Snapshotted from the device at the moment of loss, so the report stays
     *  immutable while the device goes on to be rebuilt with a new identity. */
    GfxDeviceIdentity identity;

    /** Driver-supplied text; empty when the backend offered no detail. */
    std::string message;

    /** What the device was doing when it died, when the site knows (e.g. "createTexture 4096x4096"). */
    std::string context;

    /** The frame the loss was observed on. */
    u64 frame = 0;
};

/**
 * @brief The whole report as one line, for a log or a crash report.
 * @details Single formatter so the C++ log, the JS diagnostics record and a
 *          user-facing error all name the failure identically.
 */
inline std::string gfxFormatDeviceLost(const GfxDeviceLostInfo& info);

/** @brief Human-readable reason, for logs and diagnostics reports. */
inline const char* gfxDeviceLostReasonName(GfxDeviceLostReason reason) {
    switch (reason) {
    case GfxDeviceLostReason::ContextLost: return "context-lost";
    case GfxDeviceLostReason::OutOfMemory: return "out-of-memory";
    case GfxDeviceLostReason::Reset:       return "device-reset";
    case GfxDeviceLostReason::Removed:     return "device-removed";
    case GfxDeviceLostReason::Destroyed:   return "destroyed";
    case GfxDeviceLostReason::Validation:  return "validation";
    case GfxDeviceLostReason::Internal:    return "internal-error";
    case GfxDeviceLostReason::Unknown:     break;
    }
    return "unknown";
}

inline std::string gfxFormatDeviceLost(const GfxDeviceLostInfo& info) {
    std::string out = "GPU device lost: ";
    out += gfxDeviceLostReasonName(info.reason);
    out += " [backend=" + (info.identity.backend.empty() ? std::string("unknown") : info.identity.backend);
    if (!info.identity.vendor.empty())   out += ", vendor=" + info.identity.vendor;
    if (!info.identity.renderer.empty()) out += ", gpu=" + info.identity.renderer;
    if (!info.identity.version.empty())  out += ", driver=" + info.identity.version;
    out += ", frame=" + std::to_string(info.frame) + "]";
    if (!info.context.empty()) out += " during " + info.context;
    if (!info.message.empty()) out += " — " + info.message;
    return out;
}

// =============================================================================
// Shader Program Creation
// =============================================================================

/**
 * @brief Shader source language a backend consumes.
 * @details GLDevice accepts GLSL ES 300; a WebGPU backend accepts WGSL. Queried
 *          via GfxDevice::supportsShaderLanguage, so the RHI itself is
 *          language-neutral and a caller fails fast instead of feeding a backend
 *          text it cannot compile (REARCH_WGSL Phase 1 seam).
 */
enum class GfxShaderLanguage : u8 {
    GLSL_ES300 = 0,
    WGSL       = 1,
};

/** @brief A program's source pair, tagged with the language it is written in. */
struct GfxShaderSource {
    GfxShaderLanguage language = GfxShaderLanguage::GLSL_ES300;
    const char* vertexSrc = nullptr;
    const char* fragmentSrc = nullptr;
    /**
     * Where the backend can (GfxDevice::supportsDeferredLink), create the program without waiting for it to compile and link: the work is queued and
     * the wait is the first thing that needs the program (a draw, a uniform location). Programs asked for one after another then compile side by
     * side instead of one by one. A program that fails to link is only found out then; use it for source that is known to compile.
     */
    bool deferLink = false;
};

/** @brief A compute program's source. Compute is WGSL only: GLSL ES 3.00 has no compute stage. */
struct GfxComputeSource {
    GfxShaderLanguage language = GfxShaderLanguage::WGSL;
    const char* source = nullptr;
    /// The `@compute` entry point.
    const char* entry = "main";
};

/**
 * @brief One resource a compute dispatch binds, at `@group(0) @binding(binding)`.
 *
 * @details The bind-group layout is built from the list itself, so the shader's declarations have to say the
 *          same thing: `Uniform` is `var<uniform>`, `StorageRead` is `var<storage, read>`, `Storage` is
 *          `var<storage, read_write>`, `Texture` is a `texture_2d<f32>` (or `texture_depth_2d` for a depth
 *          texture) read with `textureLoad`, `StorageTexture` is a `texture_storage_2d<FORMAT, write>` whose
 *          format is the texture's, and `Sampler` is a `sampler`.
 */
struct GfxComputeBinding {
    enum class Kind : u8 { Uniform, StorageRead, Storage, Texture, StorageTexture, Sampler, DepthTexture };
    Kind kind = Kind::Uniform;
    u32 binding = 0;
    BufferHandle buffer = BufferHandle::Invalid;
    /// Bytes into the buffer (a multiple of 256 for a uniform, 4 for storage) and how many; 0 = the rest.
    u32 offset = 0;
    u32 size = 0;
    TextureHandle texture = TextureHandle::Invalid;
    /// A Sampler's filter.
    TextureFilter filter = TextureFilter::Linear;

    static GfxComputeBinding uniform(u32 binding, BufferHandle buffer, u32 offset = 0, u32 size = 0) {
        GfxComputeBinding b; b.kind = Kind::Uniform; b.binding = binding; b.buffer = buffer; b.offset = offset; b.size = size; return b;
    }
    static GfxComputeBinding storageRead(u32 binding, BufferHandle buffer, u32 offset = 0, u32 size = 0) {
        GfxComputeBinding b; b.kind = Kind::StorageRead; b.binding = binding; b.buffer = buffer; b.offset = offset; b.size = size; return b;
    }
    static GfxComputeBinding storage(u32 binding, BufferHandle buffer, u32 offset = 0, u32 size = 0) {
        GfxComputeBinding b; b.kind = Kind::Storage; b.binding = binding; b.buffer = buffer; b.offset = offset; b.size = size; return b;
    }
    static GfxComputeBinding sampled(u32 binding, TextureHandle texture) {
        GfxComputeBinding b; b.kind = Kind::Texture; b.binding = binding; b.texture = texture; return b;
    }
    static GfxComputeBinding storageTexture(u32 binding, TextureHandle texture) {
        GfxComputeBinding b; b.kind = Kind::StorageTexture; b.binding = binding; b.texture = texture; return b;
    }
    /// A depth texture, read with textureLoad as `texture_depth_2d`. An Invalid handle binds a stand-in, so a shader
    /// that declares the binding runs whether or not the frame wrote depth.
    static GfxComputeBinding depth(u32 binding, TextureHandle texture) {
        GfxComputeBinding b; b.kind = Kind::DepthTexture; b.binding = binding; b.texture = texture; return b;
    }
    static GfxComputeBinding sampler(u32 binding, TextureFilter filter) {
        GfxComputeBinding b; b.kind = Kind::Sampler; b.binding = binding; b.filter = filter; return b;
    }
};

/**
 * @brief Vertex attribute location binding applied before a program is linked.
 * @note `name` is borrowed for the duration of the createProgram() call only.
 */
struct GfxAttribBinding {
    u32 index = 0;
    const char* name = nullptr;
};

/** @brief Which pipeline stage rejected a program during createProgram(). */
enum class GfxShaderStage : u8 {
    None,
    Vertex,
    Fragment,
    Link,
};

// =============================================================================
// Uniform Reflection
// =============================================================================

enum class GfxUniformType : u8 {
    Unknown,
    Float,
    Vec2,
    Vec3,
    Vec4,
    Int,
    IVec2,
    IVec3,
    IVec4,
    Bool,
    Mat2,
    Mat3,
    Mat4,
    Sampler2D,
    SamplerCube,
    Sampler2DArray,
};

struct GfxUniformInfo {
    std::string name;
    GfxUniformType type = GfxUniformType::Unknown;
    i32 location = -1;
    u32 arraySize = 1;
};

}  // namespace esengine