如何在 GLSL 中编写片段着色器来对 9 个浮点数的数组进行排序 [英] How to write a fragment shader in GLSL to sort an array of 9 floating point numbers

查看:24
本文介绍了如何在 GLSL 中编写片段着色器来对 9 个浮点数的数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个片段着色器,以便将 9 个图像放在一起.

I am writing a fragment shader in order to median 9 images together.

我以前从未使用过 GLSL,但它似乎是适合这项工作的工具,因为 OpenCL 在 iOS 上不可用,而且 CPU 上的中位数效率低下.到目前为止,这是我所拥有的:

I have never worked with GLSL before, but it seemed like the right tool for the job, as OpenCL isn't available on iOS and medianing on the CPU is inefficient. Here's what I have so far:

uniform sampler2D frames[9];
uniform vec2 wh;

void main(void)
{
    vec4 sortedFrameValues[9];
    float sortedGrayScaleValues[9];

    for (int i = 0; i < 9; i++)
    {
        sortedFrameValues[i] = texture2D(frames[i], -gl_FragCoord.xy / wh);
        sortedGrayScaleValues[i] = dot(sortedFrameValues[i].xyz, vec3(0.299, 0.587, 0.114));
    }

        // TODO: Sort sortedGrayScaleValues

    float gray = sortedGrayScaleValues[4];
    gl_FragColor = vec4(gray, gray, gray, 0);
}

推荐答案

好吧,我最终实现了冒泡排序并使用了中间值.

Well, I ended up implementing a bubble sort and using the middle value.

这是我的解决方案的样子:

This is what my solution looks like:

uniform sampler2D frames[9];
uniform vec2 wh;

vec4 frameValues[9];
float arr[9];

void bubbleSort()
{
    bool swapped = true;
    int j = 0;
    float tmp;
    for (int c = 0; c < 3; c--)
    {
        if (!swapped)
            break;
        swapped = false;
        j++;
        for (int i = 0; i < 3; i++)
        {
            if (i >= 3 - j)
                break;
            if (arr[i] > arr[i + 1])
            {
                tmp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = tmp;
                swapped = true;
            }
        }
    }
}

void main(void)
{

    for (int i = 0; i < 9; i++)
    {
        frameValues[i] = texture2D(frames[i], -gl_FragCoord.xy / wh);
        arr[i] = dot(frameValues[i].xyz, vec3(0.299, 0.587, 0.114));
    }

    bubbleSort();

    float gray = arr[4];
    gl_FragColor =vec4(gray, gray, gray, 0);
}

这篇关于如何在 GLSL 中编写片段着色器来对 9 个浮点数的数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆