如何使用GLSL计算指定的彩色像素数? [英] how to calculate the number of specified colored pixels using GLSL?

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

问题描述

我有一个灰度纹理(8000 * 8000),每个像素的值是一个ID(实际上,该ID是该片段所属的三角形的ID,我想使用此方法来计算多少个三角形和在我的场景中可以看到哪些三角形).

I have a grayscale texture (8000*8000) , the value of each pixel is an ID (actually, this ID is the ID of triangle to which the fragment belongs, I want to using this method to calculate how many triangles and which triangles are visible in my scene).

现在我需要计算有多少个唯一ID,它们是什么.我想用GLSL来实现这一点,并尽量减少GPU RAM和RAM之间的数据传输.

now I need to count how many unique IDs there are and what are them. I want to implement this with GLSL and minimize the data transfer between GPU RAM and RAM.

我想出的最初想法是使用着色器存储缓冲区,将其绑定到GLSL中的数组,其大小为totalTriangleNum,然后遍历着色器中的ID纹理,将索引等于1的数组元素增加识别纹理.

The initial idea I come up with is to use a shader storage buffer, bind it to an array in GLSL, its size is totalTriangleNum, then iterate through the ID texture in shader, increase the array element by 1 that have index equal to ID in texture.

之后,将缓冲区读取到OpenGL应用程序并获取我想要的.这是一种有效的方法吗?还是有一些更好的解决方案,例如计算着色器(我不熟悉)或其他解决方案.

After that, read the buffer to OpenGL application and get what I want. Is this a efficient way to do so? Or are there some better solutions like compute-shader (well I'm not familiar with it) or something else.

推荐答案

我想用这种方法来计算场景中有多少个三角形以及可见的三角形

I want to using this method to calculate how many triangles and which triangles are visible in my scene)

鉴于您对数据的描述,让我重新表述一下:

Given your description of your data let me rephrase that a bit:

您想确定数据集中有多少个不同的值,以及每个值出现的频率.

这通常被称为 直方图 .不幸的是(对您而言)生成直方图是在GPU上无法轻松解决的问题之一.本质上,您必须将图像划分为越来越小的子图像(BSP,四叉树等),直到划分为要执行评估的单个像素为止.然后回溯传播子直方图,实质上是对直方图执行插入或合并排序.

This is commonly known as a Histogram. Unfortunately (for you) generating histograms are among the problems not that trivially solved on GPUs. Essentially you have to divide down your image into smaller and smaller subimages (BSP, quadtree, etc.) until divided down to single pixels on which you perform the evaluation. Then you backtrack propagating up the sub-histograms, essentially performing an insertion or merge sort on the histogram.

仍在积极地研究使用GPU生成直方图,因此,建议您阅读已出版的学术著作(通常随附源代码).关键字:直方图,GPU

Generating histograms with GPUs is still actively researched, so I suggest you read up on the published academic works (usually accompanied with source code). Keywords: Histogram, GPU

这是由AMD GPU研究人员撰写的一篇不错的论文: https ://developer.amd.com/wordpress/media/2012/10/GPUHistogramGeneration_preprint.pdf

This one is a nice paper done by the AMD GPU researchers: https://developer.amd.com/wordpress/media/2012/10/GPUHistogramGeneration_preprint.pdf

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

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