Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
GfxEnums.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
15#pragma once
16
17#include "../../core/Types.hpp"
18
19#include <string>
20
21namespace esengine {
22
26
27// =============================================================================
28// Resource Handles
29// =============================================================================
30
31// Opaque, type-safe GPU resource handles. On the GL backend the value is the GL
32// object id (0 is GL's null object, matching Invalid); other backends map the
33// value to an internal resource table.
34
35enum class BufferHandle : u32 { Invalid = 0 };
36enum class TextureHandle : u32 { Invalid = 0 };
37enum class ShaderHandle : u32 { Invalid = 0 };
39enum class ComputeHandle : u32 { Invalid = 0 };
41enum class FramebufferHandle : u32 { Default = 0 };
43enum class ReadbackHandle : u32 { Invalid = 0 };
44
47enum class GfxReadbackStatus : u8 {
49 Ready = 1,
50 Failed = 2,
51};
52
53// =============================================================================
54// Texture Sampling
55// =============================================================================
56
57enum class TextureFilter : u8 {
60};
61
67
68// =============================================================================
69// Buffer Usage
70// =============================================================================
71
79
80// =============================================================================
81// Vertex Layout Id
82// =============================================================================
83
88enum class LayoutId : u8 {
89 Batch = 0,
91 Shape = 2,
99 Sky = 4,
100};
101
102static constexpr u32 LAYOUT_COUNT = 5;
103
108static constexpr u32 MESH_INSTANCE_TEXELS = 8;
111static constexpr u32 MESH_INSTANCE_TEXEL_MODEL = 0;
112static constexpr u32 MESH_INSTANCE_TEXEL_TINT = 3;
115static constexpr u32 MESH_INSTANCE_TEXEL_NORMAL = 4;
122static constexpr u32 PARTICLE_INSTANCE_STRIDE = 60;
125static constexpr u32 MESH_INSTANCE_TEXTURE_WIDTH = 2048;
133static constexpr u32 MESH_INSTANCE_TEXTURE_UNIT = 15;
138static constexpr u32 MESH_MAX_BONES = 256;
142static constexpr u32 MESH_MAX_ACTIVE_MORPHS = 64;
145static constexpr u32 MORPH_TEXTURE_WIDTH = 2048;
150static constexpr u32 MESH_VARIANTS = 4096;
158enum class MeshChannel : u8 {
160 Color = 1,
169};
170
177constexpr bool meshShaderReads(MeshChannel c) {
178 (void)c;
179 return true;
180}
181
188enum class MeshChannelType : u8 {
193 UInt8 = 3,
196};
197
198// =============================================================================
199// Data Type (vertex attributes, index type, pixel data type)
200// =============================================================================
201
209
210// =============================================================================
211// Vertex Layout (part of a pipeline's identity; see PipelineDesc)
212// =============================================================================
213
214// 16 because that is what GLES3 and WebGPU both guarantee, and because a mesh
215// carrying its own channels plus a per-instance transform passes 8 as soon as it
216// has normals — the limit was the batch layouts' size, not the hardware's.
217static constexpr u32 MAX_VERTEX_ATTRIBUTES = 16;
218static constexpr u32 MAX_VERTEX_BUFFER_SLOTS = 2;
219
228
241
242enum class VertexLayoutHandle : u32 { Invalid = 0 };
243
244// =============================================================================
245// Stencil Function
246// =============================================================================
247
258
259// =============================================================================
260// Stencil Operation
261// =============================================================================
262
273
274// =============================================================================
275// Pixel Format (texture internal format + transfer format)
276// =============================================================================
277
294
300 switch (fmt) {
301 case GfxPixelFormat::RGB8: return 3;
302 case GfxPixelFormat::RGBA16F: return 8;
303 case GfxPixelFormat::RGBA32F: return 16;
304 default: return 4;
305 }
306}
307
308// =============================================================================
309// Compressed Texture Format (GPU-compressed internal formats)
310// =============================================================================
311
337
347 switch (fmt) {
348 case GfxCompressedFormat::S3TC_DXT1: return {4, 4, 8}; // BC1
349 case GfxCompressedFormat::ETC2_RGB8: return {4, 4, 8};
350 case GfxCompressedFormat::ASTC_8x8: return {8, 8, 16};
357 default: return {4, 4, 16};
358 }
359}
360
366inline u32 gfxWholeBlockLevels(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels) {
367 const GfxBlockInfo bi = gfxCompressedBlockInfo(fmt);
368 u32 levels = 0;
369 for (u32 level = 0; level < (mipLevels ? mipLevels : 1); ++level) {
370 const u32 lw = (width >> level) ? (width >> level) : 1u;
371 const u32 lh = (height >> level) ? (height >> level) : 1u;
372 if ((lw % bi.blockWidth) != 0 || (lh % bi.blockHeight) != 0) break;
373 ++levels;
374 }
375 return levels;
376}
377
381inline u32 gfxCompressedPyramidBytes(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels) {
382 const GfxBlockInfo bi = gfxCompressedBlockInfo(fmt);
383 u32 total = 0;
384 for (u32 level = 0; level < (mipLevels ? mipLevels : 1); ++level) {
385 const u32 lw = (width >> level) ? (width >> level) : 1u;
386 const u32 lh = (height >> level) ? (height >> level) : 1u;
387 total += ((lw + bi.blockWidth - 1) / bi.blockWidth)
388 * ((lh + bi.blockHeight - 1) / bi.blockHeight) * bi.bytesPerBlock;
389 }
390 return total;
391}
392
393// =============================================================================
394// Resource Descriptors
395// =============================================================================
396
408
446
474
487 bool clearColor = false;
488 bool clearDepth = false;
489 bool clearStencil = false;
490 f32 clearColorValue[4] = {0.0f, 0.0f, 0.0f, 1.0f};
492 i32 clearX = 0, clearY = 0;
493 u32 clearW = 0, clearH = 0;
501 bool discardDepth = false;
502};
503
504// =============================================================================
505// Backend Queries (diagnostics / capabilities)
506// =============================================================================
507
515
522
523// =============================================================================
524// Device Loss
525// =============================================================================
526
541
553
566
583 std::string backend;
584 std::string vendor;
585 std::string renderer;
586 std::string version;
587
588 bool known() const { return !backend.empty(); }
589};
590
607
613inline std::string gfxFormatDeviceLost(const GfxDeviceLostInfo& info);
614
617 switch (reason) {
618 case GfxDeviceLostReason::ContextLost: return "context-lost";
619 case GfxDeviceLostReason::OutOfMemory: return "out-of-memory";
620 case GfxDeviceLostReason::Reset: return "device-reset";
621 case GfxDeviceLostReason::Removed: return "device-removed";
622 case GfxDeviceLostReason::Destroyed: return "destroyed";
623 case GfxDeviceLostReason::Validation: return "validation";
624 case GfxDeviceLostReason::Internal: return "internal-error";
626 }
627 return "unknown";
628}
629
630inline std::string gfxFormatDeviceLost(const GfxDeviceLostInfo& info) {
631 std::string out = "GPU device lost: ";
632 out += gfxDeviceLostReasonName(info.reason);
633 out += " [backend=" + (info.identity.backend.empty() ? std::string("unknown") : info.identity.backend);
634 if (!info.identity.vendor.empty()) out += ", vendor=" + info.identity.vendor;
635 if (!info.identity.renderer.empty()) out += ", gpu=" + info.identity.renderer;
636 if (!info.identity.version.empty()) out += ", driver=" + info.identity.version;
637 out += ", frame=" + std::to_string(info.frame) + "]";
638 if (!info.context.empty()) out += " during " + info.context;
639 if (!info.message.empty()) out += " — " + info.message;
640 return out;
641}
642
643// =============================================================================
644// Shader Program Creation
645// =============================================================================
646
654enum class GfxShaderLanguage : u8 {
656 WGSL = 1,
657};
658
662 const char* vertexSrc = nullptr;
663 const char* fragmentSrc = nullptr;
669 bool deferLink = false;
670};
671
675 const char* source = nullptr;
677 const char* entry = "main";
678};
679
700
716
724};
725
732 const char* name = nullptr;
733};
734
742
743// =============================================================================
744// Uniform Reflection
745// =============================================================================
746
765
772
773} // namespace esengine
Core type definitions and utilities for ESEngine.
2D texture for GPU rendering
Definition Texture.hpp:116
static constexpr u32 MORPH_TEXTURE_WIDTH
Definition GfxEnums.hpp:145
FramebufferHandle
Handle 0 is the default framebuffer (backbuffer); it cannot be created or deleted.
Definition GfxEnums.hpp:41
constexpr bool meshShaderReads(MeshChannel c)
Whether the mesh shaders READ a channel, as opposed to the file carrying it.
Definition GfxEnums.hpp:177
constexpr AsyncComputeFence kNoAsyncComputeFence
Definition GfxEnums.hpp:25
static constexpr u32 MESH_INSTANCE_STRIDE
Definition GfxEnums.hpp:121
AsyncComputePriority
Definition GfxEnums.hpp:23
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
@ MirroredRepeat
Tile with mirroring.
Definition GfxEnums.hpp:65
@ Repeat
Tile the texture.
Definition GfxEnums.hpp:63
@ ClampToEdge
Clamp to edge pixels.
Definition GfxEnums.hpp:64
GfxStringName
Backend identification strings.
Definition GfxEnums.hpp:509
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
static constexpr u32 MESH_INSTANCE_TEXELS
Definition GfxEnums.hpp:108
GfxDeviceLostReason
Why the device was lost. The report leads with this.
Definition GfxEnums.hpp:543
@ Validation
The backend rejected our commands fatally.
Definition GfxEnums.hpp:550
@ Reset
Driver/GPU reset — a hang watchdog (TDR) or a peer process's fault.
Definition GfxEnums.hpp:547
@ Removed
The adapter itself is gone: driver update, eGPU unplugged.
Definition GfxEnums.hpp:548
@ Destroyed
We destroyed it. A loss report that is not a failure.
Definition GfxEnums.hpp:549
@ Internal
The backend failed inside itself and said no more than that.
Definition GfxEnums.hpp:551
@ OutOfMemory
An allocation the backend could not satisfy.
Definition GfxEnums.hpp:546
@ ContextLost
The host took the context away (web webglcontextlost, surface teardown).
Definition GfxEnums.hpp:545
ClipDepthRange
What a device's clip volume does with depth.
Definition GfxEnums.hpp:562
@ ZeroToOne
WebGPU, and every native API behind it.
Definition GfxEnums.hpp:564
@ MinusOneToOne
OpenGL / OpenGL ES / WebGL.
Definition GfxEnums.hpp:563
static constexpr u32 MESH_INSTANCE_TEXEL_LIGHTMAP
Definition GfxEnums.hpp:118
static constexpr u32 MESH_INSTANCE_TEXEL_TINT
Definition GfxEnums.hpp:112
@ Bool
Boolean (1 byte).
Definition Buffer.hpp:55
@ Storage
Read and written by compute shaders (var<storage, read_write>), and readable back....
Definition GfxEnums.hpp:77
static constexpr u32 MESH_INSTANCE_MAX_RECORDS
Definition GfxEnums.hpp:128
ComputeHandle
A compute program (GfxDevice::createCompute); only a device that supportsCompute() issues one.
Definition GfxEnums.hpp:39
GfxShaderLanguage
Shader source language a backend consumes.
Definition GfxEnums.hpp:654
static constexpr u32 MAX_VERTEX_ATTRIBUTES
Definition GfxEnums.hpp:217
@ Linear
Bilinear interpolation (smooth).
Definition GfxEnums.hpp:59
@ Nearest
No interpolation (pixelated look).
Definition GfxEnums.hpp:58
GfxShaderStage
Which pipeline stage rejected a program during createProgram().
Definition GfxEnums.hpp:736
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
Definition GfxEnums.hpp:326
@ ETC2_RGBA8_SRGB
GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC (core).
Definition GfxEnums.hpp:333
@ S3TC_DXT5
GL_COMPRESSED_RGBA_S3TC_DXT5_EXT (extension).
Definition GfxEnums.hpp:332
@ ETC2_RGB8
GL_COMPRESSED_RGB8_ETC2 (core).
Definition GfxEnums.hpp:327
@ S3TC_DXT5_SRGB
GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT (extension).
Definition GfxEnums.hpp:335
@ ASTC_8x8
GL_COMPRESSED_RGBA_ASTC_8x8_KHR (extension).
Definition GfxEnums.hpp:330
@ S3TC_DXT1
GL_COMPRESSED_RGBA_S3TC_DXT1_EXT (extension).
Definition GfxEnums.hpp:331
@ ASTC_4x4_SRGB
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR (extension).
Definition GfxEnums.hpp:334
@ ETC2_RGBA8
GL_COMPRESSED_RGBA8_ETC2_EAC (core).
Definition GfxEnums.hpp:328
@ ASTC_4x4
GL_COMPRESSED_RGBA_ASTC_4x4_KHR (extension).
Definition GfxEnums.hpp:329
static constexpr u32 LAYOUT_COUNT
Definition GfxEnums.hpp:102
MeshChannelType
How a channel's components are stored, as the file spells it.
Definition GfxEnums.hpp:188
@ UNorm16
Skin weights at high precision.
Definition GfxEnums.hpp:195
@ UInt8
Joint indices of a skin or chunk of at most 256 bones: a byte each.
Definition GfxEnums.hpp:193
u32 gfxCompressedPyramidBytes(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels)
Total bytes of a full mip pyramid (levels 0..mipLevels-1) for a base-size compressed texture — the bl...
Definition GfxEnums.hpp:381
GfxDeviceStatus
Whether the device can still be drawn to.
Definition GfxEnums.hpp:535
@ Dead
Unrecoverable — the renderer will not draw again this run.
Definition GfxEnums.hpp:539
@ Recovering
A replacement device is being built; old handles stay dead.
Definition GfxEnums.hpp:538
@ Live
Normal operation.
Definition GfxEnums.hpp:536
@ Lost
Every handle issued before is dead; submission has stopped.
Definition GfxEnums.hpp:537
GfxReadbackStatus
State of an in-flight readback. GL completes at request time (first poll reports Ready); WebGPU resol...
Definition GfxEnums.hpp:47
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
static constexpr u32 MESH_INSTANCE_TEXTURE_WIDTH
Definition GfxEnums.hpp:125
static constexpr u32 MESH_INSTANCE_TEXTURE_UNIT
Definition GfxEnums.hpp:133
std::uint8_t u8
Unsigned 8-bit integer.
Definition Types.hpp:41
static constexpr u32 MESH_VARIANTS
Definition GfxEnums.hpp:150
@ Color
The page's RGBA, tinted: a rasterized atlas that carries native-AA coverage in alpha,...
static constexpr u32 MESH_MAX_ACTIVE_MORPHS
Definition GfxEnums.hpp:142
u32 gfxWholeBlockLevels(GfxCompressedFormat fmt, u32 width, u32 height, u32 mipLevels)
How many mip levels of a width x height image are WHOLE blocks, counting from the base — the levels a...
Definition GfxEnums.hpp:366
MeshChannel
What a mesh vertex channel MEANS, and the attribute location it binds to.
Definition GfxEnums.hpp:158
u32 gfxBytesPerPixel(GfxPixelFormat fmt)
Bytes one pixel occupies in the CALLER's upload buffer — the single source both backends size a read ...
Definition GfxEnums.hpp:299
static constexpr u32 MESH_INSTANCE_TEXEL_NORMAL
Definition GfxEnums.hpp:115
LayoutId
Identifies a transient vertex stream / vertex format. Each layout has its own VBO+EBO+VAO in Transien...
Definition GfxEnums.hpp:88
@ ParticleInstance
Instanced: a static unit quad + a per-particle instance stream.
Definition GfxEnums.hpp:90
i32 AsyncComputeFence
Definition GfxEnums.hpp:24
static constexpr u32 MAX_VERTEX_BUFFER_SLOTS
Definition GfxEnums.hpp:218
const char * gfxDeviceLostReasonName(GfxDeviceLostReason reason)
Human-readable reason, for logs and diagnostics reports.
Definition GfxEnums.hpp:616
static constexpr u32 MESH_INSTANCE_TEXEL_MODEL
Definition GfxEnums.hpp:111
static constexpr u32 PARTICLE_INSTANCE_STRIDE
Definition GfxEnums.hpp:122
GfxBlockInfo gfxCompressedBlockInfo(GfxCompressedFormat fmt)
Definition GfxEnums.hpp:346
static constexpr u32 MORPH_MAX_TEXELS
Definition GfxEnums.hpp:149
GfxIntParam
Backend integer capabilities/limits.
Definition GfxEnums.hpp:517
ReadbackHandle
An in-flight framebuffer readback (GfxDevice::requestReadback).
Definition GfxEnums.hpp:43
float f32
32-bit floating point
Definition Types.hpp:59
std::string gfxFormatDeviceLost(const GfxDeviceLostInfo &info)
The whole report as one line, for a log or a crash report.
Definition GfxEnums.hpp:630
static constexpr u32 MESH_MAX_BONES
Definition GfxEnums.hpp:138
Immutable creation parameters of a GPU buffer.
Definition GfxEnums.hpp:403
GfxBufferUsage usage
Definition GfxEnums.hpp:404
Attachments of an offscreen render target. The attach point of depthStencil follows its texture's pix...
Definition GfxEnums.hpp:451
TextureHandle resolveColor2
Definition GfxEnums.hpp:468
TextureHandle resolveColor0
Definition GfxEnums.hpp:457
TextureHandle resolveDepthStencil
Definition GfxEnums.hpp:458
TextureHandle resolveColor1
Its single-sample twin, when the target is multisampled.
Definition GfxEnums.hpp:464
TextureHandle depthStencil
Definition GfxEnums.hpp:453
Vertex attribute location binding applied before a program is linked.
Definition GfxEnums.hpp:730
Block footprint of a compressed format, the single source both backends use to size each mip level's ...
Definition GfxEnums.hpp:341
One resource a compute dispatch binds, at @group(0) @binding(binding).
Definition GfxEnums.hpp:689
static GfxComputeBinding storageTexture(u32 binding, TextureHandle texture)
Definition GfxEnums.hpp:713
static GfxComputeBinding depth(u32 binding, TextureHandle texture)
Definition GfxEnums.hpp:718
static GfxComputeBinding uniform(u32 binding, BufferHandle buffer, u32 offset=0, u32 size=0)
Definition GfxEnums.hpp:701
static GfxComputeBinding sampled(u32 binding, TextureHandle texture)
Definition GfxEnums.hpp:710
static GfxComputeBinding storageRead(u32 binding, BufferHandle buffer, u32 offset=0, u32 size=0)
Definition GfxEnums.hpp:704
static GfxComputeBinding storage(u32 binding, BufferHandle buffer, u32 offset=0, u32 size=0)
Definition GfxEnums.hpp:707
u32 offset
Bytes into the buffer (a multiple of 256 for a uniform, 4 for storage) and how many; 0 = the rest.
Definition GfxEnums.hpp:695
TextureFilter filter
A Sampler's filter.
Definition GfxEnums.hpp:699
static GfxComputeBinding sampler(u32 binding, TextureFilter filter)
Definition GfxEnums.hpp:721
A compute program's source. Compute is WGSL only: GLSL ES 3.00 has no compute stage.
Definition GfxEnums.hpp:673
GfxShaderLanguage language
Definition GfxEnums.hpp:674
const char * entry
The @compute entry point.
Definition GfxEnums.hpp:677
Everything a human needs to know about one device loss.
Definition GfxEnums.hpp:582
std::string version
Driver / API version string.
Definition GfxEnums.hpp:586
std::string backend
"WebGL2", "WebGPU".
Definition GfxEnums.hpp:583
GfxDeviceIdentity identity
Definition GfxEnums.hpp:596
GfxDeviceLostReason reason
Definition GfxEnums.hpp:592
A program's source pair, tagged with the language it is written in.
Definition GfxEnums.hpp:660
GfxShaderLanguage language
Definition GfxEnums.hpp:661
u32 offset
Byte offset within one element of its buffer slot.
Definition GfxEnums.hpp:225
One render pass: a target, which attachments to clear on entry, and the clear values — full load-op s...
Definition GfxEnums.hpp:485
u32 clearH
Clear region size; 0 = the whole target.
Definition GfxEnums.hpp:493
i32 clearY
Clear region origin (pixels).
Definition GfxEnums.hpp:492
FramebufferHandle target
Definition GfxEnums.hpp:486
Immutable creation parameters of a 2D texture or 2D texture array.
Definition GfxEnums.hpp:414
bool asyncShareable
Allow a native compute queue to share this single-sample colour target.
Definition GfxEnums.hpp:441
TextureFilter minFilter
Definition GfxEnums.hpp:422
bool layered() const
An array or a volume: pixels given slice by slice, fixed at creation, never an attachment.
Definition GfxEnums.hpp:444
TextureFilter magFilter
Definition GfxEnums.hpp:423
GfxPixelFormat format
Definition GfxEnums.hpp:421
Describes how vertex-buffer bytes map to shader attributes.
Definition GfxEnums.hpp:235
GfxVertexAttribute attributes[MAX_VERTEX_ATTRIBUTES]
Definition GfxEnums.hpp:236
u32 strides[MAX_VERTEX_BUFFER_SLOTS]
Definition GfxEnums.hpp:238
bool instanceStep[MAX_VERTEX_BUFFER_SLOTS]
Definition GfxEnums.hpp:239