用于计算像素数的着色器 [英] Shader for counting number of pixels

查看:127
本文介绍了用于计算像素数的着色器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找可以计算红色像素或我想要的任何其他颜色数量的着色器CG或HLSL.

I'm looking for a shader CG or HLSL, that can count number of red pixels or any other colors that I want.

推荐答案

您可以使用原子计数器在片段着色器中.只需测试输出颜色以查看其是否在红色的一定公差范围内,如果是,则递增计数器.进行绘制调用后,您应该能够读取CPU上计数器的值,并随心所欲地对其进行操作.

You could do this with atomic counters in a fragment shader. Just test the output color to see if it's within a certain tolerance of red, and if so, increment the counter. After the draw call you should be able to read the counter's value on the CPU and do whatever you like with it.

添加了一个非常简单的示例片段着色器:

edit: added a very simple example fragment shader:

// Atomic counters require 4.2 or higher according to
// https://www.opengl.org/wiki/Atomic_Counter

#version 440
#extension GL_EXT_gpu_shader4 : enable

// Since this is a full-screen quad rendering, 
// the only input we care about is texture coordinate. 
in vec2 texCoord;

// Screen resolution
uniform vec2 screenRes;

// Texture info in case we use it for some reason
uniform sampler2D tex;

// Atomic counters! INCREDIBLE POWER
layout(binding = 0, offset = 0) uniform atomic_uint ac1;

// Output variable!
out vec4 colorOut;

bool isRed(vec4 c)
{
    return c.r > c.g && c.r > c.b;
}

void main() 
{
    vec4 result = texture2D(tex, texCoord);

    if (isRed(result))
    {
        uint cval = atomicCounterIncrement(ac1);
    }

    colorOut = result;
}

您还需要在代码中设置原子计数器:

You would also need to set up the atomic counter in your code:

GLuint acBuffer = 0;
glGenBuffers(1, &acBuffer);

glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, acBuffer);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint), NULL, GL_DYNAMIC_DRAW);

这篇关于用于计算像素数的着色器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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