Search code examples
c#classgpualeagpu

How to use/Convert C# class in Alea?


I'm working on a RayMarching Program(HomeWork) and I want it to go faster so I use the GPU with the ALEA extension. I have a problem because I can't Use The class camera In the parallel for (GPU). Thanks for your help.

I already tried to change the tag of the class and creating them inside the Parallel for

        [GpuManaged, Test]
        public static Bitmap DelegateWithClosureGpu(Scene s, Camera my_camera, SDF sdfList, int w, int h)
        {
            my_camera.SetScreenData(w,h);

            int nbsteps;
            float dyst;
            Bitmap res = new Bitmap(w,h);
            ParallelForTest.camera = my_camera;
            Gpu.Default.For(0, res.Height , i =>
            {
                for (int j = 0; j < res.Height; j++)
                {
                    Vector3 ray = ParallelForTest.camera.GetRay(i, j);
                    ray.Normalized();
                    s.RayMarch(sdfList, ray, ParallelForTest.camera.origin,out nbsteps,out dyst);
                    if (Scene.FloatEq(dyst,0f))
                    {
                        res.SetPixel(i,j,Color.White);
                    }
                    else
                    {
                        res.SetPixel(i,j,Color.Black);
                    }
                }
            });
            return res;
        }
using System;
using Alea;

namespace Raymarcher
{
    [GpuManaged]
    public class Camera
    {
        [GpuParam]
        public Vector3 origin;
        [GpuParam]
        private Vector3 forward;
        [GpuParam]
        private Vector3 up;
        private Vector3 right;
        private Vector3 screenOrigin;
        private float stepY;
        private float stepX;
        private float sizeX;
        private float sizeY;


        public Camera(Vector3 origin, Vector3 forward,  float fov)
        {
            this.forward = forward.Normalized();
            this.right=(new Vector3(-forward.z,0,forward.x)).Normalized();
            this.up = (right * forward).Normalized();
            this.origin = origin;
        }
        public void SetScreenData(int width, int height)
        {
            sizeY = (width / height) * sizeX;
            stepX =  sizeX/width;
            stepY =  sizeY/height;
            screenOrigin = origin+forward + (up * (sizeY / 2f)) - (right * (sizeX / 2f));
        }

        public Vector3 GetRay(int x, int y)
        {
            return screenOrigin-origin+stepX*x*right-up*y*stepY;
        }
    }
}

and Class Vector" is only a custom class that overload operators.


Solution

  • You should set some variables to be the camera's variables and use them in the parallel for loop, you should also make static versions of the camera's functions and use them in the for loop. while I'm not sure this will fix it, it's something I think you should try because you said you can't use the class camera in the parallel for and now you won't be using it in there.