コード例 #1
0
ファイル: buffer.js プロジェクト: hughsk/gl-buffer
function makeScratchTypeArray(array, dtype) {
  var res = pool.malloc(array.length, dtype)
  var n = array.length
  for(var i=0; i<n; ++i) {
    res[i] = array[i]
  }
  return res
}
コード例 #2
0
function createBuffer(gl, type, data, usage) {
  if(data === undefined) {
    data = type
    type = gl.ARRAY_BUFFER
  }
  if(!usage) {
    usage = gl.DYNAMIC_DRAW
  }
  var len = 0
  var handle = gl.createBuffer()
  gl.bindBuffer(type, handle)
  if(typeof data === "number") {
    gl.bufferData(type, data, usage)
    len = data
  } else if(data instanceof Array) {
    if(type === gl.ELEMENT_ARRAY_BUFFER) {
      gl.bufferData(type, new Uint16Array(data), usage)
    } else {
      gl.bufferData(type, new Float32Array(data), usage)
    }
    len = data.length
  } else if(data.length) {
    gl.bufferData(type, data, usage)
    len = data.length
  } else if(data.shape) {
    var dtype = data.dtype
    if(dtype === "float64" || dtype === "array" || dtype === "generic") {
      dtype = "float32"
    }
    if(type === gl.ELEMENT_ARRAY_BUFFER) {
      dtype = "uint16"
    }
    if(data.shape.length !== 1) {
      throw new Error("gl-buffer: Array shape must be 1D")
    }
    var len = data.shape[0]
    if(dtype === data.type && data.stride[0] === 1) {
      gl.bufferData(type, data.data.subarray(data.offset, data.offset+len), usage)
    } else {
      var tmp = pool.malloc(data.shape[0], dtype)
      var ndt = ndarray(tmp)
      ops.assign(ndt, data)
      gl.bufferData(type, tmp, usage)
      pool.free(tmp)
    }
  } else {
    throw new Error("gl-buffer: Invalid format for buffer data")
  }
  if(type !== gl.ARRAY_BUFFER && type !== gl.ELEMENT_ARRAY_BUFFER) {
    throw new Error("gl-buffer: Invalid type for webgl buffer")
  }
  if(usage !== gl.DYNAMIC_DRAW && usage !== gl.STATIC_DRAW && usage !== gl.STREAM_DRAW) {
    throw new Error("gl-buffer: Invalid usage for buffer")
  }
  return new GLBuffer(gl, type, handle, len, usage)
}
コード例 #3
0
ファイル: buffer.js プロジェクト: hughsk/gl-buffer
GLBuffer.prototype.update = function(array, offset) {
  if(!offset) {
    offset = 0
  }
  this.bind()
  if(typeof array === "number") {
    if(offset >= 0) {
      throw new Error("Offset must be < 0 to resize buffer")
    }
    this.gl.bufferData(this.type, array, this.usage)
    this.length = array
  } else if(array.shape) {
    var dtype = array.dtype
    if(dtype === "float64" || dtype === "array" || dtype === "generic") {
      dtype = "float32"
    }
    if(this.type === this.gl.ELEMENT_ARRAY_BUFFER) {
      dtype = "uint16"
    }
    if(array.shape.length !== 1) {
      throw new Error("Array length must be 1")
    }
    if(dtype === array.dtype && array.stride[0] === 1) {
      if(array.offset === 0 && array.data.length === array.shape[0]) {
        this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array.data, offset)
      } else {
        this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array.data.subarray(array.offset, array.shape[0]), offset)
      }
    } else {
      var tmp = pool.malloc(array.shape[0], dtype)
      var ndt = ndarray(tmp)
      ops.assign(ndt, array)
      this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, tmp, offset)
      pool.free(tmp)
    }
  } else if(array instanceof Array) {
    if(this.type === this.gl.ELEMENT_ARRAY_BUFFER) {
      var t = makeScratchTypeArray(array, "uint16")
      this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, t.subarray(0, array.length), offset)
      pool.freeUint16(t)
    } else {
      var t = makeScratchTypeArray(array, "float32")
      this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, t.subarray(0, array.length), offset)
      pool.freeFloat32(t)
    }
  } else {
    this.length = updateTypeArray(this.gl, this.type, this.length, this.usage, array, offset)
  }
}
コード例 #4
0
ファイル: translate.js プロジェクト: scijs/ndarray-translate
function translateZeroBC(arr, t) {
  var shape = arr.shape.slice()
    , d = shape.length
    , v = new Array(d)
    , u = new Array(d)
    , i, j
  for(i=0; i<d; ++i) {
    if(Math.abs(t[i]) >= shape[i]) {
      ops.assigns(arr, 0)
      return arr
    }
    j = -Math.round(t[i])|0
    if(j > 0) {
      v[i] = j
      u[i] = shape[i] - j
    } else {
      v[i] = 0
      u[i] = shape[i]
    }
  }
  var q = arr.lo.apply(arr, v)
    , p = arr.hi.apply(arr, u)
  for(i=0; i<d; ++i) {
    j = -Math.round(t[i])|0
    if(j < 0) {
      v[i] = q.shape[i] + j
      u[i] = -j
    } else {
      v[i] = q.shape[i]
      u[i] = 0
    }
  }
  q = q.hi.apply(q, v)
  p = p.lo.apply(p, u)
  var y_t = pool.malloc(q.size, arr.dtype)
    , y = ndarray(y_t, q.shape)
  ops.assign(y, q)
  ops.assigns(arr, 0)
  ops.assign(p, y)
  pool.free(y_t)
  return arr
}
コード例 #5
0
ファイル: kdtree.js プロジェクト: KimGressens/static-kdtree
function createKDTree(points) {
  var n, d, indexed
  if(Array.isArray(points)) {
    n = points.length
    if(n === 0) {
      return new KDTree(null, null, 0, 0)
    }
    d = points[0].length
    indexed = ndarray(pool.mallocDouble(n*(d+1)), [n, d+1])
    pack(points, indexed.hi(n, d))
  } else {
    n = points.shape[0]
    d = points.shape[1]

    //Round up data type size
    var type = points.dtype
    if(type === "int8" ||
       type === "int16" ||
       type === "int32" ) {
      type = "int32"
    } else if(type === "uint8" ||
      type === "uint8_clamped" ||
      type === "buffer" ||
      type === "uint16" ||
      type === "uint32") {
      type = "uint32"
    } else if(type === "float32") {
      type = "float32"
    } else {
      type = "float64"
    }
    indexed = ndarray(pool.malloc(n*(d+1)), [n, d+1])
    ops.assign(indexed.hi(n,d), points)
  }
  for(var i=0; i<n; ++i) {
    indexed.set(i, d, i)
  }

  var pointArray = ndscratch.malloc([n, d], points.dtype)
  var indexArray = pool.mallocInt32(n)
  var pointer = 0
  var pointData = pointArray.data
  var arrayData = indexed.data
  var l2_n = bits.log2(bits.nextPow2(n))

  var sel_cmp = ndselect.compile(indexed.order, true, indexed.dtype)

  //Walk tree in level order
  var toVisit = [indexed]
  while(pointer < n) {
    var head = toVisit.shift()
    var array = head
    var nn = array.shape[0]|0
    
    //Find median
    if(nn > 1) {
      var k = bits.log2(pointer+1)%d
      var median
      var n_2 = inorderTree.root(nn)
      median = sel_cmp(array, n_2, function(a,b) {
        return a.get(k) - b.get(k)
      })

      //Copy into new array
      var pptr = pointArray.index(pointer, 0)
      var mptr = median.offset
      for(var i=0; i<d; ++i) {
        pointData[pptr++] = arrayData[mptr++]
      }
      indexArray[pointer] = arrayData[mptr]
      pointer += 1

      //Queue new items
      toVisit.push(array.hi(n_2))
      if(nn > 2) {
        toVisit.push(array.lo(n_2+1))
      }
    } else {
      //Copy into new array
      var mptr = array.offset
      var pptr = pointArray.index(pointer, 0)
      for(var i=0; i<d; ++i) {
        pointData[pptr+i] = arrayData[mptr++]
      }
      indexArray[pointer] = arrayData[mptr]
      pointer += 1
    }
  }

  //Release indexed
  pool.free(indexed.data)

  return new KDTree(pointArray, indexArray, n, d)
}
コード例 #6
0
ファイル: texture.js プロジェクト: timoxley/gl-texture2d
//Creates a texture from an ndarray
function createTextureArray(gl, array) {
  var dtype = array.dtype
  var shape = array.shape.slice()
  var maxSize = gl.getParameter(gl.MAX_TEXTURE_SIZE)
  if(shape[0] < 0 || shape[0] > maxSize || shape[1] < 0 || shape[1] > maxSize) {
    throw new Error('gl-texture2d: Invalid texture size')
  }
  var packed = isPacked(shape, array.stride.slice())
  var type = 0
  if(dtype === 'float32') {
    type = gl.FLOAT
  } else if(dtype === 'float64') {
    type = gl.FLOAT
    packed = false
    dtype = 'float32'
  } else if(dtype === 'uint8') {
    type = gl.UNSIGNED_BYTE
  } else {
    type = gl.UNSIGNED_BYTE
    packed = false
    dtype = 'uint8'
  }
  var format = 0
  if(shape.length === 2) {
    format = gl.LUMINANCE
    shape = [shape[0], shape[1], 1]
    array = ndarray(array.data, shape, [array.stride[0], array.stride[1], 1], array.offset)
  } else if(shape.length === 3) {
    if(shape[2] === 1) {
      format = gl.ALPHA
    } else if(shape[2] === 2) {
      format = gl.LUMINANCE_ALPHA
    } else if(shape[2] === 3) {
      format = gl.RGB
    } else if(shape[2] === 4) {
      format = gl.RGBA
    } else {
      throw new Error('gl-texture2d: Invalid shape for pixel coords')
    }
  } else {
    throw new Error('gl-texture2d: Invalid shape for texture')
  }
  if(type === gl.FLOAT && !webglew(gl).texture_float) {
    type = gl.UNSIGNED_BYTE
    packed = false
  }
  var buffer, buf_store
  var size = array.size
  if(!packed) {
    var stride = [shape[2], shape[2]*shape[0], 1]
    buf_store = pool.malloc(size, dtype)
    var buf_array = ndarray(buf_store, shape, stride, 0)
    if((dtype === 'float32' || dtype === 'float64') && type === gl.UNSIGNED_BYTE) {
      convertFloatToUint8(buf_array, array)
    } else {
      ops.assign(buf_array, array)
    }
    buffer = buf_store.subarray(0, size)
  } else if (array.offset === 0 && array.data.length === size) {
    buffer = array.data
  } else {
    buffer = array.data.subarray(array.offset, array.offset + size)
  }
  var tex = initTexture(gl)
  gl.texImage2D(gl.TEXTURE_2D, 0, format, shape[0], shape[1], 0, format, type, buffer)
  if(!packed) {
    pool.free(buf_store)
  }
  return new Texture2D(gl, tex, shape[1], shape[0], format, type)
}