Skip to content

Commit

Permalink
Added discard parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
rougier committed Jan 15, 2015
1 parent 975ea49 commit 9d9bf9e
Show file tree
Hide file tree
Showing 10 changed files with 208 additions and 126 deletions.
3 changes: 0 additions & 3 deletions examples/snippet-usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@
program["transform_7"] = transform_3(transform_1("G.x"), transform_2("G.y"))
program["transform_8"] = transform_3(transform_1('.x', name='x'),
transform_2('.y', name='y'))

transform = transform_3(transform_1('.x', name='x'),
transform_2('.y', name='y'))
print program.vertex.code


Expand Down
6 changes: 4 additions & 2 deletions glumpy/gloo/shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ def replace_with_args(match):
s = snippet
for item in subhook.split('.')[:-1]:
if isinstance(s[item], Snippet):
print s, s[item]
s = s[item]
subhook = subhook.split('.')[-1]
# Do we have a class alias ? We don't return it yet since we
# need its translation from the symbol table
if subhook in s.aliases.keys():
subhook = s.aliases[subhook]
# If subhook is a variable (uniform/attribute/varying)
if subhook in s.globals:
return s.globals[subhook]
return s.mangled_call(subhook, match.group("args"))

#args = match.group('args')
# If subhook is a variable (uniform/attribute/varying)
if subhook in snippet.globals:
return snippet.globals[subhook]
Expand Down
17 changes: 12 additions & 5 deletions glumpy/gloo/snippet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class Snippet(object):
# Internal id counter for automatic snippets name mangling
_id_counter = 1

aliases = { }

def __init__(self, code=None, default=None, *args, **kwargs):

# Original source code
Expand Down Expand Up @@ -131,6 +133,7 @@ def locals(self):
objects = self._objects
for name,dtype in objects["uniforms"]+ objects["attributes"] + objects["varyings"]:
symbols[name] = self.symbols[name]

# return self._symbols
return symbols

Expand Down Expand Up @@ -293,13 +296,15 @@ def mangled_call(self, function=None, arguments=None):
# WARN: what about Viewport(Transform) ?
if len(self._objects["functions"]):

# Has a function be specified when building the snippet ?
if self._call is not None:
name = self._call
# Is there a function specified in the shader source ?
# Such as <transform.forward>
elif function:
if function:
name = function

# Has a function be specified when building the snippet ?
# Snippet(..., call="some_function")
elif self._call is not None:
name = self._call
else:
_,name,_,_ = self._objects["functions"][0]

Expand All @@ -309,7 +314,9 @@ def mangled_call(self, function=None, arguments=None):
s += "("
for i,arg in enumerate(self._args):
if isinstance(arg,Snippet):
s += arg.mangled_call(function,arguments)
# We do not propagate given function to to other snippets
# s += arg.mangled_call(function,arguments)
s += arg.mangled_call(None,arguments)
else:
# This handle call of the form: transform('.x')
if arguments is not None and arg.startswith('.'):
Expand Down
76 changes: 46 additions & 30 deletions glumpy/library/transforms/linear-scale.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
(measured in pixels) in a histogram.
*/
uniform int linear_scale_clamp;
uniform int linear_scale_discard;
uniform vec2 linear_scale_range;
uniform vec2 linear_scale_domain;

Expand All @@ -21,32 +22,36 @@ float forward(float value)
{
vec2 domain = linear_scale_domain;
vec2 range = linear_scale_range;

float t = (value - domain.x) /(domain.y - domain.x);

if (linear_scale_clamp > 0)
#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (linear_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (linear_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);

return range.x + t*(range.y - range.x);
}

vec2 forward(vec2 value)
{
vec2 domain = linear_scale_domain;
vec2 range = linear_scale_range;

vec2 t = (value - domain.x) /(domain.y - domain.x);
if (linear_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (linear_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (linear_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);

return range.x + t*(range.y - range.x);
}
Expand All @@ -55,15 +60,17 @@ vec3 forward(vec3 value)
{
vec2 domain = linear_scale_domain;
vec2 range = linear_scale_range;

vec3 t = (value - domain.x) /(domain.y - domain.x);
if (linear_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (linear_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (linear_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);

return range.x + t*(range.y - range.x);
}
Expand All @@ -72,46 +79,55 @@ float inverse(float value)
{
vec2 domain = linear_scale_domain;
vec2 range = linear_scale_range;

float t = (abs(value) - range.x) / (range.y - range.x);
if (linear_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (linear_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (linear_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);

return domain.x + t*(domain.y - domain.x);
}

vec2 inverse(vec2 value)
{
vec2 domain = linear_scale_domain;
vec2 range = linear_scale_range;

vec2 t = (abs(value) - range.x) / (range.y - range.x);
if (linear_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (linear_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (linear_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);

return domain.x + t*(domain.y - domain.x);
}

vec3 inverse(vec3 value)
{
vec2 domain = linear_scale_domain;
vec2 range = linear_scale_range;

vec3 t = (abs(value) - range.x) / (range.y - range.x);
if (linear_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (linear_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (linear_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);

return domain.x + t*(domain.y - domain.x);
}
75 changes: 43 additions & 32 deletions glumpy/library/transforms/log-scale.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// ----------------------------------------------------------------------------

uniform int log_scale_clamp;
uniform int log_scale_discard;
uniform vec2 log_scale_range;
uniform vec2 log_scale_domain;
uniform float log_scale_base;
Expand All @@ -16,13 +17,16 @@ float forward(float value)

float v = log(value) / log(base);
float t = (v - domain.x) /(domain.y - domain.x);
if (log_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (log_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (log_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);
return sign(value) * (range.x + t*(range.y - range.x));
}

Expand All @@ -31,16 +35,18 @@ vec2 forward(vec2 value)
vec2 domain = log_scale_domain;
vec2 range = log_scale_range;
float base = log_scale_base;

vec2 v = log(value) / log(base);
vec2 t = (v - domain.x) /(domain.y - domain.x);
if (log_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (log_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (log_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);
return sign(value) * (range.x + t*(range.y - range.x));
}

Expand All @@ -49,17 +55,18 @@ vec3 forward(vec3 value)
vec2 domain = log_scale_domain;
vec2 range = log_scale_range;
float base = log_scale_base;

vec3 v = log(value) / log(base);
vec3 t = (v - domain.x) /(domain.y - domain.x);
if (log_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (log_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif


if (log_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);
return sign(value) * (range.x + t*(range.y - range.x));
}

Expand All @@ -69,15 +76,17 @@ float inverse(float value)
vec2 domain = log_scale_domain;
vec2 range = log_scale_range;
float base = log_scale_base;

float t = (abs(value) - range.x) / (range.y - range.x);
if (log_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (log_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (log_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);
float v = domain.x + t*(domain.y - domain.x);
return sign(value) * pow(base, abs(v));
}
Expand All @@ -87,17 +96,18 @@ vec2 inverse(vec2 value)
vec2 domain = log_scale_domain;
vec2 range = log_scale_range;
float base = log_scale_base;

vec2 t = (abs(value) - range.x) / (range.y - range.x);
if (log_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (log_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (log_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);
vec2 v = domain.x + t*(domain.y - domain.x);

return sign(value) * pow(vec2(base), abs(v));
}

Expand All @@ -106,16 +116,17 @@ vec3 inverse(vec3 value)
vec2 domain = log_scale_domain;
vec2 range = log_scale_range;
float base = log_scale_base;

vec3 t = (abs(value) - range.x) / (range.y - range.x);
if (log_scale_clamp > 0)

#ifdef __FRAGMENT_SHADER__
if (t != clamp(t, 0.0, 1.0)) discard;
#else
t = clamp(t, 0.0, 1.0);
if (log_scale_discard > 0) {
if (t != clamp(t, 0.0, 1.0))
discard;
}
#endif

if (log_scale_clamp > 0)
t = clamp(t, 0.0, 1.0);
vec3 v = domain.x + t*(domain.y - domain.x);

return sign(value) * pow(vec3(base), abs(v));
}
Loading

0 comments on commit 9d9bf9e

Please sign in to comment.