Example #1
0
        public static void Execute(byte[] bitmap)
        {
            CudafyModule km = CudafyModule.TryDeserialize();

            if (km == null || !km.TryVerifyChecksums())
            {
                km = CudafyTranslator.Cudafy(typeof(SphereOpenCL), typeof(ray_opencl_const));
                km.TrySerialize();
            }

            GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);

            gpu.LoadModule(km);

            // capture the start time
            gpu.StartTimer();

            // allocate memory on the GPU for the bitmap (same size as ptr)
            byte[] dev_bitmap = gpu.Allocate(bitmap);

            // allocate memory for the Sphere dataset
            //SphereOpenCL[] s = gpu.Allocate<SphereOpenCL>(SPHERES);

            // allocate temp memory, initialize it, copy to constant memory on the GPU
            SphereOpenCL[] temp_s = new SphereOpenCL[SPHERES];
            for (int i = 0; i < SPHERES; i++)
            {
                temp_s[i].r = rnd(1.0f);
                temp_s[i].g = rnd(1.0f);
                temp_s[i].b = rnd(1.0f);

                temp_s[i].x      = rnd(1000.0f) - 500;
                temp_s[i].y      = rnd(1000.0f) - 500;
                temp_s[i].z      = rnd(1000.0f) - 500;
                temp_s[i].radius = rnd(100.0f) + 20;
            }
            //gpu.CopyToDevice(temp_s, s);
            gpu.CopyToConstantMemory(temp_s, spheres);

            // generate a bitmap from our sphere data
            dim3 grids   = new dim3(ray_gui.DIM / 16, ray_gui.DIM / 16);
            dim3 threads = new dim3(16, 16);

            //gpu.Launch(grids, threads).kernel(s, dev_bitmap); // Dynamic
            gpu.Launch(grids, threads, ((Action <GThread, byte[]>)thekernel), dev_bitmap); // Strongly typed

            // copy our bitmap back from the GPU for display
            gpu.CopyFromDevice(dev_bitmap, bitmap);

            // get stop time, and display the timing results
            float elapsedTime = gpu.StopTimer();

            Console.WriteLine("Time to generate: {0} ms", elapsedTime);

            gpu.FreeAll();
        }
Example #2
0
        public static float hit(SphereOpenCL s, float ox1, float oy1, ref float n1)
        {
            float dx = ox1 - s.x;
            float dy = oy1 - s.y;

            if (dx * dx + dy * dy < s.radius * s.radius)
            {
                float dz = GMath.Sqrt(s.radius * s.radius - dx * dx - dy * dy);
                n1 = dz / GMath.Sqrt(s.radius * s.radius);
                return(dz + s.z);
            }
            return(-2e10f);
        }