@babylonjs/lite-gl 1.23.0 → 1.24.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.
- package/CHANGELOG.md +12 -0
- package/README.md +98 -2
- package/context.d.ts +5 -0
- package/context.js +8 -0
- package/context.js.map +1 -1
- package/depth-stencil.d.ts +1 -14
- package/depth-stencil.js.map +1 -1
- package/effect-renderer.d.ts +1 -11
- package/effect-renderer.js.map +1 -1
- package/effect.d.ts +5 -27
- package/effect.js +22 -0
- package/effect.js.map +1 -1
- package/family.d.ts +470 -0
- package/family.js +14 -0
- package/family.js.map +1 -0
- package/index.d.ts +8 -6
- package/index.js +3 -3
- package/index.js.map +1 -1
- package/mesh.d.ts +4 -1
- package/mesh.js +10 -3
- package/mesh.js.map +1 -1
- package/native/command-stream.d.ts +1 -0
- package/native/command-stream.js +135 -0
- package/native/command-stream.js.map +1 -0
- package/native/context.d.ts +26 -0
- package/native/context.js +89 -0
- package/native/context.js.map +1 -0
- package/native/effect.d.ts +44 -0
- package/native/effect.js +276 -0
- package/native/effect.js.map +1 -0
- package/native/family.d.ts +75 -0
- package/native/family.js +2 -0
- package/native/family.js.map +1 -0
- package/native/identity.d.ts +11 -0
- package/native/identity.js +2 -0
- package/native/identity.js.map +1 -0
- package/native/index.d.ts +25 -0
- package/native/index.js +7 -0
- package/native/index.js.map +1 -0
- package/native/mesh.d.ts +53 -0
- package/native/mesh.js +137 -0
- package/native/mesh.js.map +1 -0
- package/native/render-target.d.ts +35 -0
- package/native/render-target.js +159 -0
- package/native/render-target.js.map +1 -0
- package/native/runtime.d.ts +1 -0
- package/native/runtime.js +12 -0
- package/native/runtime.js.map +1 -0
- package/native/state.d.ts +19 -0
- package/native/state.js +104 -0
- package/native/state.js.map +1 -0
- package/native/texture.d.ts +37 -0
- package/native/texture.js +86 -0
- package/native/texture.js.map +1 -0
- package/package.json +11 -3
- package/render-target.d.ts +7 -0
- package/render-target.js +38 -0
- package/render-target.js.map +1 -1
- package/scissor.d.ts +2 -2
- package/scissor.js +2 -2
- package/scissor.js.map +1 -1
- package/state.d.ts +1 -0
- package/state.js +2 -5
- package/state.js.map +1 -1
- package/text/text-renderer.js.map +1 -1
- package/web-family.d.ts +87 -0
- package/web-family.js +2 -0
- package/web-family.js.map +1 -0
package/native/mesh.js
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/** Uploads immutable float vertex data. */
|
|
2
|
+
export function createVertexBuffer(engine, data) {
|
|
3
|
+
const fields = {
|
|
4
|
+
byteLength: data.byteLength,
|
|
5
|
+
_handle: engine._native.createVertexBuffer(data.buffer, data.byteOffset, data.byteLength, false),
|
|
6
|
+
_index: false,
|
|
7
|
+
_engine: engine,
|
|
8
|
+
_disposed: false,
|
|
9
|
+
};
|
|
10
|
+
return fields;
|
|
11
|
+
}
|
|
12
|
+
/** Uploads immutable index data. */
|
|
13
|
+
export function createIndexBuffer(engine, data) {
|
|
14
|
+
const fields = {
|
|
15
|
+
count: data.length,
|
|
16
|
+
_handle: engine._native.createIndexBuffer(data.buffer, data.byteOffset, data.byteLength, data.BYTES_PER_ELEMENT === 4, false),
|
|
17
|
+
_index: true,
|
|
18
|
+
_engine: engine,
|
|
19
|
+
_disposed: false,
|
|
20
|
+
};
|
|
21
|
+
return fields;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Records a mesh's attribute and index bindings independently of shader
|
|
25
|
+
* ownership. Attribute names the effect did not declare are resolved from its
|
|
26
|
+
* program here, once. Throws before any host call for a disposed engine, or for
|
|
27
|
+
* an effect or buffer of another engine or one that has been disposed.
|
|
28
|
+
*/
|
|
29
|
+
export function createMeshVao(engine, vertexBuffers, indexBuffer, effect) {
|
|
30
|
+
if (engine._disposed || !isUsable(engine, effect) || !isUsable(engine, indexBuffer) || !vertexBuffers.every((binding) => isUsable(engine, binding.buffer))) {
|
|
31
|
+
throw new Error("Native Lite GL createMeshVao requires an effect and buffers of this engine that have not been disposed.");
|
|
32
|
+
}
|
|
33
|
+
const handle = engine._native.createVertexArray();
|
|
34
|
+
const fields = { indexCount: indexBuffer.count, _handle: handle, _engine: engine, _disposed: false };
|
|
35
|
+
const mesh = fields;
|
|
36
|
+
try {
|
|
37
|
+
engine._native.recordIndexBuffer(handle, indexBuffer._handle);
|
|
38
|
+
for (const binding of vertexBuffers) {
|
|
39
|
+
let stride = 0;
|
|
40
|
+
if (binding.computeStride) {
|
|
41
|
+
for (const attribute of binding.attributes) {
|
|
42
|
+
stride += attribute.size * 4;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
for (const attribute of binding.attributes) {
|
|
46
|
+
const location = getAttributeLocation(engine, effect, attribute.name);
|
|
47
|
+
if (location >= 0) {
|
|
48
|
+
engine._native.recordVertexBuffer(handle, binding.buffer._handle, location, attribute.offset ?? 0, stride || attribute.size * 4, attribute.size, engine._runtime.Engine.ATTRIB_TYPE_FLOAT, false, attribute.divisor);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return mesh;
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
disposeMeshVao(mesh);
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function isUsable(engine, resource) {
|
|
60
|
+
return resource._engine === engine && !resource._disposed;
|
|
61
|
+
}
|
|
62
|
+
function getAttributeLocation(engine, effect, name) {
|
|
63
|
+
let location = effect._attributes[name];
|
|
64
|
+
if (location === undefined) {
|
|
65
|
+
location = engine._native.getAttributes(effect._program, [name])[0] ?? -1;
|
|
66
|
+
effect._attributes[name] = location;
|
|
67
|
+
}
|
|
68
|
+
return location;
|
|
69
|
+
}
|
|
70
|
+
/** Selects recorded geometry without repeating an unchanged native binding. No-op for disposed geometry or engine. */
|
|
71
|
+
export function bindMeshVao(mesh) {
|
|
72
|
+
const engine = mesh._engine;
|
|
73
|
+
if (mesh._disposed || engine._disposed) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (engine._vertexArray !== mesh._handle) {
|
|
77
|
+
const commands = engine._commands;
|
|
78
|
+
commands.start(engine._runtime.Engine.COMMAND_BINDVERTEXARRAY);
|
|
79
|
+
commands.handle(mesh._handle);
|
|
80
|
+
commands.finish();
|
|
81
|
+
engine._vertexArray = mesh._handle;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Draws a triangle range from recorded composition geometry; the range defaults
|
|
86
|
+
* to the indices from `indexStart` to the end and must lie within the mesh.
|
|
87
|
+
* Native composition does not instance. Draws nothing without a current
|
|
88
|
+
* program or while the scissor rectangle is empty.
|
|
89
|
+
*/
|
|
90
|
+
export function drawMesh(mesh, _instanceCount = 0, indexStart = 0, indexCount = mesh.indexCount - indexStart) {
|
|
91
|
+
if (!Number.isInteger(indexStart) || !Number.isInteger(indexCount) || indexStart < 0 || indexCount < 0 || indexStart + indexCount > mesh.indexCount) {
|
|
92
|
+
throw new RangeError(`Native Lite GL drawMesh range (start ${indexStart}, count ${indexCount}) must be whole indices within the mesh's ${mesh.indexCount}.`);
|
|
93
|
+
}
|
|
94
|
+
const engine = mesh._engine;
|
|
95
|
+
if (engine._disposed || mesh._disposed || engine._program === null || engine._scissorEmpty) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
bindMeshVao(mesh);
|
|
99
|
+
const commands = engine._commands;
|
|
100
|
+
commands.start(engine._runtime.Engine.COMMAND_DRAWINDEXED);
|
|
101
|
+
commands.uint(0);
|
|
102
|
+
commands.uint(indexStart);
|
|
103
|
+
commands.uint(indexCount);
|
|
104
|
+
commands.finish();
|
|
105
|
+
}
|
|
106
|
+
/** Releases owned vertex or index storage; after engine disposal it only marks the buffer disposed. */
|
|
107
|
+
export function disposeBuffer(buffer) {
|
|
108
|
+
if (!buffer._disposed) {
|
|
109
|
+
buffer._disposed = true;
|
|
110
|
+
const engine = buffer._engine;
|
|
111
|
+
if (engine._disposed) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const commands = engine._commands;
|
|
115
|
+
commands.start(buffer._index ? engine._runtime.Engine.COMMAND_DELETEINDEXBUFFER : engine._runtime.Engine.COMMAND_DELETEVERTEXBUFFER);
|
|
116
|
+
commands.handle(buffer._handle);
|
|
117
|
+
commands.finish();
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** Releases recorded geometry without releasing its caller-owned buffers or shader; after engine disposal it only marks the geometry disposed. */
|
|
121
|
+
export function disposeMeshVao(mesh) {
|
|
122
|
+
if (!mesh._disposed) {
|
|
123
|
+
mesh._disposed = true;
|
|
124
|
+
const engine = mesh._engine;
|
|
125
|
+
if (engine._disposed) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const commands = engine._commands;
|
|
129
|
+
commands.start(engine._runtime.Engine.COMMAND_DELETEVERTEXARRAY);
|
|
130
|
+
commands.handle(mesh._handle);
|
|
131
|
+
commands.finish();
|
|
132
|
+
if (engine._vertexArray === mesh._handle) {
|
|
133
|
+
engine._vertexArray = null;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=mesh.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mesh.js","sourceRoot":"","sources":["../../src/native/mesh.ts"],"names":[],"mappings":"AA0DA,2CAA2C;AAC3C,MAAM,UAAU,kBAAkB,CAAC,MAAuB,EAAE,IAAkB;IAC1E,MAAM,MAAM,GAAyC;QACjD,UAAU,EAAE,IAAI,CAAC,UAAU;QAC3B,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,KAAK,CAAC;QAChG,MAAM,EAAE,KAAK;QACb,OAAO,EAAE,MAAM;QACf,SAAS,EAAE,KAAK;KACnB,CAAC;IACF,OAAO,MAAwB,CAAC;AACpC,CAAC;AAED,oCAAoC;AACpC,M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JAAkJ;AAClJ,MAAM,UAAU,cAAc,CAAC,IAAe;IAC1C,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;QAClB,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QACtB,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC;QAC5B,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;YACnB,OAAO;QACX,CAAC;QACD,MAAM,QAAQ,GAAG,MAAM,CAAC,SAAS,CAAC;QAClC,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,yBAAyB,CAAC,CAAC;QACjE,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC9B,QAAQ,CAAC,MAAM,EAAE,CAAC;QAClB,IAAI,MAAM,CAAC,YAAY,KAAK,IAAI,CAAC,OAAO,EAAE,CAAC;YACvC,MAAM,CAAC,YAAY,GAAG,IAAI,CAAC;QAC/B,CAAC;IACL,CAAC;AACL,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\nimport type { GLEffect } from \"./effect.js\";\nimport type { NativeResource, NativeResourceFields } from \"./identity.js\";\nimport type { NativeHandle } from \"./runtime.js\";\n\n/** Owned immutable float vertex storage. */\nexport interface GLVertexBuffer extends NativeResource<\"vertexBuffer\"> {\n readonly byteLength: number;\n /** @internal */\n readonly _handle: NativeHandle;\n /** @internal */\n readonly _index: false;\n /** @internal The engine that owns this buffer. */\n readonly _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n}\n\n/** Owned immutable index storage. */\nexport interface GLIndexBuffer extends NativeResource<\"indexBuffer\"> {\n readonly count: number;\n /** @internal */\n readonly _handle: NativeHandle;\n /** @internal */\n readonly _index: true;\n /** @internal The engine that owns this buffer. */\n readonly _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n}\n\n/** A non-instanced float attribute in composition geometry. */\nexport interface GLAttributeDescriptor {\n name: string;\n size: number;\n offset?: number;\n /** Lite GL treats an omitted divisor as instanced, so per-vertex attributes state `0`. */\n divisor: 0;\n}\n\n/** Attribute layout supplied by one vertex buffer. */\nexport interface GLMeshVertexBuffer {\n buffer: GLVertexBuffer;\n attributes: readonly GLAttributeDescriptor[];\n computeStride?: boolean;\n}\n\n/** Recorded composition geometry; the caller retains ownership of its buffers. */\nexport interface GLMeshVao extends NativeResource<\"meshVao\"> {\n readonly indexCount: number;\n /** @internal */\n readonly _handle: NativeHandle;\n /** @internal The engine that owns this vertex array. */\n readonly _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n}\n\n/** Uploads immutable float vertex data. */\nexport function createVertexBuffer(engine: GLEngineContext, data: Float32Array): GLVertexBuffer {\n const fields: NativeResourceFields<GLVertexBuffer> = {\n byteLength: data.byteLength,\n _handle: engine._native.createVertexBuffer(data.buffer, data.byteOffset, data.byteLength, false),\n _index: false,\n _engine: engine,\n _disposed: false,\n };\n return fields as GLVertexBuffer;\n}\n\n/** Uploads immutable index data. */\nexport function createIndexBuffer(engine: GLEngineContext, data: Uint16Array | Uint32Array): GLIndexBuffer {\n const fields: NativeResourceFields<GLIndexBuffer> = {\n count: data.length,\n _handle: engine._native.createIndexBuffer(data.buffer, data.byteOffset, data.byteLength, data.BYTES_PER_ELEMENT === 4, false),\n _index: true,\n _engine: engine,\n _disposed: false,\n };\n return fields as GLIndexBuffer;\n}\n\n/**\n * Records a mesh's attribute and index bindings independently of shader\n * ownership. Attribute names the effect did not declare are resolved from its\n * program here, once. Throws before any host call for a disposed engine, or for\n * an effect or buffer of another engine or one that has been disposed.\n */\nexport function createMeshVao(engine: GLEngineContext, vertexBuffers: readonly GLMeshVertexBuffer[], indexBuffer: GLIndexBuffer, effect: GLEffect): GLMeshVao {\n if (engine._disposed || !isUsable(engine, effect) || !isUsable(engine, indexBuffer) || !vertexBuffers.every((binding) => isUsable(engine, binding.buffer))) {\n throw new Error(\"Native Lite GL createMeshVao requires an effect and buffers of this engine that have not been disposed.\");\n }\n const handle = engine._native.createVertexArray();\n const fields: NativeResourceFields<GLMeshVao> = { indexCount: indexBuffer.count, _handle: handle, _engine: engine, _disposed: false };\n const mesh = fields as GLMeshVao;\n try {\n engine._native.recordIndexBuffer(handle, indexBuffer._handle);\n for (const binding of vertexBuffers) {\n let stride = 0;\n if (binding.computeStride) {\n for (const attribute of binding.attributes) {\n stride += attribute.size * 4;\n }\n }\n for (const attribute of binding.attributes) {\n const location = getAttributeLocation(engine, effect, attribute.name);\n if (location >= 0) {\n engine._native.recordVertexBuffer(\n handle,\n binding.buffer._handle,\n location,\n attribute.offset ?? 0,\n stride || attribute.size * 4,\n attribute.size,\n engine._runtime.Engine.ATTRIB_TYPE_FLOAT,\n false,\n attribute.divisor\n );\n }\n }\n }\n return mesh;\n } catch (error) {\n disposeMeshVao(mesh);\n throw error;\n }\n}\n\nfunction isUsable(engine: GLEngineContext, resource: { readonly _engine: GLEngineContext; readonly _disposed: boolean }): boolean {\n return resource._engine === engine && !resource._disposed;\n}\n\nfunction getAttributeLocation(engine: GLEngineContext, effect: GLEffect, name: string): number {\n let location = effect._attributes[name];\n if (location === undefined) {\n location = engine._native.getAttributes(effect._program, [name])[0] ?? -1;\n effect._attributes[name] = location;\n }\n return location;\n}\n\n/** Selects recorded geometry without repeating an unchanged native binding. No-op for disposed geometry or engine. */\nexport function bindMeshVao(mesh: GLMeshVao): void {\n const engine = mesh._engine;\n if (mesh._disposed || engine._disposed) {\n return;\n }\n if (engine._vertexArray !== mesh._handle) {\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_BINDVERTEXARRAY);\n commands.handle(mesh._handle);\n commands.finish();\n engine._vertexArray = mesh._handle;\n }\n}\n\n/**\n * Draws a triangle range from recorded composition geometry; the range defaults\n * to the indices from `indexStart` to the end and must lie within the mesh.\n * Native composition does not instance. Draws nothing without a current\n * program or while the scissor rectangle is empty.\n */\nexport function drawMesh(mesh: GLMeshVao, _instanceCount: 0 = 0, indexStart = 0, indexCount = mesh.indexCount - indexStart): void {\n if (!Number.isInteger(indexStart) || !Number.isInteger(indexCount) || indexStart < 0 || indexCount < 0 || indexStart + indexCount > mesh.indexCount) {\n throw new RangeError(`Native Lite GL drawMesh range (start ${indexStart}, count ${indexCount}) must be whole indices within the mesh's ${mesh.indexCount}.`);\n }\n const engine = mesh._engine;\n if (engine._disposed || mesh._disposed || engine._program === null || engine._scissorEmpty) {\n return;\n }\n bindMeshVao(mesh);\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_DRAWINDEXED);\n commands.uint(0);\n commands.uint(indexStart);\n commands.uint(indexCount);\n commands.finish();\n}\n\n/** Releases owned vertex or index storage; after engine disposal it only marks the buffer disposed. */\nexport function disposeBuffer(buffer: GLVertexBuffer | GLIndexBuffer): void {\n if (!buffer._disposed) {\n buffer._disposed = true;\n const engine = buffer._engine;\n if (engine._disposed) {\n return;\n }\n const commands = engine._commands;\n commands.start(buffer._index ? engine._runtime.Engine.COMMAND_DELETEINDEXBUFFER : engine._runtime.Engine.COMMAND_DELETEVERTEXBUFFER);\n commands.handle(buffer._handle);\n commands.finish();\n }\n}\n\n/** Releases recorded geometry without releasing its caller-owned buffers or shader; after engine disposal it only marks the geometry disposed. */\nexport function disposeMeshVao(mesh: GLMeshVao): void {\n if (!mesh._disposed) {\n mesh._disposed = true;\n const engine = mesh._engine;\n if (engine._disposed) {\n return;\n }\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_DELETEVERTEXARRAY);\n commands.handle(mesh._handle);\n commands.finish();\n if (engine._vertexArray === mesh._handle) {\n engine._vertexArray = null;\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { GLEngineContext } from "./context.js";
|
|
2
|
+
import type { NativeResource } from "./identity.js";
|
|
3
|
+
import type { GLTexture, NativeTextureHandle } from "./texture.js";
|
|
4
|
+
/** Options for {@link createRenderTarget}. */
|
|
5
|
+
export interface GLRenderTargetOptions {
|
|
6
|
+
/** Caller-owned color attachment to render into instead of an owned RGBA8 texture. */
|
|
7
|
+
colorTexture?: GLTexture;
|
|
8
|
+
}
|
|
9
|
+
/** Native framebuffer and its color attachment. */
|
|
10
|
+
export interface GLRenderTarget extends NativeResource<"renderTarget"> {
|
|
11
|
+
readonly width: number;
|
|
12
|
+
readonly height: number;
|
|
13
|
+
readonly texture: GLTexture;
|
|
14
|
+
}
|
|
15
|
+
/** Creates a composition target around an owned or supplied color attachment of a live engine; a supplied attachment must be a live, renderable texture of this engine. */
|
|
16
|
+
export declare function createRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLRenderTargetOptions): GLRenderTarget;
|
|
17
|
+
/**
|
|
18
|
+
* Selects the drawing target, resolving the prior framebuffer before changing it,
|
|
19
|
+
* and sets the viewport to cover it. No-op for a disposed target or engine.
|
|
20
|
+
*/
|
|
21
|
+
export declare function bindRenderTarget(target: GLRenderTarget): void;
|
|
22
|
+
/** Selects the default output, resolving the prior framebuffer, and sets the viewport to cover it. No-op for a disposed engine. */
|
|
23
|
+
export declare function restoreDefaultFramebuffer(engine: GLEngineContext): void;
|
|
24
|
+
/**
|
|
25
|
+
* Repoints a borrowed texture while preserving its render-target identities.
|
|
26
|
+
* If the host cannot create a replacement framebuffer, the texture and its
|
|
27
|
+
* targets keep their previous handles, but Lite GL cannot restore host storage
|
|
28
|
+
* that already changed behind them; retry the update or restore the host
|
|
29
|
+
* storage before using the texture or its targets.
|
|
30
|
+
* A host can replace the storage behind the same handle object, so every call
|
|
31
|
+
* rebuilds the framebuffers; call it only when the storage has changed.
|
|
32
|
+
*/
|
|
33
|
+
export declare function updateWrappedNativeTexture(texture: GLTexture, replacement: NativeTextureHandle): void;
|
|
34
|
+
/** Releases a framebuffer and only the color storage it owns; after engine disposal it only marks them disposed. */
|
|
35
|
+
export declare function disposeRenderTarget(target: GLRenderTarget | null | undefined): void;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { setNormalizedViewport } from "./state.js";
|
|
2
|
+
import { configureTexture, disposeTexture, makeTexture, validateTextureSize } from "./texture.js";
|
|
3
|
+
/** Creates a composition target around an owned or supplied color attachment of a live engine; a supplied attachment must be a live, renderable texture of this engine. */
|
|
4
|
+
export function createRenderTarget(engine, width, height, options) {
|
|
5
|
+
if (engine._disposed) {
|
|
6
|
+
throw new Error("Native Lite GL createRenderTarget requires an engine that has not been disposed.");
|
|
7
|
+
}
|
|
8
|
+
validateTextureSize(engine, width, height);
|
|
9
|
+
const colorTexture = options?.colorTexture;
|
|
10
|
+
if (colorTexture && (colorTexture._engine !== engine || colorTexture._disposed)) {
|
|
11
|
+
throw new Error("Native Lite GL createRenderTarget requires a color texture of this engine that has not been disposed.");
|
|
12
|
+
}
|
|
13
|
+
if (colorTexture?._sampledOnly) {
|
|
14
|
+
throw new Error("Native Lite GL createRenderTarget cannot render into a sampled-only texture such as one from createRawTexture.");
|
|
15
|
+
}
|
|
16
|
+
let texture = colorTexture;
|
|
17
|
+
if (!texture) {
|
|
18
|
+
const handle = engine._native.createTexture();
|
|
19
|
+
try {
|
|
20
|
+
engine._native.initializeTexture(handle, width, height, false, engine._runtime.Engine.TEXTURE_FORMAT_RGBA8, true, false, 1);
|
|
21
|
+
texture = makeTexture(engine, handle, true, false);
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
engine._native.deleteTexture(handle);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const fields = {
|
|
30
|
+
width,
|
|
31
|
+
height,
|
|
32
|
+
texture,
|
|
33
|
+
_framebuffer: engine._native.createFrameBuffer(texture._handle, width, height, false, false, 1),
|
|
34
|
+
_ownsTexture: !colorTexture,
|
|
35
|
+
_engine: engine,
|
|
36
|
+
_disposed: false,
|
|
37
|
+
};
|
|
38
|
+
const target = fields;
|
|
39
|
+
texture._targets.push(target);
|
|
40
|
+
return target;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
if (!colorTexture) {
|
|
44
|
+
disposeTexture(texture);
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Selects the drawing target, resolving the prior framebuffer before changing it,
|
|
51
|
+
* and sets the viewport to cover it. No-op for a disposed target or engine.
|
|
52
|
+
*/
|
|
53
|
+
export function bindRenderTarget(target) {
|
|
54
|
+
if (target._disposed || target._engine._disposed) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
selectFramebuffer(target._engine, target);
|
|
58
|
+
setNormalizedViewport(target._engine, 0, 0, 1, 1, 1, 1);
|
|
59
|
+
}
|
|
60
|
+
/** Selects the default output, resolving the prior framebuffer, and sets the viewport to cover it. No-op for a disposed engine. */
|
|
61
|
+
export function restoreDefaultFramebuffer(engine) {
|
|
62
|
+
if (engine._disposed) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
selectFramebuffer(engine, null);
|
|
66
|
+
setNormalizedViewport(engine, 0, 0, 1, 1, 1, 1);
|
|
67
|
+
}
|
|
68
|
+
function selectFramebuffer(engine, target) {
|
|
69
|
+
if (engine._target === target) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const commands = engine._commands;
|
|
73
|
+
if (engine._target) {
|
|
74
|
+
commands.start(engine._runtime.Engine.COMMAND_UNBINDFRAMEBUFFER);
|
|
75
|
+
commands.handle(engine._target._framebuffer);
|
|
76
|
+
commands.finish();
|
|
77
|
+
}
|
|
78
|
+
if (target) {
|
|
79
|
+
commands.start(engine._runtime.Engine.COMMAND_BINDFRAMEBUFFER);
|
|
80
|
+
commands.handle(target._framebuffer);
|
|
81
|
+
commands.finish();
|
|
82
|
+
}
|
|
83
|
+
engine._target = target;
|
|
84
|
+
// The native viewport belongs to the framebuffer binding, so the cached value no longer applies.
|
|
85
|
+
engine._viewport.fill(NaN);
|
|
86
|
+
}
|
|
87
|
+
const NO_FRAMEBUFFERS = [];
|
|
88
|
+
/**
|
|
89
|
+
* Repoints a borrowed texture while preserving its render-target identities.
|
|
90
|
+
* If the host cannot create a replacement framebuffer, the texture and its
|
|
91
|
+
* targets keep their previous handles, but Lite GL cannot restore host storage
|
|
92
|
+
* that already changed behind them; retry the update or restore the host
|
|
93
|
+
* storage before using the texture or its targets.
|
|
94
|
+
* A host can replace the storage behind the same handle object, so every call
|
|
95
|
+
* rebuilds the framebuffers; call it only when the storage has changed.
|
|
96
|
+
*/
|
|
97
|
+
export function updateWrappedNativeTexture(texture, replacement) {
|
|
98
|
+
const engine = texture._engine;
|
|
99
|
+
if (texture._owned || texture.width !== engine._native.getTextureWidth(replacement) || texture.height !== engine._native.getTextureHeight(replacement)) {
|
|
100
|
+
throw new Error("Replacement native texture must preserve the borrowed texture's shape.");
|
|
101
|
+
}
|
|
102
|
+
const targets = texture._targets;
|
|
103
|
+
const framebuffers = targets.length === 0 ? NO_FRAMEBUFFERS : createFramebuffers(engine, targets, replacement);
|
|
104
|
+
for (const target of targets) {
|
|
105
|
+
if (engine._target === target) {
|
|
106
|
+
selectFramebuffer(engine, null);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
texture._handle = replacement;
|
|
110
|
+
configureTexture(engine, texture);
|
|
111
|
+
for (let index = 0; index < targets.length; index++) {
|
|
112
|
+
const target = targets[index];
|
|
113
|
+
deleteFramebuffer(engine, target._framebuffer);
|
|
114
|
+
target._framebuffer = framebuffers[index];
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
function createFramebuffers(engine, targets, texture) {
|
|
118
|
+
const framebuffers = [];
|
|
119
|
+
try {
|
|
120
|
+
for (const target of targets) {
|
|
121
|
+
framebuffers.push(engine._native.createFrameBuffer(texture, target.width, target.height, false, false, 1));
|
|
122
|
+
}
|
|
123
|
+
return framebuffers;
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
for (const framebuffer of framebuffers) {
|
|
127
|
+
deleteFramebuffer(engine, framebuffer);
|
|
128
|
+
}
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function deleteFramebuffer(engine, framebuffer) {
|
|
133
|
+
const commands = engine._commands;
|
|
134
|
+
commands.start(engine._runtime.Engine.COMMAND_DELETEFRAMEBUFFER);
|
|
135
|
+
commands.handle(framebuffer);
|
|
136
|
+
commands.finish();
|
|
137
|
+
}
|
|
138
|
+
/** Releases a framebuffer and only the color storage it owns; after engine disposal it only marks them disposed. */
|
|
139
|
+
export function disposeRenderTarget(target) {
|
|
140
|
+
if (!target || target._disposed) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
target._disposed = true;
|
|
144
|
+
const engine = target._engine;
|
|
145
|
+
if (!engine._disposed) {
|
|
146
|
+
if (engine._target === target) {
|
|
147
|
+
selectFramebuffer(engine, null);
|
|
148
|
+
}
|
|
149
|
+
deleteFramebuffer(engine, target._framebuffer);
|
|
150
|
+
}
|
|
151
|
+
const index = target.texture._targets.indexOf(target);
|
|
152
|
+
if (index >= 0) {
|
|
153
|
+
target.texture._targets.splice(index, 1);
|
|
154
|
+
}
|
|
155
|
+
if (target._ownsTexture) {
|
|
156
|
+
disposeTexture(target.texture);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
//# sourceMappingURL=render-target.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render-target.js","sourceRoot":"","sources":["../../src/native/render-target.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAwBlG,2KAA2K;AAC3K,MAAM,UAAU,kBAAkB,CAAC,MAAuB,EAAE,KAAa,EAAE,MAAc,EAAE,OAA+B;IACtH,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,kFAAkF,CAAC,CAAC;IACxG,CAAC;IACD,mBAAmB,CAAC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,CAAC;IACjE,QAAQ,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAC7B,QAAQ,CAAC,MAAM,EAAE,CAAC;AACtB,CAAC;AAED,oHAAoH;AACpH,MAAM,UAAU,mBAAmB,CAAC,MAAyC;IACzE,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,SAAS,EAAE,CAAC;QAC9B,OAAO;IACX,CAAC;IACD,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC;IACxB,MAAM,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC;IAC9B,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC;QACpB,IAAI,MAAM,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;YAC5B,iBAAiB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACpC,CAAC;QACD,iBAAiB,CAAC,MAAM,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC;IACnD,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IACtD,IAAI,KAAK,IAAI,CAAC,EAAE,CAAC;QACb,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IAC7C,CAAC;IACD,IAAI,MAAM,CAAC,YAAY,EAAE,CAAC;QACtB,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACnC,CAAC;AACL,CAAC","sourcesContent":["import type { GLEngineContext } from \"./context.js\";\nimport type { NativeResource, NativeResourceFields } from \"./identity.js\";\nimport type { NativeHandle } from \"./runtime.js\";\nimport { setNormalizedViewport } from \"./state.js\";\nimport { configureTexture, disposeTexture, makeTexture, validateTextureSize } from \"./texture.js\";\nimport type { GLTexture, NativeTextureHandle } from \"./texture.js\";\n\n/** Options for {@link createRenderTarget}. */\nexport interface GLRenderTargetOptions {\n /** Caller-owned color attachment to render into instead of an owned RGBA8 texture. */\n colorTexture?: GLTexture;\n}\n\n/** Native framebuffer and its color attachment. */\nexport interface GLRenderTarget extends NativeResource<\"renderTarget\"> {\n readonly width: number;\n readonly height: number;\n readonly texture: GLTexture;\n /** @internal */\n _framebuffer: NativeHandle;\n /** @internal */\n readonly _ownsTexture: boolean;\n /** @internal The engine that owns this framebuffer. */\n readonly _engine: GLEngineContext;\n /** @internal */\n _disposed: boolean;\n}\n\n/** Creates a composition target around an owned or supplied color attachment of a live engine; a supplied attachment must be a live, renderable texture of this engine. */\nexport function createRenderTarget(engine: GLEngineContext, width: number, height: number, options?: GLRenderTargetOptions): GLRenderTarget {\n if (engine._disposed) {\n throw new Error(\"Native Lite GL createRenderTarget requires an engine that has not been disposed.\");\n }\n validateTextureSize(engine, width, height);\n const colorTexture = options?.colorTexture;\n if (colorTexture && (colorTexture._engine !== engine || colorTexture._disposed)) {\n throw new Error(\"Native Lite GL createRenderTarget requires a color texture of this engine that has not been disposed.\");\n }\n if (colorTexture?._sampledOnly) {\n throw new Error(\"Native Lite GL createRenderTarget cannot render into a sampled-only texture such as one from createRawTexture.\");\n }\n let texture = colorTexture;\n if (!texture) {\n const handle = engine._native.createTexture();\n try {\n engine._native.initializeTexture(handle, width, height, false, engine._runtime.Engine.TEXTURE_FORMAT_RGBA8, true, false, 1);\n texture = makeTexture(engine, handle, true, false);\n } catch (error) {\n engine._native.deleteTexture(handle);\n throw error;\n }\n }\n try {\n const fields: NativeResourceFields<GLRenderTarget> = {\n width,\n height,\n texture,\n _framebuffer: engine._native.createFrameBuffer(texture._handle, width, height, false, false, 1),\n _ownsTexture: !colorTexture,\n _engine: engine,\n _disposed: false,\n };\n const target = fields as GLRenderTarget;\n texture._targets.push(target);\n return target;\n } catch (error) {\n if (!colorTexture) {\n disposeTexture(texture);\n }\n throw error;\n }\n}\n\n/**\n * Selects the drawing target, resolving the prior framebuffer before changing it,\n * and sets the viewport to cover it. No-op for a disposed target or engine.\n */\nexport function bindRenderTarget(target: GLRenderTarget): void {\n if (target._disposed || target._engine._disposed) {\n return;\n }\n selectFramebuffer(target._engine, target);\n setNormalizedViewport(target._engine, 0, 0, 1, 1, 1, 1);\n}\n\n/** Selects the default output, resolving the prior framebuffer, and sets the viewport to cover it. No-op for a disposed engine. */\nexport function restoreDefaultFramebuffer(engine: GLEngineContext): void {\n if (engine._disposed) {\n return;\n }\n selectFramebuffer(engine, null);\n setNormalizedViewport(engine, 0, 0, 1, 1, 1, 1);\n}\n\nfunction selectFramebuffer(engine: GLEngineContext, target: GLRenderTarget | null): void {\n if (engine._target === target) {\n return;\n }\n const commands = engine._commands;\n if (engine._target) {\n commands.start(engine._runtime.Engine.COMMAND_UNBINDFRAMEBUFFER);\n commands.handle(engine._target._framebuffer);\n commands.finish();\n }\n if (target) {\n commands.start(engine._runtime.Engine.COMMAND_BINDFRAMEBUFFER);\n commands.handle(target._framebuffer);\n commands.finish();\n }\n engine._target = target;\n // The native viewport belongs to the framebuffer binding, so the cached value no longer applies.\n engine._viewport.fill(NaN);\n}\n\nconst NO_FRAMEBUFFERS: readonly NativeHandle[] = [];\n\n/**\n * Repoints a borrowed texture while preserving its render-target identities.\n * If the host cannot create a replacement framebuffer, the texture and its\n * targets keep their previous handles, but Lite GL cannot restore host storage\n * that already changed behind them; retry the update or restore the host\n * storage before using the texture or its targets.\n * A host can replace the storage behind the same handle object, so every call\n * rebuilds the framebuffers; call it only when the storage has changed.\n */\nexport function updateWrappedNativeTexture(texture: GLTexture, replacement: NativeTextureHandle): void {\n const engine = texture._engine;\n if (texture._owned || texture.width !== engine._native.getTextureWidth(replacement) || texture.height !== engine._native.getTextureHeight(replacement)) {\n throw new Error(\"Replacement native texture must preserve the borrowed texture's shape.\");\n }\n const targets = texture._targets;\n const framebuffers = targets.length === 0 ? NO_FRAMEBUFFERS : createFramebuffers(engine, targets, replacement);\n for (const target of targets) {\n if (engine._target === target) {\n selectFramebuffer(engine, null);\n }\n }\n texture._handle = replacement;\n configureTexture(engine, texture);\n for (let index = 0; index < targets.length; index++) {\n const target = targets[index] as GLRenderTarget;\n deleteFramebuffer(engine, target._framebuffer);\n target._framebuffer = framebuffers[index] as NativeHandle;\n }\n}\n\nfunction createFramebuffers(engine: GLEngineContext, targets: readonly GLRenderTarget[], texture: NativeHandle): NativeHandle[] {\n const framebuffers: NativeHandle[] = [];\n try {\n for (const target of targets) {\n framebuffers.push(engine._native.createFrameBuffer(texture, target.width, target.height, false, false, 1));\n }\n return framebuffers;\n } catch (error) {\n for (const framebuffer of framebuffers) {\n deleteFramebuffer(engine, framebuffer);\n }\n throw error;\n }\n}\n\nfunction deleteFramebuffer(engine: GLEngineContext, framebuffer: NativeHandle): void {\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_DELETEFRAMEBUFFER);\n commands.handle(framebuffer);\n commands.finish();\n}\n\n/** Releases a framebuffer and only the color storage it owns; after engine disposal it only marks them disposed. */\nexport function disposeRenderTarget(target: GLRenderTarget | null | undefined): void {\n if (!target || target._disposed) {\n return;\n }\n target._disposed = true;\n const engine = target._engine;\n if (!engine._disposed) {\n if (engine._target === target) {\n selectFramebuffer(engine, null);\n }\n deleteFramebuffer(engine, target._framebuffer);\n }\n const index = target.texture._targets.indexOf(target);\n if (index >= 0) {\n target.texture._targets.splice(index, 1);\n }\n if (target._ownsTexture) {\n disposeTexture(target.texture);\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export function getRuntime() {
|
|
3
|
+
const runtime = globalThis._native;
|
|
4
|
+
if (!runtime) {
|
|
5
|
+
throw new Error("Native Lite GL requires the Babylon Native runtime.");
|
|
6
|
+
}
|
|
7
|
+
if (runtime.Engine.PROTOCOL_VERSION !== 9) {
|
|
8
|
+
throw new Error(`Native Lite GL requires protocol 9, received ${runtime.Engine.PROTOCOL_VERSION}.`);
|
|
9
|
+
}
|
|
10
|
+
return runtime;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=runtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../../src/native/runtime.ts"],"names":[],"mappings":"AA2GA,gBAAgB;AAChB,MAAM,UAAU,UAAU;IACtB,MAAM,OAAO,GAAI,UAA8D,CAAC,OAAO,CAAC;IACxF,IAAI,CAAC,OAAO,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IAC3E,CAAC;IACD,IAAI,OAAO,CAAC,MAAM,CAAC,gBAAgB,KAAK,CAAC,EAAE,CAAC;QACxC,MAAM,IAAI,KAAK,CAAC,gDAAgD,OAAO,CAAC,MAAM,CAAC,gBAAgB,GAAG,CAAC,CAAC;IACxG,CAAC;IACD,OAAO,OAAO,CAAC;AACnB,CAAC","sourcesContent":["/** @internal */\nexport type NativeHandle = Uint32Array<ArrayBuffer>;\n\n/** @internal */\nexport interface NativeEngine {\n dispose(): void;\n setRenderResetCallback(callback: () => void): void;\n setHardwareScalingLevel(level: number): void;\n getRenderWidth(): number;\n getRenderHeight(): number;\n setCommandDataStream(stream: object): void;\n submitCommands(): void;\n createProgram(vertex: string, fragment: string): NativeHandle;\n getUniforms(program: NativeHandle, names: string[]): (NativeHandle | null)[];\n getAttributes(program: NativeHandle, names: string[]): number[];\n createVertexArray(): NativeHandle;\n createVertexBuffer(data: ArrayBufferLike, offset: number, length: number, dynamic: boolean): NativeHandle;\n createIndexBuffer(data: ArrayBufferLike, offset: number, length: number, is32Bits: boolean, dynamic: boolean): NativeHandle;\n recordIndexBuffer(vao: NativeHandle, buffer: NativeHandle): void;\n recordVertexBuffer(\n vao: NativeHandle,\n buffer: NativeHandle,\n location: number,\n offset: number,\n stride: number,\n components: number,\n type: number,\n normalized: boolean,\n divisor: number\n ): void;\n createTexture(): NativeHandle;\n loadRawTexture(handle: NativeHandle, data: ArrayBufferView, width: number, height: number, format: number, mipmaps: boolean, invertY: boolean): void;\n initializeTexture(handle: NativeHandle, width: number, height: number, mipmaps: boolean, format: number, renderTarget: boolean, srgb: boolean, samples: number): void;\n getTextureWidth(handle: NativeHandle): number;\n getTextureHeight(handle: NativeHandle): number;\n deleteTexture(handle: NativeHandle): void;\n createFrameBuffer(texture: NativeHandle, width: number, height: number, stencil: boolean, depth: boolean, samples: number): NativeHandle;\n}\n\ntype CommandName =\n | \"SETPROGRAM\"\n | \"DELETEPROGRAM\"\n | \"SETFLOAT\"\n | \"SETFLOAT2\"\n | \"SETFLOAT3\"\n | \"SETFLOAT4\"\n | \"SETINT\"\n | \"SETFLOATARRAY\"\n | \"SETFLOATARRAY2\"\n | \"SETFLOATARRAY3\"\n | \"SETFLOATARRAY4\"\n | \"SETMATRICES\"\n | \"SETTEXTURE\"\n | \"SETTEXTURESAMPLING\"\n | \"SETTEXTUREWRAPMODE\"\n | \"SETTEXTUREANISOTROPICLEVEL\"\n | \"DELETEVERTEXBUFFER\"\n | \"DELETEINDEXBUFFER\"\n | \"DELETEVERTEXARRAY\"\n | \"BINDVERTEXARRAY\"\n | \"DRAWINDEXED\"\n | \"BINDFRAMEBUFFER\"\n | \"UNBINDFRAMEBUFFER\"\n | \"DELETEFRAMEBUFFER\"\n | \"SETVIEWPORT\"\n | \"SETSCISSOR\"\n | \"SETBLENDMODE\"\n | \"SETDEPTHTEST\"\n | \"SETDEPTHWRITE\"\n | \"SETSTATE\"\n | \"CLEAR\";\n\n/** @internal */\nexport type NativeEngineConstructor = {\n new (info: { version: string; nonFloatVertexBuffers: true }): NativeEngine;\n readonly PROTOCOL_VERSION: number;\n readonly CAPS_LIMITS_MAX_TEXTURE_SIZE: number;\n readonly ATTRIB_TYPE_FLOAT: number;\n readonly TEXTURE_FORMAT_RGBA8: number;\n readonly TEXTURE_LINEAR_LINEAR: number;\n readonly TEXTURE_NEAREST_NEAREST: number;\n readonly ADDRESS_MODE_CLAMP: number;\n readonly DEPTH_TEST_ALWAYS: number;\n readonly ALPHA_DISABLE: number;\n readonly ALPHA_COMBINE: number;\n readonly ALPHA_ADD: number;\n readonly ALPHA_PREMULTIPLIED: number;\n} & { readonly [K in CommandName as `COMMAND_${K}`]: NativeHandle };\n\n/** @internal */\nexport interface NativeDataStreamConstructor {\n new (flush: () => void): { writeBuffer(buffer: ArrayBuffer, words: number): void };\n readonly VALIDATION_ENABLED: boolean;\n readonly VALIDATION_UINT_32: number;\n readonly VALIDATION_INT_32: number;\n readonly VALIDATION_FLOAT_32: number;\n readonly VALIDATION_FLOAT_32_ARRAY: number;\n readonly VALIDATION_NATIVE_DATA: number;\n readonly VALIDATION_BOOLEAN: number;\n}\n\n/** @internal */\nexport interface NativeRuntime {\n Engine: NativeEngineConstructor;\n NativeDataStream: NativeDataStreamConstructor;\n}\n\n/** @internal */\nexport function getRuntime(): NativeRuntime {\n const runtime = (globalThis as typeof globalThis & { _native?: NativeRuntime })._native;\n if (!runtime) {\n throw new Error(\"Native Lite GL requires the Babylon Native runtime.\");\n }\n if (runtime.Engine.PROTOCOL_VERSION !== 9) {\n throw new Error(`Native Lite GL requires protocol 9, received ${runtime.Engine.PROTOCOL_VERSION}.`);\n }\n return runtime;\n}\n"]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { GLEngineContext } from "./context.js";
|
|
2
|
+
import type { GLClearOptions, GLViewport } from "../family.js";
|
|
3
|
+
/** Blend presets shared with Lite GL: disabled, additive, combine and premultiplied. */
|
|
4
|
+
export type GLBlendMode = 0 | 1 | 2 | 7;
|
|
5
|
+
/** Changes the drawing viewport in pixels of the bound target. Defaults to the size of the default output, whichever target is bound. No-op for a disposed engine. */
|
|
6
|
+
export declare function setViewport(engine: GLEngineContext, viewport?: GLViewport): void;
|
|
7
|
+
/**
|
|
8
|
+
* Restricts drawing and clearing to a bottom-left-origin rectangle in physical
|
|
9
|
+
* pixels; portable callers pass nonnegative integer dimensions. A rectangle
|
|
10
|
+
* without area allows neither until the scissor changes. No-op for a disposed
|
|
11
|
+
* engine.
|
|
12
|
+
*/
|
|
13
|
+
export declare function setScissor(engine: GLEngineContext, x: number, y: number, width: number, height: number): void;
|
|
14
|
+
/** Restores unrestricted drawing. No-op for a disposed engine. */
|
|
15
|
+
export declare function disableScissor(engine: GLEngineContext): void;
|
|
16
|
+
/** Sets a Lite GL blend preset. No-op for a disposed engine. */
|
|
17
|
+
export declare function setBlendMode(engine: GLEngineContext, mode: GLBlendMode): void;
|
|
18
|
+
/** Clears the current composition target within its scissor, without changing the scissor. No-op for a disposed engine. */
|
|
19
|
+
export declare function clearEngine(engine: GLEngineContext, options: GLClearOptions): void;
|
package/native/state.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { getRenderHeight, getRenderWidth } from "./context.js";
|
|
2
|
+
/** Changes the drawing viewport in pixels of the bound target. Defaults to the size of the default output, whichever target is bound. No-op for a disposed engine. */
|
|
3
|
+
export function setViewport(engine, viewport) {
|
|
4
|
+
if (engine._disposed) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const target = engine._target;
|
|
8
|
+
// The default output size is a host query, so an explicit viewport of a bound target does not read it.
|
|
9
|
+
if (viewport && target) {
|
|
10
|
+
setNormalizedViewport(engine, viewport.x, viewport.y, viewport.w, viewport.h, target.width, target.height);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const outputWidth = getRenderWidth(engine);
|
|
14
|
+
const outputHeight = getRenderHeight(engine);
|
|
15
|
+
setNormalizedViewport(engine, viewport?.x ?? 0, viewport?.y ?? 0, viewport?.w ?? outputWidth, viewport?.h ?? outputHeight, target?.width ?? outputWidth, target?.height ?? outputHeight);
|
|
16
|
+
}
|
|
17
|
+
/** @internal Native viewports are fractions of the bound target. */
|
|
18
|
+
export function setNormalizedViewport(engine, x, y, width, height, targetWidth, targetHeight) {
|
|
19
|
+
x /= targetWidth;
|
|
20
|
+
y /= targetHeight;
|
|
21
|
+
width /= targetWidth;
|
|
22
|
+
height /= targetHeight;
|
|
23
|
+
const values = engine._viewport;
|
|
24
|
+
if (values[0] === x && values[1] === y && values[2] === width && values[3] === height) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
values[0] = x;
|
|
28
|
+
values[1] = y;
|
|
29
|
+
values[2] = width;
|
|
30
|
+
values[3] = height;
|
|
31
|
+
const commands = engine._commands;
|
|
32
|
+
commands.start(engine._runtime.Engine.COMMAND_SETVIEWPORT);
|
|
33
|
+
commands.float(x);
|
|
34
|
+
commands.float(y);
|
|
35
|
+
commands.float(width);
|
|
36
|
+
commands.float(height);
|
|
37
|
+
commands.finish();
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Restricts drawing and clearing to a bottom-left-origin rectangle in physical
|
|
41
|
+
* pixels; portable callers pass nonnegative integer dimensions. A rectangle
|
|
42
|
+
* without area allows neither until the scissor changes. No-op for a disposed
|
|
43
|
+
* engine.
|
|
44
|
+
*/
|
|
45
|
+
export function setScissor(engine, x, y, width, height) {
|
|
46
|
+
if (engine._disposed) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
// The host reads an all-zero rectangle as "no scissor", so an empty one is enforced here instead.
|
|
50
|
+
engine._scissorEmpty = width <= 0 || height <= 0;
|
|
51
|
+
if (!engine._scissorEmpty) {
|
|
52
|
+
encodeScissor(engine, x, y, width, height);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Restores unrestricted drawing. No-op for a disposed engine. */
|
|
56
|
+
export function disableScissor(engine) {
|
|
57
|
+
if (engine._disposed) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
engine._scissorEmpty = false;
|
|
61
|
+
encodeScissor(engine, 0, 0, 0, 0);
|
|
62
|
+
}
|
|
63
|
+
function encodeScissor(engine, x, y, width, height) {
|
|
64
|
+
const commands = engine._commands;
|
|
65
|
+
commands.start(engine._runtime.Engine.COMMAND_SETSCISSOR);
|
|
66
|
+
commands.float(x);
|
|
67
|
+
commands.float(y);
|
|
68
|
+
commands.float(width);
|
|
69
|
+
commands.float(height);
|
|
70
|
+
commands.finish();
|
|
71
|
+
}
|
|
72
|
+
/** Sets a Lite GL blend preset. No-op for a disposed engine. */
|
|
73
|
+
export function setBlendMode(engine, mode) {
|
|
74
|
+
if (engine._blend === mode || engine._disposed) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const native = engine._runtime.Engine;
|
|
78
|
+
const resolved = mode === 0 ? native.ALPHA_DISABLE : mode === 1 ? native.ALPHA_ADD : mode === 2 ? native.ALPHA_COMBINE : native.ALPHA_PREMULTIPLIED;
|
|
79
|
+
const commands = engine._commands;
|
|
80
|
+
commands.start(native.COMMAND_SETBLENDMODE);
|
|
81
|
+
commands.uint(resolved);
|
|
82
|
+
commands.finish();
|
|
83
|
+
engine._blend = mode;
|
|
84
|
+
}
|
|
85
|
+
/** Clears the current composition target within its scissor, without changing the scissor. No-op for a disposed engine. */
|
|
86
|
+
export function clearEngine(engine, options) {
|
|
87
|
+
if (engine._scissorEmpty || engine._disposed) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const color = options.color;
|
|
91
|
+
const commands = engine._commands;
|
|
92
|
+
commands.start(engine._runtime.Engine.COMMAND_CLEAR);
|
|
93
|
+
commands.uint(color ? 1 : 0);
|
|
94
|
+
commands.float(color?.r ?? 0);
|
|
95
|
+
commands.float(color?.g ?? 0);
|
|
96
|
+
commands.float(color?.b ?? 0);
|
|
97
|
+
commands.float(color?.a ?? 1);
|
|
98
|
+
commands.uint(options.depth ? 1 : 0);
|
|
99
|
+
commands.float(1);
|
|
100
|
+
commands.uint(options.stencil ? 1 : 0);
|
|
101
|
+
commands.uint(0);
|
|
102
|
+
commands.finish();
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"state.js","sourceRoot":"","sources":["../../src/native/state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAM/D,sKAAsK;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derHeight, getRenderWidth } from \"./context.js\";\nimport type { GLClearOptions, GLViewport } from \"../family.js\";\n\n/** Blend presets shared with Lite GL: disabled, additive, combine and premultiplied. */\nexport type GLBlendMode = 0 | 1 | 2 | 7;\n\n/** Changes the drawing viewport in pixels of the bound target. Defaults to the size of the default output, whichever target is bound. No-op for a disposed engine. */\nexport function setViewport(engine: GLEngineContext, viewport?: GLViewport): void {\n if (engine._disposed) {\n return;\n }\n const target = engine._target;\n // The default output size is a host query, so an explicit viewport of a bound target does not read it.\n if (viewport && target) {\n setNormalizedViewport(engine, viewport.x, viewport.y, viewport.w, viewport.h, target.width, target.height);\n return;\n }\n const outputWidth = getRenderWidth(engine);\n const outputHeight = getRenderHeight(engine);\n setNormalizedViewport(\n engine,\n viewport?.x ?? 0,\n viewport?.y ?? 0,\n viewport?.w ?? outputWidth,\n viewport?.h ?? outputHeight,\n target?.width ?? outputWidth,\n target?.height ?? outputHeight\n );\n}\n\n/** @internal Native viewports are fractions of the bound target. */\nexport function setNormalizedViewport(engine: GLEngineContext, x: number, y: number, width: number, height: number, targetWidth: number, targetHeight: number): void {\n x /= targetWidth;\n y /= targetHeight;\n width /= targetWidth;\n height /= targetHeight;\n const values = engine._viewport;\n if (values[0] === x && values[1] === y && values[2] === width && values[3] === height) {\n return;\n }\n values[0] = x;\n values[1] = y;\n values[2] = width;\n values[3] = height;\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_SETVIEWPORT);\n commands.float(x);\n commands.float(y);\n commands.float(width);\n commands.float(height);\n commands.finish();\n}\n\n/**\n * Restricts drawing and clearing to a bottom-left-origin rectangle in physical\n * pixels; portable callers pass nonnegative integer dimensions. A rectangle\n * without area allows neither until the scissor changes. No-op for a disposed\n * engine.\n */\nexport function setScissor(engine: GLEngineContext, x: number, y: number, width: number, height: number): void {\n if (engine._disposed) {\n return;\n }\n // The host reads an all-zero rectangle as \"no scissor\", so an empty one is enforced here instead.\n engine._scissorEmpty = width <= 0 || height <= 0;\n if (!engine._scissorEmpty) {\n encodeScissor(engine, x, y, width, height);\n }\n}\n\n/** Restores unrestricted drawing. No-op for a disposed engine. */\nexport function disableScissor(engine: GLEngineContext): void {\n if (engine._disposed) {\n return;\n }\n engine._scissorEmpty = false;\n encodeScissor(engine, 0, 0, 0, 0);\n}\n\nfunction encodeScissor(engine: GLEngineContext, x: number, y: number, width: number, height: number): void {\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_SETSCISSOR);\n commands.float(x);\n commands.float(y);\n commands.float(width);\n commands.float(height);\n commands.finish();\n}\n\n/** Sets a Lite GL blend preset. No-op for a disposed engine. */\nexport function setBlendMode(engine: GLEngineContext, mode: GLBlendMode): void {\n if (engine._blend === mode || engine._disposed) {\n return;\n }\n const native = engine._runtime.Engine;\n const resolved = mode === 0 ? native.ALPHA_DISABLE : mode === 1 ? native.ALPHA_ADD : mode === 2 ? native.ALPHA_COMBINE : native.ALPHA_PREMULTIPLIED;\n const commands = engine._commands;\n commands.start(native.COMMAND_SETBLENDMODE);\n commands.uint(resolved);\n commands.finish();\n engine._blend = mode;\n}\n\n/** Clears the current composition target within its scissor, without changing the scissor. No-op for a disposed engine. */\nexport function clearEngine(engine: GLEngineContext, options: GLClearOptions): void {\n if (engine._scissorEmpty || engine._disposed) {\n return;\n }\n const color = options.color;\n const commands = engine._commands;\n commands.start(engine._runtime.Engine.COMMAND_CLEAR);\n commands.uint(color ? 1 : 0);\n commands.float(color?.r ?? 0);\n commands.float(color?.g ?? 0);\n commands.float(color?.b ?? 0);\n commands.float(color?.a ?? 1);\n commands.uint(options.depth ? 1 : 0);\n commands.float(1);\n commands.uint(options.stencil ? 1 : 0);\n commands.uint(0);\n commands.finish();\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { GLEngineContext } from "./context.js";
|
|
2
|
+
import type { NativeResource } from "./identity.js";
|
|
3
|
+
/** Opaque texture storage supplied by the Babylon Native host. */
|
|
4
|
+
export type NativeTextureHandle = Uint32Array<ArrayBuffer>;
|
|
5
|
+
/** Sampled native image with explicit storage ownership. */
|
|
6
|
+
export interface GLTexture extends NativeResource<"texture"> {
|
|
7
|
+
readonly width: number;
|
|
8
|
+
readonly height: number;
|
|
9
|
+
/** Whether the image is available for sampling. */
|
|
10
|
+
isReady: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Borrows native texture storage without taking responsibility for deleting it. */
|
|
13
|
+
export declare function wrapNativeTexture(engine: GLEngineContext, handle: NativeTextureHandle): GLTexture;
|
|
14
|
+
/** Upload options shared by native texture creators. */
|
|
15
|
+
export interface GLTextureOptions {
|
|
16
|
+
/** Flip rows during upload. Default `false`. */
|
|
17
|
+
invertY?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** Options for {@link createRawTexture}. */
|
|
20
|
+
export interface GLRawTextureOptions extends GLTextureOptions {
|
|
21
|
+
/** Pixel format of `data`. Only `gl.RGBA` (the default) is supported. */
|
|
22
|
+
format?: number;
|
|
23
|
+
/** Pixel type of `data`. Only `gl.UNSIGNED_BYTE` (the default) is supported. */
|
|
24
|
+
type?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates an owned, sampled-only texture from raw pixels. Only `gl.RGBA` / `gl.UNSIGNED_BYTE` data is supported.
|
|
28
|
+
* The host submits the commands recorded so far before uploading, so create raw textures before `beginFrame` when the number of submissions matters.
|
|
29
|
+
*/
|
|
30
|
+
export declare function createRawTexture(engine: GLEngineContext, data: Uint8Array, width: number, height: number, options?: GLRawTextureOptions): GLTexture;
|
|
31
|
+
/**
|
|
32
|
+
* Releases owned image storage, never a borrowed host texture. The storage
|
|
33
|
+
* outlives every command already recorded, so a frame may still draw with,
|
|
34
|
+
* or into, a texture it disposes. After engine disposal it only marks the
|
|
35
|
+
* texture disposed.
|
|
36
|
+
*/
|
|
37
|
+
export declare function disposeTexture(texture: GLTexture): void;
|