public void PushUniform(ShaderUniformDeclaration decl) { int offset = 0; if (Uniforms.Count > 0) { ShaderUniformDeclaration previous = Uniforms.Last(); offset = previous.Offset + previous.Size; } decl.Offset = offset; Size += decl.Size; Uniforms.Add(decl); }
/// <summary> /// Retrieve the uniform locations using the uniform names and add them to Uniforms. /// </summary> /// <param name="gl"></param> /// <param name="uniformNames"></param> private void AddUniformIds(OpenGL gl, IEnumerable <string> uniformNames) { if (Uniforms == null) { Uniforms = new Dictionary <string, int>(); } foreach (var name in uniformNames) { var location = gl.GetUniformLocation(ShaderProgramId, name); Uniforms.Add(name, location); } }
private void AddFunction(RequiredFunction requiredFunction, ICodeGen codeGen) { if (!_visitedFunctions.Add(requiredFunction.Name)) { return; } foreach (var uniform in codeGen.GetRequiredUniforms(requiredFunction)) { Uniforms.Add(uniform); } foreach (var depFunction in codeGen.GetRequiredFunctions(requiredFunction)) { AddFunction(depFunction, codeGen); } Functions.Add(requiredFunction); }
/// <summary> /// Initializes a new instance of the <see cref="GLShaderProgram"/> class. /// </summary> /// <param name="shaders">The shaders.</param> internal GLShaderProgram(IReadOnlyList <GLShader> shaders) { int program = OpenTK.Graphics.OpenGL.GL.CreateProgram(); foreach (GLShader shader in shaders) { OpenTK.Graphics.OpenGL.GL.AttachShader(program, shader.ShaderRef); } OpenTK.Graphics.OpenGL.GL.LinkProgram(program); string infoLog = OpenTK.Graphics.OpenGL.GL.GetProgramInfoLog(program); Debug.WriteLine(infoLog); foreach (GLShader shader in shaders) { OpenTK.Graphics.OpenGL.GL.DetachShader(program, shader.ShaderRef); } this.ProgramRef = program; this.Uniforms = new Dictionary <string, int>(); // Go work out what uniforms are available in this program. unsafe { int numActiveUniforms; OpenTK.Graphics.OpenGL.GL.GetProgram(this.ProgramRef, OpenTK.Graphics.OpenGL.GetProgramParameterName.ActiveUniforms, &numActiveUniforms); for (int i = 0; i < numActiveUniforms; i++) { int unifLocation; int size; OpenTK.Graphics.OpenGL.ActiveUniformType type; string name = OpenTK.Graphics.OpenGL.GL.GetActiveUniform(this.ProgramRef, i, out size, out type); unifLocation = OpenTK.Graphics.OpenGL.GL.GetUniformLocation(this.ProgramRef, name); Uniforms.Add(name, unifLocation); } } }
public void AddUniform(string name, int location, UniformDataType type) { EngineCore.CheckDuplicateName(Uniforms.Keys, name); Uniforms.Add(name, new Uniform(location, type)); UniformsFilled.Add(name, false); }
internal GL_Shader(GL_Graphics graphics, ShaderSource source) { this.graphics = graphics; if (graphics.MainThreadId != Thread.CurrentThread.ManagedThreadId) { lock (graphics.BackgroundContext) { graphics.System.SetCurrentGLContext(graphics.BackgroundContext); Create(); GL.Flush(); graphics.System.SetCurrentGLContext(null); } } else { Create(); } void Create() { ID = GL.CreateProgram(); Span <uint> shaders = stackalloc uint[2]; // vertex shader if (source.Vertex != null) { uint shaderId = GL.CreateShader(GLEnum.VERTEX_SHADER); shaders[0] = shaderId; string glsl = Encoding.UTF8.GetString(source.Vertex); GL.ShaderSource(shaderId, 1, new[] { glsl }, new int[] { glsl.Length }); GL.CompileShader(shaderId); string?errorMessage = GL.GetShaderInfoLog(shaderId); if (!string.IsNullOrEmpty(errorMessage)) { throw new Exception(errorMessage); } GL.AttachShader(ID, shaderId); } // fragment shader if (source.Fragment != null) { uint shaderId = GL.CreateShader(GLEnum.FRAGMENT_SHADER); shaders[1] = shaderId; string glsl = Encoding.UTF8.GetString(source.Fragment); GL.ShaderSource(shaderId, 1, new[] { glsl }, new int[] { glsl.Length }); GL.CompileShader(shaderId); string?errorMessage = GL.GetShaderInfoLog(shaderId); if (!string.IsNullOrEmpty(errorMessage)) { throw new Exception(errorMessage); } GL.AttachShader(ID, shaderId); } GL.LinkProgram(ID); string?programError = GL.GetProgramInfoLog(ID); if (!string.IsNullOrEmpty(programError)) { throw new Exception(programError); } // get attributes GL.GetProgramiv(ID, GLEnum.ACTIVE_ATTRIBUTES, out int attributeCount); for (int i = 0; i < attributeCount; i++) { GL.GetActiveAttrib(ID, (uint)i, out _, out _, out string name); int location = GL.GetAttribLocation(ID, name); if (location >= 0) { Attributes.Add(name, new ShaderAttribute(name, (uint)location)); } } // get uniforms GL.GetProgramiv(ID, GLEnum.ACTIVE_UNIFORMS, out int uniformCount); for (int i = 0; i < uniformCount; i++) { GL.GetActiveUniform(ID, (uint)i, out int size, out GLEnum type, out string name); int location = GL.GetUniformLocation(ID, name); if (location >= 0) { if (size > 1 && name.EndsWith("[0]")) { name = name.Substring(0, name.Length - 3); } Uniforms.Add(name, new GL_Uniform(this, name, size, location, type)); } } // dispose shaders for (int i = 0; i < shaders.Length; i++) { if (shaders[i] != 0) { GL.DetachShader(ID, shaders[i]); GL.DeleteShader(shaders[i]); } } } }
private protected override void SetupUniforms() { Uniforms.Add("test", new Uniform <int>(this, "test", 1)); }
public ShaderProgram(string vertexShader, string fragmentShader, string name, bool fromFile = true, string geometryShader = null) { if (fromFile) { vertexShader = File.ReadAllText(vertexShader); fragmentShader = File.ReadAllText(fragmentShader); if (geometryShader != null) { geometryShader = File.ReadAllText(geometryShader); } } Shader vertex = new Shader(vertexShader, ShaderType.VertexShader); this.ShaderInfoLog += vertex.InfoLog; Shader fragment = new Shader(fragmentShader, ShaderType.FragmentShader); this.ShaderInfoLog += fragment.InfoLog; Shader geometry = null; if (geometryShader != null) { geometry = new Shader(geometryShader, ShaderType.GeometryShader); this.ShaderInfoLog += geometry.InfoLog; } this.Attributes = new Dictionary <string, AttributeInfo>(); this.Uniforms = new Dictionary <string, UniformInfo>(); this.WarningLogs = new List <string>(); this.Name = name; Pointer = GL.CreateProgram(); GL.AttachShader(Pointer, vertex.Pointer); GL.AttachShader(Pointer, fragment.Pointer); if (geometry != null) { GL.AttachShader(Pointer, geometry.Pointer); } GL.LinkProgram(Pointer); ProgramInfoLog = GL.GetProgramInfoLog(Pointer); int count = 0; GL.GetProgram(Pointer, GetProgramParameterName.ActiveAttributes, out count); int size = 0; for (int i = 0; i < count; i++) { AttributeInfo info = new AttributeInfo(); info.name = GL.GetActiveAttrib(Pointer, i, out size, out info.type); info.address = GL.GetAttribLocation(Pointer, info.name); Attributes.Add(info.name, info); } GL.GetProgram(Pointer, GetProgramParameterName.ActiveUniforms, out count); for (int i = 0; i < count; i++) { UniformInfo uInfo = new UniformInfo(); uInfo.name = GL.GetActiveUniform(Pointer, i, out size, out uInfo.type); uInfo.address = GL.GetUniformLocation(Pointer, uInfo.name); Uniforms.Add(uInfo.name, uInfo); } if (!string.IsNullOrEmpty(ShaderInfoLog)) { Console.WriteLine("\n-----" + name + "-----"); Console.WriteLine(ShaderInfoLog); } }