Example #1
0
        void Load()
        {
            ShaderProgramHandle = GL.CreateProgram(); MyGL.Check();

            var builder = new ShaderBuilder(file.FileSystem);
            var success = true;

            builder.LoadAndParse(file);

            if (builder.buildResults.Count == 0)
            {
                Log.Error("no shader parts were found, possible part markers are: " + Enum.GetNames(typeof(ShaderType)).Select(s => "[" + s + "]").Join(" "));
            }

            foreach (var r in builder.buildResults)
            {
                success &= AttachShader(builder, r.shaderContents, r.shaderType, r.filePath);
            }

            FinalizeInit();

            if (success)
            {
                Log.Info(typeof(Shader) + " " + file + " loaded successfully");
                LoadState    = State.LoadedSuccess;
                VersionOnGpu = VersionInFile;
            }
            else
            {
                LoadState = State.LoadedError;
                Log.Error("fix the error then press any key to reload ...");
                Console.ReadKey();
                Log.Error("reloading ...");
                Load();
                return;
            }

            file.OnFileChanged(NotifyFileChanged);
            builder.includedFiles.ForEach(f => f.OnFileChanged(NotifyFileChanged));

            Uniforms.MarkAllUniformsAsChanged();
            cachedUniformLocations.Clear();
        }
Example #2
0
        bool AttachShader(ShaderBuilder builder, string source, ShaderType type, string resource)
        {
            if (type == ShaderType.TessControlShader)
            {
                HasTesselation = true;
            }
            //source = source.Replace(maxNumberOfLights_name, maxNumberOfLights.ToString());

            int handle = GL.CreateShader(type); MyGL.Check();


            GL.ShaderSource(handle, source); MyGL.Check();

            GL.CompileShader(handle); MyGL.Check();

            string logInfo;

            GL.GetShaderInfoLog(handle, out logInfo); MyGL.Check();
            if (logInfo.Length > 0)
            {
                Log.Error($"Error occured during compilation of {type} from '{resource}'");
                Log.Error(logInfo);
                try
                {
                    var lines = logInfo.Split('\n');
                    for (int i = 0; i < lines.Length; i++)
                    {
                        var line = lines[i];
                        if (line.Contains("(") && line.Contains(")"))
                        {
                            var fileIdAndLineNumber = line.TakeStringBefore(")");
                            var fileId     = fileIdAndLineNumber.TakeStringBefore("(");
                            var lineNumber = fileIdAndLineNumber.TakeStringAfter("(");
                            var fileName   = builder.includedFiles[int.Parse(fileId, CultureInfo.InvariantCulture)];

                            line = line.Replace(fileIdAndLineNumber, fileName + "(" + lineNumber);

                            lines[i] = line;
                        }
                    }
                    logInfo = lines.Join('\n');
                    Log.Error("niceifed:");
                    Log.Error(logInfo);
                }
                catch (Exception e)
                {
                    Log.Error("failed to niceify error:" + e);
                }
                return(false);
            }

            int statusCode = 0;

            GL.GetShader(handle, ShaderParameter.CompileStatus, out statusCode); MyGL.Check();
            if (statusCode != 1)
            {
                var error = GL.GetShaderInfoLog(handle); MyGL.Check();
                //Log.Error(type.ToString() + " :: " + source + "\n" + error + "\n in file: " + resource);
                return(false);
            }

            GL.AttachShader(ShaderProgramHandle, handle); MyGL.Check();


            return(true);
        }