@gjsify/webgl 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (149) hide show
  1. package/README.md +78 -6
  2. package/lib/esm/canvas-webgl-widget.js +116 -0
  3. package/lib/esm/html-canvas-element.js +31 -50
  4. package/lib/esm/index.js +7 -0
  5. package/lib/esm/index.spec.js +629 -120
  6. package/lib/esm/test-utils.js +87 -0
  7. package/lib/esm/test.js +5 -1
  8. package/lib/esm/utils.js +1 -1
  9. package/lib/esm/webgl-active-info.js +2 -2
  10. package/lib/esm/webgl-buffer.js +2 -3
  11. package/lib/esm/webgl-context-attributes.js +2 -3
  12. package/lib/esm/webgl-framebuffer.js +3 -4
  13. package/lib/esm/webgl-program.js +2 -3
  14. package/lib/esm/webgl-query.js +15 -0
  15. package/lib/esm/webgl-renderbuffer.js +2 -3
  16. package/lib/esm/webgl-rendering-context.js +105 -101
  17. package/lib/esm/webgl-sampler.js +15 -0
  18. package/lib/esm/webgl-shader-precision-format.js +2 -3
  19. package/lib/esm/webgl-shader.js +2 -3
  20. package/lib/esm/webgl-sync.js +15 -0
  21. package/lib/esm/webgl-texture.js +2 -3
  22. package/lib/esm/webgl-transform-feedback.js +15 -0
  23. package/lib/esm/webgl-uniform-location.js +2 -3
  24. package/lib/esm/webgl-vertex-array-object.js +21 -0
  25. package/lib/esm/webgl2-rendering-context.js +591 -0
  26. package/lib/esm/webgl2.spec.js +578 -0
  27. package/lib/types/canvas-webgl-widget.d.ts +509 -0
  28. package/lib/types/extensions/ext-blend-minmax.d.ts +2 -2
  29. package/lib/types/extensions/ext-texture-filter-anisotropic.d.ts +2 -2
  30. package/lib/types/extensions/oes-element-index-unit.d.ts +2 -2
  31. package/lib/types/extensions/oes-standard-derivatives.d.ts +2 -2
  32. package/lib/types/extensions/oes-texture-float-linear.d.ts +2 -2
  33. package/lib/types/extensions/oes-texture-float.d.ts +2 -2
  34. package/lib/types/extensions/stackgl-destroy-context.d.ts +3 -3
  35. package/lib/types/extensions/stackgl-resize-drawing-buffer.d.ts +3 -3
  36. package/lib/types/html-canvas-element.d.ts +19 -30
  37. package/lib/types/index.d.ts +7 -0
  38. package/lib/types/test-utils.d.ts +45 -0
  39. package/lib/types/types/extension.d.ts +2 -2
  40. package/lib/types/utils.d.ts +14 -15
  41. package/lib/types/webgl-active-info.d.ts +1 -2
  42. package/lib/types/webgl-buffer.d.ts +5 -6
  43. package/lib/types/webgl-context-attributes.d.ts +1 -2
  44. package/lib/types/webgl-framebuffer.d.ts +4 -5
  45. package/lib/types/webgl-program.d.ts +4 -5
  46. package/lib/types/webgl-query.d.ts +7 -0
  47. package/lib/types/webgl-renderbuffer.d.ts +4 -5
  48. package/lib/types/webgl-rendering-context.d.ts +15 -12
  49. package/lib/types/webgl-sampler.d.ts +7 -0
  50. package/lib/types/webgl-shader-precision-format.d.ts +1 -2
  51. package/lib/types/webgl-shader.d.ts +4 -5
  52. package/lib/types/webgl-sync.d.ts +7 -0
  53. package/lib/types/webgl-texture-unit.d.ts +3 -3
  54. package/lib/types/webgl-texture.d.ts +6 -7
  55. package/lib/types/webgl-transform-feedback.d.ts +7 -0
  56. package/lib/types/webgl-uniform-location.d.ts +3 -4
  57. package/lib/types/webgl-vertex-array-object.d.ts +9 -0
  58. package/lib/types/webgl-vertex-attribute.d.ts +5 -5
  59. package/lib/types/webgl2-rendering-context.d.ts +124 -0
  60. package/package.json +27 -26
  61. package/prebuilds/linux-aarch64/Gwebgl-0.1.typelib +0 -0
  62. package/prebuilds/linux-aarch64/libgwebgl.so +0 -0
  63. package/prebuilds/linux-x86_64/Gwebgl-0.1.typelib +0 -0
  64. package/prebuilds/linux-x86_64/libgwebgl.so +0 -0
  65. package/lib/cjs/@types/glsl-tokenizer/index.d.js +0 -0
  66. package/lib/cjs/extensions/ext-blend-minmax.js +0 -18
  67. package/lib/cjs/extensions/ext-texture-filter-anisotropic.js +0 -18
  68. package/lib/cjs/extensions/oes-element-index-unit.js +0 -14
  69. package/lib/cjs/extensions/oes-standard-derivatives.js +0 -17
  70. package/lib/cjs/extensions/oes-texture-float-linear.js +0 -14
  71. package/lib/cjs/extensions/oes-texture-float.js +0 -14
  72. package/lib/cjs/extensions/stackgl-destroy-context.js +0 -12
  73. package/lib/cjs/extensions/stackgl-resize-drawing-buffer.js +0 -12
  74. package/lib/cjs/html-canvas-element.js +0 -70
  75. package/lib/cjs/index.js +0 -18
  76. package/lib/cjs/index.spec.js +0 -146
  77. package/lib/cjs/linkable.js +0 -50
  78. package/lib/cjs/test.js +0 -3
  79. package/lib/cjs/types/constructor.js +0 -0
  80. package/lib/cjs/types/extension.js +0 -0
  81. package/lib/cjs/types/index.js +0 -5
  82. package/lib/cjs/types/typed-array.js +0 -0
  83. package/lib/cjs/types/webgl-constants.js +0 -0
  84. package/lib/cjs/types/webgl-context-attribute-options.js +0 -0
  85. package/lib/cjs/utils.js +0 -212
  86. package/lib/cjs/webgl-active-info.js +0 -10
  87. package/lib/cjs/webgl-buffer.js +0 -18
  88. package/lib/cjs/webgl-context-attributes.js +0 -24
  89. package/lib/cjs/webgl-drawing-buffer-wrapper.js +0 -10
  90. package/lib/cjs/webgl-framebuffer.js +0 -109
  91. package/lib/cjs/webgl-program.js +0 -26
  92. package/lib/cjs/webgl-renderbuffer.js +0 -24
  93. package/lib/cjs/webgl-rendering-context.js +0 -3343
  94. package/lib/cjs/webgl-shader-precision-format.js +0 -11
  95. package/lib/cjs/webgl-shader.js +0 -23
  96. package/lib/cjs/webgl-texture-unit.js +0 -12
  97. package/lib/cjs/webgl-texture.js +0 -22
  98. package/lib/cjs/webgl-uniform-location.js +0 -15
  99. package/lib/cjs/webgl-vertex-attribute.js +0 -147
  100. package/meson.build +0 -39
  101. package/src/test/app.vala +0 -60
  102. package/src/ts/@types/glsl-tokenizer/index.d.ts +0 -18
  103. package/src/ts/extensions/angle-instanced-arrays.ts.off +0 -232
  104. package/src/ts/extensions/ext-blend-minmax.ts +0 -18
  105. package/src/ts/extensions/ext-texture-filter-anisotropic.ts +0 -18
  106. package/src/ts/extensions/oes-element-index-unit.ts +0 -14
  107. package/src/ts/extensions/oes-standard-derivatives.ts +0 -17
  108. package/src/ts/extensions/oes-texture-float-linear.ts +0 -14
  109. package/src/ts/extensions/oes-texture-float.ts +0 -14
  110. package/src/ts/extensions/oes-vertex-array-object.ts.off +0 -128
  111. package/src/ts/extensions/stackgl-destroy-context.ts +0 -12
  112. package/src/ts/extensions/stackgl-resize-drawing-buffer.ts +0 -14
  113. package/src/ts/extensions/webgl-draw-buffers.ts.off +0 -107
  114. package/src/ts/html-canvas-element.ts +0 -98
  115. package/src/ts/index.spec.ts +0 -186
  116. package/src/ts/index.ts +0 -21
  117. package/src/ts/linkable.ts +0 -55
  118. package/src/ts/test.ts +0 -6
  119. package/src/ts/types/constructor.ts +0 -3
  120. package/src/ts/types/extension.ts +0 -3
  121. package/src/ts/types/index.ts +0 -26
  122. package/src/ts/types/typed-array.ts +0 -1
  123. package/src/ts/types/webgl-constants.ts +0 -300
  124. package/src/ts/types/webgl-context-attribute-options.ts +0 -12
  125. package/src/ts/utils.ts +0 -266
  126. package/src/ts/webgl-active-info.ts +0 -13
  127. package/src/ts/webgl-buffer.ts +0 -21
  128. package/src/ts/webgl-context-attributes.ts +0 -24
  129. package/src/ts/webgl-drawing-buffer-wrapper.ts +0 -10
  130. package/src/ts/webgl-framebuffer.ts +0 -133
  131. package/src/ts/webgl-program.ts +0 -30
  132. package/src/ts/webgl-renderbuffer.ts +0 -28
  133. package/src/ts/webgl-rendering-context.ts +0 -4050
  134. package/src/ts/webgl-shader-precision-format.ts +0 -12
  135. package/src/ts/webgl-shader.ts +0 -29
  136. package/src/ts/webgl-texture-unit.ts +0 -16
  137. package/src/ts/webgl-texture.ts +0 -27
  138. package/src/ts/webgl-uniform-location.ts +0 -18
  139. package/src/ts/webgl-vertex-attribute.ts +0 -169
  140. package/src/vala/handle-types.vala +0 -23
  141. package/src/vala/webgl-rendering-context-base.vala +0 -1265
  142. package/src/vala/webgl-rendering-context.vala +0 -265
  143. package/src/vapi/epoxy.vapi +0 -14558
  144. package/src/vapi/glesv2.vapi +0 -670
  145. package/test.gjs.js +0 -39972
  146. package/test.gjs.js.meta.json +0 -1
  147. package/tmp/.tsbuildinfo +0 -1
  148. package/tsconfig.json +0 -38
  149. package/tsconfig.types.json +0 -7
package/README.md CHANGED
@@ -1,14 +1,86 @@
1
1
  # @gjsify/webgl
2
2
 
3
- WebGL module for Gjs
3
+ GJS implementation of WebGL 1.0/2.0 using a custom Vala extension (gwebgl). Provides CanvasWebGLWidget extending Gtk.GLArea.
4
4
 
5
- ## Dependencies
5
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
6
6
 
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @gjsify/webgl
11
+ # or
12
+ yarn add @gjsify/webgl
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { CanvasWebGLWidget } from '@gjsify/webgl';
19
+
20
+ const widget = new CanvasWebGLWidget();
21
+ widget.installGlobals();
22
+
23
+ widget.onReady((canvas, gl) => {
24
+ gl.clearColor(0, 0, 0, 1);
25
+ gl.clear(gl.COLOR_BUFFER_BIT);
26
+ });
27
+
28
+ window.set_child(widget);
29
+ ```
30
+
31
+ ## Running GJS apps that use this package
32
+
33
+ This package ships prebuilt native libraries for supported platforms:
34
+
35
+ ```
36
+ prebuilds/
37
+ linux-x86_64/ libgwebgl.so + Gwebgl-0.1.typelib
38
+ linux-aarch64/ libgwebgl.so + Gwebgl-0.1.typelib
39
+ ```
40
+
41
+ Use the gjsify CLI to run your app — it automatically sets `LD_LIBRARY_PATH` and
42
+ `GI_TYPELIB_PATH` so GJS can find the native library:
43
+
44
+ ```bash
45
+ gjsify run dist/gjs.js
46
+ ```
47
+
48
+ To see what env vars are needed for running directly with `gjs`:
49
+
50
+ ```bash
51
+ gjsify info dist/gjs.js
52
+ # or for shell eval:
53
+ eval $(gjsify info --export)
54
+ gjs -m dist/gjs.js
7
55
  ```
8
- sudo dnf install libepoxy-devel
56
+
57
+ ## Building the native library locally
58
+
59
+ Requires: `meson`, `valac`, `gcc`, `libepoxy-devel`, `gtk4-devel`, `gdk-pixbuf2-devel`, `gobject-introspection-devel`
60
+
61
+ ```bash
62
+ # Fedora / RHEL
63
+ sudo dnf install meson vala gcc libepoxy-devel gtk4-devel gdk-pixbuf2-devel gobject-introspection-devel
64
+
65
+ # Build
66
+ yarn build:meson
67
+
68
+ # Build and copy to prebuilds/ for the current architecture
69
+ yarn build:prebuilds
9
70
  ```
10
71
 
72
+ ## Prebuilt binaries
73
+
74
+ Prebuilds for `linux-x86_64` and `linux-aarch64` are built automatically by CI
75
+ (`.github/workflows/prebuilds.yml`) when the Vala source changes and committed back
76
+ to the repository. They are included in the npm package via the `files` field.
77
+
11
78
  ## Inspirations and credits
12
- * https://github.com/realh/gwebgl
13
- * https://github.com/stackgl/headless-gl
14
- * https://github.com/Maia-Everett/valagl
79
+
80
+ - [realh/gwebgl](https://github.com/realh/gwebgl)
81
+ - [stackgl/headless-gl](https://github.com/stackgl/headless-gl)
82
+ - [Maia-Everett/valagl](https://github.com/Maia-Everett/valagl)
83
+
84
+ ## License
85
+
86
+ MIT
@@ -0,0 +1,116 @@
1
+ import GObject from "gi://GObject";
2
+ import GLib from "gi://GLib?version=2.0";
3
+ import Gtk from "gi://Gtk?version=4.0";
4
+ import { HTMLCanvasElement as OurHTMLCanvasElement } from "./html-canvas-element.js";
5
+ import { attachEventControllers } from "@gjsify/event-bridge";
6
+ const CanvasWebGLWidget = GObject.registerClass(
7
+ { GTypeName: "GjsifyCanvasWebGLWidget" },
8
+ class CanvasWebGLWidget2 extends Gtk.GLArea {
9
+ constructor(params) {
10
+ super(params);
11
+ this._canvas = null;
12
+ this._readyCallbacks = [];
13
+ this._renderTag = null;
14
+ this._tickCallbackId = null;
15
+ this._frameCallback = null;
16
+ // Time origin in microseconds (GLib monotonic clock).
17
+ // Both requestAnimationFrame timestamps and performance.now() are
18
+ // relative to this origin, matching the browser DOMHighResTimeStamp spec.
19
+ this._timeOrigin = GLib.get_monotonic_time();
20
+ this.set_use_es(true);
21
+ this.set_required_version(3, 2);
22
+ this.set_has_depth_buffer(true);
23
+ this.set_has_stencil_buffer(true);
24
+ attachEventControllers(this, () => this._canvas);
25
+ const initId = this.connect("render", () => {
26
+ this.disconnect(initId);
27
+ this.make_current();
28
+ this._canvas = new OurHTMLCanvasElement(this);
29
+ const gl = this._canvas.getContext("webgl");
30
+ if (gl) {
31
+ for (const cb of this._readyCallbacks) {
32
+ cb(this._canvas, gl);
33
+ }
34
+ this._readyCallbacks = [];
35
+ }
36
+ return true;
37
+ });
38
+ this.connect("unrealize", () => {
39
+ if (this._renderTag !== null) {
40
+ this.disconnect(this._renderTag);
41
+ this._renderTag = null;
42
+ }
43
+ if (this._tickCallbackId !== null) {
44
+ this.remove_tick_callback(this._tickCallbackId);
45
+ this._tickCallbackId = null;
46
+ }
47
+ this._canvas = null;
48
+ });
49
+ }
50
+ /** The HTMLCanvasElement wrapping this GLArea. Available after the first render. */
51
+ get canvas() {
52
+ return this._canvas;
53
+ }
54
+ /**
55
+ * Registers a callback to be called once the WebGL context is ready.
56
+ * If the context is already available, the callback fires synchronously.
57
+ */
58
+ onReady(cb) {
59
+ if (this._canvas) {
60
+ const gl = this._canvas.getContext("webgl");
61
+ if (gl) {
62
+ cb(this._canvas, gl);
63
+ return;
64
+ }
65
+ }
66
+ this._readyCallbacks.push(cb);
67
+ }
68
+ /**
69
+ * @deprecated Use `onReady()` instead.
70
+ */
71
+ onWebGLReady(cb) {
72
+ this.onReady(cb);
73
+ }
74
+ /**
75
+ * Schedules a single animation frame callback, matching the browser `requestAnimationFrame` API.
76
+ * Backed by GTK frame clock (vsync-synced) + the GLArea render signal.
77
+ * Returns 0 (handle — cancel not yet implemented).
78
+ */
79
+ requestAnimationFrame(cb) {
80
+ this._frameCallback = cb;
81
+ if (this._tickCallbackId === null) {
82
+ this._tickCallbackId = this.add_tick_callback((_widget, _frameClock) => {
83
+ this._tickCallbackId = null;
84
+ if (this._renderTag === null) {
85
+ this._renderTag = this.connect("render", () => {
86
+ this.disconnect(this._renderTag);
87
+ this._renderTag = null;
88
+ const time = (GLib.get_monotonic_time() - this._timeOrigin) / 1e3;
89
+ this._frameCallback?.(time);
90
+ return true;
91
+ });
92
+ }
93
+ this.queue_render();
94
+ return GLib.SOURCE_REMOVE;
95
+ });
96
+ }
97
+ this.queue_render();
98
+ return 0;
99
+ }
100
+ /**
101
+ * Sets browser globals (`requestAnimationFrame`, `performance`) so that
102
+ * browser-targeted code (e.g. Three.js) works unchanged on GJS.
103
+ */
104
+ installGlobals() {
105
+ globalThis.requestAnimationFrame = (cb) => this.requestAnimationFrame(cb);
106
+ const timeOrigin = this._timeOrigin;
107
+ globalThis.performance = {
108
+ now: () => (GLib.get_monotonic_time() - timeOrigin) / 1e3,
109
+ timeOrigin: Date.now()
110
+ };
111
+ }
112
+ }
113
+ );
114
+ export {
115
+ CanvasWebGLWidget
116
+ };
@@ -1,70 +1,51 @@
1
- import { warnNotImplemented, notImplemented } from "@gjsify/utils";
2
- import { GjsifyWebGLRenderingContext } from "./webgl-rendering-context.js";
3
- class GjsifyHTMLCanvasElement {
1
+ import { HTMLCanvasElement as BaseHTMLCanvasElement } from "@gjsify/dom-elements";
2
+ import { WebGLRenderingContext as OurWebGLRenderingContext } from "./webgl-rendering-context.js";
3
+ import { WebGL2RenderingContext as OurWebGL2RenderingContext } from "./webgl2-rendering-context.js";
4
+ class HTMLCanvasElement extends BaseHTMLCanvasElement {
4
5
  constructor(gtkGlArea) {
6
+ super();
5
7
  this.gtkGlArea = gtkGlArea;
6
8
  }
7
- _getGlArea() {
8
- return this.gtkGlArea;
9
+ /** Width from the GTK GLArea allocated size (overrides DOM attr-backed getter). */
10
+ get width() {
11
+ return this.gtkGlArea.get_allocated_width();
9
12
  }
10
- /** Gets the height of a canvas element on a document. */
13
+ set width(_width) {
14
+ }
15
+ /** Height from the GTK GLArea allocated size (overrides DOM attr-backed getter). */
11
16
  get height() {
12
17
  return this.gtkGlArea.get_allocated_height();
13
18
  }
14
- get clientHeight() {
15
- return this.height;
16
- }
17
- /** Sets the height of a canvas element on a document. */
18
19
  set height(_height) {
19
- warnNotImplemented("GjsifyHTMLCanvasElement.set_height");
20
- }
21
- /** Gets the width of a canvas element on a document. */
22
- get width() {
23
- return this.gtkGlArea.get_allocated_width();
24
- }
25
- /** Sets the width of a canvas element on a document. */
26
- set width(_width) {
27
- warnNotImplemented("GjsifyHTMLCanvasElement.set_width");
28
20
  }
29
21
  get clientWidth() {
30
22
  return this.width;
31
23
  }
32
- captureStream(_frameRequestRate) {
33
- notImplemented("HTMLCanvasElement.captureStream");
34
- return new MediaStream();
35
- }
36
- getContext(contextId, options) {
37
- switch (contextId) {
38
- case "webgl":
39
- if (this._webgl) {
40
- return this._webgl;
41
- }
42
- this._webgl = new GjsifyWebGLRenderingContext(this, options);
43
- return this._webgl;
44
- default:
45
- warnNotImplemented(`GjsifyHTMLCanvasElement.getContext("${contextId}")`);
46
- }
47
- return null;
24
+ get clientHeight() {
25
+ return this.height;
48
26
  }
49
- toBlob(_callback, _type, _quality) {
50
- notImplemented("HTMLCanvasElement.toBlob");
27
+ /** Returns the underlying Gtk.GLArea. Used by WebGLRenderingContext for GLSL version detection. */
28
+ getGlArea() {
29
+ return this.gtkGlArea;
51
30
  }
52
31
  /**
53
- * Returns the content of the current canvas as an image that you can use as a source for another canvas or an HTML element.
54
- * @param _type The standard MIME type for the image format to return. If you do not specify this parameter, the default value is a PNG format image.
32
+ * Returns a WebGL rendering context backed by the underlying Gtk.GLArea.
33
+ * 'webgl' and 'experimental-webgl' return a WebGLRenderingContext (WebGL 1.0).
34
+ * 'webgl2' returns a WebGL2RenderingContext (WebGL 2.0).
35
+ * Other context types emit a warning and return null.
55
36
  */
56
- toDataURL(_type, _quality) {
57
- notImplemented("HTMLCanvasElement.toDataURL");
58
- return "";
59
- }
60
- addEventListener(_type, _listener, _options) {
61
- notImplemented("HTMLCanvasElement.addEventListener");
62
- }
63
- removeEventListener(_type, _listener, _options) {
64
- notImplemented("HTMLCanvasElement.removeEventListener");
37
+ getContext(contextId, options) {
38
+ if (contextId === "webgl" || contextId === "experimental-webgl") {
39
+ this._webgl ??= new OurWebGLRenderingContext(this, options);
40
+ return this._webgl;
41
+ }
42
+ if (contextId === "webgl2") {
43
+ this._webgl2 ??= new OurWebGL2RenderingContext(this, options);
44
+ return this._webgl2;
45
+ }
46
+ return super.getContext(contextId, options);
65
47
  }
66
48
  }
67
49
  export {
68
- GjsifyHTMLCanvasElement,
69
- GjsifyHTMLCanvasElement as HTMLCanvasElement
50
+ HTMLCanvasElement
70
51
  };
package/lib/esm/index.js CHANGED
@@ -2,6 +2,7 @@ import "@girs/gjs";
2
2
  import "@girs/gio-2.0";
3
3
  import "@girs/gtk-4.0";
4
4
  export * from "./html-canvas-element.js";
5
+ export * from "./canvas-webgl-widget.js";
5
6
  export * from "./webgl-active-info.js";
6
7
  export * from "./webgl-buffer.js";
7
8
  export * from "./webgl-context-attributes.js";
@@ -9,8 +10,14 @@ export * from "./webgl-drawing-buffer-wrapper.js";
9
10
  export * from "./webgl-framebuffer.js";
10
11
  export * from "./webgl-program.js";
11
12
  export * from "./webgl-renderbuffer.js";
13
+ export * from "./webgl-query.js";
12
14
  export * from "./webgl-rendering-context.js";
15
+ export * from "./webgl-sampler.js";
13
16
  export * from "./webgl-shader-precision-format.js";
17
+ export * from "./webgl-sync.js";
18
+ export * from "./webgl-transform-feedback.js";
19
+ export * from "./webgl-vertex-array-object.js";
20
+ export * from "./webgl2-rendering-context.js";
14
21
  export * from "./webgl-shader.js";
15
22
  export * from "./webgl-texture-unit.js";
16
23
  export * from "./webgl-texture.js";