public Shader(string vertexFile, string fragmentFile, ShaderInputLayout layout) { _uniformCache = new Dictionary <string, int>(); _samplers = new Dictionary <string, int>(); _arraySamplers = new Dictionary <string, int>(); string vertexCode = File.ReadAllText(vertexFile); string fragmentCode = File.ReadAllText(fragmentFile); //Find samplers { int cSampler = 0; string findCode = fragmentCode; int i = findCode.IndexOf("uniform sampler2D"); while (i != -1) { findCode = findCode.Substring(i + 18); int nx = findCode.IndexOf(';'); string var = findCode.Substring(0, nx); if (var.EndsWith("]")) //Array sampler { nx = findCode.IndexOf('['); string name = var.Substring(0, nx); var = var.Substring(nx, var.Length - nx); var = var.Substring(1, var.Length - 1).Substring(0, var.Length - 2); int num = Convert.ToInt32(var); _arraySamplers.Add(name, num); cSampler += num; } else { _samplers.Add(var, cSampler); cSampler++; } i = findCode.IndexOf("uniform sampler2D"); } } int vs = GL.CreateShader(ShaderType.VertexShader); int ps = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(vs, vertexCode); GL.ShaderSource(ps, fragmentCode); GL.CompileShader(vs); GL.CompileShader(ps); string vertexLog = GL.GetShaderInfoLog(vs); string fragmentLog = GL.GetShaderInfoLog(ps); if (!string.IsNullOrEmpty(vertexLog) || !string.IsNullOrEmpty(fragmentLog)) { MessageBox.Show("Failed to compile shader:\nVertex shader log: " + vertexLog + "\nFragment shader log: " + fragmentLog); return; } _shader = GL.CreateProgram(); GL.AttachShader(_shader, vs); GL.AttachShader(_shader, ps); if (layout != null) { layout.BindLayouts(_shader); } GL.LinkProgram(_shader); GL.DeleteShader(vs); GL.DeleteShader(ps); string programLog = GL.GetShaderInfoLog(vs); if (!string.IsNullOrEmpty(programLog)) { MessageBox.Show("Failed to compile shader: " + programLog); return; } }
public Shader(string vertexFile, string fragmentFile, ShaderInputLayout layout) { _uniformCache = new Dictionary<string, int>(); _samplers = new Dictionary<string, int>(); _arraySamplers = new Dictionary<string, int>(); string vertexCode = File.ReadAllText(vertexFile); string fragmentCode = File.ReadAllText(fragmentFile); //Find samplers { int cSampler = 0; string findCode = fragmentCode; int i = findCode.IndexOf("uniform sampler2D"); while (i != -1) { findCode = findCode.Substring(i + 18); int nx = findCode.IndexOf(';'); string var = findCode.Substring(0, nx); if (var.EndsWith("]")) //Array sampler { nx = findCode.IndexOf('['); string name = var.Substring(0, nx); var = var.Substring(nx, var.Length - nx); var = var.Substring(1, var.Length - 1).Substring(0, var.Length - 2); int num = Convert.ToInt32(var); _arraySamplers.Add(name, num); cSampler += num; } else { _samplers.Add(var, cSampler); cSampler++; } i = findCode.IndexOf("uniform sampler2D"); } } int vs = GL.CreateShader(ShaderType.VertexShader); int ps = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(vs, vertexCode); GL.ShaderSource(ps, fragmentCode); GL.CompileShader(vs); GL.CompileShader(ps); string vertexLog = GL.GetShaderInfoLog(vs); string fragmentLog = GL.GetShaderInfoLog(ps); if (!string.IsNullOrEmpty(vertexLog) || !string.IsNullOrEmpty(fragmentLog)) { MessageBox.Show("Failed to compile shader:\nVertex shader log: " + vertexLog + "\nFragment shader log: " + fragmentLog); return; } _shader = GL.CreateProgram(); GL.AttachShader(_shader, vs); GL.AttachShader(_shader, ps); if (layout != null) layout.BindLayouts(_shader); GL.LinkProgram(_shader); GL.DeleteShader(vs); GL.DeleteShader(ps); string programLog = GL.GetShaderInfoLog(vs); if (!string.IsNullOrEmpty(programLog)) { MessageBox.Show("Failed to compile shader: " + programLog); return; } }
public void Dispose() { PixelShader.Dispose(); VertexShader.Dispose(); ShaderInputLayout.Dispose(); }