Commits

David Lin  committed 0a5cea9

refactored context

  • Participants
  • Parent commits 1364b57

Comments (0)

Files changed (3)

File www/js/app/fx/grayscale.js

 define([
   'zepto',
+  './shared',
   'text!./grayscale.cl'
-], function($, code){
-  
-  function setupContext()
-  {
-    var platform = WebCL.getPlatformIDs()[0];
-    var props = [WebCL.CL_CONTEXT_PLATFORM, platform];
-    var type = WebCL.CL_DEVICE_TYPE_GPU;
-    return WebCL.createContextFromType(props, type);
-  }
+], function($, fx, code){
   
   function sourceImageElement()
   {
   
   function execute()
   {
-    var ctx = setupContext();
-    var dev = ctx.getContextInfo(WebCL.CL_CONTEXT_DEVICES)[0];
-    var cmd = ctx.createCommandQueue(dev, 0);
-    _execute(ctx, cmd);
-    ctx.releaseCLResources();
+    _execute(fx.context, fx.commandQueue);
   }
   
   function _execute(ctx, cmd)
     cmd.enqueueReadImage(imgOut, false, [0,0,0], [result.width, result.height, 1], 0, 0, result.data, []);
     cmd.finish();
     canvasCtx.putImageData(result, 0, 0);
+    imgIn.releaseCLResources();
+    imgOut.releaseCLResources();
   }
   
   function setup()

File www/js/app/fx/invert.js

 define([
   'zepto',
+  './shared',
   'text!./invert.cl'
-], function($, code){
-  
-  function setupContext()
-  {
-    var platform = WebCL.getPlatformIDs()[0];
-    var props = [WebCL.CL_CONTEXT_PLATFORM, platform];
-    var type = WebCL.CL_DEVICE_TYPE_GPU;
-    return WebCL.createContextFromType(props, type);
-  }
+], function($, fx, code){
   
   function sourceImageElement()
   {
   
   function execute()
   {
-    var ctx = setupContext();
-    var dev = ctx.getContextInfo(WebCL.CL_CONTEXT_DEVICES)[0];
-    var cmd = ctx.createCommandQueue(dev, 0);
-    _execute(ctx, cmd);
-    ctx.releaseCLResources();
+    _execute(fx.context, fx.commandQueue);
   }
   
   function _execute(ctx, cmd)
     cmd.enqueueReadImage(imgOut, false, [0,0,0], [result.width, result.height, 1], 0, 0, result.data, []);
     cmd.finish();
     canvasCtx.putImageData(result, 0, 0);
+    imgIn.releaseCLResources();
+    imgOut.releaseCLResources();
   }
   
   function setup()

File www/js/app/fx/shared.js

+define(function(){
+  
+  var context = (function(){
+    var platform = WebCL.getPlatformIDs()[0];
+    var props = [WebCL.CL_CONTEXT_PLATFORM, platform];
+    var type = WebCL.CL_DEVICE_TYPE_GPU;
+    return WebCL.createContextFromType(props, type);
+  })();
+  var device = context.getContextInfo(WebCL.CL_CONTEXT_DEVICES)[0];
+  var commandQueue = context.createCommandQueue(device, 0);
+  
+  return {
+    context: context,
+    device: device,
+    commandQueue: commandQueue
+  };
+});