Skip to content

Commit bddef0d

Browse files
committed
mode_gpu/cpu minor cleanup
1 parent c3125fb commit bddef0d

5 files changed

Lines changed: 114 additions & 146 deletions

File tree

src/backend/GPUCore.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,85 @@ var GPUCore = (function() {
8989
}
9090
GPUCore.prototype.getPromiseModeExecutor = getPromiseModeExecutor;
9191

92+
93+
///
94+
/// Prepare the synchrnous mode executor,
95+
/// With additional functionalities attached (standard)
96+
///
97+
/// Parameters:
98+
/// ret {JS Function} Function object to extend
99+
/// opt {Object} Options object
100+
///
101+
/// Returns:
102+
/// {JS Function} the same ret object
103+
///
104+
function setupExecutorExtendedFunctions(ret, opt) {
105+
var gpu = this;
106+
107+
// Allow original class object reference from kernel
108+
ret.gpujs = gpu;
109+
110+
ret.dimensions = function(dim) {
111+
opt.dimensions = dim;
112+
return ret;
113+
};
114+
115+
ret.debug = function(flag) {
116+
opt.debug = flag;
117+
return ret;
118+
};
119+
120+
ret.graphical = function(flag) {
121+
opt.graphical = flag;
122+
return ret;
123+
};
124+
125+
ret.loopMaxIterations = function(max) {
126+
opt.loopMaxIterations = max;
127+
return ret;
128+
};
129+
130+
ret.constants = function(constants) {
131+
opt.constants = constants;
132+
return ret;
133+
};
134+
135+
ret.wraparound = function(flag) {
136+
console.warn("Wraparound mode is not supported and undocumented.");
137+
opt.wraparound = flag;
138+
return ret;
139+
};
140+
141+
ret.hardcodeConstants = function(flag) {
142+
opt.hardcodeConstants = flag;
143+
return ret;
144+
};
145+
146+
ret.outputToTexture = function(flag) {
147+
opt.outputToTexture = flag;
148+
return ret;
149+
};
150+
151+
ret.floatTextures = function(flag) {
152+
opt.floatTextures = flag;
153+
return ret;
154+
};
155+
156+
ret.mode = function(mode) {
157+
opt.mode = mode;
158+
return gpu.createKernel(
159+
gpu._kernelFunction,
160+
gpu._kernelParamObj
161+
);
162+
};
163+
164+
ret.getCanvas = function() {
165+
return gpu.getCanvas();
166+
};
167+
168+
return ret;
169+
}
170+
GPUCore.prototype.setupExecutorExtendedFunctions = setupExecutorExtendedFunctions;
171+
92172
return GPUCore;
93173
})();

src/backend/mode_cpu.js

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
(function (GPU) {
2-
function getArgumentType(arg) {
3-
if (Array.isArray(arg)) {
4-
return 'Array';
5-
} else if (typeof arg == "number") {
6-
return 'Number';
7-
} else if (arg instanceof GPUTexture) {
8-
return 'Texture';
9-
} else {
10-
return 'Unknown';
11-
}
12-
}
13-
2+
143
/// JS fallback transformation, basically pure JS
154
///
165
/// @param inputFunction The calling to perform the conversion
@@ -26,7 +15,7 @@
2615
throw "Auto dimensions only supported for kernels with only one input";
2716
}
2817

29-
var argType = getArgumentType(arguments[0]);
18+
var argType = GPUUtils.getArgumentType(arguments[0]);
3019
if (argType == "Array") {
3120
opt.dimensions = getDimensions(argType);
3221
} else if (argType == "Texture") {
@@ -38,7 +27,7 @@
3827

3928
var kernelArgs = [];
4029
for (var i=0; i<arguments.length; i++) {
41-
var argType = getArgumentType(arguments[i]);
30+
var argType = GPUUtils.getArgumentType(arguments[i]);
4231
if (argType == "Array" || argType == "Number") {
4332
kernelArgs[i] = arguments[i];
4433
} else if (argType == "Texture") {
@@ -135,62 +124,7 @@
135124

136125
return ret;
137126
}
138-
139-
ret.dimensions = function(dim) {
140-
opt.dimensions = dim;
141-
return ret;
142-
};
143-
144-
ret.debug = function(flag) {
145-
opt.debug = flag;
146-
return ret;
147-
};
148-
149-
ret.graphical = function(flag) {
150-
opt.graphical = flag;
151-
return ret;
152-
};
153-
154-
ret.loopMaxIterations = function(max) {
155-
opt.loopMaxIterations = max;
156-
return ret;
157-
};
158-
159-
ret.constants = function(constants) {
160-
opt.constants = constants;
161-
return ret;
162-
};
163-
164-
ret.wraparound = function(flag) {
165-
console.warn("Wraparound mode is not supported and undocumented.");
166-
opt.wraparound = flag;
167-
return ret;
168-
};
169-
170-
ret.hardcodeConstants = function(flag) {
171-
opt.hardcodeConstants = flag;
172-
return ret;
173-
};
174-
175-
ret.outputToTexture = function(flag) {
176-
opt.outputToTexture = flag;
177-
return ret;
178-
};
179127

180-
ret.floatTextures = function(flag) {
181-
opt.floatTextures = flag;
182-
return ret;
183-
};
184-
185-
ret.mode = function(mode) {
186-
opt.mode = mode;
187-
return gpu.createKernel(kernel, opt);
188-
};
189-
190-
ret.getCanvas = function() {
191-
return gpu.getCanvas('cpu');
192-
};
193-
194-
return ret;
128+
return gpu.setupExecutorExtendedFunctions(ret, opt);
195129
};
196130
})(GPU);

src/backend/mode_gpu.js

Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,10 @@
7373
return tmp;
7474
}
7575

76-
function getArgumentType(arg) {
77-
if (Array.isArray(arg)) {
78-
return 'Array';
79-
} else if (typeof arg == "number") {
80-
return 'Number';
81-
} else if (arg instanceof GPUTexture) {
82-
return 'Texture';
83-
} else {
84-
return 'Unknown';
85-
}
86-
}
87-
8876
function getProgramCacheKey(args, opt, outputDim) {
8977
var key = '';
9078
for (var i=0; i<args.length; i++) {
91-
var argType = getArgumentType(args[i]);
79+
var argType = GPUUtils.getArgumentType(args[i]);
9280
key += argType;
9381
if (opt.hardcodeConstants) {
9482
var dimensions;
@@ -148,7 +136,7 @@
148136
throw "Auto dimensions only supported for kernels with only one input";
149137
}
150138

151-
var argType = getArgumentType(arguments[0]);
139+
var argType = GPUUtils.getArgumentType(arguments[0]);
152140
if (argType == "Array") {
153141
opt.dimensions = getDimensions(argType);
154142
} else if (argType == "Texture") {
@@ -193,7 +181,7 @@
193181

194182
var paramType = [];
195183
for (var i=0; i<paramNames.length; i++) {
196-
var argType = getArgumentType(arguments[i]);
184+
var argType = GPUUtils.getArgumentType(arguments[i]);
197185
paramType.push(argType);
198186
if (opt.hardcodeConstants) {
199187
if (argType == "Array" || argType == "Texture") {
@@ -449,7 +437,7 @@
449437
var textureCount = 0;
450438
for (textureCount=0; textureCount<paramNames.length; textureCount++) {
451439
var paramDim, paramSize, texture;
452-
var argType = getArgumentType(arguments[textureCount]);
440+
var argType = GPUUtils.getArgumentType(arguments[textureCount]);
453441
if (argType == "Array") {
454442
paramDim = getDimensions(arguments[textureCount], true);
455443
paramSize = dimToTexSize(gpu, paramDim);
@@ -558,63 +546,8 @@
558546
}
559547
}
560548
}
561-
562-
ret.dimensions = function(dim) {
563-
opt.dimensions = dim;
564-
return ret;
565-
};
566-
567-
ret.debug = function(flag) {
568-
opt.debug = flag;
569-
return ret;
570-
};
571-
572-
ret.graphical = function(flag) {
573-
opt.graphical = flag;
574-
return ret;
575-
};
576-
577-
ret.loopMaxIterations = function(max) {
578-
opt.loopMaxIterations = max;
579-
return ret;
580-
};
581549

582-
ret.constants = function(constants) {
583-
opt.constants = constants;
584-
return ret;
585-
};
586-
587-
ret.wraparound = function(flag) {
588-
console.warn("Wraparound mode is not supported and undocumented.");
589-
opt.wraparound = flag;
590-
return ret;
591-
};
592-
593-
ret.hardcodeConstants = function(flag) {
594-
opt.hardcodeConstants = flag;
595-
return ret;
596-
};
597-
598-
ret.outputToTexture = function(flag) {
599-
opt.outputToTexture = flag;
600-
return ret;
601-
};
602-
603-
ret.floatTextures = function(flag) {
604-
opt.floatTextures = flag;
605-
return ret;
606-
};
607-
608-
ret.mode = function(mode) {
609-
opt.mode = mode;
610-
return gpu.createKernel(kernel, opt);
611-
};
612-
613-
ret.getCanvas = function() {
614-
return gpu.getCanvas();
615-
};
616-
617-
return ret;
550+
return gpu.setupExecutorExtendedFunctions(ret, opt);
618551
};
619552

620553
})(GPU);

src/gpu.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ var GPU = (function() {
175175
/// {Canvas object} that the instance use
176176
///
177177
function getCanvas(mode) {
178-
if (mode == "cpu") {
179-
return null;
180-
}
181178
return this.canvas;
182179
};
183180
GPU.prototype.getCanvas = getCanvas;

src/utils.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var GPUUtils = (function() {
133133

134134
//-----------------------------------------------------------------------------
135135
//
136-
// Object cloning and setup
136+
// Object / function cloning and manipulation
137137
//
138138
//-----------------------------------------------------------------------------
139139

@@ -210,6 +210,30 @@ var GPUUtils = (function() {
210210
}
211211
GPUUtils.functionBinder = functionBinder;
212212

213+
///
214+
/// Function: getArgumentType
215+
///
216+
/// Evaluate the argument type, to apply respective logic for it
217+
///
218+
/// Parameters:
219+
/// arg - {Object} The argument object to evaluate type
220+
///
221+
/// Returns:
222+
/// {String} Argument type Array/Number/Texture/Unknown
223+
///
224+
function getArgumentType(arg) {
225+
if (Array.isArray(arg)) {
226+
return 'Array';
227+
} else if (typeof arg == "number") {
228+
return 'Number';
229+
} else if (arg instanceof GPUTexture) {
230+
return 'Texture';
231+
} else {
232+
return 'Unknown';
233+
}
234+
}
235+
GPUUtils.getArgumentType = getArgumentType;
236+
213237
//-----------------------------------------------------------------------------
214238
//
215239
// Canvas validation and support

0 commit comments

Comments
 (0)