Skip to content

Commit 56ecd57

Browse files
committed
more experiments
1 parent 5706352 commit 56ecd57

12 files changed

+342
-79
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ function render() {
5151
}
5252
render()
5353
```
54+
55+
56+
## API (WORK IN PROGRESS)
57+
58+
```JavaScript
59+
60+
var regl = require('regl')
61+
```

error.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib/check').error

lib/buffer.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
var check = require('./validate')
1+
var dtypes = require('./dtypes.json')
2+
var check = require('./check')
23

3-
module.exports = function createBufferSet (gl, mirrorData) {
4+
module.exports = function createBufferSet (gl, extensions) {
45
var bufferCount = 0
56
var bufferSet = {}
67

8+
var GL_UNSIGNED_BYTE = gl.UNSIGNED_BYTE
9+
710
var GL_ARRAY_BUFFER = gl.ARRAY_BUFFER
811
var GL_ELEMENT_ARRAY_BUFFER = gl.ELEMENT_ARRAY_BUFFER
912
var bufferTypes = {
@@ -25,6 +28,7 @@ module.exports = function createBufferSet (gl, mirrorData) {
2528
this.usage = GL_DYNAMIC_DRAW
2629
this.byteLength = 0
2730
this.data = null
31+
this.dtype = GL_UNSIGNED_BYTE
2832
}
2933

3034
Object.assign(REGLBuffer.prototype, {
@@ -45,13 +49,23 @@ module.exports = function createBufferSet (gl, mirrorData) {
4549
var data = options.data
4650
if (data === null) {
4751
this.byteLength = 0
52+
this.dtype = GL_UNSIGNED_BYTE
4853
} else {
4954
if (Array.isArray(data)) {
50-
data = new Float32Array(data)
55+
if (this.type === GL_ELEMENT_ARRAY_BUFFER) {
56+
if (extensions['oes_element_index_uint']) {
57+
data = new Uint32Array(data)
58+
} else {
59+
data = new Uint16Array(data)
60+
}
61+
} else {
62+
data = new Float32Array(data)
63+
}
5164
} else {
5265
check.isTypedArray(data, 'invalid data type buffer data')
5366
}
5467
this.byteLength = data.byteLength
68+
this.dtype = dtypes[data]
5569
}
5670
this.data = data
5771
} else if ('length' in options) {
@@ -60,19 +74,23 @@ module.exports = function createBufferSet (gl, mirrorData) {
6074
'buffer length must be a nonnegative integer')
6175
this.data = null
6276
this.byteLength = options.length | 0
77+
this.dtype = GL_UNSIGNED_BYTE
6378
}
6479

6580
this.bind()
6681
gl.bufferData(this.type, this.data || this.byteLength, this.usage)
6782
},
6883

6984
refresh: function () {
70-
this.bind()
71-
gl.bufferData(this.type, this.data || this.byteLength, this.usage)
85+
if (!gl.isBuffer(this.buffer)) {
86+
this.buffer = gl.createBuffer()
87+
}
88+
this.update({})
7289
},
7390

7491
destroy: function () {
7592
check(this.buffer, 'buffer must not be deleted already')
93+
gl.destroyBuffer(this.buffer)
7694
}
7795
})
7896

lib/validate.js lib/check.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
var dtypes = require('./dtypes.json')
2+
13
function extend (Parent, Child, name) {
24
var proto = Child.prototype = new Parent()
35
proto.name = name
@@ -41,7 +43,9 @@ function checkParameter (param, possibilities, message) {
4143
}
4244

4345
function checkIsTypedArray (data, message) {
44-
assert()
46+
assert(
47+
Object.prototype.toString.call(data) in dtypes,
48+
message)
4549
}
4650

4751
module.exports = Object.assign(assert, {

lib/context.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*globals HTMLElement,WebGLRenderingContext,Canvas*/
2+
3+
var check = require('./check')
4+
5+
function createCanvas (element, options) {
6+
var canvas = document.createElement('canvas')
7+
var args = getContext(element, options)
8+
9+
Object.assign(canvas.style, {
10+
position: 'absolute',
11+
top: 0,
12+
left: 0
13+
})
14+
element.appendChild(canvas)
15+
resize()
16+
17+
var scale = +window.devicePixelRatio
18+
function resize () {
19+
var w = window.innerWidth
20+
var h = window.innerHeight
21+
if (element !== document.body) {
22+
23+
}
24+
canvas.width = scale * w
25+
canvas.height = scale * h
26+
canvas.style.width = w
27+
canvas.style.height = h
28+
}
29+
30+
window.addEventListener('resize', resize, false)
31+
32+
var prevDestroy = args.options.onDestroy
33+
args.options = Object.assign({}, args.options, {
34+
onDestroy: function () {
35+
window.removeEventListener('resize', resize)
36+
element.removeChild(canvas)
37+
prevDestroy && prevDestroy()
38+
}
39+
})
40+
41+
return args
42+
}
43+
44+
function getContext (canvas, options) {
45+
var glOptions = options.glOptions
46+
47+
function get (name) {
48+
try {
49+
return canvas.getContext(name, glOptions)
50+
} catch (e) {
51+
return null
52+
}
53+
}
54+
55+
var gl = get('webgl') ||
56+
get('experimental-webgl') ||
57+
get('webgl-experimental')
58+
59+
check(gl, 'webgl not supported')
60+
61+
return {
62+
gl: gl,
63+
options: options
64+
}
65+
}
66+
67+
module.exports = function parseArgs (args) {
68+
if (typeof document !== 'undefined' ||
69+
typeof HTMLElement !== 'undefined') {
70+
return {
71+
gl: args[0],
72+
options: args[1] || {}
73+
}
74+
}
75+
76+
var element = document.body
77+
var options = args[1] || {}
78+
79+
if (typeof args[0] === 'string') {
80+
element = document.querySelector(args[0]) || document.body
81+
} else if (typeof args[0] === 'object') {
82+
if (args[0] instanceof HTMLElement) {
83+
element = args[0]
84+
} else if (args[0] instanceof WebGLRenderingContext) {
85+
return {
86+
gl: args[0],
87+
options: options
88+
}
89+
} else {
90+
options = args[0]
91+
}
92+
}
93+
94+
if (element instanceof Canvas) {
95+
return getContext(element, options)
96+
} else {
97+
return createCanvas(element, options)
98+
}
99+
}

lib/draw.js

-12
This file was deleted.

lib/dtypes.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"[object Int8Array]": 5120
3+
, "[object Int16Array]": 5122
4+
, "[object Int32Array]": 5124
5+
, "[object Uint8Array]": 5121
6+
, "[object Uint8ClampedArray]": 5121
7+
, "[object Uint16Array]": 5123
8+
, "[object Uint32Array]": 5125
9+
, "[object Float32Array]": 5126
10+
, "[object Float64Array]": 5121
11+
, "[object ArrayBuffer]": 5121
12+
}

lib/extension.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var check = require('./check')
2+
3+
module.exports = function createExtensionCache (gl, required) {
4+
var extensions = {}
5+
6+
function refreshExtensions () {
7+
[
8+
'oes_texture_float',
9+
'oes_texture_float_linear',
10+
'oes_texture_half_float',
11+
'oes_texture_half_float_linear',
12+
'oes_standard_derivatives',
13+
'oes_element_index_uint',
14+
'oes_fbo_render_mipmap',
15+
16+
'webgl_depth_texture',
17+
'webgl_draw_buffers',
18+
'webgl_color_buffer_float',
19+
20+
'ext_texture_filter_anisotropic',
21+
'ext_frag_depth',
22+
'ext_blend_minmax',
23+
'ext_shader_texture_lod',
24+
'ext_color_buffer_half_float',
25+
'ext_srgb',
26+
27+
'angle_instanced_arrays'
28+
].forEach(function (ext) {
29+
extensions[ext] = gl.getExtension(ext)
30+
})
31+
32+
required.forEach(function (ext) {
33+
check(extensions[ext.toLowerCase()],
34+
'required extension "' + ext + '" is unsupported')
35+
})
36+
}
37+
38+
refreshExtensions()
39+
40+
return {
41+
extensions: extensions,
42+
refresh: refreshExtensions
43+
}
44+
}

lib/shader.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
1-
'use strict'
2-
3-
// This module implements caching for shader objects
4-
5-
var REGLError = require('./error')
1+
var check = require('./check')
62
var formatCompilerError = require('gl-format-compiler-error')
73

8-
function REGLProgram (program, uniforms, attributes) {
9-
this.program = program
10-
this.uniforms = uniforms
11-
this.attributes = attributes
12-
}
13-
144
module.exports = function createShaderCache (gl) {
155
var shaders = {}
166
var programs = {}
177

8+
var GL_FRAGMENT_SHADER = gl.FRAGMENT_SHADER
9+
var GL_VERTEX_SHADER = gl.VERTEX_SHADER
10+
11+
function REGLProgram (program, uniforms, attributes) {
12+
this.program = program
13+
this.uniforms = uniforms
14+
this.attributes = attributes
15+
}
16+
1817
function clearCache () {
19-
shaders[gl.FRAGMENT_SHADER] = {}
20-
shaders[gl.VERTEX_SHADER] = {}
21-
programs = {}
18+
Object.keys(shaders).forEach(function (type) {
19+
Object.keys(shaders[type]).forEach(function (shader) {
20+
gl.destroyShader(shader)
21+
})
22+
})
23+
shaders[GL_FRAGMENT_SHADER] = {}
24+
shaders[GL_VERTEX_SHADER] = {}
25+
26+
// TODO destroy programs
2227
}
2328

2429
function refreshCache () {
30+
shaders[GL_FRAGMENT_SHADER] = {}
31+
shaders[GL_VERTEX_SHADER] = {}
32+
33+
// TODO recompile and link all programs
2534
}
2635

2736
function getShader (type, source) {
@@ -38,9 +47,9 @@ module.exports = function createShaderCache (gl) {
3847
try {
3948
var fmt = formatCompilerError(errLog, source, type)
4049
} catch (e) {
41-
throw new REGLError(errLog, 'Error compiling shader:\n' + errLog)
50+
check.raiseRuntime(errLog, 'Error compiling shader:\n' + errLog)
4251
}
43-
throw new REGLError(errLog, fmt.short, fmt.long)
52+
check.raiseRuntime(errLog, fmt.short, fmt.long)
4453
}
4554
cache[source] = shader
4655
}
@@ -99,7 +108,7 @@ module.exports = function createShaderCache (gl) {
99108

100109
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
101110
var errLog = gl.getProgramInfoLog(program)
102-
throw new REGLError(errLog, 'Error linking program:' + errLog)
111+
check.raiseRuntime(errLog, 'Error linking program:' + errLog)
103112
}
104113

105114
// Construct result
@@ -123,11 +132,10 @@ module.exports = function createShaderCache (gl) {
123132
return program
124133
}
125134

126-
// Invalidate cache
127135
clearCache()
128136

129137
return {
130-
get: getProgram,
138+
create: getProgram,
131139
clear: clearCache,
132140
refresh: refreshCache
133141
}

lib/state.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = function stateCache (
2+
gl,
3+
extensions,
4+
shaderCache,
5+
bufferCache,
6+
textureCache,
7+
fboCache) {
8+
9+
function createEnvironment (options) {
10+
}
11+
12+
function clearCache () {
13+
}
14+
15+
function refreshCache () {
16+
}
17+
18+
return {
19+
create: createEnvironment,
20+
clear: clearCache,
21+
refresh: refreshCache
22+
}
23+
}

0 commit comments

Comments
 (0)