Skip to content

Add js/wasm (WebGL2) backend#8

Open
fisherevans wants to merge 2 commits into
gopxl:mainfrom
fisherevans:wasm-support
Open

Add js/wasm (WebGL2) backend#8
fisherevans wants to merge 2 commits into
gopxl:mainfrom
fisherevans:wasm-support

Conversation

@fisherevans

@fisherevans fisherevans commented Apr 21, 2026

Copy link
Copy Markdown

Summary

Adds a parallel WebGL2 implementation of the glhf primitives so consumers can build for GOOS=js GOARCH=wasm without changing call-sites. Everything is keyed on the js && wasm build tag; existing files gain //go:build !js so desktop behavior is untouched.

  • New gl_wasm.go — thin wrappers around the syscall/js WebGL2 context and enum map.
  • New glhf_wasm.go — WebGL2 implementations of Shader, Texture, VertexSlice, VertexArray, Frame, state setters (Clear, Bounds, blend funcs), plus a #version 330 core#version 300 es preprocessor so consumers can compile their existing shader source unchanged. The preprocessor is covered by glhf_wasm_test.go.
  • orphan.go (desktop build) grows additional helpers downstream consumers were reaching past glhf for:
    • BlendFuncSeparate, BlendEquation — needed by pixel's ComposeScreen path.
    • ActiveTexture(unit int) — needed by pixel's sampler-uniform path.
    • Four new blend factor constants: SrcColor, DstColor, OneMinusSrcColor, OneMinusDstColor — needed by ComposeMultiply and friends.
  • doc.go and README.md have a new section describing the WASM backend (SetContext usage, GLSL adaptation, CLAMP_TO_BORDERCLAMP_TO_EDGE substitution).
  • examples/demo/main.go is tagged !js since it imports GLFW.

Related PRs

Test plan

  • Desktop builds unchanged (go build ./...).
  • GOOS=js GOARCH=wasm go build ./... succeeds; go test -c compiles the new test file cleanly.
  • Exercised end-to-end: textured quads render in a browser via the downstream pixel WASM backend.

Parallel WebGL2 implementation of Shader, Texture, VertexSlice, Frame,
and VertexArray under the `js && wasm` build tag; existing files now
tagged `!js` so desktop builds are untouched. Includes a GLSL 330 core
to 300 es preprocessor so consumers can run their existing shaders in
the browser unchanged.

Also adds `BlendFuncSeparate` and `BlendEquation` helpers needed by
downstream pixel consumers.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
… test

- examples/demo imports GLFW and broke GOOS=js GOARCH=wasm go build ./...
  (the advertised build test). Tag it !js so the example is desktop-only.
- Document the SetContext / shader-preprocessor behavior under WebGL2 in
  the README and package doc so users building for the browser know the
  required init order and the GLSL 330 -> 300 es adaptation.
- Add a unit test for preprocessShaderForES300. The rest of the WebGL2
  backend still needs a real context to exercise, but the preprocessor
  is pure Go and worth pinning down.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Comment thread glhf_wasm.go

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind breaking this file out into its component files (shader_wasm, frame_wasm, etc)? I appreciate trying to keep the wasm stuff contained, but I think it'll be easier to review/maintain going forward (and more easily shows why the frame.go, etc have the !js build flag

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants