简体   繁体   中英

openTK: run for loop on gpu?

I am currently developping a C# game engine in XNA, I am currently working on a routine for masking a 2D texture with another 2D texture.

so far I have this routine:

public static Texture2D MaskToTexture2DByTexture2D(GraphicsDevice device, Texture2D texture, Texture2D mask)
    {

        Texture2D output = new Texture2D(device, texture.Width, texture.Height);

        int numberOfPixels = texture.Width * texture.Height;

        Color[] ColorArray = new Color[numberOfPixels];
        Color[] maskColorArray = new Color[numberOfPixels];
        float[] maskArray = new float[numberOfPixels];

        mask = ResizeEngine.ResizeToSize(device, mask, new Point(texture.Width, texture.Height));

        mask.GetData<Color>(maskColorArray);

        maskArray = ColorEngine.ConvertColorArrayToMaskValues(maskColorArray);

        texture.GetData<Color>(ColorArray);

        Parallel.For(0, ColorArray.Length, index => 
        {
            ColorArray[index] *= maskArray[index];
        });

        output.SetData<Color>(ColorArray);

        return output;
    }

ColorEngine is currently executing following method:

public static float[] ConvertColorArrayToMaskValues(Color[] colors)
    {

        float[] mask = new float[colors.Count()];

        Parallel.For(0, colors.Length, index => { mask[index] = ConvertColorToMaskValue(colors[index]); });

        return mask;
    }

    public static float ConvertColorToMaskValue(Color color)
    {
        float mask = (color.R + color.G + color.B) / 765.0f;

        return mask;
    }

this works, but not on a basis where I can use it in the real time render routine, I'd love to replace the Parallel.For loops into a loop executed by the GPU in parallel. I imported the OpenTK library, but I can't find any documentation for GPU code execution appart from default graphic drawcalls.

is this possible? Or am I wasting my time here?

OpenTK is a CLI/CLR/.Net binding for OpenGL (and other media APIs). OpenGL is a drawing API aimed at GPUs. Programms running on GPUs that control drawing operations are called "shaders" . OpenGL has functions to load and use shaders for its drawing operations. Thus if you want to use OpenTK → OpenGL you'll have to learn the OpenGL API and how to write OpenGL shader programs.

is this possible? Or am I wasting my time here?

most certainly. This is a prime example of what you'd do in a so called fragment shader .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM