/// <summary> /// Initializes a new instance of this compute shader.<br/> /// Retrieves shader source filenames from ShaderSourceAttributes tagged to this type. /// </summary> protected ComputeProgram() { if (ShaderSourceAttribute.GetShaderSources(GetType()).Any(_ => _.Type != ShaderType.ComputeShader)) { throw new ApplicationException("Invalid ShaderType supplied to compute shader via ShaderSourceAttribute(s)."); } }
public static void Main(string[] args) { // create a hidden GameWindow to initialize an OpenGL context using (new GameWindow()) { // iterate over given arguments foreach (var path in args) { // check if file exists if (!File.Exists(path)) { Console.Out.WriteLine("{0}: error 0: ShaderCompiler: file not found", path); continue; } Console.Out.WriteLine("Compiling shaders of: {0}", path); // load assembly var assembly = Assembly.LoadFrom(path); // set working directory Directory.SetCurrentDirectory(Path.GetDirectoryName(assembly.Location)); // iterate over all non-abstract shader programs foreach (var type in assembly.GetTypes().Where(_ => !_.IsAbstract && typeof(Program).IsAssignableFrom(_))) { // check if the program has any shader sources tagged to it if (ShaderSourceAttribute.GetShaderSources(type).Count == 0) { continue; } Console.Out.WriteLine("Compiling: {0}", type.FullName); // get generic program factory method var method = typeof(ProgramFactory).GetMethod("Create"); var generic = method.MakeGenericMethod(type); try { // invoke program factory var program = (Program)generic.Invoke(null, null); program.Dispose(); } catch (TargetInvocationException ex) { Console.Out.WriteLine(ex.InnerException.Message); // reformat OpenGL information log if existing var exception = ex.InnerException as ProgramException; if (exception != null) { Console.Out.WriteLine(FormatInfoLog(exception.InfoLog)); } } } } } }
/// <summary> /// Initializes a program object using the shader sources tagged to the type with <see cref="ShaderSourceAttribute"/>. /// </summary> /// <typeparam name="T">Specifies the program type to create.</typeparam> /// <returns>A compiled and linked program.</returns> public static T Create <T>() where T : Program { // retrieve shader types and filenames from attributes var shaders = ShaderSourceAttribute.GetShaderSources(typeof(T)); if (shaders.Count == 0) { throw new ObjectTKException("ShaderSourceAttribute(s) missing!"); } // create program instance var program = (T)Activator.CreateInstance(typeof(T)); try { // compile and attach all shaders foreach (var attribute in shaders) { // create a new shader of the appropriate type using (var shader = new Shader(attribute.Type)) { Logger.DebugFormat("Compiling {0}: {1}", attribute.Type, attribute.EffectKey); // load the source from effect(s) var included = new List <Effect.Section>(); var source = GetShaderSource(attribute.EffectKey, included); // assign source filenames for proper information log output shader.SourceFiles = included.Select(_ => _.Effect.Path).ToList(); // compile shader source shader.CompileSource(source); // attach shader to the program program.Attach(shader); } } // link and return the program program.Link(); } catch { program.Dispose(); throw; } return(program); }