Skip to content

Which corner is the origin of a copyExternalImageToTexture? #2110

Closed

Description

@kainino0x

@shaoboyan and I were discussing this, and we realized it's unspecified which corner of an external image is the origin (0,0) when doing a copyExternalImageToTexture operation. This is ambiguous in particular because inside WebGL, the origin of the canvas is the bottom-left.

I'm strongly inclined to say it is (at least by default) the top-left corner of the source image/canvas. However we could have an exception for WebGL that puts it at the bottom-left to match WebGL's own coordinate space.

Should we make it configurable? Prior art does so - though primarily due to the fact that WebGL is inverted relative to everything else.

Activity

  1. added this to the V1.0 milestone on Sep 15, 2021
  2. kainino0x commented on Sep 15, 2021

    @kainino0x
    ContributorAuthor
  3. kdashg commented on Sep 21, 2021

    @kdashg
    Contributor

    We should ensure we have a fastpath that can reasonably do a resource->resource copy from WebGL without requiring a flip. If this cannot simply be the default, then we should add configuration options to allow it.

    Generally speaking anything that accepts a HTMLCanvasElement should consider that canvas's coordinates to be y-down (with 0,0 at top left). (and similar for ImageBitmap)

  4. kenrussell commented on Oct 1, 2021

    @kenrussell
    Member

    The imageOrientation in ImageBitmapOptions was really a concession to WebGL / OpenGL, which defines the lower-left corner of textures as the origin. Note that if the flipY option is used, then the ImageBitmap will be vertically flipped when drawn normally to a 2D canvas context. This parameter was added to give web apps the highest performance when decoding images (JPEGs, PNGs, etc.) to ImageBitmaps and uploading to WebGL textures, since the web app knows how the ImageBitmap is intended to be consumed.

    Considering uploading WebGL-rendered canvases to WebGPU textures: since a copy is involved, and it will be done on the GPU, it seems fine to me to just internalize the needed Y-flip and not expose this detail to web applications. The only other reason control over the vertical flip would be needed is for ImageBitmaps, and it doesn't seem necessary to me to support that; the web app should just not flip its ImageBitmaps when it intends to import them to WebGPU. All other external image sources on the web use an upper-left origin, like WebGPU's textures, so no vertical flip is needed.

  5. kainino0x commented on Oct 1, 2021

    @kainino0x
    ContributorAuthor

    since a copy is involved, and it will be done on the GPU, it seems fine to me to just internalize the needed Y-flip

    I don't know much about how these flips are implemented usually. It's free to flip in a fullscreen quad (and probably in a blit), but I think using copy commands it may require one copy per row which could be expensive.

    There's one other reason I could think of needing a flip in webgl->webgpu, and that's if you have some interop between them. In principle the app could just sample the texture flipped, or output flipped data from WebGL, but it could be difficult in some apps - e.g. if they followed it with a copyTextureToBuffer then they couldn't flip in sampling.

  6. kenrussell commented on Oct 1, 2021

    @kenrussell
    Member

    Any interop between WebGL and WebGPU textures (specifically, to bring WebGL textures more directly into WebGPU) would require at least one copy on basically all platforms, because WebGL textures aren't allocated specially - they're not backed by surfaces which would enable WebGPU to sample them directly. The y-flip could be done during that copy, hiding the semantic differences between the APIs.

  7. kainino0x commented on Oct 1, 2021

    @kainino0x
    ContributorAuthor

    Ah, I'm thinking only about WebGL canvases. I don't think we're considering any interop with WebGLTexture.

  8. kainino0x commented on Oct 4, 2021

    @kainino0x
    ContributorAuthor

    Meeting resolution: No need to do this right now, cost to flip from webgl canvases (and opengl backed resources) should be minimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions