public static async Task <ShaderBytecode> CompileFromFileAsync(string hlslFile, string entryPoint, string profile, ShaderMacro[] defines = null)
        {
            if (!Path.IsPathRooted(hlslFile))
            {
                hlslFile = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, hlslFile);
            }

            CompilationResult result = null;

            await Task.Run(() =>
            {
                var shaderSource = SharpDX.IO.NativeFile.ReadAllText(hlslFile);

                // Compile the shader file
                ShaderFlags flags = ShaderFlags.None;
#if DEBUG
                flags |= ShaderFlags.Debug | ShaderFlags.SkipOptimization;
#endif
                var includeHandler = new HLSLFileIncludeHandler(Path.GetDirectoryName(hlslFile));
                result             = ShaderBytecode.Compile(shaderSource, entryPoint, profile, flags, EffectFlags.None, defines, includeHandler, Path.GetFileName(hlslFile));

                if (!String.IsNullOrEmpty(result.Message))
                {
                    throw new CompilationException(result.ResultCode, result.Message);
                }
            });

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Compile the HLSL file using the provided <paramref name="entryPoint"/>, shader <paramref name="profile"/> and optionally conditional <paramref name="defines"/>
        /// </summary>
        /// <param name="hlslFile">Absolute path to HLSL file, or path relative to application installation location</param>
        /// <param name="entryPoint">Shader function name e.g. VSMain</param>
        /// <param name="profile">Shader profile, e.g. vs_5_0</param>
        /// <param name="defines">An optional list of conditional defines.</param>
        /// <returns>The compiled ShaderBytecode</returns>
        /// <exception cref="CompilationException">Thrown if the compilation failed</exception>
        public static ShaderBytecode CompileFromFile(string hlslFile, string entryPoint, string profile, ShaderMacro[] defines = null)
        {
            if (!Path.IsPathRooted(hlslFile))
            {
                hlslFile = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), hlslFile);
            }
            var shaderSource         = SharpDX.IO.NativeFile.ReadAllText(hlslFile);
            CompilationResult result = null;

            // Compile the shader file
            ShaderFlags flags = ShaderFlags.None;

#if DEBUG
            flags |= ShaderFlags.Debug | ShaderFlags.SkipOptimization;
#endif
            var includeHandler = new HLSLFileIncludeHandler(Path.GetDirectoryName(hlslFile));
            result = ShaderBytecode.Compile(shaderSource, entryPoint, profile, flags, EffectFlags.None, defines, includeHandler, Path.GetFileName(hlslFile));

            if (result.ResultCode.Failure)
            {
                throw new CompilationException(result.ResultCode, result.Message);
            }

            return(result);
        }
        public static async Task<ShaderBytecode> CompileFromFileAsync(string hlslFile, string entryPoint, string profile, ShaderMacro[] defines = null)
        {
            if (!Path.IsPathRooted(hlslFile))
                hlslFile = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, hlslFile);

            CompilationResult result = null;
            
            await Task.Run(() =>
            {
                var shaderSource = SharpDX.IO.NativeFile.ReadAllText(hlslFile);

                // Compile the shader file
                ShaderFlags flags = ShaderFlags.None;
#if DEBUG
                flags |= ShaderFlags.Debug | ShaderFlags.SkipOptimization;
#endif
                var includeHandler = new HLSLFileIncludeHandler(Path.GetDirectoryName(hlslFile));
                result = ShaderBytecode.Compile(shaderSource, entryPoint, profile, flags, EffectFlags.None, defines, includeHandler, Path.GetFileName(hlslFile));

                if (!String.IsNullOrEmpty(result.Message))
                    throw new CompilationException(result.ResultCode, result.Message);
            });

            return result;
        }
        /// <summary>
        /// Compile the HLSL file using the provided <paramref name="entryPoint"/>, shader <paramref name="profile"/> and optionally conditional <paramref name="defines"/>
        /// </summary>
        /// <param name="hlslFile">Absolute path to HLSL file, or path relative to application installation location</param>
        /// <param name="entryPoint">Shader function name e.g. VSMain</param>
        /// <param name="profile">Shader profile, e.g. vs_5_0</param>
        /// <param name="defines">An optional list of conditional defines.</param>
        /// <returns>The compiled ShaderBytecode</returns>
        /// <exception cref="CompilationException">Thrown if the compilation failed</exception>
        public static ShaderBytecode CompileFromFile(string hlslFile, string entryPoint, string profile, ShaderMacro[] defines = null)
        {
            if (!Path.IsPathRooted(hlslFile))
                hlslFile = Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), hlslFile);
            var shaderSource = SharpDX.IO.NativeFile.ReadAllText(hlslFile);
            CompilationResult result = null;

            // Compile the shader file
            ShaderFlags flags = ShaderFlags.None;
            #if DEBUG
            flags |= ShaderFlags.Debug | ShaderFlags.SkipOptimization;
            #endif
            var includeHandler = new HLSLFileIncludeHandler(Path.GetDirectoryName(hlslFile));
            result = ShaderBytecode.Compile(shaderSource, entryPoint, profile, flags, EffectFlags.None, defines, includeHandler, Path.GetFileName(hlslFile));

            if (result.ResultCode.Failure)
                throw new CompilationException(result.ResultCode, result.Message);

            return result;
        }
        // Compile compute shader from file and add to computeShaders dictionary
        public void CompileComputeShader(string csFunction, string csFile = @"Shaders\ImageProcessingCS.hlsl", string csVersion = "cs_5_0")
        {
            var shaderFlags = ShaderFlags.None;
            #if DEBUG
            shaderFlags = ShaderFlags.Debug | ShaderFlags.SkipOptimization;
            #endif
            SharpDX.Direct3D.ShaderMacro[] defines = new[] {
                new SharpDX.Direct3D.ShaderMacro("THREADSX", ThreadsX),
                new SharpDX.Direct3D.ShaderMacro("THREADSY", ThreadsY),
            };

            // Use our HLSL file include handler to resolve #include directives in the HLSL source
            var includeHandler = new HLSLFileIncludeHandler(System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), "Shaders"));

            using (var bytecode = ShaderBytecode.CompileFromFile(csFile, csFunction, csVersion, shaderFlags, EffectFlags.None, defines, includeHandler))
            {
                computeShaders[csFunction] = ToDispose(new ComputeShader(this.DeviceManager.Direct3DDevice, bytecode));
            }
        }