Skip to content

add toString method to shader, for convenience of debugging #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ Here is the result:

# Install

npm install gl-shader
npm install @lfdoherty/gl-shader

# API

```javascript
var createShader = require('gl-shader')
var createShader = require('@lfdoherty/gl-shader')
```


Expand Down
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ function compareAttributes(a, b) {
return 1
}

proto.toString = function(){
var result = {}
if(!this.uniforms){
result.neverBound = true;
}else{
result.types = this.types
result.uniforms = {}
var local = this;
Object.keys(this.types.uniforms).forEach(function(uniformName){
result.uniforms[uniformName] = local.uniforms[uniformName]
})
}
return JSON.stringify(result)
}

//Update export hook for glslify-live
proto.update = function(
vertSource
Expand Down
30 changes: 20 additions & 10 deletions lib/create-uniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
return proc(gl, wrapper, locations)
}

function makePropSetter(path, index, type) {
function makePropSetter(path, index, type, size, name) {
switch(type) {
case 'bool':
case 'int':
Expand All @@ -47,12 +47,18 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
if(d < 2 || d > 4) {
throw new GLError('', 'Invalid data type')
}
let checkStr = ''
if(size && size > d){
checkStr = 'if(!(obj instanceof Float32Array)){throw new Error("must use Float32Array to set uniform array '+name+'")};\n';
const valueSize = size * d;
checkStr += 'if(obj.length !== '+valueSize+'){throw new Error("for uniform '+name+' size is " + obj.length + " but should be ' + valueSize + '")};\n'
}
switch(type.charAt(0)) {
case 'b':
case 'i':
return 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')'
return checkStr + 'gl.uniform' + d + 'iv(locations[' + index + '],obj' + path + ')'
case 'v':
return 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')'
return checkStr + 'gl.uniform' + d + 'fv(locations[' + index + '],obj' + path + ')'
default:
throw new GLError('', 'Unrecognized data type for vector ' + name + ': ' + type)
}
Expand Down Expand Up @@ -99,7 +105,7 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
var path = item[0]
var idx = item[1]
if(locations[idx]) {
code.push(makePropSetter(path, idx, uniforms[idx].type))
code.push(makePropSetter(path, idx, uniforms[idx].type, uniforms[idx].size, uniforms[idx].name))
}
}
code.push('return obj}')
Expand Down Expand Up @@ -143,17 +149,17 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {

function storeProperty(obj, prop, type) {
if(typeof type === 'object') {
var child = processObject(type)
// var child = processObject(type)
Object.defineProperty(obj, prop, {
get: identity(child),
//get: identity(child),
set: makeSetter(type),
enumerable: true,
configurable: false
})
} else {
if(locations[type]) {
Object.defineProperty(obj, prop, {
get: makeGetter(type),
// get: makeGetter(type),
set: makeSetter(type),
enumerable: true,
configurable: false
Expand All @@ -167,13 +173,17 @@ function createUniformWrapper(gl, wrapper, uniforms, locations) {
function processObject(obj) {
var result
if(Array.isArray(obj)) {
result = new Array(obj.length)
throw new Error('should not happen')
/*result = new Array(obj.length)
for(var i=0; i<obj.length; ++i) {
storeProperty(result, i, obj[i])
}
}*/
} else {
result = {}
for(var id in obj) {
//for(var id in obj) {
const keys = Object.keys(obj)
for(let i=0;i<keys.length;++i){
const id = keys[i];
storeProperty(result, id, obj[id])
}
}
Expand Down
11 changes: 6 additions & 5 deletions lib/runtime-reflect.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,20 @@ function runtimeUniforms(gl, program) {
var info = gl.getActiveUniform(program, i)
if(info) {
var type = getType(gl, info.type)
if(info.size > 1) {
/*if(info.size > 1) {
for(var j=0; j<info.size; ++j) {
result.push({
name: info.name.replace('[0]', '[' + j + ']'),
type: type
})
}
} else {
} else {*/
result.push({
name: info.name,
type: type
name: info.name.replace('[0]', ''),
type: type,
size: info.size,
})
}
// }
}
}
return result
Expand Down
5 changes: 3 additions & 2 deletions lib/shader-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ function compileShader(gl, type, src) {
var fmt = formatCompilerError(errLog, src, type);
} catch (e){
console.warn('Failed to format compiler error: ' + e);
console.log('src is:\n'+src)
throw new GLError(errLog, 'Error compiling shader:\n' + errLog)
}
throw new GLError(errLog, fmt.short, fmt.long)
Expand All @@ -71,7 +72,7 @@ proto.getShaderReference = function(type, src) {
var gl = this.gl
var shaders = this.shaders[(type === gl.FRAGMENT_SHADER)|0]
var shader = shaders[src]
if(!shader || !gl.isShader(shader.shader)) {
if(!shader/* || !gl.isShader(shader.shader)*/) {
var shaderObj = compileShader(gl, type, src)
shader = shaders[src] = new ShaderReference(
SHADER_COUNTER++,
Expand Down Expand Up @@ -105,7 +106,7 @@ function linkProgram(gl, vshader, fshader, attribs, locations) {
proto.getProgram = function(vref, fref, attribs, locations) {
var token = [vref.id, fref.id, attribs.join(':'), locations.join(':')].join('@')
var prog = this.programs[token]
if(!prog || !this.gl.isProgram(prog)) {
if(!prog/* || !this.gl.isProgram(prog)*/) {
this.programs[token] = prog = linkProgram(
this.gl,
vref.shader,
Expand Down
16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gl-shader",
"version": "4.2.0",
"name": "@lfdoherty/gl-shader",
"version": "4.2.3",
"description": "WebGL shader wrapper",
"main": "index.js",
"directories": {
Expand All @@ -15,11 +15,11 @@
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "beefy --open example/example.js"
"start": "beefy example/example.js"
},
"repository": {
"type": "git",
"url": "git://github.com/stackgl/gl-shader.git"
"url": "git://github.com/lfdoherty/gl-shader.git"
},
"keywords": [
"webgl",
Expand All @@ -32,11 +32,7 @@
"3d",
"gl"
],
"author": "Mikola Lysenko",
"author": "Mikola Lysenko, Liam Doherty",
"license": "MIT",
"readmeFilename": "README.md",
"gitHead": "e28ce1d36277aeb6d109d886e847c7cdc1903cd9",
"bugs": {
"url": "https://github.com/stackgl/gl-shader/issues"
}
"readmeFilename": "README.md"
}