Shader CreateShader(bool cg, ShaderType type, string source, string entry, bool required) { var sw = new ShaderWrapper(); if (cg) { var cgc = new CGC(); var results = cgc.Run(source, entry, type == ShaderType.FragmentShader ? "glslf" : "glslv", false); if (!results.Succeeded) { Console.WriteLine("CGC failed"); Console.WriteLine(results.Errors); return(new Shader(this, null, false)); } source = results.Code; sw.MapCodeToNative = results.MapCodeToNative; sw.MapNativeToCode = results.MapNativeToCode; } int sid = GL.CreateShader(type); bool ok = CompileShaderSimple(sid, source, required); if (!ok) { GL.DeleteShader(sid); sid = 0; } sw.sid = sid; return(new Shader(this, sw, ok)); }
/// <exception cref="InvalidOperationException"><paramref name="required"/> is <see langword="true"/> and compilation error occurred</exception> public Shader CreateVertexShader(bool cg, string source, string entry, bool required) { try { ShaderWrapper sw = new ShaderWrapper(); if (cg) { var cgc = new CGC(); var results = cgc.Run(source, entry, "hlslv", true); source = results.Code; entry = "main"; if (!results.Succeeded) { if (required) { throw new InvalidOperationException(results.Errors); } else { return(new Shader(this, null, false)); } } sw.MapCodeToNative = results.MapCodeToNative; sw.MapNativeToCode = results.MapNativeToCode; } string errors = null; d3d9.ShaderBytecode bytecode = null; try { //cgc can create shaders that will need backwards compatibility... string profile = "vs_1_1"; if (cg) { profile = "vs_3_0"; //todo - smarter logic somehow } bytecode = d3d9.ShaderBytecode.Compile(source, null, null, entry, profile, ShaderFlags.EnableBackwardsCompatibility, out errors); } catch (Exception ex) { throw new InvalidOperationException($"Error compiling shader: {errors}", ex); } sw.vs = new VertexShader(dev, bytecode); sw.bytecode = bytecode; Shader s = new Shader(this, sw, true); sw.IGLShader = s; return(s); } catch (Exception ex) { if (required) { throw; } var s = new Shader(this, null, false); s.Errors = ex.ToString(); return(s); } }
public Shader CreateVertexShader(bool cg, string source, string entry, bool required) { try { ShaderWrapper sw = new ShaderWrapper(); if (cg) { var cgc = new CGC(); var results = cgc.Run(source, entry, "hlslv", true); source = results.Code; entry = "main"; if (!results.Succeeded) { if (required) throw new InvalidOperationException(results.Errors); else return new Shader(this, null, false); } sw.MapCodeToNative = results.MapCodeToNative; sw.MapNativeToCode = results.MapNativeToCode; } string errors = null; d3d9.ShaderBytecode bytecode = null; try { //cgc can create shaders that will need backwards compatibility... string profile = "vs_1_1"; if (cg) profile = "vs_3_0"; //todo - smarter logic somehow bytecode = d3d9.ShaderBytecode.Compile(source, null, null, entry, profile, ShaderFlags.EnableBackwardsCompatibility, out errors); } catch (Exception ex) { throw new InvalidOperationException("Error compiling shader: " + errors, ex); } sw.vs = new VertexShader(dev, bytecode); sw.bytecode = bytecode; Shader s = new Shader(this, sw, true); sw.IGLShader = s; return s; } catch(Exception ex) { if (required) throw; var s = new Shader(this, null, false); s.Errors = ex.ToString(); return s; } }
Shader CreateShader(bool cg, ShaderType type, string source, string entry, bool required) { var sw = new ShaderWrapper(); if (cg) { var cgc = new CGC(); var results = cgc.Run(source, entry, type == ShaderType.FragmentShader ? "glslf" : "glslv", false); if (!results.Succeeded) return new Shader(this, null, false); source = results.Code; sw.MapCodeToNative = results.MapCodeToNative; sw.MapNativeToCode = results.MapNativeToCode; } int sid = GL.CreateShader(type); bool ok = CompileShaderSimple(sid, source, required); if(!ok) { GL.DeleteShader(sid); sid = 0; } sw.sid = sid; return new Shader(this, sw, ok); }
public Shader CreateFragmentShader(bool cg, string source, string entry, bool required) { try { ShaderWrapper sw = new ShaderWrapper(); if (cg) { var cgc = new CGC(); var results = cgc.Run(source, entry, "hlslf", true); source = results.Code; entry = "main"; if (!results.Succeeded) { if (required) throw new InvalidOperationException(results.Errors); else return new Shader(this, null, false); } sw.MapCodeToNative = results.MapCodeToNative; sw.MapNativeToCode = results.MapNativeToCode; } string errors = null; d3d9.ShaderBytecode bytecode = null; try { //cgc can create shaders that will need backwards compatibility... string profile = "ps_1_0"; if (cg) profile = "ps_3_0"; //todo - smarter logic somehow //ShaderFlags.EnableBackwardsCompatibility - used this once upon a time (please leave a note about why) // bytecode = d3d9.ShaderBytecode.Compile(source, null, null, entry, profile, ShaderFlags.UseLegacyD3DX9_31Dll, out errors); } catch (Exception ex) { throw new InvalidOperationException("Error compiling shader: " + errors, ex); } sw.ps = new PixelShader(dev, bytecode); sw.bytecode = bytecode; Shader s = new Shader(this, sw, true); sw.IGLShader = s; return s; } catch { if (required) throw; else return new Shader(this, null, false); } }
public Shader CreateFragmentShader(bool cg, string source, string entry, bool required) { ShaderWrapper sw = new ShaderWrapper(); if (cg) { var cgc = new CGC(); var results = cgc.Run(source, entry, "hlslf"); source = results.Code; entry = "main"; if (!results.Succeeded) { if (required) throw new InvalidOperationException(results.Errors); else return new Shader(this, null, false); } sw.MapCodeToNative = results.MapCodeToNative; sw.MapNativeToCode = results.MapNativeToCode; } string errors = null; d3d9.ShaderBytecode bytecode = null; try { //cgc can create shaders that will need backwards compatibility... bytecode = d3d9.ShaderBytecode.Compile(source, null, null, entry, "ps_3_0", ShaderFlags.EnableBackwardsCompatibility, out errors); } catch(Exception ex) { throw new InvalidOperationException("Error compiling shader: " + errors, ex); } sw.ps = new PixelShader(dev, bytecode); sw.bytecode = bytecode; Shader s = new Shader(this, sw, true); sw.IGLShader = s; return s; }