def source(self): with self._context: _shader_source_length = _gl.GLint() _gl.glGetShaderiv(self._id, _gl.GL_SHADER_SOURCE_LENGTH, _gl.pointer(_shader_source_length)) _source = _gl.create_string_buffer(_shader_source_length.value) _gl.glGetShaderSource(self._id, _shader_source_length, _gl.POINTER(_gl.GLint)(), _source) return _source.value
def _log(self): with self._context: _info_log_length = _gl.GLint() _gl.glGetShaderiv(self._id, _gl.GL_INFO_LOG_LENGTH, _gl.pointer(_info_log_length)) _info_log = _gl.create_string_buffer(_info_log_length.value) _gl.glGetShaderInfoLog(self._id, _info_log_length, _gl.POINTER(_gl.GLint)(), _info_log) return _info_log.value
def _log(self): """The current shader info log. @rtype: C{string} """ _info_log = _gl.create_string_buffer(self._info_log_length) with self._context: _gl.glGetProgramInfoLog(self._id, self._info_log_length, _gl.POINTER(_gl.GLint)(), _info_log) return _info_log.value
def set_data(self, data=None, shape=None, dtype=None, level=0, depth=False, stencil=False, mipmap=False, set_default_interpolation=False): if data is None: if shape is None: raise ValueError("must specify either data or shape") if dtype is None: dtype = float32 else: data = coerce_array(data, dtype) if shape is not None: data = data.reshape(shape) shape = data.shape dtype = Datatype.from_numpy(data.dtype) if len(shape) != self._ndim: raise TypeError("shape must be %d-dimensional" % self._ndim) _iformat = dtype_to_gl_iformat[dtype, shape[-1]] _format = dtype_to_gl_format[dtype, shape[-1]] _type = dtype._as_gl() if depth and stencil: raise NotImplementedError("depth-and-stencil textures are currently not supported") elif depth: _iformat = _format = None for (nptype, colors), ifmt, (gltype, fmt) in texture_formats: # @UnusedVariable if nptype == dtype and colors == shape[-1] and fmt == _gl.GL_DEPTH_COMPONENT: _iformat, _format = ifmt, fmt if _iformat is None or _format is None: raise TypeError("no matching depth texture format for %d %s colors" % (shape[-1], dtype)) elif stencil: raise NotImplementedError("stencil textures are currently not supported") _data = data.ctypes if data is not None else _gl.POINTER(_gl.GLvoid)() _gl.glPixelStorei(_gl.GL_UNPACK_ALIGNMENT, 1) with self: args = [self._target, level, _iformat] + list(reversed(shape[:-1])) + [0, _format, _type, _data] self._set(*args) if not dtype.is_float(): # NEAREST is the only possible options for non-floats. self.min_filter = self.min_filters.NEAREST_MIPMAP_NEAREST if mipmap else self.min_filters.NEAREST # mipmap untested, use at own risk! self.mag_filter = self.mag_filters.NEAREST elif set_default_interpolation: self.min_filter = self.min_filters.LINEAR_MIPMAP_LINEAR if mipmap else self.min_filters.LINEAR self.mag_filter = self.mag_filters.LINEAR # No unambiguous way to determine depth component from dtype, # so we have to save it separately. self._depth = depth self._stencil = stencil if mipmap: self.generate_mipmap()
def __setitem__(self, index, value): if value is None: with self: _gl.glVertexAttribPointer(index, 4, _gl.GL_FLOAT, _gl.GL_FALSE, 0, _gl.POINTER(_gl.GLvoid)()) _gl.glDisableVertexAttribArray(index) else: if not isinstance(value, ArrayBuffer): value = ArrayBuffer(value, context=self._context) with self: with value: value._use(index) _gl.glEnableVertexAttribArray(index) self._attributes[index] = value
def set_data(self, data=None, shape=None, dtype=None, usage=None): if data is None: if shape is None or dtype is None: raise ValueError("must specify either data or both shape and dtype") self._shape = shape self._dtype = dtype.coerced(force_gl=True) else: data = coerce_array(data, dtype, force_gl=True) if shape is not None: data = data.reshape(shape) self._shape = data.shape self._dtype = Datatype.from_numpy(data.dtype) if usage is None: usage = self.usage _nbytes = _np.prod(self._shape) * self._dtype.nbytes _data = data.ctypes if data is not None else _gl.POINTER(_gl.GLvoid)() with self: _gl.glBufferData(self._target, _nbytes, _data, usage._value)
def source(self, source): with self._context: _gl.glShaderSource( self._id, 1, _gl.pointer(_gl.c_char_p(source.encode("ascii"))), _gl.POINTER(_gl.GLint)())