请告诉VertexShader错误解决方案

时间:2016-01-20 03:59:39

标签: opengl glsl shader

使用OpenGL的学生。 不会说英语。 所以请理解。

目前存在问题

#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in mat4 instance_ModelMatrix [3];

VertexShader代码。 上面的代码,但它运行

layout (location = 2) in mat4 instance_ModelMatrix [3];

- >

layout (location = 2) in mat4 instance_ModelMatrix [4];

改变运行

  

属性instance_ModelMatrix是一个矩阵或数组,没有空间将其插入绑定的通用属性通道。   资源错误。

这会出错。

我有什么方法可以使用当前超过60的安排

谢谢你看看问题

1 个答案:

答案 0 :(得分:3)

为什么它不起作用

属性的最大数量由GL_MAX_VERTEX_ATTRIBS确定,这对于每个实现都是不同的,但必须至少为16(因此它应该工作......?)。您可以使用glGetIntegerv()获取价值:

int maxVertexAttribs;
glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs);

由于每个mat4计为四个属性,并且每个vec3计为一个,因此以下代码使用14个属性:

#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in mat4 instance_ModelMatrix [3];

在此代码中,instance_ModelMatrix实际上将使用位置2到13.当您更改数组大小时:

#version 400
layout (location = 0) in vec3 VertexPosition;
layout (location = 1) in vec3 VertexNormal;
layout (location = 2) in mat4 instance_ModelMatrix [4];

这使用18个顶点属性,在插槽2到17中使用instance_ModelMatrix。我的猜测是系统上的顶点属性的最大数量是16,所以这不适合。

解决方案

如果要使用大量的每个实例数据,则必须使用制服,统一缓冲区对象或缓冲区纹理。 Uniform buffer objects可能适合您的应用。然后,您可以使用gl_InstanceID作为实例数据的索引。