Best practices for coding , compiling, debugging and maintaining shader programs in opengl? [closed]

倾然丶 夕夏残阳落幕 提交于 2019-12-08 14:21:54

问题


I'm a newbie.. trying to learn shaders. I feel like its not that easy to code and debug it. Examples and tutorials I see on internet allows to code them in text files and read them as char pointer to pass them to shader compiler. Is there any best practices for coding - with intellisence , compiling , debugging ( printing the value some where in console atleast? ) And how to maintain and organise multiple shaders in a giant source code?

I'm using visual studio. Is there any tools or extensions for it to support opengl shaders?

If not is there any dedicated IDE only for opengl shaders? - if so how to organise them with other c++ source files in visual studio?.

How do they actually do it?

Thanks.


回答1:


The absolute minimum is to have access to GLSL compile and link logs here example:

  • complete GL+GLSL+VAO/VBO C++ example

    just look for glsl_log in the code.

For really hard stuff its best to write your shader as CPU side C++ code and when fully debugged then run it as shader. But for that you would need to write some kind of framework that would emulate shaders. For starters you need 1/2/3/4D vector and matrix arithmetics from GLSL. It is not that easy to code it but there are also another options like GLM. I am using my template but just to enable vec datatypes its around 228 KByte of hideous code due to its getters setters for more info look here:

  • GLSL like vec template for C++

And another absolute minimum is handling input and output. As the fun part is usually located in fragment itst often enough to process only fragment shader this way so you need just 2 nested loops looping through test image calling your shader main function and setting the pixel according to its result. I usually place #define texture(...)... to emulate texture access and that is in nutshell all. For example these shaders I would not be possible to debug otherwise:

  • raytrace through 3D mesh
  • raytrace through 3D volume

As was mentioned in comments for simpler shaders you can write them in any C++ IDE to enable syntax highlight. But its a good idea to write/debug the stuff in your target App where you simply add some subwindow where you render the GLSL info logs as opening text file after each compilation is a dull click work.

When I started coding shaders I was tired of such and write my own IDE for shaders. Its buggy and far from perfect but it fulfills my needs. It looks like this:

Link to it is in (edit3) here:

  • How do I get textures to work in OpenGL?

There are some similar tools out there but having my own with source code allows a whole lot of stuff like using targets app CPU side code etc ...

Another very useful thing is to be able to print some variable value from fragment shader. Exactly that I was able to do like this:

  • GLSL debug prints

you just need to decide correct conditions for entire area of the print (so the value you are printing is the same while printing all its pixels). This help me a lot especially to use 3D textures and geometry encoded in textures for the first time...

However in most cases you can use color output instead of debug prints but having the ability to print actual numbers can save you a lot of time.




回答2:


I'm using visual studio. Is there any tools or extensions for it to support opengl shaders?

There is a GLSL Integration at Visual Studio Marketplace. I generally make a separate folder for shaders to organize them.

Its a bit hard and frustrating to debug shader code, but with practice and multiple iterations of debugging sessions it becomes more natural. I have started to experiment with my shaders at Shadertoy before porting them back at GLSL, which is surprisingly trivial.

Having said that, there are some cool tools you can use to debug your graphics, namely RenderDoc or AMD CodeXL

A friend suggested that I add vogl to the list of debuggers too. I have not used it, but heard good praises.



来源:https://stackoverflow.com/questions/51297676/best-practices-for-coding-compiling-debugging-and-maintaining-shader-programs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!