繁体   English   中英

如何将变量从顶点着色器移动到几何着色器?

[英]How do you move variables from Vertex shader to Geometry shader?

我有以下顶点着色器:

#version 330

layout (location = 0) in ivec4 inOHLC;
layout (location = 1) in int inVolume;
layout (location = 2) in int inTimestamp;

out ivec4 outOHLC;
out int outVolume;
out int outTimestamp;

void main()
{
    outOHLC = inOHLC;
    outVolume= inVolume;
    outTimestamp = inTimestamp;
}

我想在几何着色器中接收outOHLCoutVolumeoutTimestamp 我这样编码:

#version 330

in ivec4 inOHLC;
in int inVolume;
in int inTimestamp;

layout (line_strip, max_vertices = 2) out;

void main() {  

    float x = (float)inTimestamp / 100.0;
    float y1 = (float)inOHLC[1] / 100.0;
    float y2 = (float)inOHLC[1] / 100.0;
    gl_Position = vec4(x, y1, 0.0, 0.0);
    EmitVertex();    
    gl_Position = vec4(x, y2, 0.0, 0.0);
    EmitVertex();    
    EndPrimitive();

}  

但我收到以下错误:

run:
. . . vertex compilation success.
. . . geometry compilation failed.
Shader Info Log: 
ERROR: 7:1: 'inOHLC' : geometry shader input varying variable must be declared as an array
ERROR: 8:1: 'inVolume' : geometry shader input varying variable must be declared as an array
ERROR: 9:1: 'inTimestamp' : geometry shader input varying variable must be declared as an array
ERROR: 15:1: ')' : syntax error syntax error

在@Rabbid76 评论之后,我将代码修改如下:

#version 330

in ivec4 ohlc[];
in int volume[];
in int timestamp[];

layout (line_strip, max_vertices = 2) out;

void main()
{

    float x = (float)timestamp / 100.0;
    float y1 = (float)ohlc[1] / 100.0;
    float y2 = (float)ohlc[2] / 100.0;
    gl_Position = vec4(x, y1, 0.0, 0.0);
    EmitVertex();    
    gl_Position = vec4(x, y2, 0.0, 0.0);
    EmitVertex();    
    EndPrimitive();

} 

现在我只得到一个错误:

run:
. . . vertex compilation success.
. . . geometry compilation failed.
Shader Info Log: 
ERROR: 40:1: ')' : syntax error syntax error

我猜这与(float) cast(它甚至存在于 GLSL 中吗?)或浮点变量定义有关。

仔细阅读消息:

几何着色器输入变量必须声明为数组

几何着色器的输入是一个图元。 这意味着构成图元的顶点着色器的所有输出都是组合的。 因此几何着色器的输入是 arrays:

in ivec4 inOHLC[];
in int inVolume[];
in int inTimestamp[];

此外,您必须为几何着色器指定输入图元类型 例如一行:

layout(lines​) in;

原始类型lines意味着几何着色器接收 2 个顶点(输入的数组大小为 2)。


GLSL 没有像 C 这样的转换运算符。 如果要将int转换为float ,则必须构造一个新的float 重载的float构造函数接受一个int参数:

float y1 = (float)ohlc[1] / 100.0;

float y1 = float(ohlc[1]) / 100.0;

Indices start at 0 not at 1 (like in C, C++, C#, Java, JavaScript, Python, ...):

float y1 = float(ohlc[0]) / 100.0;
float y2 = float(ohlc[1]) / 100.0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM