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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
feat(renderer): sRGB + half-float formats land in the RHI - color groundwork
esengine87d45314c935
diff --git a/src/esengine/renderer/Framebuffer.cpp b/src/esengine/renderer/Framebuffer.cppindex 426fdb52f..dee456d22 100644--- a/src/esengine/renderer/Framebuffer.cpp+++ b/src/esengine/renderer/Framebuffer.cpp@@ -107,7 +107,7 @@ bool Framebuffer::initialize() {TextureDesc colorDesc;colorDesc.width = spec_.width;colorDesc.height = spec_.height;- colorDesc.format = GfxPixelFormat::RGBA8;+ colorDesc.format = spec_.colorFormat;colorDesc.minFilter = filter;colorDesc.magFilter = filter;colorAttachment_ = device_->createTexture(colorDesc, nullptr);diff --git a/src/esengine/renderer/Framebuffer.hpp b/src/esengine/renderer/Framebuffer.hppindex 2ad787889..4cf1665fb 100644--- a/src/esengine/renderer/Framebuffer.hpp+++ b/src/esengine/renderer/Framebuffer.hpp@@ -54,6 +54,8 @@ struct FramebufferSpec {bool depthStencil = true;/** @brief Texture filter mode: false=GL_NEAREST, true=GL_LINEAR */bool linearFilter = false;+ /** @brief Color attachment format. RGBA16F needs GfxDevice::supportsFloatTargets. */+ GfxPixelFormat colorFormat = GfxPixelFormat::RGBA8;};// =============================================================================diff --git a/src/esengine/renderer/GLDevice.cpp b/src/esengine/renderer/GLDevice.cppindex c389df0df..1d001a3d2 100644--- a/src/esengine/renderer/GLDevice.cpp+++ b/src/esengine/renderer/GLDevice.cpp@@ -48,6 +48,24 @@#ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3#endif+#ifndef GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC+ #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279+#endif+#ifndef GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR+ #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0+#endif+#ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT+ #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F+#endif+#ifndef GL_SRGB8_ALPHA8+ #define GL_SRGB8_ALPHA8 0x8C43+#endif+#ifndef GL_RGBA16F+ #define GL_RGBA16F 0x881A+#endif+#ifndef GL_HALF_FLOAT+ #define GL_HALF_FLOAT 0x140B+#endif#ifndef GL_UNSIGNED_INT_24_8#define GL_UNSIGNED_INT_24_8 0x84FA#endif@@ -156,6 +174,8 @@ GLPixelFormatInfo toGLPixelFormat(GfxPixelFormat fmt) {switch (fmt) {case GfxPixelFormat::RGB8: return { GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE };case GfxPixelFormat::RGBA8: return { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE };+ case GfxPixelFormat::SRGB8_ALPHA8: return { GL_SRGB8_ALPHA8, GL_RGBA, GL_UNSIGNED_BYTE };+ case GfxPixelFormat::RGBA16F: return { GL_RGBA16F, GL_RGBA, GL_HALF_FLOAT };case GfxPixelFormat::DepthComponent24: return { GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT };case GfxPixelFormat::Depth24Stencil8: return { GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8 };default: return { GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE };@@ -170,6 +190,9 @@ GLenum toGLCompressedFormat(GfxCompressedFormat fmt) {case GfxCompressedFormat::ASTC_8x8: return GL_COMPRESSED_RGBA_ASTC_8x8_KHR;case GfxCompressedFormat::S3TC_DXT1: return GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;case GfxCompressedFormat::S3TC_DXT5: return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;+ case GfxCompressedFormat::ETC2_RGBA8_SRGB: return GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC;+ case GfxCompressedFormat::ASTC_4x4_SRGB: return GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR;+ case GfxCompressedFormat::S3TC_DXT5_SRGB: return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;default: return GL_COMPRESSED_RGBA8_ETC2_EAC;}}@@ -1111,8 +1134,24 @@ bool GLDevice::supportsCompressedFormat(GfxCompressedFormat format) {case GfxCompressedFormat::S3TC_DXT5:return glExtensionPresent("GL_EXT_texture_compression_s3tc")|| glExtensionPresent("WEBGL_compressed_texture_s3tc");+ case GfxCompressedFormat::ETC2_RGBA8_SRGB:+ return true; // sRGB ETC2/EAC is core alongside the UNORM variant+ case GfxCompressedFormat::ASTC_4x4_SRGB:+ return glExtensionPresent("GL_KHR_texture_compression_astc_ldr")+ || glExtensionPresent("WEBGL_compressed_texture_astc");+ case GfxCompressedFormat::S3TC_DXT5_SRGB:+ // Desktop GL ships sRGB DXT via EXT_texture_sRGB; WebGL splits it out.+ return glExtensionPresent("GL_EXT_texture_sRGB")+ || glExtensionPresent("WEBGL_compressed_texture_s3tc_srgb");}return false;}+bool GLDevice::supportsFloatTargets() {+ // Rendering INTO RGBA16F needs EXT_color_buffer_float on WebGL2 (sampling+ // half-float textures is core; only attachment renderability is gated).+ return glExtensionPresent("GL_EXT_color_buffer_float")+ || glExtensionPresent("EXT_color_buffer_float");+}+} // namespace esenginediff --git a/src/esengine/renderer/GLDevice.hpp b/src/esengine/renderer/GLDevice.hppindex 979d74ba6..f7878a7c8 100644--- a/src/esengine/renderer/GLDevice.hpp+++ b/src/esengine/renderer/GLDevice.hpp@@ -68,6 +68,7 @@ public:void generateMipmaps(TextureHandle texture) override;void bindTexture(u32 slot, TextureHandle texture) override;bool supportsCompressedFormat(GfxCompressedFormat format) override;+ bool supportsFloatTargets() override;bool supportsShaderLanguage(GfxShaderLanguage language) const override {return language == GfxShaderLanguage::GLSL_ES300;diff --git a/src/esengine/renderer/GfxDevice.hpp b/src/esengine/renderer/GfxDevice.hppindex d9becf71b..edccc2f74 100644--- a/src/esengine/renderer/GfxDevice.hpp+++ b/src/esengine/renderer/GfxDevice.hpp@@ -191,6 +191,15 @@ public:*/virtual bool supportsCompressedFormat(GfxCompressedFormat format) = 0;+ /**+ * @brief Whether RGBA16F color attachments are renderable.+ * @details WebGL2 gates float-target rendering behind EXT_color_buffer_float+ * (sampling half-float textures is core); WebGPU always supports it.+ * Probe this before creating an HDR framebuffer and fall back to+ * SRGB8_ALPHA8/RGBA8.+ */+ virtual bool supportsFloatTargets() = 0;+// =========================================================================// Shader Programs// =========================================================================diff --git a/src/esengine/renderer/GfxEnums.hpp b/src/esengine/renderer/GfxEnums.hppindex 46d751ec6..fc53cfe25 100644--- a/src/esengine/renderer/GfxEnums.hpp+++ b/src/esengine/renderer/GfxEnums.hpp@@ -165,6 +165,12 @@ enum class GfxStencilOp : u8 {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,DepthComponent24,Depth24Stencil8,};@@ -188,12 +194,15 @@ enum class GfxPixelFormat : u8 {* 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_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)};// =============================================================================diff --git a/src/esengine/renderer/Texture.cpp b/src/esengine/renderer/Texture.cppindex d2c667842..8a51e950a 100644--- a/src/esengine/renderer/Texture.cpp+++ b/src/esengine/renderer/Texture.cpp@@ -28,13 +28,20 @@ GfxPixelFormat toGfxPixelFormat(TextureFormat format) {switch (format) {case TextureFormat::RGB8: return GfxPixelFormat::RGB8;case TextureFormat::RGBA8: return GfxPixelFormat::RGBA8;+ case TextureFormat::SRGB8A8: return GfxPixelFormat::SRGB8_ALPHA8;+ case TextureFormat::RGBA16F: return GfxPixelFormat::RGBA16F;case TextureFormat::Depth24: return GfxPixelFormat::DepthComponent24;default: return GfxPixelFormat::RGBA8;}}u32 bytesPerPixel(TextureFormat format) {- return format == TextureFormat::RGBA8 ? 4u : 3u;+ switch (format) {+ case TextureFormat::RGBA16F: return 8u;+ case TextureFormat::RGBA8:+ case TextureFormat::SRGB8A8: return 4u;+ default: return 3u;+ }}} // namespacediff --git a/src/esengine/renderer/Texture.hpp b/src/esengine/renderer/Texture.hppindex 5341092a7..8debfd6f3 100644--- a/src/esengine/renderer/Texture.hpp+++ b/src/esengine/renderer/Texture.hpp@@ -43,9 +43,11 @@ class GfxDevice;*/enum class TextureFormat {None = 0,- RGB8, ///< 3 channels, 8 bits each (24 bpp)- RGBA8, ///< 4 channels, 8 bits each (32 bpp)- Depth24 ///< Depth buffer format (24 bits)+ RGB8, ///< 3 channels, 8 bits each (24 bpp)+ RGBA8, ///< 4 channels, 8 bits each (32 bpp)+ SRGB8A8, ///< sRGB-encoded RGBA8: hardware-linearized on sample (32 bpp)+ RGBA16F, ///< Half-float HDR (64 bpp); render use gated on supportsFloatTargets+ Depth24 ///< Depth buffer format (24 bits)};// =============================================================================diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.cpp b/src/esengine/renderer/webgpu/WebGPUDevice.cppindex cfa3f6426..2def788ba 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.cpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.cpp@@ -36,6 +36,14 @@ u32 dsVariantOf(WGPUTextureFormat format) {return hasStencilPlanes(format) ? WebGPUDevice::kDsDepthStencil : WebGPUDevice::kDsDepthOnly;}+u32 colorVariantOf(WGPUTextureFormat format) {+ switch (format) {+ case WGPUTextureFormat_RGBA8UnormSrgb: return WebGPUDevice::kColorSrgb8;+ case WGPUTextureFormat_RGBA16Float: return WebGPUDevice::kColorRgba16f;+ default: return WebGPUDevice::kColorRgba8;+ }+}+u8 packSamplerKey(TextureFilter minFilter, TextureFilter magFilter,TextureWrap wrapS, TextureWrap wrapT) {return static_cast<u8>((minFilter == TextureFilter::Nearest ? 1u : 0u) |@@ -447,13 +455,14 @@ void WebGPUDevice::updateTexture(TextureHandle texture, i32 x, i32 y, u32 width,dst.texture = it->second.texture;dst.origin = WGPUOrigin3D{static_cast<u32>(x), static_cast<u32>(y), 0};+ const u32 bpp = it->second.format == WGPUTextureFormat_RGBA16Float ? 8u : 4u;WGPUTexelCopyBufferLayout layout{};- layout.bytesPerRow = width * 4; // RGBA8 — the engine's only uncompressed upload format+ layout.bytesPerRow = width * bpp;layout.rowsPerImage = height;WGPUExtent3D extent{width, height, 1};wgpuQueueWriteTexture(queue_, &dst, pixels,- static_cast<usize>(width) * height * 4, &layout, &extent);+ static_cast<usize>(width) * height * bpp, &layout, &extent);}void WebGPUDevice::setTextureParams(TextureHandle texture, TextureFilter minFilter,@@ -590,7 +599,8 @@ PipelineHandle WebGPUDevice::createPipeline(const PipelineDesc& desc) {WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {auto it = pipelines_.find(id);if (it == pipelines_.end() || !device_) return nullptr;- const u32 variant = dsVariantOf(pass_ds_format_);+ const u32 dsVariant = dsVariantOf(pass_ds_format_);+ const u32 variant = dsVariant * kColorVariantCount + colorVariantOf(pass_color_format_);if (it->second.variants[variant]) return it->second.variants[variant];const PipelineDesc& desc = it->second.desc;@@ -632,7 +642,7 @@ WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {blendState.alpha = blend.alpha;WGPUColorTargetState target{};- target.format = surface_format_;+ target.format = pass_color_format_;target.blend = desc.blendEnabled ? &blendState : nullptr;target.writeMask = (desc.stencil == GfxStencilMode::Write) ? WGPUColorWriteMask_None: WGPUColorWriteMask_All;@@ -660,7 +670,7 @@ WGPURenderPipeline WebGPUDevice::ensurePipeline(u32 id) {pd.multisample.mask = 0xFFFFFFFFu;pd.fragment = &fragment;WGPUDepthStencilState ds{};- if (variant != kDsNone) {+ if (dsVariant != kDsNone) {ds = toWGPUDepthStencil(desc, pass_ds_format_);pd.depthStencil = &ds;} else if (desc.depthTest || desc.stencil != GfxStencilMode::Off) {@@ -723,7 +733,8 @@ WGPUSampler WebGPUDevice::samplerFor(u8 key) {// =============================================================================WGPURenderPipeline WebGPUDevice::ensureClearPipeline(bool color, bool depth, bool stencil) {- const u32 key = dsVariantOf(pass_ds_format_) << 3 |+ const u32 key = colorVariantOf(pass_color_format_) << 5 |+ dsVariantOf(pass_ds_format_) << 3 |(color ? 1u : 0u) | (depth ? 2u : 0u) | (stencil ? 4u : 0u);auto it = clear_pipelines_.find(key);if (it != clear_pipelines_.end()) return it->second;@@ -784,7 +795,7 @@ struct ClearColor { value : vec4f };WGPUShaderModule fs = makeModule(kClearFS);WGPUColorTargetState target{};- target.format = surface_format_;+ target.format = pass_color_format_;target.writeMask = color ? WGPUColorWriteMask_All : WGPUColorWriteMask_None;WGPUFragmentState fragment{};@@ -1135,6 +1146,7 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {WGPUTextureView targetView = nullptr;WGPUTextureView dsView = nullptr;pass_ds_format_ = WGPUTextureFormat_Undefined;+ pass_color_format_ = surface_format_;if (desc.target != FramebufferHandle::Default) {auto fbIt = framebuffers_.find(static_cast<u32>(desc.target));if (fbIt == framebuffers_.end()) {@@ -1147,6 +1159,7 @@ void WebGPUDevice::beginRenderPass(const RenderPassDesc& desc) {return;}targetView = texIt->second.view;+ pass_color_format_ = texIt->second.format;pass_width_ = texIt->second.width;pass_height_ = texIt->second.height;if (fbIt->second.depthStencil != 0) {diff --git a/src/esengine/renderer/webgpu/WebGPUDevice.hpp b/src/esengine/renderer/webgpu/WebGPUDevice.hppindex cd6e5a81d..237105eeb 100644--- a/src/esengine/renderer/webgpu/WebGPUDevice.hpp+++ b/src/esengine/renderer/webgpu/WebGPUDevice.hpp@@ -48,6 +48,10 @@ public:* against the pass it draws into (GL has no such coupling). */enum DsVariant : u32 { kDsNone = 0, kDsDepthOnly = 1, kDsDepthStencil = 2, kDsVariantCount = 3 };+ /** @brief Color attachment format of a pass — like DsVariant, WebGPU+ * validates the pipeline's color target format against the pass. */+ enum ColorVariant : u32 { kColorRgba8 = 0, kColorSrgb8 = 1, kColorRgba16f = 2, kColorVariantCount = 3 };+/** @brief @p device may be null for bookkeeping-only use (tests, bring-up). */explicit WebGPUDevice(WGPUDevice device = nullptr);~WebGPUDevice() override;@@ -84,6 +88,8 @@ public:void generateMipmaps(TextureHandle texture) override;void bindTexture(u32 slot, TextureHandle texture) override;bool supportsCompressedFormat(GfxCompressedFormat format) override;+ // Float-target rendering is a WebGPU core capability.+ bool supportsFloatTargets() override { return true; }bool supportsShaderLanguage(GfxShaderLanguage language) const override {return language == GfxShaderLanguage::WGSL;@@ -186,7 +192,8 @@ private:};struct PipelineRec {PipelineDesc desc;- WGPURenderPipeline variants[kDsVariantCount] = {}; ///< Lazily built per pass DS shape.+ /// Lazily built per pass shape: [ds * kColorVariantCount + color].+ WGPURenderPipeline variants[kDsVariantCount * kColorVariantCount] = {};};struct FramebufferRec {u32 color0 = 0; ///< TextureHandle id of the color attachment.@@ -288,6 +295,7 @@ private:u32 pass_width_ = 0; ///< Current pass target size (scissor-off rectangle).u32 pass_height_ = 0;WGPUTextureFormat pass_ds_format_ = WGPUTextureFormat_Undefined;+ WGPUTextureFormat pass_color_format_ = WGPUTextureFormat_RGBA8Unorm;WGPUCommandEncoder encoder_ = nullptr;WGPURenderPassEncoder pass_ = nullptr;WGPUTexture frame_texture_ = nullptr; ///< Acquired surface texture (released at end).diff --git a/src/esengine/renderer/webgpu/WebGPUMappings.hpp b/src/esengine/renderer/webgpu/WebGPUMappings.hppindex fc225ee9d..5fd08771a 100644--- a/src/esengine/renderer/webgpu/WebGPUMappings.hpp+++ b/src/esengine/renderer/webgpu/WebGPUMappings.hpp@@ -153,12 +153,19 @@ inline WGPUTextureFormat toWGPUTextureFormat(GfxPixelFormat fmt) {// (the GL backend's swizzle-free equivalent — expansion happens at upload).case GfxPixelFormat::RGB8: return WGPUTextureFormat_RGBA8Unorm;case GfxPixelFormat::RGBA8: return WGPUTextureFormat_RGBA8Unorm;+ case GfxPixelFormat::SRGB8_ALPHA8: return WGPUTextureFormat_RGBA8UnormSrgb;+ case GfxPixelFormat::RGBA16F: return WGPUTextureFormat_RGBA16Float;case GfxPixelFormat::DepthComponent24: return WGPUTextureFormat_Depth24Plus;case GfxPixelFormat::Depth24Stencil8: return WGPUTextureFormat_Depth24PlusStencil8;default: return WGPUTextureFormat_RGBA8Unorm;}}+/** Bytes per pixel of an uncompressed upload in @p fmt (RGB8 expands to RGBA8). */+inline u32 wgpuBytesPerPixel(GfxPixelFormat fmt) {+ return fmt == GfxPixelFormat::RGBA16F ? 8u : 4u;+}+inline WGPUTextureFormat toWGPUCompressedFormat(GfxCompressedFormat fmt) {switch (fmt) {case GfxCompressedFormat::ETC2_RGB8: return WGPUTextureFormat_ETC2RGB8Unorm;@@ -167,6 +174,9 @@ inline WGPUTextureFormat toWGPUCompressedFormat(GfxCompressedFormat fmt) {case GfxCompressedFormat::ASTC_8x8: return WGPUTextureFormat_ASTC8x8Unorm;case GfxCompressedFormat::S3TC_DXT1: return WGPUTextureFormat_BC1RGBAUnorm;case GfxCompressedFormat::S3TC_DXT5: return WGPUTextureFormat_BC3RGBAUnorm;+ case GfxCompressedFormat::ETC2_RGBA8_SRGB: return WGPUTextureFormat_ETC2RGBA8UnormSrgb;+ case GfxCompressedFormat::ASTC_4x4_SRGB: return WGPUTextureFormat_ASTC4x4UnormSrgb;+ case GfxCompressedFormat::S3TC_DXT5_SRGB: return WGPUTextureFormat_BC3RGBAUnormSrgb;default: return WGPUTextureFormat_Undefined;}}