【问题标题】:What happens to vertex shader `varying` variable with `highp` qualifier if high precision is unsupported in fragment shader?如果片段着色器不支持高精度,带有“highp”限定符的顶点着色器“variing”变量会发生什么?
【发布时间】:2018-03-07 01:35:34
【问题描述】:

在 OpenGL ES 2.0 (Shading Language 1.00) 中,如果 GL_FRAGMENT_PRECISION_HIGH 未定义,使用 highp 限定符限定 varying 顶点着色器变量是否会影响性能?

例如,当highp 在片段语言中不可用时,将以下片段着色器与以下两个顶点着色器中的每一个链接起来,一次一个,会产生等效的程序吗?

片段:

#ifdef GL_FRAGMENT_PRECISION_HIGH
varying highp vec2 vTextureCoord;
#else
varying mediump vec2 vTextureCoord;
#endif
...

顶点 1:

...
attribute vec2 aTextureCoord;

varying highp vec2 vTextureCoord;

void main() {
    ...
    vTextureCoord = aTextureCoord;
}

顶点 2:

...
attribute vec2 aTextureCoord;

#ifdef GL_FRAGMENT_PRECISION_HIGH
varying highp vec2 vTextureCoord;
#else
varying mediump vec2 vTextureCoord;
#endif

void main() {
    ...
    vTextureCoord = aTextureCoord;
}

GLSL ES 1.00 spec 中引用 GL_FRAGMENT_PRECISION_HIGH 的部分是 4.5.4。

【问题讨论】:

  • 版本一会编译/链接失败
  • @gman 你确定吗?我尝试在 Firefox 中使用 webgl 将其设置为顶点着色器中的highp 和片段着色器中的mediump(没有预处理器检查),并且它有效。还是因为提示已被忽略,因为它在桌面上,因此没有不匹配?
  • IIRC 在不支持 highp 的机器上会失败。我知道没有任何台式机可以做到这一点。你基本上需要一部旧手机来测试。

标签: webgl opengl-es-2.0 glsles


【解决方案1】:

我的经验是版本一将无法在不支持片段着色器中的 highp 的机器上编译。这些基本上是旧手机。我不确定您必须使用哪一代手机,但我知道最近的智能手机在片段着色器中支持 highp。

根据我的经验,在台式机上,即使您使用 mediump,它们也总是使用 highp。请注意,就规范而言,这很好。该规范允许实现使用比要求更高的精度。

在移动设备上,至少到 2018 年为止,大多数 GPU 确实支持 mediump,并且性能会有所不同。也将有指定的只有中等水平的精度。

Here's a small example:

// WebGL 3D Lathe Compute Normals
// from https://webgl2fundamentals.org/webgl/webgl-3d-lathe-step-03.html

"use strict";

const vs = `
attribute vec4 a_position;
attribute vec2 a_texcoord;

varying vec2 v_texcoord;

void main() {
  gl_Position = a_position;

  v_texcoord = a_texcoord;
}
`;

const fs = `
precision mediump float;

// Passed in from the vertex shader.
varying vec2 v_texcoord;

uniform float u_scale;

void main() {
  gl_FragColor = vec4(v_texcoord * u_scale, 1, 1);
}
`;

function main() {
  const m4 = twgl.m4;
  twgl.setDefaults({attribPrefix: "a_"});

  // Get A WebGL context
  /** @type {HTMLCanvasElement} */
  const canvas = document.querySelector("canvas");
  const gl = canvas.getContext("webgl");
  if (!gl) {
    return;
  }

  // setup GLSL programs
  const programInfo = twgl.createProgramInfo(gl, [vs, fs]);
  
  const size = 1/10000;
  
  const bufferInfo = twgl.createBufferInfoFromArrays(gl, {
  position: {
    data: [
    -1, -1,
     1, -1,
    -1,  1,
     1,  1,
    ],
    numComponents: 2,
  },
  texcoord: [
    0, 0, 
    size, 0,
    0, size,
    size, size,
  ],
  indices: [
    0, 1, 2, 
    2, 1, 3,
  ],
 });

  function update() {
    render();
  }
  update();

  function render() {
    twgl.resizeCanvasToDisplaySize(gl.canvas, window.devicePixelRatio);

    // Tell WebGL how to convert from clip space to pixels
    gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

    gl.enable(gl.DEPTH_TEST);

    // Clear the canvas AND the depth buffer.
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    // Compute the projection matrix
    gl.useProgram(programInfo.program);

    // Setup all the needed attributes.
    twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);

    // Set the uniforms
    // calls gl.uniformXXX, gl.activeTexture, gl.bindTexture
    twgl.setUniforms(programInfo, {
      u_scale: 1 / size,
    });

    // calls gl.drawArrays or gl.drawElements.
    twgl.drawBufferInfo(gl, bufferInfo);
  }

}

main();
body {
  margin: 0;
}
canvas {
  width: 100vw;
  height: 100vh;
  display: block;
}
<canvas></canvas>

<script src="https://webgl2fundamentals.org/webgl/resources/twgl-full.min.js"></script>

这只是绘制一个四边形并在四边形上插入值。这些值从 0 到 0.0001,然后乘以 10000 得到从 0 到 1 的值。

使用 mediump 的桌面(我的桌面 GPU 实际上会使用 highp)

iPhoneX 使用 mediump(实际上将使用 mediump)

【讨论】:

  • mediump GPU 支持是什么意思?我以为它总是可用的?
  • 我的意思是 GPU 实际上以 meduimp 精度运行 mediump 而不是 highp 精度。运行 mediump 因为 highp 是规范兼容的,但它不能帮助您测试您的代码将在实际以 mediump 精度运行 mediump 的硬件上运行
  • 对不起,我错误地认为第一段和最后一段有点矛盾,但你的意思是,大多数支持highp 的当前 GPU 将遵循mediump 精度提示,与桌面不同?
  • 2018 年大多数当前支持 highp 的手机如果在着色器中标记为 mediump,实际上将使用 mediump 精度,而不像在桌面上标记为 mediump 仍然可以获得 highp
  • 感谢您的澄清
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多