Skip to content

WebGPU doesn't implement surface viewFormats #22939

@jankrassnigg

Description

@jankrassnigg

In src\library_webgpu.js there is this big block:

#if ASSERTIONS
    var viewFormatCount = {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.viewFormatCount) }}};
    var viewFormats = {{{ makeGetValue('config', C_STRUCTS.WGPUSurfaceConfiguration.viewFormats, '*') }}};
    assert(viewFormatCount === 0 && viewFormats === 0, "TODO: Support viewFormats.");
    var alphaMode = {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.alphaMode) }}};
    assert(alphaMode === {{{ gpu.CompositeAlphaMode.Auto }}} ||
      alphaMode === {{{ gpu.CompositeAlphaMode.Opaque }}},
      "TODO: Support WGPUCompositeAlphaMode_Premultiplied.");
    assert({{{ gpu.PresentMode.Fifo }}} ===
      {{{ gpu.makeGetU32('config', C_STRUCTS.WGPUSurfaceConfiguration.presentMode) }}});
#endif

The TODO: Support viewFormats assertion is of particular interest to me. Without specifying compatible view formats for the backbuffer, it is not possible to use sRGB textures, which means I can't do Gamma correct rendering in the browser.

This should only be a matter of passing the values from C++ to JS,

In fact, this is already implemented during texture creation in wgpuDeviceCreateTexture:

var viewFormatCount = {{{ gpu.makeGetU32('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormatCount) }}};
if (viewFormatCount) {
  var viewFormatsPtr = {{{ makeGetValue('descriptor', C_STRUCTS.WGPUTextureDescriptor.viewFormats, '*') }}};
  // viewFormatsPtr pointer to an array of TextureFormat which is an enum of size uint32_t
  desc["viewFormats"] = Array.from({{{ makeHEAPView('32', 'viewFormatsPtr', `viewFormatsPtr + viewFormatCount * 4`) }}},
    function(format) { return WebGPU.TextureFormat[format]; });
}

I assume the fix would be very similar, but I'm no expert when it comes to JavaScript.

Version of emscripten/emsdk:

EMCC version:
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.65 (7f8a05d)
clang version 20.0.0git (https:/github.com/llvm/llvm-project 547917aebd1e79a8929b53f0ddf3b5185ee4df74)
Target: wasm32-unknown-emscripten
Thread model: posix

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions