Scripts: OpenCL Test - page 2

 
Svetosha:
And what is the use of these possibilities if they are not useful)))))))
The Wright brothers flew only 20 metres (so to speak, a demonstration flight), the use of their invention is intercontinental flights (as they say, feel the difference).
 
Zeleniy: The ovum before conception. I still don't understand why to put it out, it would be better to do something useful and not finalised.
Svetosha: And the use of these opportunities if they are not useful))))))

Innovations are very often met in the bayonet by such "sermyazhnye practitioners", who judge about universal problems from the point of view of their nose, for which nothing smells.

And then, when the benefit is visible to everyone, they safely forget about their attacks.

 
Mathemat:

The innovations are very often met in the flip side by such "grey practitioners", who judge universal problems from the point of view of their nose, for which nothing smells.

And then, when everyone sees the benefit, they safely forget about their attacks.

Yes, OpenCL is really cool. To be honest, I haven't tracked the situation with 5 for a long time. Is the inter-absorption of orders still there?
 
VDev:
Is the order absorption still there?

It's not that it's still there.

It can't be any other way.

 

Starting with the next build, new features will be available to create graphics resources on the fly using ResourceCreate() functions.

Here is a rewritten and simplified script that does without dumping images to disc, works faster and without resource access conflicts on disc:

//+------------------------------------------------------------------+
//|OpenCLTest.mq5 |
//| Copyright 2011, MetaQuotes Software Corp. | |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//---
#define SIZE_X 512
#define SIZE_Y 512
//+------------------------------------------------------------------+
//| OpenCL function code|
//+------------------------------------------------------------------+
const string cl_src=
                    "__kernel void MFractal(                                    \r\n"
                    "                       float x0,                           \r\n"
                    "                       float y0,                           \r\n"
                    "                       float x1,                           \r\n"
                    "                       float y1,                           \r\n"
                    "                       uint  max,                          \r\n"
                    "              __global uint *out)                          \r\n"
                    "  {                                                        \r\n"
                    "   size_t  w = get_global_size(0);                         \r\n"
                    "   size_t  h = get_global_size(1);                         \r\n"
                    "   size_t gx = get_global_id(0);                           \r\n"
                    "   size_t gy = get_global_id(1);                           \r\n"
                    "   float dx = x0 + gx * (x1-x0) / (float) w;               \r\n"
                    "   float dy = y0 + gy * (y1-y0) / (float)h;                \r\n"
                    "   float x  = 0;                                           \r\n"
                    "   float y  = 0;                                           \r\n"
                    "   float xx = 0;                                           \r\n"
                    "   float yy = 0;                                           \r\n"
                    "   float xy = 0;                                           \r\n"
                    "   uint i = 0;                                             \r\n"
                    "   while ((xx+yy)<4 && i<max)                              \r\n"
                    "     {                                                     \r\n"
                    "      xx = x*x;                                            \r\n"
                    "      yy = y*y;                                            \r\n"
                    "      xy = x*y;                                            \r\n"
                    "      y = xy+xy+dy;                                        \r\n"
                    "      x = xx-yy+dx;                                        \r\n"
                    "      i++;                                                 \r\n"
                    "     }                                                     \r\n"
                    "   if(i==max)                                              \r\n"
                    "      out[w*gy+gx] = 0;                                    \r\n"
                    "   else                                                    \r\n"
                    "      out[w*gy+gx] = (uint)((float)0xFFFFFF/(float)max)*i; \r\n"
                    "  }                                                        \r\n";
//+------------------------------------------------------------------+
//| Script programme start function|
//+------------------------------------------------------------------+
void OnStart()
  {
   int cl_ctx;
   int cl_prg;
   int cl_krn;
   int cl_mem;
//--- initializing OpenCL objects
   if((cl_ctx=CLContextCreate())==INVALID_HANDLE)
     {
      Print("OpenCL not found");
      return;
     }
   if((cl_prg=CLProgramCreate(cl_ctx,cl_src))==INVALID_HANDLE)
     {
      CLContextFree(cl_ctx);
      Print("OpenCL program create failed");
      return;
     }
   if((cl_krn=CLKernelCreate(cl_prg,"MFractal"))==INVALID_HANDLE)
     {
      CLProgramFree(cl_prg);
      CLContextFree(cl_ctx);
      Print("OpenCL kernel create failed");
      return;
     }
   if((cl_mem=CLBufferCreate(cl_ctx,SIZE_X*SIZE_Y*sizeof(uint),CL_MEM_READ_WRITE))==INVALID_HANDLE)
     {
      CLKernelFree(cl_krn);
      CLProgramFree(cl_prg);
      CLContextFree(cl_ctx);
      Print("OpenCL buffer create failed");
      return;
     }
//--- getting ready for execution
   float x0       =-2;
   float y0       =-0.5;
   float x1       =-1;
   float y1       = 0.5;
   uint  max      = 20000;
   uint  offset[2]={0,0};
   uint  work  [2]={SIZE_X,SIZE_Y};
   string objname ="OpenCL_"+IntegerToString(ChartID());
   string resname ="Mandelbrot_"+IntegerToString(ChartID());
//--- setting unchangeable OpenCL function parameters
   CLSetKernelArg(cl_krn,4,max);
   CLSetKernelArgMem(cl_krn,5,cl_mem);
//--- creating the object for graphics display
   ObjectCreate(0,objname,OBJ_BITMAP_LABEL,0,0,0);
   ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,4);
   ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,26);
//--- create initial empty picture
   uint buf[];

   ArrayResize(buf,SIZE_X*SIZE_Y);
   ResourceCreate(resname,buf,SIZE_X,SIZE_Y,0,0,SIZE_X,COLOR_FORMAT_XRGB_NOALPHA);
   ObjectSetString(0,objname,OBJPROP_BMPFILE,"::"+resname);
//--- rendering, until we are not stopped from the outside
   while(!IsStopped())
     {
      uint x=GetTickCount();
      //--- setting floating parameters
      CLSetKernelArg(cl_krn,0,x0);
      CLSetKernelArg(cl_krn,1,y0);
      CLSetKernelArg(cl_krn,2,x1);
      CLSetKernelArg(cl_krn,3,y1);
      //--- rendering the frame
      CLExecute(cl_krn,2,offset,work);
      //--- taking the frame data
      CLBufferRead(cl_mem,buf);
      //--- outputting the rendering time
      Comment(IntegerToString(GetTickCount()-x)+" msec per frame");
      //--- saving the frame in memory and drawing it
      ResourceCreate(resname,buf,SIZE_X,SIZE_Y,0,0,SIZE_X,COLOR_FORMAT_XRGB_NOALPHA);
      ChartRedraw();
      //--- a small pause and parameters update for the next frame
      Sleep(10);
      x0+=0.001 f;
      x1-=0.001 f;
      y0+=0.001 f;
      y1-=0.001 f;
     }
//--- removing OpenCL objects
   CLBufferFree(cl_mem);
   CLKernelFree(cl_krn);
   CLProgramFree(cl_prg);
   CLContextFree(cl_ctx);
//--- remove object
   ObjectDelete(0,objname);
  }
//+------------------------------------------------------------------+


Thus you can create any dynamic 32 bit images with alpha channels for detailed rendering.

Dynamically created resources get into the resource manager and are available to all MQL5 programs of the terminal.


 
Renat new possibilities to create graphical resources on the fly using ResourceCreate() functions will be available.
will only ResourceCreate() function be announced or will a class for working with graphical objects/primitives be prepared ?
 
IgorM:
will only ResourceCreate() function be announced or a class for working with graphical objects/primitives will be prepared ?

This function will be already available.

With its help you can already make your own libraries for drawing in your own buffer and then bind them to objects. Thus you can completely get rid of external drawing libraries in external BMP files.

Here is a small example - you can directly draw in the buf buffer and then create a resource based on it and assign it to an object.

   uint buf[];

   ArrayResize(buf,SIZE_X*SIZE_Y);
   ResourceCreate(resname,buf,SIZE_X,SIZE_Y,0,0,SIZE_X,COLOR_FORMAT_XRGB_NOALPHA);
   ObjectSetString(0,objname,OBJPROP_BMPFILE,"::"+resname);


A little later we will make a library of primitives for drawing in the same buffers using native 2D acceleration functions. This will allow to draw much faster.

 
Renat:

This feature will already be available.

With its help you can already make your own libraries for drawing in your own buffer and then bind them to objects. Thus you can completely get rid of external drawing libraries in external BMP files.


this function will be available for all MQL, and not just for working with OpenCL?

and what is the situation with the alpha channel in the created BMP resources ?

 
A little later we will make a library of primitives for drawing in the same buffers using native 2D acceleration functions.

I see, but imho, it is better for developers to announce at once both new function and base class for working with graphics, otherwise again will start "shouting from the hall" about difficulties in programming on mql5.

ZY: is it only me who doesn't have the "reply" link working ? instead of quoting a post the "New comment" window appears.

 

sergeev 2012.03.19 17:25 #

and what is the situation with alpha channel in created BMP resources ?
so it seems to work