go-gl / gl

Go bindings for OpenGL (generated via glow)
MIT License
1.08k stars 74 forks source link

Strs returned free not actually working properly #131

Closed DutchEllie closed 3 years ago

DutchEllie commented 3 years ago

Hi, I've been trying to get this stuff to work now for a little bit, but I've ran into a little issue. It seems that the vertex shade (which compiles first) is getting compiled alright, but then when I apply pretty much the same function for the fragment shader compilation (comes straight from the example code for the cube in 4.1, where I am running 4.6) the source in memory from the vertex shader isn't actually cleared properly or at all.

The compiler gives an error, it differs almost every time but always includes the following line: error C0000: syntax error, unexpected '.', expecting "::" at token "." always for the last line of code, which makes sense in a bit.

I am absolutely 100% sure that this:

#version 460 core
     out vec4 FragColor;

     void main()
     {
         FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
     }

is an absolutely perfectly valid shader, for as little as I know about OpenGL, anyway.

When I print the fragmentShaderSource, which is the go type string that this code is put in, it prints out perfectly fine. However when I try to print out the cstring returned by the gl.Strs function using something like this: fmt.Println(gl.GoStr(*csources)) the following is returned:

#version 460 core
         out vec4 FragColor;

         void main()
         {
                 FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
         }s.y, aPos.z,▼����∟☺

This only happens when this is the second shader to be compiled. The same print function for the vertexshader (which comes first) is not broken and perfectly fine all the time. It's only the second one that is broken.

How is this even possible at all, when your own example works perfectly fine for me. The cube example works great.

DutchEllie commented 3 years ago

Adding + "\x00" after the string declaration fixed this issue. Kind of unintuitive how the function doesn't just do this for me, but I suppose there probably is a reason somewhere... Sorry for a useless issue!

dmitshur commented 3 years ago

Sometimes the "\x00" isn't added automatically out of consideration to avoid allocations, since they can be easily avoided if the caller provides a null-terminated string.

That said, Strs does add "\x00" if it's missing. But Str and GoStr require the input string to be already null-terminated. This is stated in their documentation:

Strs takes a list of Go strings (with or without null-termination) ...
Str takes a null-terminated Go string ...
GoStr takes a null-terminated string returned by OpenGL ...