gpt4 book ai didi

c++ - glGetUniformLocation 返回 -1 即使我在着色器中使用了变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:02:37 25 4
gpt4 key购买 nike

似乎glsl 编译器优化了未使用的变量(删除)。在我的例子中,我使用了变量,但 glGetUniformLocation 返回 -1

rgbScene.addRenderStage(
[&obj = std::as_const(model)](Camera* cam) {

auto& mesh = std::get<Mesh>(obj);
auto& program = std::get<ShaderProgram>(obj);

glUseProgram(program);

glBindVertexArray(mesh.getData<VAO>());
int loc;
glUniformMatrix4fv(
loc = glGetUniformLocation(program, "proj_matrix"),
1, GL_FALSE,
glm::value_ptr(cam->getProjectionMatrix())
);

glUniformMatrix4fv(
loc = glGetUniformLocation(program, "view_matrix"),
1, GL_FALSE,
glm::value_ptr(cam->getViewMatrix())
);

glUniformMatrix4fv(
loc = glGetUniformLocation(program, "model_matrix"),
1, GL_FALSE,
glm::value_ptr(mesh.getModelMatrix())
);

glUniform3fv(
loc = glGetUniformLocation(program, "light_direction"),
1, glm::value_ptr(-(cam->getForward()))
);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, mesh.getData<VertexData>());
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, mesh.getData<NormalData>());
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);

glDrawArrays(GL_TRIANGLES, 0, mesh.getSize());

glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
});

我通过在 Visual Studio 2019 中逐行调试来检查变量 loc,最后 glGetUniformLocation 返回 -1

这是我的顶点着色器代码

#version 460 core

uniform mat4 proj_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
uniform vec3 light_direction;

layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;

out VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} vs_out;

void main(void)
{
vec4 P = view_matrix * model_matrix * vec4(pos, 1.0);

vs_out.N = mat3(view_matrix * model_matrix) * normal;
vs_out.L = mat3(view_matrix) * (-light_direction);
vs_out.V = -P.xyz;

gl_Position = proj_matrix * P;
}

我试过改变变量名,不同的顺序......但无法解决这个问题

shader中uniform变量还有其他规则吗??

-- 编辑--对于片段着色器,

#version 460 core

layout (location = 0) out vec4 color;

in VS_OUT
{
vec3 N;
vec3 L;
vec3 V;
} fs_in;

uniform vec3 diffuse_albedo = vec3(0.8, 0.3, 0.2);
uniform vec3 specular_albedo = vec3(0.7);
uniform float specular_power = 128.0;

void main(void)
{
vec3 N = normalize(fs_in.N);
vec3 L = normalize(fs_in.L);
vec3 V = normalize(fs_in.V);

vec3 R = reflect(-L, N);

vec3 diffuse = max(dot(N, L), 0.0) * diffuse_albedo;
vec3 specular = pow(max(dot(R, V), 0.0), specular_power) * specular_albedo;

color = vec4(diffuse + specular, 1.0);

// color = vec4(1.0,1.0, 1.0, 1.0);
}

最佳答案

片段着色器输入 NLV 变量也必须“使用”

请注意,事件资源是在程序链接时确定的。如果未使用片段着色器的输入,则在顶点着色器中设置相应输出变量的制服可能不会变为事件状态。

参见 OpenGL 4.6 Core Profile Specification - 7.3.1 Program Interfaces , 第 102 页:

7.3.1 Program Interfaces

When a program object is made part of the current rendering state, its executable code may communicate with other GL pipeline stages or application code through a variety of interfaces. When a program is linked, the GL builds a list of active resources for each interface. Examples of active resources include variables, interface blocks, and subroutines used by shader code. Resources referenced in shader code are considered active unless the compiler and linker can conclusively determine that they have no observable effect on the results produced by the executable code of the program. For example, variables might be considered inactive if they are declared but not used in executable code, used only in a clause of an if statement that would never be executed, used only in functions that are never called, or used only in computations of temporary variables having no effect on any shader output. In cases where the compiler or linker cannot make a conclusive determination, any resource referenced by shader code will be considered active. The set of active resources for any interface is implementation-dependent because it depends on various analysis and optimizations performed by the compiler and linker

If a program is linked successfully, the GL will generate lists of active resources based on the executable code produced by the link.

关于c++ - glGetUniformLocation 返回 -1 即使我在着色器中使用了变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56883308/

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