gpt4 book ai didi

opengl - 仅使用可变变量的一部分

转载 作者:行者123 更新时间:2023-12-04 14:55:23 25 4
gpt4 key购买 nike

假设我在任何两个 GLSL 着色器阶段(例如顶点和片段阶段)之间有一个可变变量,声明为 vec4 :

in/out/varying vec4 texCoord;

如果我只在两个着色器中使用该变量的一部分(例如,通过 swizzling)会发生什么,即我只在顶点着色器中写入它的一部分并且只从片段着色器中的同一部分读取?
// vertex shader
texCoord.st = ...

// fragment shader
... = texture2D(..., texCoord.st);

是否保证(即通过规范)始终产生合理的结果? 这样做似乎是合理的,但是我不太了解 GLSL 语言律师的复杂性,并且不知道编译器/链接器是否将该可变变量解释为“不完整”,因为它不是在前一阶段完全写入。我确定 texCoord.pq 的值无论如何都会未定义,但是 这会影响texCoord.st的有效性吗?太或者整个变化的系统是否在纯组件级别上运行?

乍一看,我在 GLSL 规范中没有发现任何与此相关的内容,我更喜欢基于实际规范或任何其他“官方”保证的答案,而不是声明它应该在合理的硬件上工作(当然,除非这case 只是未指定或实现定义)。我也对整个 GLSL 历史中任何可能的变化感兴趣,包括一直回到它的设备到不推荐使用的内置变量,如 gl_TexCoord[]在旧的 GLSL 1.10 中。

最佳答案

根据规范,我试图争辩说您的代码会很好。但是,我不确定您是否会发现我的推理 100% 令人信服,因为我认为规范对此似乎有些不精确。我要引用OpenGL 4.5 Core Profile SpecificationOpenGL Shading language 4.50 specification.

关于输入和输出变量,GLSL 规范在 4.3.4 节中建立了以下内容

Shader input variables are declared with the storage qualifier in. They form the input interface between previous stages of the OpenGL pipeline and the declaring shader. [...] Values from the previous pipeline stage are copied into input variables at the beginning of shader execution.



和 4.3.6 分别为:

Shader output variables are declared with a storage qualifier using the storage qualifier out. They form the output interface between the declaring shader and the subsequent stages of the OpenGL pipeline. [...] During shader execution they will behave as normal unqualified global variables. Their values are copied out to the subsequent pipeline stage on shader exit. Only output variables that are read by the subsequent pipeline stage need to be written; it is allowed to have superfluous declarations of output variables.



第 5.8 节“分配”规定

Reading a variable before writing (or initializing) it is legal, however the value is undefined.



自从分配了 .st vector 将写入子向量,我们可以确定此变量将在着色器调用结束时包含两个已初始化和两个未初始化的组件,并且整个向量将被复制到输出。

GL 规范的第 11.1.2.1 节规定:

If the output variables are passed directly to the vertex processing stages leading to rasterization, the values of all outputs are expected to be interpolated across the primitive being rendered, unless flatshaded. Otherwise the values of all outputs are collected by the primitive assembly stage and passed on to the subsequent pipeline stage once enough data for one primitive has been collected.



“所有输出的值”由着色器决定,虽然有些组件有未定义的值,但它们仍然有值,这里没有未定义或实现定义的行为。线和多边形图元的插值公式(第 14.5.1 和 14.6.1 节)也不会在组件之间混合,因此任何定义的组件值都将导致插值基准中的定义值。

第 11.1.2.1 节还包含有关顶点着色器输出的声明:

When a program is linked, all components of any outputs written by a vertex shader will count against this limit. A program whose vertex shader writes more than the value of MAX_VERTEX_OUTPUT_COMPONENTS components worth of outputs may fail to link, unless device-dependent optimizations are able to make the program fit within available hardware resources.



请注意,这种语言意味着 vec4 的全部 4 个组件一旦写入单个组件,就会根据限制进行计数。

关于opengl - 仅使用可变变量的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com