gpt4 book ai didi

java - If Else 语句导致 stage 行为 GLSL

转载 作者:行者123 更新时间:2023-12-01 12:48:03 24 4
gpt4 key购买 nike

所以我的顶点着色器是这样的

String vertexshader = "#version 330 core"
+ System.getProperty("line.separator")
+ "in vec4 vertexin;"
+ "in vec2 textcoords;"
+ "in int texture;"
+ "uniform mat4 orthogonal;"
+ "out vec2 supertextcoords;"
+ "out int textureoi;"
+ "void main(){"
+ "gl_Position = orthogonal * vertexin ;"
+ "supertextcoords = textcoords;"
+ "textureoi=texture;"
+ "}";

我的片段着色器是这样的

String fragmentshader="#version 330 core"
+ System.getProperty("line.separator")
+"out vec4 outcolor;"
+ "in int textureoi;"
+"in vec2 supertextcoords;"
+"uniform sampler2D texture1;"
+"uniform sampler2D texture2;"
+ "vec4 texturedecode();"
+ "vec4 texturedecode() {"
+ "vec4 color;"
+ "if(textureoi==0) {"
+ "color= texture2D(texture1, supertextcoords); return color; }"
+ "else if(textureoi==1) {"
+ " color= texture2D(texture2, supertextcoords); return color;"
+ "} else {"
+ " color= texture2D(texture2, supertextcoords); return color;}"
+ "}"
+"void main(){"
+" outcolor =texturedecode();"
+"}";

问题是,当我 glUseProgram() 时,我收到错误 1282。仅当我使用函数texturedecode() 时,才会出现该问题。如果我在 outcolor 变量上使用它或单独声明一些未使用的变量,这并不重要。它使 opengl 崩溃。

我发送变量的方式如下。

glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, TextureInit.loadTexture(io.getSubimage(0,0, 32, 32), 4));
int loc1 = glGetUniformLocation(program, "texture1");
glUniform1i(loc1, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, TextureInit.loadTexture(io.getSubimage(32,0, 32, 32), 4));
int loc2 = glGetUniformLocation(program, "texture2");
glUniform1i(loc2, 0);
int orthographicloc=glGetUniformLocation(program,"orthogonal");
glUniformMatrix4(orthographicloc,false, orthographicBuffer);
glBindAttribLocation(program, 0, "vertexin");
glBindAttribLocation(program, 1, "textcoords");
glLinkProgram(program);


glVertexAttribPointer(0, 4, GL_FLOAT,false,0,0);
glVertexAttribPointer(1,2,GL_FLOAT,false,0,vertices.length*datasize);
glVertexAttribIPointer(2,1,GL_INT,0,vertices.length*datasize+textcoord.length*4);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glDrawArrays(GL_TRIANGLES, 0, vertices.length/4);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);

最佳答案

来自OpenGL wiki :

In fragment shaders, there is one other circumstance that can cause all of your non-"Lod" or "Grad" texture accesses to become undefined: non-uniform flow control.

Uniform flow control for a particular location in code means that, no matter how a shader is executed, the shader will follow the same path to get to that location of code. Consider the following GLSL code for a fragment shader:

void main()
{
vec4 firstData = texture(someSampler, textureCoords);

if(parameterValue < 20)
{
firstData += texture(someOtherSampler, otherTexCoords);
}

vec4 moreData = texture(thirdSampler, thirdTexCoords);
}

The first texture access happens in uniform flow control. Thus, the texture access produces definite results. However, the next texture access is not in uniform flow. If the accessed texture uses mipmapping or anisotropic filtering of any kind, then any texture function that is not "Lod" or "Grad" will retrieve undefined results. The last texture access happens in uniform flow control again and will produce definite results.

Note: The GLSL compiler will not give you an error for this. It is perfectly legal GLSL code. It only becomes undefined when certain texture state is set on those textures. Specifically, if mipmap or anisotropic filtering is used.

为了缓解这种情况,您可以对两种纹理进行采样并使用 if/else 返回适当的颜色值,或者手动使用 textureGrad

关于java - If Else 语句导致 stage 行为 GLSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493315/

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