/// <summary>
        /// Compiles a shader.
        /// </summary>
        /// <param name="type">The type of shader to compile.</param>
        /// <param name="source">The shader source.</param>
        /// <returns>The id of the compiled shader.</returns>
        private uint CompileShader(ShaderType type, string source)
        {
            // Create and compile the shader.
            uint shaderPointer = Gl.CreateShader(type);

            Gl.ShaderSource(shaderPointer, new[] { source });
            Gl.CompileShader(shaderPointer);

            CheckError($"shader compilation\n{source}");

            // Check if there's a log to print.
            Gl.GetShader(shaderPointer, ShaderParameterName.InfoLogLength, out int lLength);
            if (lLength > 0)
            {
                // Get the info log.
                StringBuilder compileStatusReader = new StringBuilder(lLength);
                Gl.GetShaderInfoLog(shaderPointer, lLength, out int _, compileStatusReader);
                string compileStatus = compileStatusReader.ToString();
                Engine.Log.Warning($"Compilation log for shader of type {type}: {compileStatus}.\nSource:{source}", MessageSource.GL);
            }

            // Check if the shader compiled successfully, if not return 0.
            Gl.GetShader(shaderPointer, ShaderParameterName.CompileStatus, out int status);
            return(status == 1 ? shaderPointer : 0);
        }
Esempio n. 2
0
        uint compileShader(OpenGL.ShaderType type, string source)
        {
            uint id = Gl.CreateShader(type);

            Gl.ShaderSource(id, new string[] { source });
            Gl.CompileShader(id);

            // Check for compilation errors
            int result;

            Gl.GetShader(id, ShaderParameterName.CompileStatus, out result);
            if (result == 0)
            {
                int length;
                Gl.GetShader(id, ShaderParameterName.InfoLogLength, out length);

                StringBuilder infoLog = new StringBuilder(length);
                Gl.GetShaderInfoLog(id, length, out length, infoLog);

                string shaderType = "Vertex";
                if (type == OpenGL.ShaderType.GeometryShader)
                {
                    shaderType = "Geometry";
                }
                if (type == OpenGL.ShaderType.FragmentShader)
                {
                    shaderType = "Fragment";
                }
                Debug.WriteLine("ERROR: " + shaderType + " shader: " + infoLog.ToString());
            }

            return(id);
        }
Esempio n. 3
0
        public Shader(string fragGlslPath, string vertGlslPath)
        {
            var fragGlslContents = File.ReadAllText(fragGlslPath);
            var vertGlslContents = File.ReadAllText(vertGlslPath);

            var fragId = Gl.CreateShader(ShaderType.FragmentShader);

            Gl.ShaderSource(fragId, new[] { fragGlslContents });
            Gl.CompileShader(fragId);

            CheckForErrors(fragId);

            var vertId = Gl.CreateShader(ShaderType.VertexShader);

            Gl.ShaderSource(vertId, new[] { vertGlslContents });
            Gl.CompileShader(vertId);

            CheckForErrors(vertId);

            Id = Gl.CreateProgram();
            Gl.AttachShader(Id, fragId);
            Gl.AttachShader(Id, vertId);
            Gl.LinkProgram(Id);

            Gl.DeleteShader(fragId);
            Gl.DeleteShader(vertId);
        }
Esempio n. 4
0
        public Shader(string source, ShaderType type)
        {
            Type = type;
            ID   = Gl.CreateShader(type);
            Gl.ShaderSource(ID, new[] { source });
            Gl.CompileShader(ID);
            int compiled;

            Gl.GetShader(ID, ShaderParameterName.CompileStatus, out compiled);
            if (compiled != 0)
            {
                Trace.TraceInformation("Compiled {0} successfully.", type);
                return;
            }

            const int logMaxLength = 1024;
            var       infoLog      = new System.Text.StringBuilder(logMaxLength);
            int       infoLogLength;

            Gl.GetShaderInfoLog(ID, logMaxLength, out infoLogLength, infoLog);

            string message = $"Unable to compile shader: {infoLog}";

            Trace.TraceError(message);
            throw new InvalidOperationException(message);
        }
Esempio n. 5
0
        /// <summary>
        /// Creates and compiles a shader object.
        /// </summary>
        /// <param name="shaderType">
        /// Specifies the type of shader to be created.
        /// </param>
        /// <param name="source">
        /// Specifies an array of pointers to strings containing the source code to be loaded into the shader.
        /// </param>
        /// <returns>
        /// It returns a shader object of type <paramref name="shaderType"/>.
        /// </returns>
        protected static UInt32 CreateShaderObject(ShaderType shaderType, string[] source)
        {
            UInt32 shaderObject = 0;

            try {
                shaderObject = Gl.CreateShader(shaderType);
                Gl.ShaderSource(shaderObject, source);
                Gl.CompileShader(shaderObject);

                int compileStatus;

                Gl.GetShader(shaderObject, ShaderParameterName.CompileStatus, out compileStatus);
                if (compileStatus == Gl.FALSE)
                {
                    StringBuilder log = new StringBuilder(4096);
                    int           logLength;

                    Gl.GetShaderInfoLog(shaderObject, log.Capacity, out logLength, log);

                    throw new InvalidOperationException(log.ToString());
                }

                return(shaderObject);
            } catch {
                if (shaderObject != 0)
                {
                    Gl.DeleteShader(shaderObject);
                }
                throw;
            }
        }
Esempio n. 6
0
        public static uint Compile(string[] source, ShaderType type)
        {
            uint shader = Gl.CreateShader(type);

            Gl.ShaderSource(shader, source);
            Gl.CompileShader(shader);

            int compiled;

            Gl.GetShader(shader, ShaderParameterName.CompileStatus, out compiled);
            if (compiled != 0)
            {
                return(shader);
            }

            Gl.DeleteShader(shader);

            const int     logMaxLength = 1024;
            StringBuilder infoLog      = new StringBuilder(logMaxLength);
            int           infoLogLength;

            Gl.GetShaderInfoLog(shader, logMaxLength, out infoLogLength, infoLog);

            throw new InvalidOperationException($"unable to compile shader: {infoLog}");
        }
Esempio n. 7
0
        private bool compile(ref uint shaderId, ShaderType type, string[] src)
        {
            int errorcp = 0;

            shaderId = Gl.CreateShader(type);
            if (shaderId == 0)
            {
                Console.WriteLine("ERROR: shader type (" + type + ") does not exist");
            }

            Gl.ShaderSource(shaderId, src);
            Gl.CompileShader(shaderId);
            Gl.GetShader(shaderId, ShaderParameterName.CompileStatus, out errorcp);

            if (errorcp != Gl.TRUE)
            {
                Console.WriteLine("ERROR: while compiling Shader :");
                int errorSize;
                Gl.GetShader(shaderId, ShaderParameterName.InfoLogLength, out errorSize);

                StringBuilder error = new StringBuilder(1024);
                Gl.GetShaderInfoLog(shaderId, errorSize, out errorSize, error);

                Console.WriteLine(error.ToString());
                error.Clear();

                Gl.DeleteShader(shaderId);
                return(false);
            }
            return(true);
        }
Esempio n. 8
0
        public void Load()
        {
            if (ShaderContainer.Shaders.Any(s => s.fragShaderAsset.MountPath == fragShaderAsset.MountPath && s.vertShaderAsset.MountPath == vertShaderAsset.MountPath))
            {
                var cachedShader = ShaderContainer.Shaders.First(s => s.fragShaderAsset.MountPath == fragShaderAsset.MountPath && s.vertShaderAsset.MountPath == vertShaderAsset.MountPath);
                Id = cachedShader.Id;
                return;
            }

            var fragGlslContents = fragShaderAsset.AsString();
            var vertGlslContents = vertShaderAsset.AsString();

            var fragId = Gl.CreateShader(ShaderType.FragmentShader);
            Gl.ShaderSource(fragId, new[] { fragGlslContents });
            Gl.CompileShader(fragId);

            CheckForErrors(fragId);

            var vertId = Gl.CreateShader(ShaderType.VertexShader);
            Gl.ShaderSource(vertId, new[] { vertGlslContents });
            Gl.CompileShader(vertId);

            CheckForErrors(vertId);

            Id = Gl.CreateProgram();
            Gl.AttachShader(Id, fragId);
            Gl.AttachShader(Id, vertId);
            Gl.LinkProgram(Id);

            Gl.DeleteShader(fragId);
            Gl.DeleteShader(vertId);
        }
Esempio n. 9
0
        private void CreateShaders()
        {
            var fragmentShader = Gl.CreateShader(ShaderType.FragmentShader);
            var vertexShader   = Gl.CreateShader(ShaderType.VertexShader);

            var sb = new StringBuilder(1000);

            void CompileShader(uint id, string file)
            {
                var source = LoadShaderFromResource(file);

                Gl.ShaderSource(id, new[] { source });
                Gl.CompileShader(id);

                Gl.GetShader(id, ShaderParameterName.CompileStatus, out int compileStatus);
                Debug.WriteLine($"Compile status: {compileStatus == Gl.TRUE}");
                Gl.GetShaderInfoLog(id, sb.Capacity, out int _, sb);
                Debug.WriteLine(sb.ToString());
            }

            CompileShader(fragmentShader, "FragmentShader.glsl");
            CompileShader(vertexShader, "VertexShader.glsl");

            _program = Gl.CreateProgram();
            Gl.AttachShader(_program, fragmentShader);
            Gl.AttachShader(_program, vertexShader);
            Gl.LinkProgram(_program);

            Gl.GetProgram(_program, ProgramProperty.LinkStatus, out int linkResult);
            Debug.WriteLine($"Link status: {linkResult == Gl.TRUE}");
            Gl.GetProgramInfoLog(_program, sb.Capacity, out int _, sb);
        }
Esempio n. 10
0
        public static ShaderStage FromSource(IEnumerable <string> codeLines, StageType type)
        {
            uint handle          = Gl.CreateShader((ShaderType)type);
            var  sourceCodeLines = codeLines.ToArray();

            for (int i = 0; i < sourceCodeLines.Length; i++)
            {
                sourceCodeLines[i] += sourceCodeLines[i].EndsWith("\n") ? "" : "\n";
            }

            Gl.ShaderSource(handle, sourceCodeLines);
            Gl.CompileShader(handle);

            Gl.GetShader(handle, ShaderParameterName.CompileStatus, out int compiled);

            if (compiled != Gl.TRUE)
            {
                Gl.GetShader(handle, ShaderParameterName.InfoLogLength, out int logLength);
                var errorLog = new StringBuilder(logLength);
                Gl.GetShaderInfoLog(handle, logLength, out int _, errorLog);
                throw new Exception("Shader Stage Compile Errors: \n" + errorLog);
            }

            return(new ShaderStage(handle, type));
        }
Esempio n. 11
0
        public GlObject(ShaderType shaderType, string[] source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            // Create
            ShaderName = Gl.CreateShader(shaderType);
            // Submit source code
            Gl.ShaderSource(ShaderName, source);
            // Compile
            Gl.CompileShader(ShaderName);
            // Check compilation status
            int compiled;

            Gl.GetShader(ShaderName, ShaderParameterName.CompileStatus, out compiled);
            if (compiled != 0)
            {
                return;
            }

            // Throw exception on compilation errors
            const int logMaxLength = 1024;

            StringBuilder infolog = new StringBuilder(logMaxLength);
            int           infologLength;

            Gl.GetShaderInfoLog(ShaderName, logMaxLength, out infologLength, infolog);

            throw new InvalidOperationException($"unable to compile shader: {infolog}");
        }
Esempio n. 12
0
        private static uint loadShader(string[] shaderString, ShaderType type)
        {
            uint shaderID = Gl.CreateShader(type);

            Console.WriteLine(shaderID);
            Gl.ShaderSource(shaderID, shaderString);
            Gl.CompileShader(shaderID);
            int status;

            Gl.GetShader(shaderID, ShaderParameterName.CompileStatus, out status);
            if (status == Gl.TRUE)
            {
                Console.WriteLine($"{type} Creation Success.");
                return(shaderID);
            }
            else if (status == Gl.FALSE)
            {
                Console.WriteLine($"{type} creation fail.");
                Gl.GetShader(shaderID, ShaderParameterName.InfoLogLength, out int logLength);
                int           logMaxLength = 1024;
                StringBuilder infoLog      = new StringBuilder(logMaxLength);
                Gl.GetShaderInfoLog(shaderID, 1024, out int infoLogLength, infoLog);
                Console.WriteLine("Errors: \n{0}", infoLog.ToString());

                return(0);
            }

            return(0);
        }
Esempio n. 13
0
 public bool Compile()
 {
     GlHelper.ThrowNullException(Id);
     Gl.CompileShader(Id);
     GlHelper.GetError();
     return(Status);
 }
Esempio n. 14
0
        private uint CompileShader(string source, ShaderType type)
        {
            uint shader = Gl.CreateShader(type);

            if (shader == 0)
            {
                return(shader);
            }

            Gl.ShaderSource(shader, new [] { source });
            Gl.CompileShader(shader);

#if DEBUG
            Gl.GetShader(shader, ShaderParameterName.CompileStatus, out var compiled);

            if (compiled == 0)
            {
                Gl.GetShader(shader, ShaderParameterName.InfoLogLength, out var logLength);

                if (logLength != 0)
                {
                    StringBuilder sb = new StringBuilder();
                    sb.EnsureCapacity(999); // this is needed due to a rare Mono bug
                    Gl.GetShaderInfoLog(shader, 999, out int _, sb);
                    Debug.WriteLine("Sparrow: Error compiling shader: " + sb);
                }
                Gl.DeleteShader(shader);
                return(0);
            }
#endif
            return(shader);
        }
Esempio n. 15
0
        private uint loadShader(string[] shaderString, ShaderType type)
        {
            uint shaderID = Gl.CreateShader(type);

            Gl.ShaderSource(shaderID, shaderString);
            Gl.CompileShader(shaderID);

            // debuging code
            int status;

            Gl.GetShader(shaderID, ShaderParameterName.CompileStatus, out status);
            if (status == Gl.TRUE)
            {
                Console.WriteLine(String.Format("shader number {0} creation succeeded", shaderID));
                return(shaderID);
            }
            else if (status == Gl.FALSE)
            {
                Console.WriteLine(String.Format("shader number {0} creation failed", shaderID));
                Gl.GetShader(shaderID, ShaderParameterName.InfoLogLength, out int logLength);
                int           logMaxLength = 1024;
                StringBuilder infoLog      = new StringBuilder(logMaxLength);
                Gl.GetShaderInfoLog(shaderID, 1024, out int infoLogLength, infoLog);
                Console.WriteLine("Errors: \n{0}", infoLog.ToString());

                return(0);
            }

            return(0);
        }
Esempio n. 16
0
        public bool CompileShader()
        {
            int infoLogLenght;

            Gl.CompileShader(ShaderID);
            Gl.GetShader(ShaderID, ShaderParameterName.CompileStatus, out base.actionStatus);
            Gl.GetShader(ShaderID, ShaderParameterName.InfoLogLength, out infoLogLenght);

            infoLog = new StringBuilder(string.Format(UNSUCCESSFUL_COMPILATION, ShaderName), infoLogLenght);
            infoLog.EnsureCapacity(infoLogLenght);

            int x;

            if (CompilationStatus == Gl.TRUE)
            {
                this.isDirty = false;
                infoLog.Clear();
                infoLog.Append(string.Format(SUCCESSFUL_COMPILATION, ShaderName));
                return(true);
            }
            else
            {
                Gl.GetShaderInfoLog(ShaderID, 1024, out x, infoLog);
                return(false);
            }
        }
Esempio n. 17
0
        private bool CreateDeviceObjects()
        {
            // Backup GL state
            // but why??
            Gl.GetInteger(GetPName.TextureBinding2d, out int lastTexture);
            Gl.GetInteger(GetPName.ArrayBufferBinding, out int lastArrayBuffer);
            Gl.GetInteger(GetPName.VertexArrayBinding, out int lastVertexArray);

            // Create shaders
            string shaderSource;

            var assembly = Assembly.GetExecutingAssembly();

            using (var data = assembly.GetManifestResourceStream("imgui-vertex.glsl"))
                using (var stream = new StreamReader(data))
                {
                    shaderSource = stream.ReadToEnd();
                }

            _vertHandle = Gl.CreateShader(ShaderType.VertexShader);
            Gl.ShaderSource(_vertHandle, new string[] { shaderSource });
            Gl.CompileShader(_vertHandle);
            CheckShader(_vertHandle, "vertex shader");

            using (var data = assembly.GetManifestResourceStream("imgui-frag.glsl"))
                using (var stream = new StreamReader(data))
                {
                    shaderSource = stream.ReadToEnd();
                }

            _fragHandle = Gl.CreateShader(ShaderType.FragmentShader);
            Gl.ShaderSource(_fragHandle, new string[] { shaderSource });
            Gl.CompileShader(_fragHandle);
            CheckShader(_fragHandle, "fragment shader");

            _shaderHandle = Gl.CreateProgram();
            Gl.AttachShader(_shaderHandle, _vertHandle);
            Gl.AttachShader(_shaderHandle, _fragHandle);
            Gl.LinkProgram(_shaderHandle);
            CheckProgram(_shaderHandle, "shader program");

            _attribLocationTex      = Gl.GetUniformLocation(_shaderHandle, "Texture");
            _attribLocationProjMtx  = Gl.GetUniformLocation(_shaderHandle, "ProjMtx");
            _attribLocationVtxPos   = Gl.GetAttribLocation(_shaderHandle, "Position");
            _attribLocationVtxUV    = Gl.GetAttribLocation(_shaderHandle, "UV");
            _attribLocationVtxColor = Gl.GetAttribLocation(_shaderHandle, "Color");

            _vboHandle      = Gl.GenBuffer();
            _elementsHandle = Gl.GenBuffer();

            CreateFontsTexture();

            // Restore modified GL state
            Gl.BindTexture(TextureTarget.Texture2d, (uint)lastTexture);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, (uint)lastArrayBuffer);
            Gl.BindVertexArray((uint)lastVertexArray);

            return(true);
        }
Esempio n. 18
0
        public Shader(ShaderType type, string path)
        {
            GlShader = Gl.CreateShader(type);
            Gl.ShaderSource(GlShader, new[] { File.ReadAllText(path) });
            Gl.CompileShader(GlShader);

            Validate();
        }
Esempio n. 19
0
        protected uint CompileShader(string[] shaderSource, ShaderType shaderType, out int compiled)
        {
            uint shader = Gl.CreateShader(shaderType);

            Gl.ShaderSource(shader, shaderSource);
            Gl.CompileShader(shader);
            Gl.GetShader(shader, ShaderParameterName.CompileStatus, out compiled);
            return(shader);
        }
Esempio n. 20
0
        private uint CreateProgramUniform(string[] srcVertex, string[] srcFragment)
        {
            uint shaderVertex = 0, shaderFragment = 0, program = 0;

            try {
                int compileStatus;

                shaderVertex = Gl.CreateShader(ShaderType.VertexShader);
                Gl.ShaderSource(shaderVertex, srcVertex);
                Gl.CompileShader(shaderVertex);
                Gl.GetShader(shaderVertex, ShaderParameterName.CompileStatus, out compileStatus);
                if (compileStatus == 0)
                {
                    throw new InvalidOperationException("unable to compiler vertex shader: " + GetShaderInfoLog(shaderVertex));
                }

                shaderFragment = Gl.CreateShader(ShaderType.FragmentShader);
                Gl.ShaderSource(shaderFragment, srcFragment);
                Gl.CompileShader(shaderFragment);
                Gl.GetShader(shaderFragment, ShaderParameterName.CompileStatus, out compileStatus);
                if (compileStatus == 0)
                {
                    throw new InvalidOperationException("unable to compiler fragment shader: " + GetShaderInfoLog(shaderFragment));
                }

                program = Gl.CreateProgram();
                Gl.AttachShader(program, shaderVertex);
                Gl.AttachShader(program, shaderFragment);
                Gl.LinkProgram(program);
                Gl.GetProgram(program, ProgramProperty.LinkStatus, out compileStatus);
                if (compileStatus == 0)
                {
                    throw new InvalidOperationException("unable to link program");
                }

                Gl.UseProgram(program);
            } catch {
                if (shaderVertex != 0)
                {
                    Gl.DeleteShader(shaderVertex);
                }
                if (shaderFragment != 0)
                {
                    Gl.DeleteShader(shaderFragment);
                }
                if (shaderFragment != 0)
                {
                    Gl.DeleteProgram(program);
                }

                throw;
            }


            return(program);
        }
Esempio n. 21
0
        /// <summary>
        /// Compiles a shader, which can be either vertex, fragment or geometry.
        /// </summary>
        /// <param name="source">Specifies the source code of the shader object.</param>
        /// <param name="type">Specifies the type of shader to create (either vertex, fragment or geometry).</param>
        public Shader(ShaderType type, string source)
        {
            this.ShaderType = type;
            this.ShaderID   = Gl.CreateShader((int)type);

            GLShortcut.ShaderSource(ShaderID, source);
            Gl.CompileShader(ShaderID);

            GetParams(source);
        }
Esempio n. 22
0
        public Shader(ShaderType type, string source)
        {
            var shaderLanguageVersion = Gl.GetString(StringName.ShadingLanguageVersion);

            if (Environment.OSVersion.Platform == PlatformID.MacOSX)
            {
                source = string.Format("#version {0}\n{1}", shaderLanguageVersion.Replace(".", ""), source);
            }

            _type   = type;
            _source = source;

            // Compile
            _shaderHandle = Gl.CreateShader(type);
            Gl.ShaderSource(_shaderHandle, new string[] { _source });
            Gl.CompileShader(_shaderHandle);


            // Get the log
            int logLen;

            Gl.GetShader(_shaderHandle, ShaderParameterName.InfoLogLength, out logLen);

            // Anything in the log?
            string log = "n/a";

            if (logLen > 0)
            {
                var infolog = new StringBuilder(logLen + 10);
                int infologLength;
                Gl.GetShaderInfoLog(_shaderHandle, logLen + 10, out infologLength, infolog);

                log = infolog.ToString();

                if (!string.IsNullOrEmpty(log))
                {
                    Console.WriteLine("Shader compile log:\n{0}", log);
                }
            }

            // Success?
            int status;

            Gl.GetShader(_shaderHandle, ShaderParameterName.CompileStatus, out status);
            if (status != 0)        // Returns GL_TRUE if successful
            {
                return;
            }

            // Delete it
            Gl.DeleteShader(_shaderHandle);
            _shaderHandle = 0;

            throw new InvalidOperationException(string.Format("Compilation of shader failed - {0}", log));
        }
Esempio n. 23
0
        private uint GetFragmentShader()
        {
            string[] text = File.ReadAllLines($"{_rootPath}{FragmentShaderPath}{_shaderSelector}.glsl")
                            .Select(s => s + "\n").ToArray();;
            uint shader = Gl.CreateShader(ShaderType.FragmentShader);

            Gl.ShaderSource(shader, text);
            Gl.CompileShader(shader);
            VerifyCompiled(shader);
            return(shader);
        }
Esempio n. 24
0
        private void RenderControl_ContextCreated(object sender, GlControlEventArgs e)
        {
            // Update Form caption
            Text = String.Format("Hello triangle with ANGLE (Version: {0})", Gl.GetString(StringName.Version));

            // Create resources
            StringBuilder infolog = new StringBuilder(1024);
            int           infologLength;
            int           compiled;

            infolog.EnsureCapacity(1024);

            // Vertex shader
            uint vertexShader = Gl.CreateShader(ShaderType.VertexShader);

            Gl.ShaderSource(vertexShader, _Es2_ShaderVertexSource);
            Gl.CompileShader(vertexShader);
            Gl.GetShader(vertexShader, ShaderParameterName.CompileStatus, out compiled);
            if (compiled == 0)
            {
                Gl.GetShaderInfoLog(vertexShader, 1024, out infologLength, infolog);
            }

            // Fragment shader
            uint fragmentShader = Gl.CreateShader(ShaderType.FragmentShader);

            Gl.ShaderSource(fragmentShader, _Es2_ShaderFragmentSource);
            Gl.CompileShader(fragmentShader);
            Gl.GetShader(fragmentShader, ShaderParameterName.CompileStatus, out compiled);
            if (compiled == 0)
            {
                Gl.GetShaderInfoLog(fragmentShader, 1024, out infologLength, infolog);
            }

            // Program
            _Es2_Program = Gl.CreateProgram();
            Gl.AttachShader(_Es2_Program, vertexShader);
            Gl.AttachShader(_Es2_Program, fragmentShader);
            Gl.LinkProgram(_Es2_Program);

            int linked;

            Gl.GetProgram(_Es2_Program, ProgramProperty.LinkStatus, out linked);

            if (linked == 0)
            {
                Gl.GetProgramInfoLog(_Es2_Program, 1024, out infologLength, infolog);
            }

            _Es2_Program_Location_uMVP      = Gl.GetUniformLocation(_Es2_Program, "uMVP");
            _Es2_Program_Location_aPosition = Gl.GetAttribLocation(_Es2_Program, "aPosition");
            _Es2_Program_Location_aColor    = Gl.GetAttribLocation(_Es2_Program, "aColor");
        }
Esempio n. 25
0
        private void Es2_ContextCreated()
        {
            int compilationStatus;

            // Vertex shader
            uint vertexShader = Gl.CreateShader(ShaderType.VertexShader);

            Gl.ShaderSource(vertexShader, _Es2_ShaderVertexSource);
            Gl.CompileShader(vertexShader);

            // Fragment shader
            uint fragmentShader = Gl.CreateShader(ShaderType.FragmentShader);

            Gl.ShaderSource(fragmentShader, _Es2_ShaderFragmentSource);
            Gl.CompileShader(fragmentShader);

            // Program
            _Es2_Program = Gl.CreateProgram();
            Gl.AttachShader(_Es2_Program, vertexShader);
            Gl.AttachShader(_Es2_Program, fragmentShader);
            Gl.LinkProgram(_Es2_Program);

            // Check for linking errors
            int lStatus;

            Gl.GetProgram(_Es2_Program, Gl.LINK_STATUS, out lStatus);

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

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

                // Obtain compilation log
                Gl.GetProgramInfoLog(_Es2_Program, 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);
                }

                throw new InvalidOperationException(sb.ToString());
            }
            _Es2_Program_Location_uMVP      = Gl.GetUniformLocation(_Es2_Program, "uMVP");
            _Es2_Program_Location_aPosition = Gl.GetAttribLocation(_Es2_Program, "aPosition");
            _Es2_Program_Location_aColor    = Gl.GetAttribLocation(_Es2_Program, "aColor");
        }
Esempio n. 26
0
        /// <summary>
        /// Creates the shader and returns the handle to it
        /// </summary>
        /// <param name="vertexSource"></param>
        /// <param name="fragmentSource"></param>
        /// <returns></returns>
        private static uint CreateShader(string[] vertexSource, string[] fragmentSource)
        {
            StringBuilder infolog = new StringBuilder(1024);
            int           infoLogLength;

            // Create a shader id
            uint programid  = Gl.CreateProgram();
            uint vertexId   = Gl.CreateShader(ShaderType.VertexShader);
            uint fragmentId = Gl.CreateShader(ShaderType.FragmentShader);

            // Create shader from source
            Gl.ShaderSource(vertexId, vertexSource);
            Gl.ShaderSource(fragmentId, fragmentSource);

            int compileStatus = 0;

            // Compile the shaders
            Gl.CompileShader(vertexId);
            Gl.GetShader(vertexId, ShaderParameterName.CompileStatus, out compileStatus);

            if (compileStatus == 0)
            {
                Gl.GetShaderInfoLog(vertexId, 1024, out infoLogLength, infolog);
            }

            Gl.CompileShader(fragmentId);
            Gl.GetShader(fragmentId, ShaderParameterName.CompileStatus, out compileStatus);

            if (compileStatus == 0)
            {
                Gl.GetShaderInfoLog(fragmentId, 1024, out infoLogLength, infolog);
            }

            if (infolog.Length > 0)
            {
                Debug.Log("Could not compile shaders " + infolog, Debug.DebugLayer.Shaders, Debug.DebugLevel.Warning);
            }

            // Attach the shaders to the shader program
            Gl.AttachShader(programid, vertexId);
            Gl.AttachShader(programid, fragmentId);

            // Link the shaders
            Gl.LinkProgram(programid);

            // Validate the shader program
            Gl.ValidateProgram(programid);

            Debug.Log("Shaders Compiled: Id " + vertexId + ":" + fragmentId, Debug.DebugLayer.Shaders, Debug.DebugLevel.Information);

            return(programid);
        }
Esempio n. 27
0
        private void RenderControl_ContextCreated_ES(object sender, GlControlEventArgs e)
        {
            StringBuilder infolog = new StringBuilder(1024);
            int           infologLength;
            int           compiled;

            infolog.EnsureCapacity(1024);

            // Vertex shader
            uint vertexShader = Gl.CreateShader(Gl.VERTEX_SHADER);

            Gl.ShaderSource(vertexShader, _Es2_ShaderVertexSource);
            Gl.CompileShader(vertexShader);
            Gl.GetShader(vertexShader, Gl.COMPILE_STATUS, out compiled);
            if (compiled == 0)
            {
                Gl.GetShaderInfoLog(vertexShader, 1024, out infologLength, infolog);
            }

            // Fragment shader
            uint fragmentShader = Gl.CreateShader(Gl.FRAGMENT_SHADER);

            Gl.ShaderSource(fragmentShader, _Es2_ShaderFragmentSource);
            Gl.CompileShader(fragmentShader);
            Gl.GetShader(fragmentShader, Gl.COMPILE_STATUS, out compiled);
            if (compiled == 0)
            {
                Gl.GetShaderInfoLog(fragmentShader, 1024, out infologLength, infolog);
            }

            // Program
            Program_Shader = Gl.CreateProgram();
            Gl.AttachShader(Program_Shader, vertexShader);
            Gl.AttachShader(Program_Shader, fragmentShader);
            Gl.LinkProgram(Program_Shader);

            int linked;

            Gl.GetProgram(Program_Shader, Gl.LINK_STATUS, out linked);

            if (linked == 0)
            {
                Gl.GetProgramInfoLog(Program_Shader, 1024, out infologLength, infolog);
            }

            Program_Location_uProjection = Gl.GetUniformLocation(Program_Shader, "uProjection");
            Program_Location_uView       = Gl.GetUniformLocation(Program_Shader, "uView");
            Program_Location_uModel      = Gl.GetUniformLocation(Program_Shader, "uModel");
            Program_Location_aPosition   = Gl.GetAttribLocation(Program_Shader, "aPosition");
            Program_Location_aColor      = Gl.GetAttribLocation(Program_Shader, "aColor");
        }
Esempio n. 28
0
        private void CompileShaders(string vertexCode, string fragmentCode)
        {
            uint          vertex, fragment;
            int           success;
            StringBuilder infoLog = new StringBuilder(512);

            // Vertex Shader
            vertex = Gl.CreateShader(Gl.VERTEX_SHADER);
            Gl.ShaderSource(vertex, new[] { vertexCode });
            Gl.CompileShader(vertex);
            // Print compile errors if any
            Gl.GetShader(vertex, Gl.COMPILE_STATUS, out success);
            if (success == 0)
            {
                int length;
                Gl.GetShaderInfoLog(vertex, 512, out length, infoLog);
                MessageBox.Show(Render.Program.bithack3D, "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" + infoLog.ToString());
                Render.Program.bithack3D.BeginInvoke(new MethodInvoker(Render.Program.bithack3D.Close));
            }
            // Fragment Shader
            fragment = Gl.CreateShader(Gl.FRAGMENT_SHADER);
            Gl.ShaderSource(fragment, new[] { fragmentCode });
            Gl.CompileShader(fragment);
            // Print compile errors if any
            Gl.GetShader(fragment, Gl.COMPILE_STATUS, out success);
            if (success == 0)
            {
                int length;
                Gl.GetShaderInfoLog(vertex, 512, out length, infoLog);
                MessageBox.Show(Render.Program.bithack3D, "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" + infoLog.ToString());
                Render.Program.bithack3D.BeginInvoke(new MethodInvoker(Render.Program.bithack3D.Close));
            }
            // Shader Program
            this.Program = Gl.CreateProgram();
            Gl.AttachShader(this.Program, vertex);
            Gl.AttachShader(this.Program, fragment);
            Gl.LinkProgram(this.Program);
            // Print linking errors if any
            Gl.GetProgram(this.Program, Gl.LINK_STATUS, out success);
            if (success == 0)
            {
                int length;
                Gl.GetProgramInfoLog(this.Program, 512, out length, infoLog);
                MessageBox.Show(Render.Program.bithack3D, "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" + infoLog.ToString());
                Render.Program.bithack3D.BeginInvoke(new MethodInvoker(Render.Program.bithack3D.Close));
            }
            // Delete the shaders as they're linked into our program now and no longer necessery
            Gl.DeleteShader(vertex);
            Gl.DeleteShader(fragment);
        }
Esempio n. 29
0
        private static uint CreateShader(uint type, string source)
        {
            var ret = Gl.CreateShader(type);

            Gl.ShaderSource(ret, source);
            Gl.CompileShader(ret);
            var success = new int[1];

            Gl.GetShader(ret, Gl.GL_COMPILE_STATUS, success);
            var str = new StringBuilder(1000);

            Gl.GetShaderInfoLog(ret, 1000, IntPtr.Zero, str);
            return(ret);
        }
Esempio n. 30
0
        private static void Initialize()
        {
            // Create resources
            StringBuilder infolog = new StringBuilder(1024);
            int           infologLength;
            int           compiled;

            infolog.EnsureCapacity(1024);

            // Vertex shader
            uint vertexShader = Gl.CreateShader(ShaderType.VertexShader);

            Gl.ShaderSource(vertexShader, _Es2_ShaderVertexSource);
            Gl.CompileShader(vertexShader);
            Gl.GetShader(vertexShader, ShaderParameterName.CompileStatus, out compiled);
            if (compiled == 0)
            {
                Gl.GetShaderInfoLog(vertexShader, 1024, out infologLength, infolog);
            }

            // Fragment shader
            uint fragmentShader = Gl.CreateShader(ShaderType.FragmentShader);

            Gl.ShaderSource(fragmentShader, _Es2_ShaderFragmentSource);
            Gl.CompileShader(fragmentShader);
            Gl.GetShader(fragmentShader, ShaderParameterName.CompileStatus, out compiled);
            if (compiled == 0)
            {
                Gl.GetShaderInfoLog(fragmentShader, 1024, out infologLength, infolog);
            }

            // Program
            _Es2_Program = Gl.CreateProgram();
            Gl.AttachShader(_Es2_Program, vertexShader);
            Gl.AttachShader(_Es2_Program, fragmentShader);
            Gl.LinkProgram(_Es2_Program);

            int linked;

            Gl.GetProgram(_Es2_Program, Gl.LINK_STATUS, out linked);

            if (linked == 0)
            {
                Gl.GetProgramInfoLog(_Es2_Program, 1024, out infologLength, infolog);
            }

            _Es2_Program_Location_uMVP      = Gl.GetUniformLocation(_Es2_Program, "uMVP");
            _Es2_Program_Location_aPosition = Gl.GetAttribLocation(_Es2_Program, "aPosition");
            _Es2_Program_Location_aColor    = Gl.GetAttribLocation(_Es2_Program, "aColor");
        }