emscripten-ports / SDL2

Other
166 stars 64 forks source link

SDL_RenderDrawLines ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 1 #134

Open DarthGandalf opened 3 years ago

DarthGandalf commented 3 years ago

I'm using SDL_RenderDrawLines.

I cannot reproduce the issue myself, but one of users sees that message in browser console, and the lines are not drawn.

https://www.asokolov.org/advent-of-code/2020/day12.html is the deployed version of it.

screenshot

However, sometimes the lines are drawn correctly, as can be seen here. First it shows correctly, then just stops. With a different input (e.g. this one) it stops drawing at a different point.

The user tried it with Chrome and Firefox, on Windows, with the same result.

This workaround helps: SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");

Source code:

blankdvth commented 3 years ago

Hey, I'm that user and I'd like to point out that your input didn't work either on the website (same issue, shows for a bit then stops, except this time it shows even less). The click here (?renderer=software) fixes it for both your and my input. Seems that the glitch occurs regardless of the input on the normal renderer.

DarthGandalf commented 3 years ago

Oh, okay, I've updated the description, thanks.

Daft-Freak commented 3 years ago

Okay, I can reproduce on Windows, but not Linux (browser doesn't seem to matter).

Looks like there's a bug in the GLES renderer. Attribute 1 is texture coordinates, which should be disabled when rendering lines. Something is causing the state to get a bit out of sync, resulting in that not happening.

This patch seems to fix it, but don't know how correct it is... (Don't really know the renderer code, usually stick to the backend)

diff --git a/src/render/opengles2/SDL_render_gles2.c b/src/render/opengles2/SDL_render_gles2.c
index 028227f75..bde9d8ab3 100644
--- a/src/render/opengles2/SDL_render_gles2.c
+++ b/src/render/opengles2/SDL_render_gles2.c
@@ -983,17 +983,17 @@ SetDrawState(GLES2_RenderData *data, const SDL_RenderCommand *cmd, const GLES2_I
         data->drawstate.cliprect_dirty = SDL_FALSE;
     }

-    if (texture != data->drawstate.texture) {
-        if ((texture != NULL) != data->drawstate.texturing) {
-            if (texture == NULL) {
-                data->glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
-                data->drawstate.texturing = SDL_FALSE;
-            } else {
-                data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
-                data->drawstate.texturing = SDL_TRUE;
-            }
+    if ((texture != NULL) != data->drawstate.texturing) {
+        if (texture == NULL) {
+            data->glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
+            data->drawstate.texturing = SDL_FALSE;
+        } else {
+            data->glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
+            data->drawstate.texturing = SDL_TRUE;
         }
+    }

+    if (texture != data->drawstate.texture) {
         if (texture) {
             GLES2_TextureData *tdata = (GLES2_TextureData *) texture->driverdata;
             if (tdata->yuv) {