/
createView.js
370 lines (329 loc) · 11.7 KB
/
createView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const invariant = require("invariant");
function ContentTextureObject (contentId) {
return { type: "content", id: contentId };
}
function ImageTextureObject (srcOrObj) {
// FIXME: we should probably clarify into more types (image, array, ...).
// and `obj.value.uri` should be simplified to `obj.uri` in the image type
if (typeof srcOrObj === "string")
srcOrObj = { uri: srcOrObj };
return { type: "image", value: srcOrObj };
}
function FramebufferTextureObject (fbId) {
return { type: "framebuffer", id: fbId };
}
function extractImages (uniforms) {
const images = [];
for (let u in uniforms) {
let value = uniforms[u];
if (value &&
typeof value === "object" &&
value.type === "image" &&
value.value &&
typeof value.value.uri === "string") {
images.push(value.value);
}
}
return images;
}
function uniqImages (arr) {
var uris = [];
var coll = [];
arr.forEach(function (item) {
if (uris.indexOf(item.uri) === -1) {
uris.push(item.uri);
coll.push(item);
}
});
return coll;
}
module.exports = function (React, Shaders, Uniform, GLComponent, renderVcontainer, renderVcontent, renderVGL) {
const {
Component,
PropTypes
} = React;
function reactFirstChildOnly (children) {
return React.Children.count(children) === 1 ?
(children instanceof Array ? children[0] : children) :
null;
}
// buildData traverses the Virtual DOM to generates a data tree
function buildData (shader, glViewUniforms, width, height, glViewChildren, preload) {
invariant(Shaders.exists(shader), "Shader #%s does not exists", shader);
const shaderName = Shaders.getName(shader);
const uniforms = { ...glViewUniforms };
const children = [];
const contents = [];
React.Children.forEach(glViewChildren, child => {
invariant(child.type === Uniform, "(Shader '%s') GL.View can only contains children of type GL.Uniform. Got '%s'", shaderName, child.type && child.type.displayName || child);
const { name, children } = child.props;
invariant(typeof name === "string" && name, "(Shader '%s') GL.Uniform must define an name String", shaderName);
invariant(!glViewUniforms || !(name in glViewUniforms), "(Shader '%s') The uniform '%s' set by GL.Uniform must not be in {uniforms} props", shaderName);
invariant(!(name in uniforms), "(Shader '%s') The uniform '%s' set by GL.Uniform must not be defined in another GL.Uniform", shaderName);
uniforms[name] = children;
});
Object.keys(uniforms)
.filter(key => { // filter out the texture types...
const value = uniforms[key];
/*
FIXME This is very weak way of detecting this.
trusting the client to give appropriate value.
we need to find a better way.
unfortunately we might not be in browser context to do this with WebGL API.
Also `null` should be accepted if you just want the texture to be the default color (black transparent?).
*/
return value && (
typeof value === "function" ||
typeof value === "string" ||
typeof value === "object" && (!(value instanceof Array) || typeof value[0] === "object"));
})
.forEach(name => {
const value = uniforms[name];
if (value) {
if (typeof value !== "object" || !(value instanceof Array) && !React.isValidElement(value)) {
uniforms[name] = ImageTextureObject(value);
return;
}
else {
let childGLView;
// Recursively unfold the children while there are GLComponent and not a GLView
/* FIXME
* React might eventually improve to ease the work done here.
* see https://github.com/facebook/react/issues/4697#issuecomment-134335822
*/
let c = value;
do {
if (c.type === GLView) {
childGLView = c;
break;
}
if (typeof c.type !== "function") {
break;
}
let instance = new c.type();
if (!(instance instanceof GLComponent)) {
break;
}
instance.props = c.props;
c = reactFirstChildOnly(instance.render());
if (c && c.type === GLView) {
childGLView = c;
break;
}
}
while(c);
if (childGLView) {
const childProps = childGLView.props;
children.push({
vdom: value,
data: buildData(
childProps.shader,
childProps.uniforms,
childProps.width || width,
childProps.height || height,
childProps.children,
"preload" in childProps ? childProps.preload : preload),
uniform: name
});
return;
}
}
}
// in other cases, we will use child as a content
contents.push({
vdom: value,
uniform: name
});
});
return {
shader,
uniforms,
width,
height,
children,
contents,
preload
};
}
// resolveData takes the output of buildData to generate the final data tree
// that have resolved framebuffers and shared computation of duplicate uniforms (e.g: content / GL.View)
function resolveData (data) {
let imagesToPreload = [];
// contents are view/canvas/image/video to be rasterized "globally"
const contentsMeta = findContentsUniq(data);
const contentsVDOM = contentsMeta.map(({vdom}) => vdom);
// recursively find all contents but without duplicates by comparing VDOM reference
function findContentsUniq (data) {
const vdoms = [];
const contents = [];
function rec (data) {
data.contents.forEach(content => {
if (vdoms.indexOf(content.vdom) === -1) {
vdoms.push(content.vdom);
contents.push(content);
}
});
data.children.forEach(child => {
rec(child.data);
});
}
rec(data);
return contents;
}
// recursively find shared VDOM across direct children.
// if a VDOM is used in 2 different children, it means we can share its computation in contextChildren
function findChildrenDuplicates (data, toIgnore) {
// FIXME the code here is a bit complex and not so performant.
// We should see if we can precompute some data once before
function childVDOMs ({vdom,data}, arrVdom, arrData) {
if (toIgnore.indexOf(vdom) === -1 && arrVdom.indexOf(vdom) === -1) {
arrVdom.push(vdom);
arrData.push(data);
}
data.children.forEach(child => childVDOMs(child, arrVdom, arrData));
}
let allVdom = [];
let allData = [];
const childrenVDOMs = data.children.map(child => {
const arrVdom = [];
const arrData = [];
childVDOMs(child, arrVdom, arrData);
allVdom = allVdom.concat(arrVdom);
allData = allData.concat(arrData);
return arrVdom;
});
return allVdom.map((vdom, allIndex) => {
let occ = 0;
for (let i=0; i<childrenVDOMs.length; i++) {
if (childrenVDOMs[i].indexOf(vdom) !== -1) {
occ ++;
if (occ > 1) return { vdom: vdom, data: allData[allIndex] };
}
}
}).filter(obj => obj);
}
// Recursively "resolve" the data to assign fboId and factorize duplicate uniforms to shared uniforms.
function rec (data, fboId, parentContext, parentFbos) {
const parentContextVDOM = parentContext.map(({vdom}) => vdom);
const genFboId = (fboIdCounter =>
() => {
fboIdCounter ++;
while (
fboIdCounter === fboId ||
parentFbos.indexOf(fboIdCounter)!==-1) // ensure fbo is not already taken in parents
fboIdCounter ++;
return fboIdCounter;
}
)(-1);
const { uniforms: dataUniforms, children: dataChildren, contents: dataContents, preload, ...dataRest } = data;
const uniforms = {...dataUniforms};
const shared = findChildrenDuplicates(data, parentContextVDOM);
const childrenContext = shared.map(({vdom}) => {
const fboId = genFboId();
return { vdom, fboId };
});
const context = parentContext.concat(childrenContext);
const contextVDOM = context.map(({vdom}) => vdom);
const contextFbos = context.map(({fboId}) => fboId);
const contextChildren = [];
const children = [];
const toRecord = dataChildren.concat(shared).map(child => {
const { data: childData, uniform, vdom } = child;
let i = contextVDOM.indexOf(vdom);
let fboId, addToCollection;
if (i===-1) {
fboId = genFboId();
addToCollection = children;
}
else {
fboId = context[i].fboId;
if (i >= parentContext.length) {// is a new context children
addToCollection = contextChildren;
}
}
if (uniform) uniforms[uniform] = FramebufferTextureObject(fboId);
return { fboId, childData, addToCollection };
});
const childrenFbos = toRecord.map(({fboId})=>fboId);
const allFbos = parentFbos.concat(contextFbos).concat(childrenFbos);
const recorded = [];
toRecord.forEach(({ fboId, childData, addToCollection }) => {
if (recorded.indexOf(fboId) === -1) {
recorded.push(fboId);
if (addToCollection) addToCollection.push(rec(childData, fboId, context, allFbos));
}
});
dataContents.forEach(({ uniform, vdom }) => {
const id = contentsVDOM.indexOf(vdom);
invariant(id!==-1, "contents was discovered by findContentsMeta");
uniforms[uniform] = ContentTextureObject(id);
});
// Check images to preload
if (preload) {
imagesToPreload = imagesToPreload.concat(extractImages(dataUniforms));
}
return {
...dataRest, // eslint-disable-line no-undef
uniforms,
contextChildren,
children,
fboId
};
}
return {
data: rec(data, -1, [], []),
contentsVDOM,
imagesToPreload: uniqImages(imagesToPreload)
};
}
class GLView extends Component {
constructor (props, context) {
super(props, context);
this._renderId = 1;
}
render() {
const renderId = this._renderId ++;
const props = this.props;
const { style, width, height, children, shader, uniforms, debug, preload, opaque, ...restProps } = props;
invariant(width && height && width>0 && height>0, "width and height are required for the root GLView");
const {data, contentsVDOM, imagesToPreload} = resolveData(buildData(shader, uniforms, width, height, children, preload||false));
const contents = contentsVDOM.map((vdom, i) => renderVcontent(data.width, data.height, i, vdom));
if (debug &&
typeof console !== "undefined" &&
console.debug // eslint-disable-line
) {
console.debug("GL.View rendered with", data, contentsVDOM); // eslint-disable-line no-console
}
return renderVcontainer(
width,
height,
contents,
renderVGL({
...restProps, // eslint-disable-line no-undef
width,
height,
data,
nbContentTextures: contents.length,
imagesToPreload,
renderId,
opaque
})
);
}
}
GLView.displayName = "GL.View";
GLView.propTypes = {
shader: PropTypes.number.isRequired,
width: PropTypes.number,
height: PropTypes.number,
uniforms: PropTypes.object,
opaque: PropTypes.bool,
preload: PropTypes.bool,
autoRedraw: PropTypes.bool,
eventsThrough: PropTypes.bool
};
GLView.defaultProps = {
opaque: true
};
return GLView;
};