Skip to content

Default descriptor initialization (and discussion of C vs C++ callbacks) #158

@mrshannon

Description

@mrshannon

Many of the descriptors in WebGPU have default values, and more importantly default values which are not 0 and so a simple memset or zero initialization is not appropriate.

An example of what does not work

WGPUTextureDataLayout TextureDataLayout = {0};
TextureDataLayout.bytesPerRow = 512;

In this case rowsPerImage = 0 instead of WGPU_COPY_STRIDE_UNDEFINED.

Therefore, I propose adding default initialization defines to webgpu.h. While it is possible for each user to create their own, there are some benefits to implementing this in webgpu.h:

  • Adding fields later with default values to descriptors does not introduce undefined behavior in existing code.
  • It is possible to change default values later without breaking existing code (while WGPU_COPY_STRIDE_UNDEFINED fixes this, many defaults do not have a define as they are simply 0).
  • One source of truth

One way to implement this is to add defines for each descriptor:

#define WGPUTextureDataLayout_DEFAULT { NULL, 0, WGPU_COPY_STRIDE_UNDEFINED, WGPU_COPY_STRIDE_UNDEFINED }

This would allow users to write:

WGPUTextureDataLayout TextureDataLayout = WGPUTextureDataLayout_DEFAULT;
TextureDataLayout.bytesPerRow = 512;

TODO list:

Metadata

Metadata

Assignees

No one assigned

    Labels

    has resolutionIssue is resolved, just needs to be done

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions