raysan5 / raylib

A simple and easy-to-use library to enjoy videogames programming
http://www.raylib.com
zlib License
21.99k stars 2.22k forks source link

[rmodels] invalid rlDisableVertexAttribute call when mesh lacks vertex color data and shader lacks vertex color input #3841

Closed velartrill closed 6 months ago

velartrill commented 7 months ago

Please, before submitting a new issue verify and check:

Issue description

when drawing a mesh with the DrawMesh() function, a high-severity GL error is raised if the following two conditions are met:

the error as reported by raylib is

GL: OpenGL debug message: GL_INVALID_VALUE in glDisableVertexAttribArray(index)
     > Type: ERROR
     > Source = API
     > Severity = HIGH

the cause of this is rmodels.c:1474, which calls rlDisableVertexAttribute() regardless of whether such an attribute was ever enabled if mesh.vboId[3] == 0 (i.e. the mesh was never given a VBO for vertex color data)

it is likely this fairly common case was missed due to a lack of a debug context during testing. i don't know enough about the GL state machine to know how likely this is to cause actual rendering problems; i encountered this as one of large number of errors that mesa reported while trying to debug a GPU crash. i am not yet certain if this bug was the culprit, but at the very least it makes debugging more difficult to have trace output clogged with this error.

i would like to stress that this error cannot be (reliably) fixed by simply adding an in vec4 vertexColor to the vertex shader. mesa at the very least will elide variables that cannot be used in the output. a viable workaround might be to forcibly insert an empty VBO into the mesh, bypassing the LoadModel/GenMesh API, but this is a rather discomfiting solution as it relies on the knowledge of internal magic numbers

it would appear that making this call to rlDisableVertexAttribute() conditional upon the presence of a vertex color input location in the Shader object would trivially solve the problem.

Environment

nixos unstable (linux 6.1.75), GL 4.5, using mesa software rendering (LIBGL_ALWAYS_SOFTWARE=1). raylib built from master using gcc 13

Issue Screenshot

n/a

Code Example

auto m = LoadModel("model.gltf"); // where model lacks vertex color data
auto s = LoadShader("shader.vert", "shader.frag"); // where shader does not use a vertexColor input
auto mat = LoadMaterialDefault();
mat.shader = s;
DrawMesh(m.meshes[0], mat, MatrixIdentity());
raysan5 commented 7 months ago

@velartrill Thanks for reporting. Afaik, most drivers take care of it without further issues...

raylib default shader setups vertex color attribute location because it is used by rlgl batching system (to draw 2d/3d shapes) but most meshes probably do not need them...

Here it is a related issue: https://github.com/raysan5/raylib/issues/3109

Not sure what would be the better solution to address this issue... I vaguely remember having some issue in the past with it...