Skip to content

Commit 4a5c8de

Browse files
author
Carlos Scheidegger
committed
disallow dFdx and friends in vertex programs
1 parent faec1c3 commit 4a5c8de

5 files changed

Lines changed: 57 additions & 25 deletions

File tree

lux.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10844,6 +10844,16 @@ Shade.program = function(program_obj)
1084410844
return exp.parents[0];
1084510845
});
1084610846

10847+
var disallowed_vertex_expressions = shade_values_vp_obj.find_if(function(x) {
10848+
if (x.expression_type === 'builtin_function{dFdx}') return true;
10849+
if (x.expression_type === 'builtin_function{dFdy}') return true;
10850+
if (x.expression_type === 'builtin_function{fwidth}') return true;
10851+
return false;
10852+
});
10853+
if (disallowed_vertex_expressions.length > 0) {
10854+
throw "'" + disallowed_vertex_expressions[0] + "' not allowed in vertex expression";
10855+
}
10856+
1084710857
vp_obj = _.object(shade_values_vp_obj.fields, shade_values_vp_obj.parents);
1084810858
vp_discard_conditions = _.values(vp_discard_conditions);
1084910859

@@ -10854,6 +10864,8 @@ Shade.program = function(program_obj)
1085410864
fp_obj.gl_FragColor = fp_obj.gl_FragColor.discard_if(vp_discard_condition);
1085510865
}
1085610866

10867+
10868+
1085710869
var common_sequence = [
1085810870
[Shade.Optimizer.is_times_zero, Shade.Optimizer.replace_with_zero,
1085910871
"v * 0", true],

lux.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/shade/program.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ Shade.program = function(program_obj)
407407
return exp.parents[0];
408408
});
409409

410+
var disallowed_vertex_expressions = shade_values_vp_obj.find_if(function(x) {
411+
if (x.expression_type === 'builtin_function{dFdx}') return true;
412+
if (x.expression_type === 'builtin_function{dFdy}') return true;
413+
if (x.expression_type === 'builtin_function{fwidth}') return true;
414+
return false;
415+
});
416+
if (disallowed_vertex_expressions.length > 0) {
417+
throw "'" + disallowed_vertex_expressions[0] + "' not allowed in vertex expression";
418+
}
419+
410420
vp_obj = _.object(shade_values_vp_obj.fields, shade_values_vp_obj.parents);
411421
vp_discard_conditions = _.values(vp_discard_conditions);
412422

@@ -417,6 +427,8 @@ Shade.program = function(program_obj)
417427
fp_obj.gl_FragColor = fp_obj.gl_FragColor.discard_if(vp_discard_condition);
418428
}
419429

430+
431+
420432
var common_sequence = [
421433
[Shade.Optimizer.is_times_zero, Shade.Optimizer.replace_with_zero,
422434
"v * 0", true],

tests/test_bugs.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@ test("Tests from previous Lux bugs", function() {
55
{"type": "constant", "guid":9997, "parents": [], "values": [0]},
66
{"type": "constant", "guid":9996, "parents": [], "values": [1]}]};
77

8-
// var condition = {"type":"gt","guid":245,"parents":[
9-
// {"type":"swizzle{z}","guid":243,"parents":[
10-
// {"type":"cross","guid":242,"parents":[
11-
// {"type":"normalize","guid":238,"parents":[
12-
// {"type":"cross","guid":237,"parents":[
13-
// {"type":"dFdx","guid":235,"parents":[
14-
// {"type":"div","guid":234,"parents":[
15-
// {"type":"swizzle{xyz}","guid":232,"parents":[{"type":"reference","guid":227}]},
16-
// {"type":"swizzle{w}","guid":233,"parents":[{"type":"reference","guid":227}]}]}]},
17-
// {"type":"dFdy","guid":236,"parents":[{"type":"reference","guid":234}]}]}]},
18-
// {"type":"vec","guid":241,"parents":[
19-
// {"type":"constant","guid":239,"parents":[],"values":[0]},
20-
// {"type":"constant","guid":240,"parents":[],"values":[0]},
21-
// {"type":"ifelse","guid":231,"parents":[
22-
// {"type":"constant","guid":228,"parents":[],"values":[true]},
23-
// {"type":"constant","guid":229,"parents":[],"values":[1]},
24-
// {"type":"constant","guid":230,"parents":[],"values":[-1]}]}]}]}]},
25-
// {"type":"constant","guid":244,"parents":[],"values":[0]}]};
26-
278
var condition = {"type":"gt","guid":245,"parents":[
289
{"type":"swizzle{z}","guid":243,"parents":[
2910
{"type":"dFdx","guid":235,"parents":[
@@ -33,11 +14,32 @@ test("Tests from previous Lux bugs", function() {
3314
{"type":"constant","guid":244,"parents":[],"values":[0]}]};
3415

3516
var node = {"type":"discard_if","guid":246,"parents":[exp,condition]};
36-
3717
var node_exp = Shade.Debug.from_json(node);
3818
var prog = { position: node_exp,
3919
color: Shade.vec(0,0,0,1) };
4020

41-
debugger;
21+
ok(Shade.program(prog), "backface culling should not crash optimizer");
22+
23+
var condition2 = {"type":"gt","guid":245,"parents":[
24+
{"type":"swizzle{z}","guid":243,"parents":[
25+
{"type":"cross","guid":242,"parents":[
26+
{"type":"normalize","guid":238,"parents":[
27+
{"type":"cross","guid":237,"parents":[
28+
{"type":"dFdx","guid":235,"parents":[
29+
{"type":"div","guid":234,"parents":[
30+
{"type":"swizzle{xyz}","guid":232,"parents":[{"type":"reference","guid":227}]},
31+
{"type":"swizzle{w}","guid":233,"parents":[{"type":"reference","guid":227}]}]}]},
32+
{"type":"dFdy","guid":236,"parents":[{"type":"reference","guid":234}]}]}]},
33+
{"type":"vec","guid":241,"parents":[
34+
{"type":"constant","guid":239,"parents":[],"values":[0]},
35+
{"type":"constant","guid":240,"parents":[],"values":[0]},
36+
{"type":"ifelse","guid":231,"parents":[
37+
{"type":"constant","guid":228,"parents":[],"values":[true]},
38+
{"type":"constant","guid":229,"parents":[],"values":[1]},
39+
{"type":"constant","guid":230,"parents":[],"values":[-1]}]}]}]}]},
40+
{"type":"constant","guid":244,"parents":[],"values":[0]}]};
41+
var prog2 = { position: Shade.Debug.from_json({"type":"discard_if","guid":246,"parents":[exp,condition2]}),
42+
color: Shade.vec(0,0,0,1) };
43+
4244
ok(Shade.program(prog), "backface culling should not crash optimizer");
4345
});

tests/tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,12 @@ test("Shade programs", function() {
671671
color: Shade.vec(1,1,1,1),
672672
position: Shade.vec(1,1,1,1)
673673
}), "Basic program");
674+
675+
raises(function() {
676+
Shade.program({
677+
position: Shade.dFdx(Shade.attribute("vec4"))
678+
});
679+
}, "'builtin_function{dFdx}' not allowed in vertex expression");
674680
});
675681

676682
test("Shade loops", function() {

0 commit comments

Comments
 (0)