Example #1
0
        public unsafe void Link(IEnumerable <Shader> shaderList)
        {
            var programId = Gl.CreateProgram();

            foreach (var sd in shaderList)
            {
                Gl.AttachShader(programId, sd.Raw());
            }
            Gl.LinkProgram(programId);
            int status;

            Gl.GetProgramiv(programId, Gl.LinkStatus, &status);
            if (status == 0)
            {
                int infoLogLength;
                Gl.GetProgramiv(programId, Gl.InfoLogLength, &infoLogLength);
                var strInfoLog = Marshal.AllocHGlobal(infoLogLength + 1);
                Gl.GetProgramInfoLog(programId, infoLogLength, null, (char *)strInfoLog);
                var infoLog = Gl.Utf8ToManaged(strInfoLog);
                Marshal.FreeHGlobal(strInfoLog);
                throw new Exception("Linker failure: " + infoLog);
            }

            _hdc = programId;
        }
Example #2
0
        /// <summary>
        /// Links a vertex and fragment shader together to create a shader program.
        /// </summary>
        /// <param name="vertexShader">Specifies the vertex shader.</param>
        /// <param name="fragmentShader">Specifies the fragment shader.</param>
        public ShaderProgram(Shader vertexShader, Shader fragmentShader)
        {
            this.VertexShader    = vertexShader;
            this.FragmentShader  = fragmentShader;
            this.ProgramID       = Gl.CreateProgram();
            this.DisposeChildren = false;

            Gl.AttachShader(ProgramID, vertexShader.ShaderID);
            Gl.AttachShader(ProgramID, fragmentShader.ShaderID);
            Gl.LinkProgram(ProgramID);

            GetParams();
        }
Example #3
0
        /// <summary>
        /// Links a vertex and fragment shader together to create a shader program.
        /// </summary>
        /// <param name="vertexShader">Specifies the vertex shader.</param>
        /// <param name="fragmentShader">Specifies the fragment shader.</param>
        public ShaderProgram(Shader vertexShader, Shader fragmentShader)
        {
            this.VertexShader    = vertexShader;
            this.FragmentShader  = fragmentShader;
            this.ProgramID       = Gl.CreateProgram();
            this.DisposeChildren = false;

            Gl.AttachShader(ProgramID, vertexShader.ShaderID);
            Gl.AttachShader(ProgramID, fragmentShader.ShaderID);
            Gl.LinkProgram(ProgramID);

            //Check whether the program linked successfully.
            //If not then throw an error with the linking error.
            if (!Gl.GetProgramLinkStatus(ProgramID))
            {
                throw new Exception(ProgramLog);
            }

            GetParams();
        }
        /// <summary>
        /// Link this ShaderProgram.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for linking this ShaderProgram.
        /// </param>
        /// <param name="cctx">
        /// A <see cref="ShaderCompilerContext"/> that specify additional compiler parameters.
        /// </param>
        /// <remarks>
        /// <para>
        /// Generate shader program source code, compile and link it. After a successfull
        /// link, obtain every information about active uniform and input variables.
        /// </para>
        /// <para>
        /// This routine generate the source code of each attached ShaderObject instance and
        /// compile it. This step is performed only if really required (tendentially every
        /// shader object is already compiled).
        /// </para>
        /// <para>
        /// After having compiled every attached shader object, it's performed the linkage between
        /// shader objects. After this process the ShaderProgram instance can be bound to issue
        /// rendering commands.
        /// </para>
        /// </remarks>
        /// <exception cref="InvalidOperationException">
        /// Exception thrown in the case this ShaderProgram is already linked.
        /// </exception>
        /// <exception cref="ShaderException">
        /// Exception throw in the case this ShaderProgram is not linkable.
        /// </exception>
        private void Link(GraphicsContext ctx, ShaderCompilerContext cctx)
        {
            CheckCurrentContext(ctx);

            if (cctx == null)
            {
                throw new ArgumentNullException("cctx");
            }

            // Using a deep copy of the shader compiler context, since it will be modified by this ShaderProgram
            // instance and the attached ShaderObject instances
            cctx = new ShaderCompilerContext(cctx);

            #region Compile and Attach Shader Objects

            // Be sure to take every attached shader
            uint[] shadersObject = null;
            int    shadersCount;

            Gl.GetProgram(ObjectName, Gl.ATTACHED_SHADERS, out shadersCount);

            if (shadersCount > 0)
            {
                shadersObject = new uint[shadersCount];
                Gl.GetAttachedShaders(ObjectName, out shadersCount, shadersObject);
                Debug.Assert(shadersCount == shadersObject.Length);
            }

            foreach (ShaderObject shaderObject in _ProgramObjects)
            {
                // Create shader object, if necessary
                if (shaderObject.Exists(ctx) == false)
                {
                    shaderObject.Create(ctx, cctx);
                }

                // Do not re-attach the same shader object
                if ((shadersObject != null) && Array.Exists(shadersObject, delegate(uint item) { return(item == shaderObject.ObjectName); }))
                {
                    continue;
                }

                // Attach shader object
                Gl.AttachShader(ObjectName, shaderObject.ObjectName);
            }

            #endregion

            #region Transform Feedback Definition

            IntPtr[] feedbackVaryingsPtrs = null;

            if ((_FeedbackVaryings != null) && (_FeedbackVaryings.Count > 0))
            {
                sLog.Debug("Feedback varyings ({0}):", cctx.FeedbackVaryingsFormat);
                sLog.Indent();
                foreach (string feedbackVarying in _FeedbackVaryings)
                {
                    sLog.Debug("- {0}", feedbackVarying);
                }
                sLog.Unindent();

                if (ctx.Caps.GlExtensions.TransformFeedback2_ARB || ctx.Caps.GlExtensions.TransformFeedback_EXT)
                {
                    string[] feedbackVaryings = _FeedbackVaryings.ToArray();

                    // Bug in NVIDIA drivers? Not exactly, but the NVIDIA driver hold the 'feedbackVaryings' pointer until
                    // glLinkProgram is executed, causing linker errors like 'duplicate varying names are not allowed' or garbaging
                    // part of the returned strings via glGetTransformFeedbackVarying
                    feedbackVaryingsPtrs = feedbackVaryings.AllocHGlobal();

                    // Specify feedback varyings
                    Gl.TransformFeedbackVaryings(ObjectName, feedbackVaryingsPtrs, (int)cctx.FeedbackVaryingsFormat);
                }
                else if (ctx.Caps.GlExtensions.TransformFeedback2_NV)
                {
                    // Nothing to do ATM
                }
                else
                {
                    throw new InvalidOperationException("transform feedback not supported");
                }
            }

            #endregion

            #region Bind Fragment Locations

            if (ctx.Caps.GlExtensions.GpuShader4_EXT)
            {
                // Setup fragment locations, where defined
                foreach (KeyValuePair <string, int> pair in _FragLocations)
                {
                    if (pair.Value >= 0)
                    {
                        Gl.BindFragDataLocation(ObjectName, (uint)pair.Value, pair.Key);
                    }
                }
            }

            #endregion

            #region Link Shader Program Objects

            int lStatus;

            sLog.Debug("Link shader program {0}", Identifier ?? "<Unnamed>");

            // Link shader program
            Gl.LinkProgram(ObjectName);
            // Check for linking errors
            Gl.GetProgram(ObjectName, Gl.LINK_STATUS, out lStatus);

            // Release feedback varyings unmanaged memory
            if (feedbackVaryingsPtrs != null)
            {
                feedbackVaryingsPtrs.FreeHGlobal();
            }

            if (lStatus != Gl.TRUE)
            {
                const int MaxInfoLength = 4096;

                StringBuilder logInfo = new StringBuilder(MaxInfoLength);
                int           logLength;

                // Obtain compilation log
                Gl.GetProgramInfoLog(ObjectName, MaxInfoLength, out logLength, logInfo);

                // Stop link process
                StringBuilder sb = new StringBuilder(logInfo.Capacity);

                string[] compilerLogLines = logInfo.ToString().Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string logLine in compilerLogLines)
                {
                    sb.AppendLine("  $ " + logLine);
                }

                sLog.Error("Shader program \"{0}\" linkage failed: {1}", Identifier ?? "<Unnamed>", sb.ToString());

                throw new ShaderException("shader program is not valid. Linker output for {0}: {1}\n", Identifier ?? "<Unnamed>", sb.ToString());
            }
            // Set linked flag
            _Linked = true;

            #endregion

            #region Collect Active Program Uniforms

            int uniformBufferSize, attributeBufferSize;
            int uniformCount;

            // Get active uniforms count
            Gl.GetProgram(ObjectName, Gl.ACTIVE_UNIFORMS, out uniformCount);
            // Get uniforms maximum length for name
            Gl.GetProgram(ObjectName, Gl.ACTIVE_UNIFORM_MAX_LENGTH, out uniformBufferSize);

            // Clear uniform mapping
            _UniformMap.Clear();
            _DefaultBlockUniformSlots = 0;

            // Collect uniform information
            for (uint i = 0; i < (uint)uniformCount; i++)
            {
                int uniformNameLength, uniformSize, uniformType;

                // Mono optimize StringBuilder capacity after P/Invoke... ensure enought room
                StringBuilder uNameBuilder = new StringBuilder(uniformBufferSize + 2);
                uNameBuilder.EnsureCapacity(uniformBufferSize);

                // Obtain active uniform informations
                Gl.GetActiveUniform(ObjectName, i, uniformBufferSize, out uniformNameLength, out uniformSize, out uniformType, uNameBuilder);

                string uniformName = uNameBuilder.ToString();

                // Obtain active uniform location
                int uLocation = Gl.GetUniformLocation(ObjectName, uniformName);

                UniformBinding uniformBinding = new UniformBinding(uniformName, i, uLocation, (ShaderUniformType)uniformType);

                // Map active uniform
                _UniformMap[uniformName] = uniformBinding;
                // Keep track of used slot
                _DefaultBlockUniformSlots += GetUniformSlotCount(uniformBinding.UniformType);
            }

            // Log uniform location mapping
            List <string> uniformNames = new List <string>(_UniformMap.Keys);

            // Make uniform list invariant respect the used driver (ease log comparation)
            uniformNames.Sort();

            sLog.Debug("Shader program active uniforms:");
            foreach (string uniformName in uniformNames)
            {
                sLog.Debug("\tUniform {0} (Type: {1}, Location: {2})", uniformName, _UniformMap[uniformName].UniformType, _UniformMap[uniformName].Location);
            }

            sLog.Debug("Shader program active uniform slots: {0}", _DefaultBlockUniformSlots);

            #endregion

            #region Collect Active Program Inputs

            // Get active inputs count
            int activeInputs;

            Gl.GetProgram(ObjectName, Gl.ACTIVE_ATTRIBUTES, out activeInputs);
            // Get inputs maximum length for name
            Gl.GetProgram(ObjectName, Gl.ACTIVE_ATTRIBUTE_MAX_LENGTH, out attributeBufferSize);

            // Clear input mapping
            _AttributesMap.Clear();

            // Collect input location mapping
            for (uint i = 0; i < (uint)activeInputs; i++)
            {
                StringBuilder nameBuffer = new StringBuilder(attributeBufferSize);
                int           nameLength, size, type;

                // Mono optimize StringBuilder capacity after P/Invoke... ensure enought room for the current loop
                nameBuffer.EnsureCapacity(attributeBufferSize);

                // Obtain active input informations
                Gl.GetActiveAttrib(ObjectName, i, attributeBufferSize, out nameLength, out size, out type, nameBuffer);
                // Obtain active input location
                string name = nameBuffer.ToString();

                int location = Gl.GetAttribLocation(ObjectName, name);
                // Map active input
                _AttributesMap[name] = new AttributeBinding((uint)location, (ShaderAttributeType)type);
            }

            // Log attribute mapping
            List <string> attributeNames = new List <string>(_AttributesMap.Keys);

            // Make attribute list invariant respect the used driver (ease log comparation)
            attributeNames.Sort();

            sLog.Debug("Shader program active attributes:");
            foreach (string attributeName in attributeNames)
            {
                sLog.Debug("\tAttribute {0} (Type: {1}, Location: {2})", attributeName, _AttributesMap[attributeName].Type, _AttributesMap[attributeName].Location);
            }

            #endregion

            #region Collect Fragment Locations

            if (ctx.Caps.GlExtensions.GpuShader4_EXT)
            {
                // Get fragment locations, just in the case automatically assigned
                foreach (string fragOutputName in new List <string>(_FragLocations.Keys))
                {
                    _FragLocations[fragOutputName] = Gl.GetFragDataLocation(ObjectName, fragOutputName);
                }
            }

            #endregion

            #region Collect Feedback Varyings

            if ((_FeedbackVaryings != null) && (_FeedbackVaryings.Count > 0))
            {
                if (ctx.Caps.GlExtensions.TransformFeedback2_ARB || ctx.Caps.GlExtensions.TransformFeedback_EXT)
                {
                    // Map active feedback
                    int feebackVaryings, feebackVaryingsMaxLength;

                    Gl.GetProgram(ObjectName, Gl.TRANSFORM_FEEDBACK_VARYINGS, out feebackVaryings);
                    Gl.GetProgram(ObjectName, Gl.TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH, out feebackVaryingsMaxLength);

                    for (uint i = 0; i < feebackVaryings; i++)
                    {
                        StringBuilder sb = new StringBuilder(feebackVaryingsMaxLength);
                        int           length = 0, size = 0, type = 0;

                        Gl.GetTransformFeedbackVarying(ObjectName, (uint)i, feebackVaryingsMaxLength, out length, out size, out type, sb);
                        _FeedbacksMap.Add(sb.ToString(), new FeedbackBinding((ShaderAttributeType)type, (uint)size));
                    }
                }
                else if (ctx.Caps.GlExtensions.TransformFeedback2_NV)
                {
                    // Activate varyings
                    foreach (string feedbackVaryingName in _FeedbackVaryings)
                    {
                        Gl.ActiveVaryingNV(ObjectName, feedbackVaryingName);
                    }

                    // Map active feedback
                    int feebackVaryings, feebackVaryingsMaxLength;

                    Gl.GetProgram(ObjectName, Gl.ACTIVE_VARYINGS_NV, out feebackVaryings);
                    Gl.GetProgram(ObjectName, Gl.ACTIVE_VARYING_MAX_LENGTH_NV, out feebackVaryingsMaxLength);

                    for (uint i = 0; i < feebackVaryings; i++)
                    {
                        StringBuilder sb = new StringBuilder(feebackVaryingsMaxLength * 2);
                        int           length = 0, size = 0, type = 0;

                        Gl.GetActiveVaryingNV(ObjectName, (uint)i, feebackVaryingsMaxLength * 2, out length, out size, out type, sb);

                        _FeedbacksMap.Add(sb.ToString(), new FeedbackBinding((ShaderAttributeType)type, (uint)size));
                    }

                    // Specify feedback varyings
                    List <int> feedbackLocations = new List <int>();

                    foreach (string feedbackVaryingName in _FeedbackVaryings)
                    {
                        int location = Gl.GetVaryingLocationNV(ObjectName, feedbackVaryingName);

                        if (location >= 0)
                        {
                            feedbackLocations.Add(location);
                        }
                    }

                    Gl.TransformFeedbackVaryingsNV(ObjectName, feedbackLocations.ToArray(), (int)cctx.FeedbackVaryingsFormat);

                    // Map active feedback
                }

                Debug.Assert(_FeedbacksMap.Count > 0);

                // Log feedback mapping
                sLog.Debug("Shader program active feedbacks:");
                foreach (string feedbackName in _FeedbacksMap.Keys)
                {
                    sLog.Debug("\tFeedback {0} (Type: {1})", feedbackName, _FeedbacksMap[feedbackName].Type);
                }
            }

            #endregion
        }