Skip to content

Commit 76bb946

Browse files
committed
Fix bad property swizzle-set
1 parent 7fc406e commit 76bb946

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

lib/index.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var Descriptor = require('./descriptor')
2323
var prepr = require('prepr')
2424

2525
var floatRE = /^-?[0-9]*(?:.[0-9]+)?(?:e-?[0-9]+)?$/i
26-
26+
var swizzleRE = /^[xyzwstpdrgba]{1,4}$/
2727

2828
/**
2929
* Create GLSL codegen instance
@@ -643,7 +643,7 @@ GLSL.prototype.transforms = {
643643
var prop = node.children[1].data
644644

645645
//ab.xyz for example
646-
if (/^[xyzwstpdrgba]{1,4}$/.test(prop)) {
646+
if (swizzleRE.test(prop)) {
647647
return this.unswizzle(node)
648648
}
649649

@@ -811,7 +811,7 @@ GLSL.prototype.transforms = {
811811

812812
//something[N] return as is
813813
return Descriptor(`${left}[${right}]`, {
814-
type: type,
814+
type: type || null,
815815
complexity: left.complexity + right.complexity + 1
816816
})
817817
}
@@ -836,7 +836,11 @@ GLSL.prototype.transforms = {
836836
}
837837

838838
var target = left
839-
var isSwizzle = node.children[0].type === 'operator' && /^[xyzwstpdrgba]{1,4}$/.test(node.children[0].children[1].data)
839+
840+
// here some targets may be unswizzled already, eg.
841+
// [a[0], a[1]], a[0][0], etc.
842+
var isSwizzle = node.children[0].type === 'operator' &&
843+
swizzleRE.test(node.children[0].children[1].data)
840844

841845
//a *= b.x
842846
if (!isSwizzle && this.types[right.type].length == 1 && this.types[target.type].length == 1) {
@@ -848,7 +852,7 @@ GLSL.prototype.transforms = {
848852

849853
//FIXME: left can be a structure property set a.prop
850854

851-
//in cases of setting swizzle - we gotta drop left unswizzle to the right
855+
//in cases of setting swizzle - we have to place left unswizzle to the right
852856
if (isSwizzle) {
853857
var positions = this.swizzlePositions(node.children[0].children[1].data)
854858
var len = this.types[this.process(node.children[0].children[0]).type].length
@@ -858,10 +862,15 @@ GLSL.prototype.transforms = {
858862
ids[positions[i]] = i
859863
}
860864

861-
target = Descriptor(node.children[0].children[0].data, {
862-
type: right.type,
863-
optimize: false
864-
})
865+
var targetType = node.children[0].children[0].type
866+
867+
// a.x = ...
868+
if ((targetType === 'ident' || targetType === 'builtin')) {
869+
target = Descriptor(node.children[0].children[0].data, {
870+
type: right.type,
871+
optimize: false
872+
})
873+
}
865874

866875
//a.wy *= a.zx →
867876
//a = [null, 1, null, 0].map(function (idx, i) {
@@ -903,10 +912,17 @@ GLSL.prototype.transforms = {
903912
}
904913
//a.x *= b → a[0] *= b
905914
else {
906-
return Descriptor(`${target}[${positions[0]}] ${operator} ${right}`, {
907-
type: right.type,
908-
optimize: false
909-
})
915+
if (targetType === 'builtin' || targetType === 'ident') {
916+
return Descriptor(`${target}[${positions[0]}] ${operator} ${right}`, {
917+
type: right.type,
918+
optimize: false
919+
})
920+
} else {
921+
return Descriptor(`${target} ${operator} ${right}`, {
922+
type: right.type,
923+
optimize: false
924+
})
925+
}
910926
}
911927
}
912928

@@ -1212,11 +1228,13 @@ GLSL.prototype.unswizzle = function (node) {
12121228
}
12131229
else {
12141230
if (args[0] == null) console.warn(`Cannot unswizzle '${ident.type}(${ident}).${prop}': ${prop} is outside the type range.`)
1231+
12151232
result = Descriptor(args[0] || `undefined`, {
12161233
type: 'float',
12171234
complexity: 1
12181235
})
12191236
}
1237+
12201238
return result
12211239
}
12221240

test/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,30 @@ test('Episodes', function () {
293293
})
294294

295295
test(`normalize(vec2(b));`, function () {
296-
var compile = GLSL({includes: false});
296+
var compile = GLSL({includes: false})
297297

298298
assert.equal(clean(compile(this.title)), clean(`
299299
normalize([b, b]);
300300
`))
301301
})
302+
303+
test(`a[0].x = 0.0; a[1].y = 1.0;`, function () {
304+
var compile = GLSL({includes: false})
305+
306+
assert.equal(clean(compile(this.title)), clean(`
307+
a[0][0] = 0.0;
308+
a[1][1] = 1.0;
309+
`))
310+
})
311+
312+
// TODO
313+
test.skip(`a[2].zw = 2.0;`, function () {
314+
var compile = GLSL({includes: false});
315+
316+
assert.equal(clean(compile(this.title)), clean(`
317+
[a[2][2], a[2][3]] = [2.0, 2.0]
318+
`))
319+
})
302320
});
303321

304322

0 commit comments

Comments
 (0)