IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

OpenCL Discussion :

2D Metaballs en OpenCL


Sujet :

OpenCL

  1. #1
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 073
    Points : 7 978
    Points
    7 978
    Par défaut 2D Metaballs en OpenCL
    Salut,

    Ca fait longtemps que je cherche après des tutoriels sur opencl pour faire diverses choses, mais a part de simple tutorials sur des somme de matrices, pas grand choses.

    C'est donc pourquoi je me suis dit, tien on va essayer en 1 heure de coder un petit truc calculé en opencl et qui a un affichage simpliste.

    J'ai donc choisi Java (lwjgl pour l'interface avec opencl) en partant de l'exemple openclSum.

    C'est tres lent et pas optimisé dutout mais ca permettra peut etre a quelqu'un de s'amuser... (je rapelle c'est du vite fait bien fait).

    En gros le kernel, va pour chaque pixel regarder si un ou des boules sont proches et affiche ou non une couleur (en gros).

    Voici 2 images :





    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.*;
    import java.io.*;
    import java.nio.ByteBuffer;
    import java.nio.FloatBuffer;
    import java.util.List;
     
    import javax.imageio.ImageIO;
    import javax.swing.*;
     
    import static org.lwjgl.opencl.CL10.*;
     
    import org.lwjgl.BufferUtils;
    import org.lwjgl.LWJGLException;
    import org.lwjgl.PointerBuffer;
    import org.lwjgl.opencl.CL;
    import org.lwjgl.opencl.CLCommandQueue;
    import org.lwjgl.opencl.CLContext;
    import org.lwjgl.opencl.CLDevice;
    import org.lwjgl.opencl.CLKernel;
    import org.lwjgl.opencl.CLMem;
    import org.lwjgl.opencl.CLPlatform;
    import org.lwjgl.opencl.CLProgram;
    import org.lwjgl.opencl.Util;
    import org.lwjgl.opencl.api.CLImageFormat;
     
    public class BlobsSimpleImage implements MouseMotionListener
    {
        /**
         * Entry point for this sample.
         *
         * @param args not used
         */
        public static void main(String args[])
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    try
    				{
    					new BlobsSimpleImage();
    				} catch (LWJGLException e)
    				{
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e)
    				{
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
                }
            });
        }
     
     
     
        /**
         * Creates a BufferedImage of with type TYPE_INT_RGB from the
         * file with the given name.
         *
         * @param fileName The file name
         * @return The image, or null if the file may not be read
         */
        private static BufferedImage createBufferedImage(String fileName)
        {
            BufferedImage image = null;
            try
            {
                image = ImageIO.read(new File(fileName));
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return null;
            }
     
            int sizeX = image.getWidth();
            int sizeY = image.getHeight();
     
            BufferedImage result = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGB);
            Graphics g = result.createGraphics();
            g.drawImage(image, 0, 0, null);
            g.dispose();
            return result;
        }
     
    	private CLDevice	device;
     
        /**
         * The input image
         */
      //  private BufferedImage inputImage;
     
        /**
         * The output image
         */
        private BufferedImage outputImage;
     
        /**
         * The OpenCL context
         */
        private CLContext context;
     
        /**
         * The OpenCL command queue
         */
        private CLCommandQueue commandQueue;
     
        /**
         * The OpenCL kernel
         */
        private CLKernel kernel;
     
        /**
         * The memory object for the output image
         */
        private CLMem outputImageMem;
      //  private CLMem inputImageMem;
     
     
        // Va servir a retenir la taille, les differentes parametres, coulerus etc de cahcune des boules.
        CLMem Mem_X;
        CLMem Mem_Y;
        CLMem Mem_Radius;
        CLMem Mem_ColorR;
        CLMem Mem_ColorG;
        CLMem Mem_ColorB;
        int number = 16;
        float [] floatX = new float[number];
    	float [] floatY = new float[number];
    	float [] float_Radiuses = new float[number];
     
    	float [] floatColorR = new float[number];
    	float [] floatColorG = new float[number];
    	float [] floatColorB = new float[number];
     
     
        /**
         * The width of the image
         */
        private int imageSizeX;
     
        /**
         * The height of the image
         */
        private int imageSizeY;
     
     
        float warping;
    	private CLCommandQueue	queue;
     
        /**
         * Creates the JOCLSimpleImage sample
         * @throws LWJGLException 
         * @throws IOException 
         */
        public BlobsSimpleImage() throws LWJGLException, IOException
        {
            // Read the input image file and create the output images
         //   String fileName = "lena512color.png";
            //fileName = "data/Porsche-14.jpg";
     
        //    inputImage = createBufferedImage(fileName);
            imageSizeX = 512;//inputImage.getWidth();
            imageSizeY = 512;//inputImage.getHeight();
     
            outputImage = new BufferedImage(imageSizeX, imageSizeY, BufferedImage.TYPE_INT_RGB);
     
            // Create the panel showing the input and output images
            JPanel mainPanel = new JPanel(new GridLayout(1,0));
         /*   JLabel inputLabel = new JLabel(new ImageIcon(inputImage));
            mainPanel.add(inputLabel, BorderLayout.CENTER);*/
            JLabel outputLabel = new JLabel(new ImageIcon(outputImage));
            mainPanel.add(outputLabel, BorderLayout.CENTER);
     
            // Create the main frame
            JFrame frame = new JFrame("JOCL Simple Image Sample");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new BorderLayout());
            frame.add(mainPanel, BorderLayout.CENTER);
            frame.pack();
            frame.setVisible(true);
            frame.addMouseMotionListener(this);
     
            initCL();
            initMemory();
            startAnimation(outputLabel);
        }
     
     
     
     
        /**
         * Starts the thread which will advance the animation state
         * and call call the animation method.
         *
         * @param outputComponent The component to repaint after each step
         */
        private void startAnimation(final Component outputComponent)
        {
            System.out.println("Starting animation...");
            Thread thread = new Thread(new Runnable()
            {
                float angle = 0.0f;
                public void run()
                {
                    while (true)
                    {
                        processImage(angle, warping);
                        angle += 0.1f;
                        outputComponent.repaint();
     
                        try
                        {
                            Thread.sleep(25);
                        }
                        catch (InterruptedException e)
                        {
                            Thread.currentThread().interrupt();
                            return;
                        }
                    }
                }
            });
            thread.setDaemon(true);
            thread.start();
        }
     
     
        /**
         * Initialize the OpenCL context, command queue and kernel
         * @throws LWJGLException 
         * @throws IOException 
         */
        void initCL() throws LWJGLException, IOException
        {
            // Obtain the platform IDs and initialize the context properties
            System.out.println("Creating context...");
            // initialization
            CL.create();
            CLPlatform platform = CLPlatform.getPlatforms().get(0);
     
            List<CLDevice> devices = platform.getDevices(CL_DEVICE_TYPE_GPU);
            context = CLContext.create(platform, devices, null, null, null);
            device =  devices.get(0);
            queue = clCreateCommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, null);
     
            // Try to create an OpenCL context on a GPU device
            if (context == null)
            {
               System.err.println("No GPU Acceleration found !");
               System.exit(-1);
            }
     
            // Check if images are supported
            PointerBuffer imageSupport = PointerBuffer.allocateDirect(1);
            clGetDeviceInfo (device, CL_DEVICE_IMAGE_SUPPORT, null, imageSupport);
            int value = (int) imageSupport.get();
            System.out.println("Images supported: "+(value==1));
            if (value==0)
            {
                System.out.println("Images are not supported");
                System.exit(1);
                return;
            }
     
            // Create a command-queue
            System.out.println("Creating command queue...");
            long properties = 0;
            properties |= CL_QUEUE_PROFILING_ENABLE;
            properties |= CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
            commandQueue = clCreateCommandQueue(context, device, properties, null);
     
            // Create the program
            System.out.println("Creating program...");
            CLProgram program = clCreateProgramWithSource(context,FiletoString("blobs.cl") , null);
     
            // Build the program
            System.out.println("Building program...");
            Util.checkCLError(clBuildProgram(program, devices.get(0), "", null));
     
            // Create the kernel
            System.out.println("Creating kernel...");
            kernel = clCreateKernel(program, "blobs", null);
     
        }
     
        /**
         * Initialize the memory objects for the input and output images
         */
        private void initMemory()
        {
            // Create the memory object for the input- and output image
        	/*DataBufferInt dataBufferSrc = (DataBufferInt)inputImage.getRaster().getDataBuffer();
        	ByteBuffer image = BufferUtils.createByteBuffer(dataBufferSrc.getSize()*4);
            int d[] = dataBufferSrc.getData();
            for (int i = 0; i < d.length; i++)
    		{
            	int f = d[i];
            	image.put((byte) (0xFF));
            	image.put((byte) ((f&0x00FF0000)>>16));
            	image.put((byte) ((f&0x0000FF00)>>8));
            	image.put((byte) ((f&0x000000FF)));
        	}
            image.rewind();*/
     
            CLImageFormat imageFormat = new CLImageFormat(CL_RGBA, CL_UNSIGNED_INT8);
           // inputImageMem = CLMem.createImage2D(context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR,imageFormat, imageSizeX, imageSizeY,imageSizeX*4, image, null);   
            outputImageMem = CLMem.createImage2D(context, CL_MEM_READ_WRITE,imageFormat, imageSizeX, imageSizeY,0, null, null);
     
            // Crée des blobs ...
            for (int i = 0; i < number; i++)
        	{
        		floatX[i]=(float) (Math.random()*16)+1;
        		floatY[i]=(float) (Math.random()*16)+1;
        		float_Radiuses[i]=(float) (Math.random()*64)+1024;
        		floatColorR[i]=(float) (Math.random());
        		floatColorG[i]=(float) (Math.random());
        		floatColorB[i]=(float) (Math.random());
        	}
     
            // Crée des buffer memoire CG & Recopie vers la mémoire de la CG.
        	FloatBuffer buffer_X = toFloatBuffer(floatX);
        	FloatBuffer buffer_Y = toFloatBuffer(floatY);
        	FloatBuffer buffer_Radiuses = toFloatBuffer(float_Radiuses);
        	FloatBuffer buffer_ColorR = toFloatBuffer(floatColorR);
        	FloatBuffer buffer_ColorG = toFloatBuffer(floatColorG);
        	FloatBuffer buffer_ColorB = toFloatBuffer(floatColorB);
     
        	Mem_X = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, buffer_X, null);
        	clEnqueueWriteBuffer(queue, Mem_X, 1, 0, buffer_X, null, null);
        	Mem_Y = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, buffer_Y, null);
        	clEnqueueWriteBuffer(queue, Mem_Y, 1, 0, buffer_Y, null, null);
        	Mem_Radius = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, buffer_Radiuses, null);
        	clEnqueueWriteBuffer(queue, Mem_Radius, 1, 0, buffer_Radiuses, null, null);
     
        	Mem_ColorR = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, buffer_ColorR, null);
        	clEnqueueWriteBuffer(queue, Mem_ColorR, 1, 0, buffer_ColorR, null, null);
        	Mem_ColorG = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, buffer_ColorG, null);
        	clEnqueueWriteBuffer(queue, Mem_ColorG, 1, 0, buffer_ColorG, null, null);
        	Mem_ColorB = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, buffer_ColorB, null);
        	clEnqueueWriteBuffer(queue, Mem_ColorB, 1, 0, buffer_ColorB, null, null);
     
        }
     
        long Start = System.currentTimeMillis();
     
        void processImage(float angle, float warping)
        {
            // Set up the work size and arguments, and execute the kernel
        	PointerBuffer kernel1DGlobalWorkSize = BufferUtils.createPointerBuffer(2);
        	kernel1DGlobalWorkSize.put(0, imageSizeX);
        	kernel1DGlobalWorkSize.put(1, imageSizeY);
     
        	double timer = (System.currentTimeMillis()-Start)/(1000.0f);
        	kernel.setArg(0, outputImageMem);
            kernel.setArg(1, (float)timer);
        	kernel.setArg(2, number);
            kernel.setArg(3, Mem_X);
            kernel.setArg(4, Mem_Y);
            kernel.setArg(5, Mem_Radius);
            kernel.setArg(6, Mem_ColorR);
            kernel.setArg(7, Mem_ColorG);
            kernel.setArg(8, Mem_ColorB);
         //   kernel.setArg(9, inputImageMem);
     
            // "Execute" le kernel
            clEnqueueNDRangeKernel(commandQueue, kernel, 2, null, kernel1DGlobalWorkSize, null,  null, null);
     
            // Prepare la lecture du resultat.
            DataBufferInt dataBufferDst = (DataBufferInt)outputImage.getRaster().getDataBuffer();
            ByteBuffer image = BufferUtils.createByteBuffer(dataBufferDst.getSize()*4);
            PointerBuffer region = PointerBuffer.allocateDirect(3);
            region.put(imageSizeX);
            region.put(imageSizeY);
            region.put(1);
            region.rewind();
            PointerBuffer origin = PointerBuffer.allocateDirect(3);
            origin.put(0);
            origin.put(0);
            origin.put(0);
            origin.rewind();
     
            // Lis et
            clEnqueueReadImage(commandQueue, outputImageMem, 1, origin, region, 0, 0,image, null, null);
            // Recopie le resultat dans le buffer de l'image
            image.rewind();
            for (int i = 0; i < image.capacity()/4; i++)
    		{
            	int col = 0;
               	col += image.get()<<24;
               	col += image.get()<<16;
               	col += image.get()<<8;
               	col += image.get();
               	dataBufferDst.setElem(i, col);
        	}
        }
        static String FiletoString(String filename) throws IOException
    	{
    	   FileInputStream file = new FileInputStream (filename);
    	         byte[] b = new byte[file.available ()];
    	         file.read(b);
    	         file.close ();
    	         String result = new String (b);
    	         return result;
    	}
    	static FloatBuffer toFloatBuffer(float[] floats)
    	{
    		FloatBuffer buf = BufferUtils.createFloatBuffer(floats.length).put(floats);
    		buf.rewind();
    		return buf;
    	}
     
    	@Override
    	public void mouseDragged(MouseEvent e)
    	{
    	}
    	@Override
    	public void mouseMoved(MouseEvent e)
    	{
    	}
    }
    et bien entendu le kernel


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    const sampler_t samplerIn = 
        CLK_NORMALIZED_COORDS_FALSE | 
        CLK_ADDRESS_CLAMP |
        CLK_FILTER_NEAREST;
     
    const sampler_t samplerOut = 
        CLK_NORMALIZED_COORDS_FALSE |
        CLK_ADDRESS_CLAMP |
        CLK_FILTER_NEAREST;
     
     
    float fonction(int2 pixel_position, float2 sphere_pos, float radius)
    {
    	float2 dd = {fabs(pixel_position.x-sphere_pos.x),fabs(pixel_position.y-sphere_pos.y)};
        float dist = (dd.x*dd.x+dd.y*dd.y);
        float val=0;
        val=(radius/dist); // donne tjrs 0 a 1;
      	return val*127;
    }
     
    __kernel void blobs(
    	__write_only image2d_t targetImage, 
    	float timer, int amount, 
    	global const float *sph_X,
    	global const float *sph_Y,
    	global const float *sph_Radiuses, 
     	global const float *ColR,
     	global const float *ColG,
     	global const float *ColB
    )
    {
    	int gidX = get_global_id(0);
        int gidY = get_global_id(1);
        //int w = get_image_width(targetImage);
        //int h = get_image_height(targetImage);
        int2 posIn = {gidX, gidY};
        float valR=0;
        float valG=0;
        float valB=0;
     
        float4 finalColor = {0,0,0,0};
     
        for (int i = 0 ; i < amount; i++)
        {
            float2 sphere_pos = {128*cos(timer/sph_X[i])+256,128*sin(timer/sph_Y[i])+256};
            float valO = fonction(posIn, sphere_pos, sph_Radiuses[i]);
            valR+=valO*ColR[i];
          	valG+=valO*ColG[i];
            valB+=valO*ColB[i];
    	}
      	uint4 pixel = {255, valR, valG, valB};
        write_imageui(targetImage, posIn, pixel);
    };
    Jouez sur la valeur de la variable number si jamais ca rame de trop...
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  2. #2
    Inactif  


    Homme Profil pro
    Inscrit en
    Novembre 2008
    Messages
    5 288
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 48
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Secteur : Santé

    Informations forums :
    Inscription : Novembre 2008
    Messages : 5 288
    Points : 15 620
    Points
    15 620
    Par défaut
    Bonjour

    Merci pour le code. Il me semble proche de l'exemple de rendu de balles par raytracing présenté dans "Cuda by example", chap 6.

  3. #3
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 073
    Points : 7 978
    Points
    7 978
    Par défaut
    Merci, C'est bien possible, vu que c'est a peu près le même genre de truc.

    Je me suis lancé dans une autre idée bizarre a savoir un synthétiseur sonore opencl, j'arrive a sortir un son (en fct d'un touche pressée) ca joue la note correspondante. Je vx voir si c'est realisable ou pas ^^.

    Ce sera si j'y arrive un petit systeme de synthese modulaires ou les modules (VCO, ADSR, ...) pourra etre connecté ensemble (chaque module serait un kernel a executer).
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Comment tester OpenCL ?
    Par ebalexandr dans le forum OpenCL
    Réponses: 14
    Dernier message: 04/05/2009, 17h32
  2. Réponses: 6
    Dernier message: 23/04/2009, 14h02
  3. Standard OpenCL terminé !
    Par Bakura dans le forum OpenCL
    Réponses: 6
    Dernier message: 12/12/2008, 15h38
  4. "Metaballs" non ronds
    Par cjuliard dans le forum Traitement d'images
    Réponses: 2
    Dernier message: 23/05/2008, 14h32

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo