c++ - OpenGL 顶点着色器在 Linux 上速度很快,但在 Windows 上非常慢

标签 c++ opengl glsl vertex-shader vertex-attributes

为了绘制信号的功率谱密度(与热图非常相似),我使用了这个顶点着色器程序。它接收每个顶点的功率值,取对数以 dB 为单位显示结果,在颜色图数组的范围内进行归一化,并为顶点分配颜色。

#version 130

uniform float max_val;
uniform float min_val;
uniform int height;

attribute float val; // power-spectral-density value assigned to each vertex

// colormap values
const float r[512] = float[]( /* red values come here */ );
const float g[512] = float[]( /* green values come here */ );
const float b[512] = float[]( /* blue values come here */ );

void main() {
  // set vertex position based on its ID
  int x = gl_VertexID / height;
  int y = gl_VertexID - x * height;
  gl_Position = gl_ModelViewProjectionMatrix * vec4(x, y, -1.0, 1.0);

  float e = log(max_val / min_val);
  float d = log(val / min_val);

  // set color
  int idx = int(d * (512 - 1) / e); // find normalized index that falls in range [0, 512)
  gl_FrontColor = vec4(r[idx], g[idx], b[idx], 1.0); // set color
}

相应的C++代码在这里:

QOpenGLShaderProgram glsl_program;
// initialization code is omitted

glsl_program.bind();
glsl_program.setUniformValue(vshader_max_uniform, max_val);
glsl_program.setUniformValue(vshader_min_uniform, min_val);
glsl_program.setUniformValue(vshader_height_uniform, max_colormap_height);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 0, colormap); // colormap is a vector that contains value of power at each vertex
glDrawElements(GL_TRIANGLE_STRIP, vertices_length, GL_UNSIGNED_INT, nullptr); // vertex_length is size of colormap
glDisableVertexAttribArray(0);

glsl_program.release();

这个程序在 Linux 上运行得足够快。但在 Windows 中,速度非常慢,并且占用大量 CPU 时间。如果我更改 GLSL 的这一行:

// int idx = int(d * (512 - 1) / e);
int idx = 0;

那么该应用程序在 Windows 上也运行得很快。所以,这一定是 GLSL 代码的问题。

我该如何解决这个问题?

最佳答案

您在那里所做的事情属于片段着色器,而不是顶点着色器。然后您将颜色查找表和光谱密度数据作为纹理提交。虽然顶点设置并不那么昂贵,但它会带来一定的开销,并且通常您希望用尽可能少的顶点数覆盖尽可能多的像素。

还要学习对数计算规则(例如log(a/b) = log(a) - log(b))并避免在整个绘制调用中进行统一的计算并在主机。

/* vertex shader */
#version 130

varying vec2 pos;    

void main() {
  // set vertex position based on its ID
  // To fill the viewport, we need just three vertices
  // of a rectangular triangle of with and height 2
  pos.x = gl_VertexID % 2;
  pos.y = gl_VertexID / 2;

  // screen position is controlled using glViewport/glScissor
  gl_Position = vec4(2*pos, 0, 1.0);
}

-

/* fragment shader */
#version 130

varying vec2 pos;

uniform sampler2D values;
uniform sampler1D colors;

uniform float log_min;
uniform float log_max;

void main() {
  float val = texture2D(values, pos).x;
  float e = log_max - log_min;
  float d = (log(val) - log_min) / e;

  gl_FragColor = vec4(texture1D(colors, d).rgb, 1.0); // set color
}

在 GLSL 的更高版本中,一些关键字已更改。 Varying 是使用 inout 而不是 variing 定义的,并且纹理访问函数已统一以涵盖所有采样器类型。

glsl_program.bind();
glsl_program.setUniformValue(vshader_log_max_uniform, log(max_val));
glsl_program.setUniformValue(vshader_log_min_uniform, log(min_val));

// specify where to draw in window pixel coordinates.
glEnable(GL_SCISSOR_TEST);
glViewport(x, y, width, height);
glScissor(x, y, width, height);

glBindTexture(GL_TEXTURE_2D, values_texture);
glTexSubImage2D(GL_TEXTURE_2D, ..., spectral_density_data);
glDrawArrays(GL_TRIANGLES, 0, 3);

glsl_program.release();

关于c++ - OpenGL 顶点着色器在 Linux 上速度很快,但在 Windows 上非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52659188/

相关文章:

c++ - 奇怪的调试器问题?

c++ - C++ 内联函数的定义必须在同一个文件中吗?

c++ - OpenGL 包装器中的 Alpha/纹理问题

c++ - 如何在 OpenGL C++ 中旋转旋转轴?

c++ - OpenGL 3.3 中的多纹理和多光源

c++ - 如何全局#define预处理器变量?

c++11 可变参数编程,如何定义 vector 塔

opengl - 在 CUDA 中使用 OpenGL 深度信息

opengl - 如何在转换中使用枢轴点

c++ - GLSL Sphere - 射线相交几何解决方案