28#include <webgpu/webgpu.h>
49 auto comp = [](WGPUBlendOperation op, WGPUBlendFactor src, WGPUBlendFactor dst) {
50 WGPUBlendComponent c{};
56 const auto add = WGPUBlendOperation_Add;
58 return { comp(add, mode ==
BlendMode::Additive ? WGPUBlendFactor_SrcAlpha : WGPUBlendFactor_One, WGPUBlendFactor_One),
59 comp(add, WGPUBlendFactor_Zero, WGPUBlendFactor_One) };
63 return { comp(add, WGPUBlendFactor_SrcAlpha, WGPUBlendFactor_One),
64 comp(add, WGPUBlendFactor_One, WGPUBlendFactor_One) };
66 return { comp(add, WGPUBlendFactor_Dst, WGPUBlendFactor_OneMinusSrcAlpha),
67 comp(add, WGPUBlendFactor_One, WGPUBlendFactor_OneMinusSrcAlpha) };
69 return { comp(add, WGPUBlendFactor_One, WGPUBlendFactor_OneMinusSrc),
70 comp(add, WGPUBlendFactor_One, WGPUBlendFactor_OneMinusSrcAlpha) };
72 return { comp(add, WGPUBlendFactor_One, WGPUBlendFactor_OneMinusSrcAlpha),
73 comp(add, WGPUBlendFactor_One, WGPUBlendFactor_OneMinusSrcAlpha) };
75 return { comp(add, WGPUBlendFactor_One, WGPUBlendFactor_One),
76 comp(add, WGPUBlendFactor_One, WGPUBlendFactor_One) };
78 return { comp(WGPUBlendOperation_Max, WGPUBlendFactor_One, WGPUBlendFactor_One),
79 comp(WGPUBlendOperation_Max, WGPUBlendFactor_One, WGPUBlendFactor_One) };
81 return { comp(WGPUBlendOperation_Min, WGPUBlendFactor_One, WGPUBlendFactor_One),
82 comp(WGPUBlendOperation_Min, WGPUBlendFactor_One, WGPUBlendFactor_One) };
90 return { comp(add, WGPUBlendFactor_SrcAlpha, WGPUBlendFactor_OneMinusSrcAlpha),
91 comp(add, WGPUBlendFactor_One, WGPUBlendFactor_OneMinusSrcAlpha) };
109 switch (components) {
110 case 1:
return WGPUVertexFormat_Float32;
111 case 2:
return WGPUVertexFormat_Float32x2;
112 case 3:
return WGPUVertexFormat_Float32x3;
113 case 4:
return WGPUVertexFormat_Float32x4;
118 if (components == 4)
return normalized ? WGPUVertexFormat_Unorm8x4 : WGPUVertexFormat_Uint8x4;
119 if (components == 2)
return normalized ? WGPUVertexFormat_Unorm8x2 : WGPUVertexFormat_Uint8x2;
122 if (components == 2)
return normalized ? WGPUVertexFormat_Unorm16x2 : WGPUVertexFormat_Uint16x2;
123 if (components == 4)
return normalized ? WGPUVertexFormat_Unorm16x4 : WGPUVertexFormat_Uint16x4;
126 switch (components) {
127 case 1:
return WGPUVertexFormat_Sint32;
128 case 2:
return WGPUVertexFormat_Sint32x2;
129 case 3:
return WGPUVertexFormat_Sint32x3;
130 case 4:
return WGPUVertexFormat_Sint32x4;
134 switch (components) {
135 case 1:
return WGPUVertexFormat_Uint32;
136 case 2:
return WGPUVertexFormat_Uint32x2;
137 case 3:
return WGPUVertexFormat_Uint32x3;
138 case 4:
return WGPUVertexFormat_Uint32x4;
151 default:
return WGPUIndexFormat_Undefined;
170 default:
return WGPUTextureFormat_RGBA8Unorm;
192 default:
return WGPUTextureFormat_Undefined;
208 default:
return WGPUFeatureName_TextureCompressionETC2;
221 default:
return WGPUAddressMode_Repeat;
236 case GfxBufferUsage::Storage:
return WGPUBufferUsage_Storage | WGPUBufferUsage_Vertex | WGPUBufferUsage_CopyDst | WGPUBufferUsage_CopySrc;
237 default:
return WGPUBufferUsage_CopyDst;
255 default:
return WGPUCompareFunction_Always;
269 default:
return WGPUStencilOperation_Keep;
277 WGPUStencilFaceState face{};
278 face.depthFailOp = WGPUStencilOperation_Keep;
281 face.compare = WGPUCompareFunction_Always;
282 face.failOp = WGPUStencilOperation_Replace;
283 face.passOp = WGPUStencilOperation_Replace;
286 face.compare = WGPUCompareFunction_Equal;
287 face.failOp = WGPUStencilOperation_Keep;
288 face.passOp = WGPUStencilOperation_Keep;
293 face.compare = WGPUCompareFunction_Equal;
294 face.failOp = WGPUStencilOperation_Keep;
297 : WGPUStencilOperation_Zero;
300 face.compare = WGPUCompareFunction_NotEqual;
301 face.failOp = WGPUStencilOperation_Keep;
302 face.passOp = WGPUStencilOperation_Keep;
306 face.compare = WGPUCompareFunction_Always;
307 face.failOp = WGPUStencilOperation_Keep;
308 face.passOp = WGPUStencilOperation_Keep;
328 WGPUDepthStencilState ds{};
331 : WGPUOptionalBool_False;
332 ds.depthCompare = desc.
depthTest ? (desc.
depthEqual ? WGPUCompareFunction_LessEqual : WGPUCompareFunction_Less) : WGPUCompareFunction_Always;
335 ds.stencilReadMask = 0xFFu;
341 ds.depthBiasSlopeScale =
static_cast<float>(desc.
depthBias);
348 return format == WGPUTextureFormat_Depth24PlusStencil8 ||
349 format == WGPUTextureFormat_Depth32FloatStencil8 ||
350 format == WGPUTextureFormat_Stencil8;
362 return (clearRequested && !scoped) ? WGPULoadOp_Clear : WGPULoadOp_Load;
373 if (!enabled)
return WGPUCullMode_None;
374 return front ? WGPUCullMode_Front : WGPUCullMode_Back;
392 if (!source)
return 0;
394 for (
const char* p = source; (p = std::strstr(p,
"@group(")) !=
nullptr;) {
397 const unsigned long g = std::strtoul(p, &end, 10);
398 if (end == p)
continue;
400 if (g != group)
continue;
401 const char* b = std::strstr(p,
"@binding(");
404 const unsigned long idx = std::strtoul(b, &end, 10);
405 if (end == b)
continue;
409 const char* semi = std::strchr(p,
';');
410 const char* type = std::strstr(p, kind);
411 if (idx < 64 && type && (!semi || type < semi)) mask |= (
u64{1} << idx);
451 return size <= capacity && offset <= capacity - size;
Blend mode enumeration for custom rendering.
Backend-agnostic graphics enumerations.
Immutable GPU pipeline state — the backend-agnostic unit of "how to draw".
Core type definitions and utilities for ESEngine.
What a texture unit is called in WGSL, and what a WGSL source says about the group-1 bindings it hold...
bool writeFitsInBuffer(u32 offset, u32 size, u32 capacity)
WGPUStencilFaceState toWGPUStencilFace(GfxStencilMode mode)
The engine's two stencil modes as WebGPU face states (same table as GLDevice::applyPipeline: Write = ...
WGPUVertexFormat toWGPUVertexFormat(u8 components, GfxDataType type, bool normalized)
Maps one RHI attribute (components × type × normalized) to a WGPUVertexFormat, or kInvalidVertexForma...
u64 scanWGSLVolumeTextureMask(const char *source, u32 group)
Bit mask of the group's bindings a WGSL source declares as texture_3d.
WGPUDepthStencilState toWGPUDepthStencil(const PipelineDesc &desc, WGPUTextureFormat format)
A pipeline's full depth-stencil state against a pass attachment format.
u32 wgpuBytesPerPixel(GfxPixelFormat fmt)
WGPUIndexFormat toWGPUIndexFormat(GfxDataType type)
Index type for drawElements' GfxDataType.
WGPUFilterMode toWGPUFilter(TextureFilter filter)
u32 toWGPUStencilWriteMask(GfxStencilMode mode)
Stencil write mask per mode — the applyStencilMode table: Write fills all planes, Test reads without ...
bool hasStencilPlanes(WGPUTextureFormat format)
True when a depth-stencil format carries stencil planes (drives whether a pass may set stencil load/s...
WGPUStencilOperation toWGPUStencilOp(GfxStencilOp op)
bool needsWriteStaging(u32 size)
WGPUAddressMode toWGPUAddressMode(TextureWrap wrap)
BlendStateWGPU toWGPUBlend(BlendMode mode, bool layerAlpha=false)
Maps BlendMode to its WebGPU blend components — mirrors GLDevice's setBlendMode table (Overlay render...
u64 scanWGSLTextureKindMask(const char *source, u32 group, const char *kind)
Bit mask of the group's bindings whose declaration names the type kind.
WGPUCompareFunction toWGPUCompare(GfxStencilFunc func)
WGPUFeatureName compressionFeatureFor(GfxCompressedFormat fmt)
constexpr WGPUVertexFormat kInvalidVertexFormat
Sentinel for "no WGPU spelling": the header defines no Undefined vertex format, so the enum's non-val...
u64 scanWGSLArrayTextureMask(const char *source, u32 group)
Bit mask of the group's bindings a WGSL source declares as texture_2d_array.
constexpr u32 kGroup1TextureUnits
Texture units the group-1 convention spans — see WGSL_MAX_TEXTURE_UNITS.
WGPUCullMode toWGPUCullMode(bool enabled, bool front)
Cull state (PipelineDesc.cullEnabled/cullFront). Front face is CCW, matching the GL backend's default...
WGPUColor toWGPUClearColor(const RenderPassDesc &desc)
WGPUBufferUsage toWGPUBufferUsage(GfxBufferUsage usage)
WGPUTextureFormat toWGPUTextureFormat(GfxPixelFormat fmt)
WGPULoadOp toWGPULoadOp(bool clearRequested, bool scoped)
WGPUTextureFormat toWGPUCompressedFormat(GfxCompressedFormat fmt)
u64 scanWGSLDepthTextureMask(const char *source, u32 group)
Bit mask of the group's bindings a WGSL source declares as DEPTH textures.
u32 alignedWriteSize(u32 size)
@ MirroredRepeat
Tile with mirroring.
@ Repeat
Tile the texture.
@ ClampToEdge
Clamp to edge pixels.
bool writesStencil(GfxStencilMode mode)
std::uint64_t u64
Unsigned 64-bit integer.
GfxStencilMode
Stencil behaviour baked into a pipeline. The reference value is dynamic (GfxDevice::setStencilReferen...
@ Test
Draw only INSIDE the mask: func Equal, op Keep.
@ Write
Write the reference where drawn (mask fill): func Always, op Replace, color write off.
@ Decrement
Restore the parent reference on leaving a nested mask.
@ Increment
Enter a nested mask only where the parent reference matches.
@ TestOutside
Draw only OUTSIDE it: func NotEqual, op Keep — a hole, not a window.
@ Off
No stencil test; full color write.
@ Zero
Retire a root mask without writing color.
@ Storage
Read and written by compute shaders (var<storage, read_write>), and readable back....
@ Nearest
No interpolation (pixelated look).
GfxCompressedFormat
GPU-compressed texture internal formats for createCompressedTexture.
@ ETC2_RGBA8_SRGB
GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC (core).
@ S3TC_DXT5
GL_COMPRESSED_RGBA_S3TC_DXT5_EXT (extension).
@ ETC2_RGB8
GL_COMPRESSED_RGB8_ETC2 (core).
@ S3TC_DXT5_SRGB
GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT (extension).
@ ASTC_8x8
GL_COMPRESSED_RGBA_ASTC_8x8_KHR (extension).
@ S3TC_DXT1
GL_COMPRESSED_RGBA_S3TC_DXT1_EXT (extension).
@ ASTC_4x4_SRGB
GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR (extension).
@ ETC2_RGBA8
GL_COMPRESSED_RGBA8_ETC2_EAC (core).
@ ASTC_4x4
GL_COMPRESSED_RGBA_ASTC_4x4_KHR (extension).
std::uint32_t u32
Unsigned 32-bit integer.
BlendMode
Predefined blend modes for rendering.
std::uint8_t u8
Unsigned 8-bit integer.
constexpr u32 WGSL_MAX_TEXTURE_UNITS
Units a tN / sN name can carry — the batch stream and a material's own.
Immutable description of a draw pipeline. Equality keys the pipeline cache, so two draws with the sam...
One render pass: a target, which attachments to clear on entry, and the clear values — full load-op s...
One RHI blend mode as a full WebGPU blend state (color + alpha).