示例#1
0
文件: Shader.cs 项目: K0bin/DotGame
 protected override void Dispose(bool isDisposing)
 {
     if (VertexShaderHandle != null)
     {
         VertexShaderHandle.Dispose();
     }
     if (PixelShaderHandle != null)
     {
         PixelShaderHandle.Dispose();
     }
     if (VertexCode != null)
     {
         VertexCode.Dispose();
     }
     if (PixelCode != null)
     {
         PixelCode.Dispose();
     }
 }
示例#2
0
        override public Result Load()
        {
            Unload();

            string add = "";

            if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.VeryHigh)
            {
                add += "#define IBL_DIFF_SAMPLE_NUM  128\n";
                add += "#define IBL_SPEC_SAMPLE_NUM  64\n";
            }
            else if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.High)
            {
                add += "#define IBL_DIFF_SAMPLE_NUM  48\n";
                add += "#define IBL_SPEC_SAMPLE_NUM  24\n";
            }
            else if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.Default)
            {
                add += "#define IBL_DIFF_SAMPLE_NUM  24\n";
                add += "#define IBL_SPEC_SAMPLE_NUM  12\n";
            }
            else if (MMW.Configuration.IBLQuality == MMWConfiguration.IBLQualityType.Low)
            {
                add += "#define IBL_DIFF_SAMPLE_NUM  12\n";
                add += "#define IBL_SPEC_SAMPLE_NUM  8\n";
            }

            if (VertexCode != null)
            {
                VertexCode = VertexCode.Replace("require(functions)", add + Resources.Functions);
            }
            if (FragmentCode != null)
            {
                FragmentCode = FragmentCode.Replace("require(functions)", add + Resources.Functions);
            }

            List <ShaderLoadData> shaders = new List <ShaderLoadData>();

            if (!string.IsNullOrEmpty(VertexCode))
            {
                shaders.Add(new ShaderLoadData(ShaderType.VertexShader, VertexCode));
            }
            if (!string.IsNullOrEmpty(FragmentCode))
            {
                shaders.Add(new ShaderLoadData(ShaderType.FragmentShader, FragmentCode));
            }
            if (!string.IsNullOrEmpty(GeometryCode))
            {
                shaders.Add(new ShaderLoadData(ShaderType.GeometryShader, GeometryCode));
            }
            if (!string.IsNullOrEmpty(TesseControlCode))
            {
                shaders.Add(new ShaderLoadData(ShaderType.TessControlShader, TesseControlCode));
            }
            if (!string.IsNullOrEmpty(TessEvaluationCode))
            {
                shaders.Add(new ShaderLoadData(ShaderType.TessEvaluationShader, TessEvaluationCode));
            }
            if (!string.IsNullOrEmpty(ComputeCode))
            {
                shaders.Add(new ShaderLoadData(ShaderType.ComputeShader, ComputeCode));
            }

            foreach (var data in shaders)
            {
                data.shader = LoadShader(data.type, data.code);
                if (data.shader == -1)
                {
                    Debug.WriteLine(Name + ": " + CompileErrorMessage);
                    //Console.WriteLine(CompileErrorMessage);
                    return(Result.CompileError);
                }
            }

            program = GL.CreateProgram();

            foreach (var data in shaders)
            {
                GL.AttachShader(program, data.shader);
                GL.DeleteShader(data.shader);
            }

            int status;

            GL.LinkProgram(program);
            GL.GetProgram(program, GetProgramParameterName.LinkStatus, out status);
            if (status == 0)
            {
                CompileErrorMessage = GL.GetProgramInfoLog(program);
                Debug.WriteLine(CompileErrorMessage);
                GL.DeleteProgram(program);
                program = -1;
                return(Result.CompileError);
            }

            foreach (var p in shaderParams)
            {
                p.Value.location = GetUniformLocation(p.Value.name);
            }

            Loaded = true;
            return(Result.Success);
        }