aybe's picture

Using GL.GetAttachedShaders

Hi,

Do you know an easier way for handling the count parameter ?
Is not using unsafe/Marshal possible at all ?

        /// <summary>
        /// Returns IDs of attached shaders.
        /// </summary>
        public unsafe int[] AttachedShaders
        {
            get
            {
                // Get number of attached shaders
                int maxCount;
                GL.GetProgram(_id, ProgramParameter.AttachedShaders, out maxCount);
 
                // Get IDs of attached shaders
                IntPtr ptr = Marshal.AllocHGlobal(sizeof(int));
                void* pointer = ptr.ToPointer();
                int* count = (int*)pointer;
                int written;
 
                int[] shaders = new int[maxCount];
                GL.GetAttachedShaders(_id, maxCount, count, shaders);
 
                // Get the number of IDs written
                written = *count;
 
                Marshal.FreeHGlobal(ptr);
 
                return shaders;
            }
        }

Thanks !


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

Please file a bug report, the count parameter should be out int, not int*.

An easy workaround is to use:

GL.GetAttachedShaders(_id, maxCount, out count, out shaders[0]);

Note that this workaround will only work (by design) in OpenTK. Regular DllImports will crash if you try to fill an array via an out parameter, so don't be tempted to use this pattern elsewhere!

aybe's picture

Sorry for the late answering ... I created this bug : GL.GetAttachedShaders overload is bloated
Personally, I left is as-is, it just works.

Thank you.