Package glitter :: Package shaders :: Module uniform
[hide private]
[frames] | no frames]

Source Code for Module glitter.shaders.uniform

  1  """Descriptors for L{ShaderProgram} uniforms. 
  2   
  3  End users should typically not need to use this module directly. 
  4   
  5  @bug: Structs and arrays of structs are currently unimplemented. 
  6   
  7  @author: Stephan Wenger 
  8  @date: 2012-02-29 
  9  """ 
 10   
 11  from collections import OrderedDict as _odict 
 12  import numpy as _np 
 13   
 14  import glitter.raw as _gl 
15 16 -class BaseUniform(object):
17 pass
18
19 -class Uniform(BaseUniform):
20 - def __init__(self, name, location, dtype, size, parent):
21 self.name = name 22 self.location = location 23 self.dtype = dtype 24 self.size = size 25 self.parent = parent 26 self.textures = []
27
28 - def __str__(self):
29 return "uniform %s %s[%d];" % (self.dtype, self.name, self.size)
30
31 - def __get__(self, obj, cls=None, get_texture_unit_instead_of_object=False):
32 if self.dtype.is_texture() and not get_texture_unit_instead_of_object: 33 return self.textures[0] if len(self.textures) == 1 else self.textures 34 else: 35 with obj._context: 36 if self.size == 1: 37 data = self.dtype.get_value(obj, self.location) 38 return data.item() if len(data) == 1 else data 39 else: 40 data = [self.dtype.get_value(obj, _gl.glGetUniformLocation(obj._id, "%s[%d]" % (self.name, i))) for i in range(self.size)] 41 return _np.concatenate([x.squeeze()[None] for x in data])
42
43 - def __set__(self, obj, value):
44 if self.dtype.is_texture(): 45 if self.parent._context.current_program == self.parent: 46 self._on_release() 47 self.textures = value if hasattr(value, "__iter__") else [value] 48 if self.parent._context.current_program == self.parent: 49 self._on_bind() 50 else: 51 self.dtype.set_value(obj, self.location, value, self.size)
52
53 - def _on_bind(self):
54 if self.dtype.is_texture(): 55 units = [self.parent._context.texture_units.bind(x) for x in self.textures] 56 if units: 57 self.dtype.set_value(self.parent, self.location, units, self.size)
58
59 - def _on_release(self):
60 if self.dtype.is_texture(): 61 for x in self.textures: 62 self.parent._context.texture_units.release(x)
63
64 -class UniformStruct(_odict, BaseUniform):
65 - def __init__(self, name, parent):
66 super(UniformStruct, self).__init__() 67 self.name = name 68 self.parent = parent
69
70 - def __str__(self):
71 return "uniform struct { %s } %s;" % (" ".join(str(value) for value in self.values()), self.name)
72
73 - def __get__(self, obj, cls=None):
74 """@todo: Implement this.""" 75 raise NotImplementedError
76
77 - def __set__(self, obj, value):
78 """@todo: Implement this.""" 79 raise NotImplementedError
80
81 - def _on_bind(self):
82 """@todo: Implement this: bind textures.""" 83 pass
84
85 - def _on_release(self):
86 """@todo: Implement this: restore old texture bindings.""" 87 pass
88
89 -class UniformStructArray(_odict, BaseUniform):
90 - def __init__(self, name, parent):
91 super(UniformStructArray, self).__init__() 92 self.name = name 93 self.parent = parent
94 95 @property
96 - def size(self):
97 return max(index for index, field in self.keys()) + 1
98
99 - def __str__(self):
100 unique_values = _odict((field, value) for ((index, field), value) in self.items()) 101 return "uniform struct { %s } %s[%d];" % (" ".join(str(value) for value in unique_values.values()), self.name, self.size)
102
103 - def __get__(self, obj, cls=None):
104 """@todo: Implement this.""" 105 raise NotImplementedError
106
107 - def __set__(self, obj, value):
108 """@todo: Implement this.""" 109 raise NotImplementedError
110
111 - def _on_bind(self):
112 """@todo: Implement this: bind textures.""" 113 pass
114
115 - def _on_release(self):
116 """@todo: Implement this: restore old texture bindings.""" 117 pass
118