I am learning WebGL and I can feel that my speed is so slow because I am having a hard time debugging my code. Is there any extension or tool with help of which I can know the value of buffer, attribpointer, matrixes, etc.
I googled and learned about chrome extension spector.js but this does not work with me. I think it supposes to show me frames or context but when I click it shows nothing.
When I click red button after a few seconds it shows: No frames detected. Try moving the camera or implementing requestAnimationFrame.
Yes, WebGL is hard to debug and I'm not sure anything will make it a whole lot easier. Most bugs are not something a debugger can find that easily. Certain bugs like un-renderable textures or buffers on the correct size already get reported by the browser. Other bugs though are usually math bugs, logic bugs, or data bugs. For example there is no easy way to step through a WebGL shader.
In any case, if you want to use spector you need to structure your code to be spector friendly. Spector is looking for frames based on requestAnimationFrame.
So, let's take this example which is the last example from this page.
The code has a
mainfunction that looks like thisI changed it to this. I renamed
maintoinitand made it so I pass in the gl context.Then I made a new
mainthat looks like thisAnd I added a button to my html
The code is the way it is because we need to get a webgl context first or else spector will not notice the canvas (there will be nothing to select). After when turn to turn on spector, and only after that click the start button to run our code. We need to execute our code in a
requestAnimationFramebecause that is what spector is looking for. It only records WebGL functions between frames.Whether or not it will help you find any bugs though is another matter.
note that, if you are on Mac, Safari also has a WebGL debugger built in but just like spector it's only designed for frames. It requires you to draw something each frame so this worked
Another thing you can do is use a helper to call
gl.getErrorafter every WebGL function. Here's a script you can useYou can either download it or you can just include it via the link above. Example (open the javascript console to see th error)