简体   繁体   English

openTK:在gpu上运行循环?

[英]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. 我目前正在使用XNA开发C#游戏引擎,目前正在研究用另一个2D纹理遮罩2D纹理的例程。

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: ColorEngine当前正在执行以下方法:

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. 这行得通,但不是基于我可以在实时渲染例程中使用它的基础上,我很乐意将Parallel.For循环替换为GPU并行执行的循环。 I imported the OpenTK library, but I can't find any documentation for GPU code execution appart from default graphic drawcalls. 我导入了OpenTK库,但是从默认的图形调用中找不到关于GPU代码执行appart的任何文档。

is this possible? 这可能吗? Or am I wasting my time here? 还是我在这里浪费时间?

OpenTK is a CLI/CLR/.Net binding for OpenGL (and other media APIs). OpenTK是OpenGL(和其他媒体API)的CLI / CLR / .Net绑定。 OpenGL is a drawing API aimed at GPUs. OpenGL是针对GPU的绘图API。 Programms running on GPUs that control drawing operations are called "shaders" . 在GPU上控制绘图操作的程序称为“着色器” OpenGL has functions to load and use shaders for its drawing operations. OpenGL具有加载和使用着色器进行绘制操作的功能。 Thus if you want to use OpenTK → OpenGL you'll have to learn the OpenGL API and how to write OpenGL shader programs. 因此,如果要使用OpenTK→OpenGL,则必须学习OpenGL API以及如何编写OpenGL着色器程序。

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 . 这是您在所谓的片段着色器中所做的主要示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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