RenderStack 11.06.1
Public Member Functions | Static Public Member Functions | Public Attributes | Properties
RenderStack.Graphics.Program Class Reference

Abstraction for OpenGL program object. More...

Inheritance diagram for RenderStack.Graphics.Program:
RenderStack.Graphics.IProgram

List of all members.

Public Member Functions

override string ToString ()
void Dispose ()
 Program ()
 Program (string vertexShader, string fragmentShader)
void Load (string vertexShader, string fragmentShader)
void OnFileChanged (string fullpath)
void OnChanged ()
 Program (string geometryShader, string vertexShader, string fragmentShader)
bool LoadFromBinary (string path)
void StoreBinaryFile (string path)
void LoadFromFiles (string geometryShader, string vertexShader, string fragmentShader)
void MakeShader (ShaderType type, string source)
void MakeShaderFromFile (ShaderType type, string fullpath)
void Use ()
void Link ()
void MapAttributes ()
void MapUniforms ()
void Bind (int slot, IUniformValue value)
void Bind (IUniformSource source)
void Bind (Dictionary< string, IUniformValue > values)
void ApplyUniforms ()
bool HasAttribute (string name)
ProgramAttribute Attribute (string name)

Static Public Member Functions

static Program Load (string filename)
static void Load (string fullpath, Program program)

Public Attributes

string Name
bool Valid = false
int SortValue

Properties

List< ProgramAttributeAttributes [get]
Uniform[] Uniforms [get]
List< ShaderShaders [get]
int ProgramObject [get]
AttributeMappings AttributeMappings [get, set]
static string ShaderSearchPath [get, set]

Detailed Description

Note:
Mostly stable, somewhat experimental

Definition at line 51 of file technologies/RenderStack.Graphics/Program.cs.


Constructor & Destructor Documentation

RenderStack.Graphics.Program.Program ( )

Definition at line 153 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.GhostManager.Gen().

        {
            programObject = GL.CreateProgram();
            GhostManager.Gen();
        }
RenderStack.Graphics.Program.Program ( string  vertexShader,
string  fragmentShader 
)

Definition at line 158 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.GhostManager.Gen().

        {
            programObject = GL.CreateProgram();
            GhostManager.Gen();
            Load(vertexShader, fragmentShader);
        }
RenderStack.Graphics.Program.Program ( string  geometryShader,
string  vertexShader,
string  fragmentShader 
)

Definition at line 212 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.GhostManager.Gen().

        {
            programObject = GL.CreateProgram();
            GhostManager.Gen();

            LoadFromFiles(geometryShader, vertexShader, fragmentShader);
        }

Member Function Documentation

override string RenderStack.Graphics.Program.ToString ( )

Definition at line 76 of file technologies/RenderStack.Graphics/Program.cs.

        {
            return Name + (Valid ? " valid "" invalid ") + Attributes.Count + " attributes, " + Uniforms.Length + " uniforms";
        }
static Program RenderStack.Graphics.Program.Load ( string  filename) [static]

Definition at line 81 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.glslVersion, and RenderStack.Graphics.Configuration.throwProgramExceptions.

        {
            try
            {
                Program program = new Program();
                string fullpath = ((Configuration.glslVersion < 330) ? "res/shaders120/" : "res/shaders330/") + filename;
                Load(fullpath, program);
                return program;
            }
            catch(System.Exception e)
            {
                Trace.TraceError("Program.Load(" + filename + ") failed - exception " + e.ToString());
                if(Configuration.throwProgramExceptions)
                {
                    throw;
                }
                return null;
            }
        }
static void RenderStack.Graphics.Program.Load ( string  fullpath,
Program  program 
) [static]

Definition at line 100 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.canUseBinaryShaders, RenderStack.Graphics.Configuration.canUseGeometryShaders, RenderStack.Graphics.Program.LoadFromBinary(), RenderStack.Graphics.Program.LoadFromFiles(), RenderStack.Graphics.Program.Name, RenderStack.Graphics.Program.StoreBinaryFile(), and RenderStack.Graphics.Configuration.useBinaryShaders.

        {
            Console.WriteLine("Loading program " + fullpath);

            string binaryPath = fullpath.Replace("res/", "data/") + ".binary";
            bool useBinary = Configuration.useBinaryShaders && Configuration.canUseBinaryShaders && System.IO.File.Exists(binaryPath);

            if(useBinary)
            {
                if(program.LoadFromBinary(binaryPath))
                {
                    program.Name = fullpath;
                    return;
                }
            }

            program.LoadFromFiles(
                Configuration.canUseGeometryShaders ? fullpath + ".gs" : null, 
                fullpath + ".vs", 
                fullpath + ".fs"
            );

            if(Configuration.useBinaryShaders && Configuration.canUseBinaryShaders)
            {
                program.StoreBinaryFile(binaryPath);
            }

            program.Name = fullpath;
        }
void RenderStack.Graphics.Program.Dispose ( )

Definition at line 135 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.GhostManager.Add().

Referenced by example.UI.Programs.Dispose(), and example.Scene.Programs.Dispose().

        {
            if(!disposed)
            {
                foreach(var shader in shaders)
                {
                    shader.Dispose();
                }
                shaders.Clear();
                if(programObject != 0)
                {
                    GhostManager.Add(new ProgramGhost(programObject));
                    programObject = 0;
                }
                GC.SuppressFinalize(this);
                disposed = true;
            }
        }
void RenderStack.Graphics.Program.Load ( string  vertexShader,
string  fragmentShader 
)

Definition at line 164 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.throwProgramExceptions.

        {
            try
            {
                GL.BindAttribLocation(this.programObject, 0, "_position");
                if(RenderStack.Graphics.Configuration.glVersion >= 300)
                {
                    GL.BindFragDataLocation(this.programObject, 0, "out_color");
                    GL.BindFragDataLocation(this.programObject, 0, "out0");
                    GL.BindFragDataLocation(this.programObject, 1, "out1");
                }
                MakeShader(ShaderType.VertexShader, vertexShader);
                MakeShader(ShaderType.FragmentShader, fragmentShader);
                Link();
                Valid = true;
            }
            catch(System.Exception e)
            {
                Valid = false;
                Trace.TraceError("Program.Load() failed - exception " + e.ToString());
                if(Configuration.throwProgramExceptions)
                {
                    throw;
                }
            }
        }
void RenderStack.Graphics.Program.OnFileChanged ( string  fullpath)

Definition at line 190 of file technologies/RenderStack.Graphics/Program.cs.

        {
            // NOP - Programs are not loaded from files, shaders are
        }
void RenderStack.Graphics.Program.OnChanged ( )

Definition at line 194 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.throwProgramExceptions.

        {
            try
            {
                Link();
                Valid = true;
            }
            catch(System.Exception e)
            {
                Valid = false;
                Trace.TraceError("Program.OnChanged() failed - exception " + e.ToString());
                if(Configuration.throwProgramExceptions)
                {
                    throw;
                }
            }
        }
bool RenderStack.Graphics.Program.LoadFromBinary ( string  path)

Definition at line 224 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.throwProgramExceptions.

Referenced by RenderStack.Graphics.Program.Load().

        {
            try
            {
                int numFormats;
                GL.GetInteger(GetPName.NumProgramBinaryFormats, out numFormats);

                int[] formats = new int[numFormats];
                GL.GetInteger(GetPName.ProgramBinaryFormats, formats);

                BinaryFormat format = (BinaryFormat)(formats[0]);

                byte[] binary = System.IO.File.ReadAllBytes(path);
                GL.ProgramBinary(programObject, format, binary, binary.Length);

                int linkStatus;
                GL.GetProgram(programObject, ProgramParameter.LinkStatus, out linkStatus);

                GL.ValidateProgram(programObject);

                int validateStatus;
                GL.GetProgram(programObject, ProgramParameter.ValidateStatus, out validateStatus);

                Use();

                MapAttributes();
                MapUniforms();

                Valid = true;

                return true;
            }
            catch(Exception e)
            {
                Trace.TraceError("Program.LoadFromBinary() failed - exception " + e.ToString());
                if(Configuration.throwProgramExceptions)
                {
                    throw;
                }
                return false;
            }
        }
void RenderStack.Graphics.Program.StoreBinaryFile ( string  path)

Definition at line 266 of file technologies/RenderStack.Graphics/Program.cs.

Referenced by RenderStack.Graphics.Program.Load().

        {
            try
            {
                int numFormats;
                GL.GetInteger(GetPName.NumProgramBinaryFormats, out numFormats);

                if(numFormats == 0)
                {
                    return;
                }

                int[] formats = new int[numFormats];
                GL.GetInteger(GetPName.ProgramBinaryFormats, formats);

                int length = 0;

                int linkStatus;
                GL.GetProgram(programObject, ProgramParameter.LinkStatus, out linkStatus);

                GL.ValidateProgram(programObject);

                int validateStatus;
                GL.GetProgram(programObject, ProgramParameter.ValidateStatus, out validateStatus);

                GL.GetProgram(programObject, ProgramParameter.ProgramBinaryLength, out length);

                BinaryFormat format = (BinaryFormat)(formats[0]);
                byte[] binary = new byte[length];
                int usedLength = 0;

                GL.GetProgramBinary<byte>(
                    programObject, 
                    binary.Length, 
                    out usedLength, 
                    out format, 
                    binary
                );

                string directory = System.IO.Path.GetDirectoryName(path);
                if(System.IO.Directory.Exists(directory) == false)
                {
                    System.IO.Directory.CreateDirectory(directory);
                }
                System.IO.File.WriteAllBytes(path, binary);
            }
            catch(Exception e)
            {
                //  Storing shader binary failed, no serious harm done
                Trace.TraceWarning("Program.StoreBinaryFile() failed - exception " + e.ToString());
            }
        }
void RenderStack.Graphics.Program.LoadFromFiles ( string  geometryShader,
string  vertexShader,
string  fragmentShader 
)

Definition at line 319 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.throwProgramExceptions.

Referenced by RenderStack.Graphics.Program.Load().

        {
            try
            {
                GL.BindAttribLocation(this.programObject, 0, "_position");
                if(RenderStack.Graphics.Configuration.glVersion >= 300)
                {
                    GL.BindFragDataLocation(this.programObject, 0, "out_color");
                }
                if(System.IO.File.Exists(geometryShader))
                {
                    MakeShaderFromFile(ShaderType.GeometryShader,   geometryShader);
                }
                if(System.IO.File.Exists(vertexShader))
                {
                    MakeShaderFromFile(ShaderType.VertexShader,     vertexShader);
                }
                if(System.IO.File.Exists(fragmentShader))
                {
                    MakeShaderFromFile(ShaderType.FragmentShader,   fragmentShader);
                }
                Link();
                Valid = true;
            }
            catch(Exception e)
            {
                Trace.TraceError(
                    "Program.LoadFromFiles("
                    + geometryShader + ", "
                    + vertexShader + ", "
                    + fragmentShader + ", "
                    + ") failed - exception " + e.ToString()
                );
                Valid = false;
                if(Configuration.throwProgramExceptions)
                {
                    throw;
                }
            }
        }
void RenderStack.Graphics.Program.MakeShader ( ShaderType  type,
string  source 
)

Definition at line 364 of file technologies/RenderStack.Graphics/Program.cs.

        {
            if(source == null)
            {
                return;
            }

            var shader = new Shader(type);
            shader.Load(source);
            Shaders.Add(shader);

            GL.AttachShader(ProgramObject, shader.ShaderObject);
        }
void RenderStack.Graphics.Program.MakeShaderFromFile ( ShaderType  type,
string  fullpath 
)

Definition at line 377 of file technologies/RenderStack.Graphics/Program.cs.

        {
            if(fullpath == null)
            {
                return;
            }

            var shader = new Shader(type);
            shader.LoadFromFile(fullpath);
            shader.AddProgram(this);
            Shaders.Add(shader);

            GL.AttachShader(ProgramObject, shader.ShaderObject);
        }
void RenderStack.Graphics.Program.Use ( )

Definition at line 391 of file technologies/RenderStack.Graphics/Program.cs.

        {
            if(Valid)
            {
                GL.UseProgram(ProgramObject);
            }
        }
void RenderStack.Graphics.Program.Link ( )

Definition at line 398 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.Configuration.useBinaryShaders.

        {
            int linkStatus;

            if(Configuration.useBinaryShaders)
            {
                try
                {
                    GL.ProgramParameter(programObject, AssemblyProgramParameterArb.ProgramBinaryRetrievableHint, (int)All.True);
                }
                catch(Exception)
                {
                }
            }

            GL.LinkProgram(ProgramObject);
            GL.GetProgram(ProgramObject, ProgramParameter.LinkStatus, out linkStatus);

            if(linkStatus == (int)Boolean.False)
            {
                string infoLog = GL.GetProgramInfoLog(ProgramObject);
                GL.DeleteProgram(ProgramObject);

                System.Diagnostics.Trace.TraceError("Program linking failed:");
                System.Diagnostics.Trace.TraceError(infoLog);

                Valid = false;

                throw new System.InvalidOperationException();
            }

            Use();

            MapAttributes();
            MapUniforms();
        }
void RenderStack.Graphics.Program.MapAttributes ( )

Definition at line 435 of file technologies/RenderStack.Graphics/Program.cs.

        {
            int attributeCount = 0;

            GL.GetProgram(
                ProgramObject, 
                ProgramParameter.ActiveAttributes, 
                out attributeCount
            );

            Attributes.Clear();
            for(int i = 0; i < attributeCount; ++i)
            {
                int                 size;
                ActiveAttribType    type;

                string name = GL.GetActiveAttrib(
                    ProgramObject,
                    i,
                    out size,
                    out type
                );

                int slot = GL.GetAttribLocation(ProgramObject, name);

                // Debug.WriteLine("\tAttribute " + name + " slot " + slot.ToString());

                var attribute = new ProgramAttribute(
                    name, 
                    slot, 
                    size, 
                    type
                );

                Attributes.Add(attribute);
            }
        }
void RenderStack.Graphics.Program.MapUniforms ( )

Definition at line 472 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.UniformMappings.ToLogical(), and RenderStack.Graphics.UniformMappings.ToParameterName().

        {
            int                 size;
            ActiveUniformType   type;
            int                 uniformCount = 0;
            int                 maxSlot = -1;

            GL.GetProgram(
                ProgramObject,
                ProgramParameter.ActiveUniforms,
                out uniformCount
            );

            // First pass; get max slot
            for(int i = 0; i < uniformCount; ++i)
            {
                string  name = GL.GetActiveUniform(ProgramObject, i, out size, out type);
                int     slot = GL.GetUniformLocation(ProgramObject, name);
                if(slot > maxSlot)
                {
                    maxSlot = slot;
                }
            }

            uniforms        = new Uniform[maxSlot + 1];
            uniformCache    = new IUniformValue[maxSlot + 1];
            uniformValues   = new IUniformValue[maxSlot + 1];
            logicalUniforms = new LogicalUniform[maxSlot + 1];
            parameterNames  = new string[maxSlot + 1];

            for(int i = 0; i < uniformCount; ++i)
            {
                string name = GL.GetActiveUniform(
                    ProgramObject,
                    i,
                    out size,
                    out type
                );

                int slot = GL.GetUniformLocation(ProgramObject, name);

                //System.Diagnostics.Debug.WriteLine("\tUniform " + name + " slot " + slot.ToString());

                uniforms[slot] = new Uniform(
                    name,
                    slot,
                    size,
                    type
                );

                logicalUniforms[slot] = UniformMappings.ToLogical(name);
                parameterNames[slot]  = UniformMappings.ToParameterName(name);
            }
        }
void RenderStack.Graphics.Program.Bind ( int  slot,
IUniformValue  value 
)

Implements RenderStack.Graphics.IProgram.

Definition at line 527 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.IUniformValue.GetUninitialized().

        {
            uniformCache[slot] = value.GetUninitialized();
            uniformValues[slot] = value;
        }
void RenderStack.Graphics.Program.Bind ( IUniformSource  source)

Implements RenderStack.Graphics.IProgram.

Definition at line 532 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.IUniformSource.Connect().

        {
            for(int i = 0; i < logicalUniforms.Length; ++i)
            {
                LogicalUniform type = logicalUniforms[i];
                if(type != LogicalUniform.GenericUniform)
                {
                    if(source.Connect(this, type, i))
                    {
                        /*System.Diagnostics.Debug.WriteLine(
                            "bound uniform slot " + i + " name " + uniforms[i].Name
                        );*/
                    }
                }
            }
        }
void RenderStack.Graphics.Program.Bind ( Dictionary< string, IUniformValue values)

Implements RenderStack.Graphics.IProgram.

Definition at line 548 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.IUniformValue.GetUninitialized().

        {
            for(int i = 0; i < parameterNames.Length; ++i)
            {
                string parameter = parameterNames[i];
                if(parameter != null && values.ContainsKey(parameter))
                {
                    this.uniformCache[i] = values[parameter].GetUninitialized();
                    this.uniformValues[i] = values[parameter];
                    /*System.Diagnostics.Debug.WriteLine(
                        "bound uniform slot " + i + " name " + uniforms[i].Name
                    );*/
                }
            }
        }
void RenderStack.Graphics.Program.ApplyUniforms ( )

Implements RenderStack.Graphics.IProgram.

Definition at line 563 of file technologies/RenderStack.Graphics/Program.cs.

References RenderStack.Graphics.IUniformValue.Apply().

        {
            context.TextureIndex = 0;
            for(context.Slot = 0; context.Slot < uniformValues.Length; ++context.Slot)
            {
                if(uniformValues[context.Slot] != null)
                {
                    if(uniformCache[context.Slot] == null)
                    {
                        //Debugger.Break(); This does not work right yet with Monodevelop
                        throw new System.InvalidOperationException();
                    }
                    if(uniformCache[context.Slot].CompareTo(uniformValues[context.Slot]) != 0)
                    {
                        uniformValues[context.Slot].Apply(ref context);
                        uniformCache[context.Slot].CacheFrom(
                            uniformValues[context.Slot]
                        );
                    }
                }
                else
                {
                    if(logicalUniforms[context.Slot] != LogicalUniform.NotMapped)
                    {
                        System.Diagnostics.Debug.WriteLine(
                            "Warning: No value for " + uniforms[context.Slot].Name
                        );
                    }
                }
            }
        }
bool RenderStack.Graphics.Program.HasAttribute ( string  name)

Definition at line 595 of file technologies/RenderStack.Graphics/Program.cs.

        {
            foreach(var attribute in Attributes)
            {
                if(attribute.Name == name)
                {
                    return true;
                }
            }
            return false;
        }
ProgramAttribute RenderStack.Graphics.Program.Attribute ( string  name)

Definition at line 607 of file technologies/RenderStack.Graphics/Program.cs.

        {
            foreach(var a in Attributes)
            {
                if(a.Name == name)
                {
                    return a;
                }
            }
            throw new System.NullReferenceException();
        }

Member Data Documentation

Definition at line 67 of file technologies/RenderStack.Graphics/Program.cs.

Definition at line 68 of file technologies/RenderStack.Graphics/Program.cs.


Property Documentation

List<ProgramAttribute> RenderStack.Graphics.Program.Attributes [get]

Definition at line 69 of file technologies/RenderStack.Graphics/Program.cs.

Uniform [] RenderStack.Graphics.Program.Uniforms [get]

Definition at line 70 of file technologies/RenderStack.Graphics/Program.cs.

List<Shader> RenderStack.Graphics.Program.Shaders [get]

Definition at line 71 of file technologies/RenderStack.Graphics/Program.cs.

int RenderStack.Graphics.Program.ProgramObject [get]

Definition at line 72 of file technologies/RenderStack.Graphics/Program.cs.

AttributeMappings RenderStack.Graphics.Program.AttributeMappings [get, set]

Definition at line 73 of file technologies/RenderStack.Graphics/Program.cs.

string RenderStack.Graphics.Program.ShaderSearchPath [static, get, set]

Definition at line 74 of file technologies/RenderStack.Graphics/Program.cs.


The documentation for this class was generated from the following file: