forked from xml3d/xml3d.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.js
More file actions
183 lines (156 loc) · 6.27 KB
/
request.js
File metadata and controls
183 lines (156 loc) · 6.27 KB
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
(function(){
/**
* Content of this file:
* Classes to request results from an Xflow graph.
*/
/**
* Abstract Request class.
* Any Request is created from a DataNode to receive the result of that DataNode.
* To allow effective optimiziation, it is recommended to create only one Request per DataNode and receive all
* results through that Request.
* @abstract
* @param {Xflow.DataNode} dataNode The DataNode from which to request results
* @param {?Array.<string>} filter A list of names filtering the values to be received (only return values with names inside the filter)
* @param {?function} callback A callback function that gets called whenever the result of the Request changes
*/
var Request = function(dataNode, filter, callback){
this._dataNode = dataNode;
this._filter = filter ? filter.slice().sort() : null;
this._listener = callback;
this._result = null;
this._dataNodeListener = this._onDataNodeChange.bind(this);
this._dataNode.addListener(this._dataNodeListener);
};
Xflow.Request = Request;
Object.defineProperty(Request.prototype, "dataNode", {
set: function(v){
throw new Error("dataNode is readonly");
},
get: function(){ return this._dataNode; }
});
Object.defineProperty(Request.prototype, "filter", {
set: function(v){
throw new Error("filter is read-only");
},
get: function(){ return this._filter; }
});
/**
* Call this function, whenever the request is not required anymore.
*/
Request.prototype.clear = function(){
this._listener = null;
if(this._result) this._result._removeRequest(this);
this._dataNode.removeListener(this._dataNodeListener);
};
Request.prototype._onListedCallback = function(data){
this._listener && this._listener(this, data)
};
function swapResultRequest(request, newResult){
if(request._result) request._result._removeRequest(request);
request._result = newResult
if(newResult) newResult._addRequest(request);
return newResult;
}
/**
* @param {Xflow.Request} request
* @param {Xflow.RESULT_STATE} notification
*/
function notifyListeners(request, notification){
Xflow._listCallback(request, notification);
};
/**
* @param {Xflow.RESULT_STATE} notification
*/
Request.prototype._onDataNodeChange = function(notification){
notifyListeners(this, notification);
}
/**
* A ComputeRequest is a Request for a ComputeResult, which contains a named map of typed values.
* @constructor
* @extends {Xflow.Request}
* @param {Xflow.DataNode} dataNode The DataNode from which to request results
* @param {?Array.<string>} filter A list of names filtering the values to be received (only return values with names inside the filter)
* @param {?function} callback A callback function that gets called whenever the result of the Request changes
*/
var ComputeRequest = function(dataNode, filter, callback){
Xflow.Request.call(this, dataNode, filter, callback);
};
Xflow.createClass(ComputeRequest, Xflow.Request);
Xflow.ComputeRequest = ComputeRequest;
ComputeRequest.prototype.getResult = function(){
return swapResultRequest(this, this._dataNode._getResult(Xflow.RESULT_TYPE.COMPUTE, this._filter));
}
ComputeRequest.prototype._onResultChanged = function(notification){
this._onDataNodeChange(notification);
}
var c_vsConnectNodeCount = {},
c_vsConnectNodeCache = {};
/**
* A VertexShaderRequest is a Request for a VertexShaderResult, used to generate a vertex shader that includes
* dataflow processing
* @constructor
* @extends {Xflow.Request}
* @param {Xflow.DataNode} dataNode
* @param {Xflow.VSConfig} vsConfig Configuraton for the output of the generated vertex shader
* @param {?function} callback A callback function that gets called whenever the result of the Request changes
*/
var VertexShaderRequest = function(dataNode, vsConfig, callback){
var filter = vsConfig.getFilter();
if(filter.length == 0)
throw new Error("vsConfig requires at least one attribute entry.");
Xflow.Request.call(this, dataNode, filter, callback);
this._vsConfig = vsConfig;
this._vsConnectNode = getVsConnectNode(dataNode, vsConfig);
};
Xflow.createClass(VertexShaderRequest, Xflow.Request);
Xflow.VertexShaderRequest = VertexShaderRequest;
VertexShaderRequest.prototype.getResult = function(){
return swapResultRequest(this, this._vsConnectNode._getResult(Xflow.RESULT_TYPE.VS, this._filter));
}
VertexShaderRequest.prototype._onDataNodeChange = function(notification){
if(notification == Xflow.RESULT_STATE.CHANGED_STRUCTURE){
var newVSConnectedNode = getVsConnectNode(this._dataNode, this._vsConfig, this._filter);
if(newVSConnectedNode != this._vsConnectNode){
clearVsConnectNode(this._vsConnectNode, this._dataNode, this._vsConfig);
this._vsConnectNode = newVSConnectedNode;
}
}
Request.prototype._onDataNodeChange.call(this, notification);
}
VertexShaderRequest.prototype._onResultChanged = function(result, notification){
this._onDataNodeChange(notification);
}
function getVsConnectNode(dataNode, vsConfig, filter){
var forwardNode = dataNode._getForwardNode(filter);
var key = getDataNodeShaderKey(forwardNode, vsConfig);
var connectNode;
if(!(connectNode = c_vsConnectNodeCache[key])){
var graph = forwardNode._graph;
connectNode = graph.createDataNode(false);
connectNode.appendChild(forwardNode);
var operator = vsConfig.getOperator();
connectNode.computeOperator = operator;
connectNode.computeInputMapping = new Xflow.OrderMapping();
connectNode.computeOutputMapping = new Xflow.OrderMapping();
vsConfig.setInputMapping(connectNode._computeInputMapping);
vsConfig.setOutputMapping(connectNode._computeOutputMapping);
c_vsConnectNodeCache[key] = connectNode;
c_vsConnectNodeCount[connectNode.id] = 1;
}
else{
c_vsConnectNodeCount[connectNode.id]++;
}
return connectNode;
}
function clearVsConnectNode(connectNode, dataNode, vsConfig){
c_vsConnectNodeCount[connectNode.id]--;
if(!c_vsConnectNodeCount[connectNode.id]){
var key = getDataNodeShaderKey(dataNode, vsConfig);
c_vsConnectNodeCache[key] = null;
connectNode.clearChildren();
}
}
function getDataNodeShaderKey(dataNode, vsConfig){
return dataNode.id + "|" + vsConfig.getKey();
}
})();