private void ReplaceShader(object sender, FileSystemEventArgs eventArgs)
        {
            // Can be triggered by temp files with suffixes
            if (!eventArgs.Name.Equals(ShaderCodeFileName))
            {
                CompileShader();
                if (!CompileError)
                {
                    var byteCode = File.ReadAllBytes(ShaderCompiledPath);
                    var effect   = new Effect(GraphicsDevice, byteCode);

                    // Delete the file as we don't need it anymore
                    File.Delete(ShaderCompiledPath);

                    OnCompile.Invoke(effect);
                }
            }
        }
예제 #2
0
        private void _compile()
        {
            // Setting default globals
            SetGlobal("MAX_NUMBER_OF_LIGHTS", Math.Max(1, Game.Game.CurrentScene.Lights.Length).ToString());

            _program = GL.CreateProgram();

            Console.WriteLine("Compiling vertex shader");
            _vertexShaderHandle = _compileSingleShader(ShaderType.VertexShader, _vertexSource);
            GL.AttachShader(_program, _vertexShaderHandle);

            if (!string.IsNullOrEmpty(_fragmentSource))
            {
                Console.WriteLine("Compiling fragment shader");
                _fragmentShaderHandle = _compileSingleShader(ShaderType.FragmentShader, _fragmentSource);
                GL.AttachShader(_program, _fragmentShaderHandle);
            }

            if (!string.IsNullOrEmpty(_tessControlSource) && !string.IsNullOrEmpty(_tessEvaluationSource))
            {
                Console.WriteLine("Compiling tesselation control shader");
                _tessCShaderHandle = _compileSingleShader(ShaderType.TessControlShader, _tessControlSource);
                GL.AttachShader(_program, _tessCShaderHandle);

                Console.WriteLine("Compiling tesselation evaluation shader");
                _tessEShaderHandle = _compileSingleShader(ShaderType.TessEvaluationShader, _tessEvaluationSource);
                GL.AttachShader(_program, _tessEShaderHandle);
                GL.PatchParameteri(PatchParameterInt.PatchVertices, 3);

                UseTesselation = true;
            }

            if (!string.IsNullOrEmpty(_geometrySource))
            {
                Console.WriteLine("Compiling geometry shader");
                _geometryShaderHandle = _compileSingleShader(ShaderType.GeometryShader, _geometrySource);
                GL.AttachShader(_program, _geometryShaderHandle);
            }

            GL.LinkProgram(_program);
            var ostr = GL.GetProgramInfoLog(_program).Trim();

            if (ostr.Length > 0)
            {
                Console.WriteLine(ostr);
            }

            int status_code;

            GL.GetProgrami(_program, GetProgramParameterName.LinkStatus, out status_code);
            if (status_code != 1)
            {
                throw new ApplicationException("Linking error");
            }

            GL.UseProgram(_program);

            ostr = GL.GetProgramInfoLog(_program).Trim();
            if (ostr.Length > 0)
            {
                Console.WriteLine(ostr);
            }

            _compiled = true;

            OnCompile?.Invoke();
        }