Skip to main content

Core Engine

createGL Function

GLRE エンジンの中核となる初期化関数です。WebGL2/WebGPU の抽象化レイヤーを提供し、リアクティブな GPU 操作環境を構築します。

Basic Usage

ParameterTypeDefaultDescription
isWebGLbooleantrueWebGL2 使用フラグ
isLoopbooleantrueアニメーションループ有効化
countnumber6描画頂点数
widthnumberwindow.innerWidthキャンバス幅
heightnumberwindow.innerHeightキャンバス高さ
vertexstring|Nodedefault vertexバーテックスシェーダー
fragmentstring|Nodedefault fragmentフラグメントシェーダー

Configuration Examples

// WebGL2での基本設定
const gl = createGL({
isWebGL: true,
width: 800,
height: 600,
fragment: vec4(fract(position.xy.div(iResolution)), 0, 1),
})

// WebGPUでの高度な設定
const gl = createGL({
isWebGL: false,
count: 36,
vertex: customVertexShader,
fragment: customFragmentShader,
})

Backend Selection

User Configuration

┌───────▼───────┐
│ isWebGL: bool │
└───────┬───────┘

┌────▼────┐
│ Check │ ──── GPU Support
│ Support │ Available?
└────┬────┘

┌────▼────┐ ┌─────────────┐
│ WebGL2 │ ──► │ WebGL2 │
│ Backend │ │ Context │
└─────────┘ └─────────────┘

┌────▼────┐ ┌─────────────┐
│ WebGPU │ ──► │ WebGPU │
│ Backend │ │ Context │
└─────────┘ └─────────────┘

Event System

GLRE のリアクティブ性の基盤となるイベント駆動アーキテクチャです。

Core Events

EventTriggerPurpose
mountエンジン初期化時GPU 設定とリソース準備
cleanエンジン終了時リソースの解放
resizeキャンバスサイズ変更時描画領域の再設定
mousemoveマウス移動時インタラクティブ操作
loopフレームごとアニメーション更新

Event Registration

// 初期化時の処理
gl('mount', () => {
console.log('Engine initialized')
gl.uniform('startTime', performance.now())
})

// クリーンアップ処理
gl('clean', () => {
console.log('Resources cleaned')
})

// カスタムイベント
gl('customEvent', (data) => {
gl.uniform('customData', data)
})

Event Flow

Application Start

┌───▼───┐
│ mount │ ─────► GPU Context Creation
└───┬───┘ Resource Allocation

┌───▼───┐
│ resize│ ─────► Canvas Size Update
└───┬───┘ Viewport Configuration

┌───▼───┐
│ loop │ ─────► Animation Frame
└───┬───┘ Uniform Updates
│ Render Execution

Application End

┌───▼───┐
│ clean │ ─────► Resource Cleanup
└───────┘ Context Destruction

Resource Management

Uniform Variables

CPU 側の値を GPU 側に効率的に転送するシステム:

MethodParametersDescription
gl.uniform(key, value)string, number|arrayUniform 変数の設定
gl.uniform(object)object複数 Uniform の一括設定
// スカラー値
gl.uniform('iTime', performance.now() / 1000)

// ベクトル値
gl.uniform('iResolution', [800, 600])
gl.uniform('color', [1.0, 0.5, 0.2, 1.0])

// マトリクス
gl.uniform('transform', [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1])

// 一括設定
gl.uniform({
iTime: time,
iMouse: [mouseX, mouseY],
amplitude: 2.0,
})

Attribute Data

頂点属性データの管理:

MethodParametersDescription
gl.attribute(key, data)string, number[]属性データの設定
gl.attribute(key, data, indices)string, number[], number[]インデックス付き属性
// 基本的な三角形
gl.attribute('position', [0.0, 0.5, -0.5, -0.5, 0.5, -0.5])

// 色付き頂点
gl.attribute('color', [
1.0,
0.0,
0.0,
1.0, // 赤
0.0,
1.0,
0.0,
1.0, // 緑
0.0,
0.0,
1.0,
1.0, // 青
])

// インデックス付きジオメトリ
gl.attribute('position', quadVertices, [0, 1, 2, 2, 3, 0])

Texture Resources

画像リソースの GPU メモリ管理:

MethodParametersDescription
gl.texture(key, source)string, string|ImageDataテクスチャの読み込み
// URL からの読み込み
gl.texture('mainTexture', './assets/image.jpg')

// ユーザー画像の利用
gl.texture('userImage', imageElement)

// データURLからの読み込み
gl.texture('proceduralTexture', canvas.toDataURL())

Lifecycle Management

Initialization Sequence

createGL() Call

┌───▼───┐
│Config │ ──── Default Values
│ Setup │ User Overrides
└───┬───┘

┌───▼───┐
│Queue │ ──── Event Queue
│ Init │ Frame Manager
└───┬───┘

┌───▼───┐
│Mount │ ──── GPU Context
│ Event │ Shader Compilation
└───┬───┘ Resource Binding

┌───▼───┐
│Loop │ ──── Animation Start
│ Start │ Reactive Updates
└───────┘

Resource Cleanup

// 明示的なクリーンアップ
gl.clean()

// 自動クリーンアップの設定
window.addEventListener('beforeunload', () => {
gl.clean()
})

// リサイズイベントでの再初期化
window.addEventListener('resize', () => {
gl.resize()
})

Debug and Monitoring

Development Tools

// デバッグ情報の有効化
const gl = createGL({
debug: true,
verbose: true,
})

// パフォーマンス監視
gl('loop', () => {
const frameTime = performance.now()
console.log(`Frame time: ${frameTime}ms`)
})

// エラーハンドリング
gl('error', (error) => {
console.error('GLRE Error:', error)
})

GLRE のコアエンジンは、これらの機能を組み合わせることで、複雑な GPU プログラミングを簡潔で直感的な API として提供します。