使用 OpenGL 着色器进行数学计算 (C++) [英] Using OpenGL shaders for math calculation (C++)

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

问题描述

我有一个矩阵(例如 100x100 尺寸):我需要对每个元素进行计算(例如matrix[i,j]*tt/8+5)

I have a matrix (for example 100x100 dimantion): I need to do calculation with each element (matrix[i,j]*tt/8+5 for example)

我有巨大的矩阵,我想使用 OpenGL 着色器实现算法.我想使用着色器:

I have huge matrix and I want to implement the algorithm using OpenGL shaders. I want to use shader like:

uniform float val;
uniform float tt;

void main()
{
    gl_Position.x = val*tt/8+5
}

我如何实施该计划?计算后如何获得矩阵(我不想显示任何窗口\图片?

How I can implement the program? How I can get matrix after calculation(I do not want to show any windows\pictures?

推荐答案

如果您创建一个 窗口帧缓冲区,这是可能的.

It is possible if you create a fake window frame buffer.

查看我的示例程序,其中我滥用了片段着色器 作为计算着色器,因为计算着色器是相当新的.该程序对矩阵进行一些高斯滤波计算并将其返回给 CPU.(实际上它做什么并不重要).

See my sample program where I abused the fragment Shader as a compute Shader because compute shaders are quite new. This program does some Gaussian Filtering calculations of the matrix and returns it to the CPU. (It actually does not matter what it does).

以下几点需要注意:

  • 在 CPU/GPU 之间传输文件很慢
  • 其他人已经指出您的矩阵不是巨大.我认为它非常非常小(它甚至可以放入 RAM).
  • the transporting of files between CPU/GPU is slow
  • other people already pointed out that your matrix is NOT huge. I consider it as very very small (it even fits in the RAM).

这篇关于使用 OpenGL 着色器进行数学计算 (C++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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