Sunday, April 14, 2019

opengl - After a succesful glLinkProgram, should I delete / detach my shaders?


After I have a linked program, and thinking about defensive programming, should I delete and detach the shaders used to link this program?



If yes, is that going to free any resources? Or do these shader objects only going to be freed after a glDeleteProgram call?


edit: Just for clarification what I am doing is (which is consistent with the answer):



glCreateShader -> glShaderSource -> glCompileShader -> glCreateProgram -> glAttachShader -> glLinkProgram -> glDetachShader -> glDeleteShader -> draw using this shader program -> and when I don't need this shader anymore glDeleteProgram




Answer



Yes, you should always do this. I didn't find out about this until just recently, but a shader won't actually be deleted by glDeleteShader until it's been detached. It's mentioned on the man page for glDetachShader


EDIT: Almost missed the bit about deleting the shaders too. Yes, you should do this as it frees up the memory used to store the shader source and unlinked object code. This is explained in more detail in this StackOverflow question.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...