コード例 #1
0
        private void AddShaderCompileSample(int frameIdx, ProfilerSample sampleData)
        {
            var compileInfo = new ShaderCompileInfo();

            compileInfo.frameIdx       = frameIdx;
            compileInfo.msec           = sampleData.timeUS / 1000.0f;
            compileInfo.callFromWarmup = IsCalledWarmup(sampleData.parent);
            if (sampleData.metaDatas != null)
            {
                var metadatas = sampleData.metaDatas.metadatas;
                if (metadatas != null)
                {
                    if (metadatas.Count > 0)
                    {
                        compileInfo.shader = metadatas[0].convertedObject as string;
                    }
                    if (metadatas.Count > 1)
                    {
                        compileInfo.pass = metadatas[1].convertedObject as string;
                        this.hasPassStageKeywordsInfo = true;
                    }

                    if (metadatas.Count > 2)
                    {
                        compileInfo.stage = metadatas[2].convertedObject as string;
                    }

                    if (metadatas.Count > 3)
                    {
                        compileInfo.keywords = metadatas[3].convertedObject as string;
                    }
                }
            }
            this.compileInfos.Add(compileInfo);
        }
コード例 #2
0
        public IShader CompileShader(string name, ShaderCompileInfo vertex, ShaderCompileInfo pixel)
        {
            AssertCurrent();

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is empty or whitespace.", "name");
            }

            if (vertex.File == null)
            {
                throw new ArgumentException("File of info is null.", "vertex");
            }
            if (pixel.File == null)
            {
                throw new ArgumentException("File of info is null.", "pixel");
            }

            string vertexCode = File.ReadAllText(vertex.File);

            vertexCode = CheckShaderVersion(name, vertexCode, vertex);

            string pixelCode = File.ReadAllText(pixel.File);

            pixelCode = CheckShaderVersion(name, pixelCode, pixel);

            return(Register(new Shader(graphicsDevice, vertexCode, pixelCode)));
        }
コード例 #3
0
 private void CheckCompileInfo(ShaderCompileInfo info, string parameterName)
 {
     if (info.File == null)
     {
         throw new ArgumentException("File of info is null.", parameterName);
     }
     if (info.Function == null)
     {
         throw new ArgumentException("Function of info is null.", parameterName);
     }
     if (info.Version == null)
     {
         throw new ArgumentException("Version of info is null.", parameterName);
     }
 }
コード例 #4
0
        private string CheckShaderVersion(string shaderName, string shaderCode, ShaderCompileInfo info)
        {
            string code = shaderCode.TrimStart('\r', '\n', ' ');

            string[] shaderVersion = code.Substring(0, shaderCode.IndexOf(Environment.NewLine)).Split(' ');
            if (shaderVersion.Length > 1 && shaderVersion[0] == "#version")
            {
                //GLSL Version string auslesen
                string glslVersionString = shaderVersion[1];
                if (string.IsNullOrWhiteSpace(glslVersionString) || glslVersionString.Length < 2)
                {
                    throw new GraphicsException("Could not determine GLSL version for shader " + shaderName);
                }

                string glslVersionStringMajor = glslVersionString.Substring(0, 1);
                string glslVersionStringMinor = glslVersionString.Substring(1, 1);

                int glslVersionMajor;
                int glslVersionMinor;
                if (!int.TryParse(glslVersionStringMajor, out glslVersionMajor) || !int.TryParse(glslVersionStringMinor, out glslVersionMinor))
                {
                    throw new GraphicsException("Could not determine GLSL version for shader " + shaderName);
                }

                Version shaderGLSLVersion = new Version(glslVersionMajor, glslVersionMinor);

                //Überprüfen, ob angegebene GLSL version vom Treiber unterstützt wird
                if (graphicsDevice.OpenGLCapabilities.GLSLVersion < shaderGLSLVersion)
                {
                    throw new PlatformNotSupportedException(string.Format("GLSL version of shader {0} is not supported by the driver.", shaderName));
                }
            }
            else
            {
                //Keine Version im Sourcecode angegeben => Version aus info benutzen

                if (string.IsNullOrWhiteSpace(info.Version))
                {
                    throw new ArgumentException("Neither the shader source nor info provide a version for the compiler");
                }

                code = "#version " + EnumConverter.ConvertToGLSLVersion(info.Version) + " core" + Environment.NewLine + code;
            }

            return(code);
        }
コード例 #5
0
        public IShader CompileShader(string name, ShaderCompileInfo vertexInfo, ShaderCompileInfo pixelInfo)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Name is empty or whitespace.", "name");
            }
            CheckCompileInfo(vertexInfo, "vertexInfo");
            CheckCompileInfo(pixelInfo, "pixelInfo");

            ShaderBytecode vertexCode, pixelCode;

            using (IncludeHandler include = IncludeHandler.CreateForShader(vertexInfo.File))
                vertexCode = ShaderBytecode.CompileFromFile(vertexInfo.File, vertexInfo.Function, vertexInfo.Version, ShaderFlags.None, EffectFlags.None, null, include);
            using (IncludeHandler include = IncludeHandler.CreateForShader(pixelInfo.File))
                pixelCode = ShaderBytecode.CompileFromFile(pixelInfo.File, pixelInfo.Function, pixelInfo.Version, ShaderFlags.None, EffectFlags.None, null, include);
            return(new Shader(graphicsDevice, name, vertexCode, pixelCode));
        }