public void Dispose() { vs.Dispose(); vs = null; ps.Dispose(); bytecode.Dispose(); bytecode = null; }
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); }
/// <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 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 (Exception ex) { if (required) { throw; } var s = new Shader(this, null, false); s.Errors = ex.ToString(); return(s); } }
protected override void UnloadHighLevelImpl() { if ( microcode != null ) microcode.Dispose(); microcode = null; }
private void CompileMicrocode() { ConstantTable constantTable = null; string errors = null; var defines = buildDefines(preprocessorDefines); var compileFlags = ShaderFlags.None; var parseFlags = ShaderFlags.None; parseFlags |= columnMajorMatrices ? ShaderFlags.PackMatrixColumnMajor : ShaderFlags.PackMatrixRowMajor; #if DEBUG compileFlags |= ShaderFlags.Debug; parseFlags |= ShaderFlags.Debug; #endif switch (optimizationLevel) { case OptimizationLevel.Default: compileFlags |= ShaderFlags.OptimizationLevel1; parseFlags |= ShaderFlags.OptimizationLevel1; break; case OptimizationLevel.None: compileFlags |= ShaderFlags.SkipOptimization; parseFlags |= ShaderFlags.SkipOptimization; break; case OptimizationLevel.LevelZero: compileFlags |= ShaderFlags.OptimizationLevel0; parseFlags |= ShaderFlags.OptimizationLevel0; break; case OptimizationLevel.LevelOne: compileFlags |= ShaderFlags.OptimizationLevel1; parseFlags |= ShaderFlags.OptimizationLevel1; break; case OptimizationLevel.LevelTwo: compileFlags |= ShaderFlags.OptimizationLevel2; parseFlags |= ShaderFlags.OptimizationLevel2; break; case OptimizationLevel.LevelThree: compileFlags |= ShaderFlags.OptimizationLevel3; parseFlags |= ShaderFlags.OptimizationLevel3; break; } // compile the high level shader to low level microcode // note, we need to pack matrices in row-major format for HLSL var effectCompiler = new EffectCompiler(Source, defines.ToArray(), includeHandler, parseFlags); try { microcode = effectCompiler.CompileShader(new EffectHandle(entry), target, compileFlags, out errors, out constantTable); } catch (Direct3D9Exception ex) { throw new AxiomException("HLSL: Unable to compile high level shader {0}:\n{1}", ex, Name); } finally { // check for errors if ( !String.IsNullOrEmpty( errors ) ) { if ( microcode != null ) { if ( LogManager.Instance != null ) { LogManager.Instance.Write( "HLSL: Warnings while compiling high level shader {0}:\n{1}", Name, errors ); } } else { throw new AxiomException( "HLSL: Unable to compile high level shader {0}:\n{1}", Name, errors ); } } // Get contents of the constant table var desc = constantTable.Description; CreateParameterMappingStructures( true ); // Iterate over the constants for ( var i = 0; i < desc.Constants; ++i ) { // Recursively descend through the structure levels ProcessParamElement( constantTable, null, "", i ); } constantTable.Dispose(); /* if ( GpuProgramManager.Instance.SaveMicrocodesToCache ) { AddMicrocodeToCache(); }*/ effectCompiler.Dispose(); } }
protected override void unload() { if (externalMicrocode != null) externalMicrocode.Dispose(); externalMicrocode = null; }