Example #1
0
        /// <summary>
        /// Compiles the specified source GLSL450 code to a Vulkan SPIR-V binary.
        /// </summary>
        /// <param name="source">GLSL450 source code.</param>
        /// <param name="kind">Shader type.</param>
        /// <param name="entry">Main entry point function name.</param>
        /// <returns>An instance of <see cref="CompileResult"/> struct containing the compilation result.</returns>
        public CompileResult CompileToSpirV(string source, ShaderKind kind, string entry)
        {
            // - Executes extern library compilation and takes the result as a pointer
            IntPtr result = shaderc_compile_into_spv(pHandle, source, source.Length, kind, "", entry, IntPtr.Zero);

            // - Gets the compilation status and metadata
            CompilationStatus status = shaderc_result_get_compilation_status(result);
            uint errors   = shaderc_result_get_num_errors(result);
            uint warnings = shaderc_result_get_num_warnings(result);

            if (status != CompilationStatus.Success)
            {
                switch (status)
                {
                case CompilationStatus.InternalError:
                case CompilationStatus.CompilationError:
                    return(new CompileResult
                    {
                        WarningCount = warnings,
                        ErrorCount = errors,
                        Errors = shaderc_result_get_error_message(result),
                        Status = status,
                        Binary = new uint[] { }
                    });

                default:
                    throw new Exception("Unknow error");
                }
            }

            // - Gets the result's binary size
            uint binarySize = shaderc_result_get_length(result);

            // - Prepare output binary
            byte[] binary  = new byte[binarySize];
            uint[] outCode = new uint[binarySize / 4];

            // - Gets shader data from the compiler
            IntPtr bytes = shaderc_result_get_bytes(result);

            // - Copy shader binary to a managed array
            Marshal.Copy(bytes, binary, 0, (int)binarySize);
            Buffer.BlockCopy(binary, 0, outCode, 0, (int)binarySize);

            // - Release result resources
            shaderc_result_release(result);

            return(new CompileResult
            {
                WarningCount = warnings,
                ErrorCount = errors,
                Errors = "",
                Status = status,
                Binary = outCode
            });
        }
Example #2
0
 static void compile(Compiler comp, string path, ShaderKind shaderKind)
 {
     using (Result res = comp.Compile(path, shaderKind)) {
         Console.WriteLine($"{path}: {res.Status}");
         if (res.Status != Status.Success)
         {
             Console.WriteLine($"\terrs:{res.ErrorCount} warns:{res.WarningCount}");
             Console.WriteLine($"\t{res.ErrorMessage}");
         }
     }
 }
Example #3
0
        /// <summary>
        /// Compile the specified path, shaderKind and entry_point.
        /// </summary>
        /// <returns>The compile.</returns>
        /// <param name="path">Full path of the shader source file</param>
        /// <param name="shaderKind">If the shader kind is not set to a specified kind, but shaderc_glslc_infer_from_source,
        /// the compiler will try to deduce the shader kind from the source string and a failure in deducing will generate an error. Currently only
        /// #pragma annotation is supported. If the shader kind is set to one of the default shader kinds, the compiler will fall back to the default shader
        /// kind in case it failed to deduce the shader kind from source string.</param>
        /// <param name="entry_point">Entry point.</param>
        public Result Compile(string path, ShaderKind shaderKind, string entry_point = "main")
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("spirv file not found", path);
            }
            string source = "";

            using (StreamReader sr = new StreamReader(path))
                source = sr.ReadToEnd();
            return(Compile(source, path, shaderKind, entry_point));
        }
Example #4
0
        public void LoadFrom(YamlMappingNode mapping)
        {
            ID = mapping.GetNode("id").ToString();

            var kind = mapping.GetNode("kind").AsString();

            switch (kind)
            {
            case "source":
                Kind = ShaderKind.Source;
                ReadSourceKind(mapping);
                break;

            case "canvas":
                Kind = ShaderKind.Canvas;
                ReadCanvasKind(mapping);
                break;

            default:
                throw new InvalidOperationException($"Invalid shader kind: '{kind}'");
            }
        }
        void ISerializationHooks.AfterDeserialization()
        {
            switch (_rawKind)
            {
            case "source":
                Kind = ShaderKind.Source;
                if (path == null)
                {
                    throw new InvalidOperationException();
                }
                Source = IoCManager.Resolve <IResourceCache>().GetResource <ShaderSourceResource>(path);

                if (paramMapping != null)
                {
                    ShaderParams = new Dictionary <string, object>();
                    foreach (var item in paramMapping !)
                    {
                        var name = item.Key;
                        if (!Source.ParsedShader.Uniforms.TryGetValue(name, out var uniformDefinition))
                        {
                            Logger.ErrorS("shader", "Shader param '{0}' does not exist on shader '{1}'", name, path);
                            continue;
                        }

                        var value = _parseUniformValue(item.Value, uniformDefinition.Type.Type);
                        ShaderParams.Add(name, value);
                    }
                }
                break;

            case "canvas":
                Kind = ShaderKind.Canvas;
                var source = "";

                if (rawMode != null)
                {
                    switch (rawMode)
                    {
                    case "normal":
                        break;

                    case "unshaded":
                        source += "light_mode unshaded;\n";
                        break;

                    default:
                        throw new InvalidOperationException($"Invalid light mode: '{rawMode}'");
                    }
                }

                if (rawBlendMode != null)
                {
                    switch (rawBlendMode)
                    {
                    case "mix":
                        source += "blend_mode mix;\n";
                        break;

                    case "add":
                        source += "blend_mode add;\n";
                        break;

                    case "subtract":
                        source += "blend_mode subtract;\n";
                        break;

                    case "multiply":
                        source += "blend_mode multiply;\n";
                        break;

                    default:
                        throw new InvalidOperationException($"Invalid blend mode: '{rawBlendMode}'");
                    }
                }

                source += "void fragment() {\n    COLOR = zTexture(UV);\n}";

                var preset = ShaderParser.Parse(source, _resourceCache);
                CompiledCanvasShader = IoCManager.Resolve <IClydeInternal>().LoadShader(preset, $"canvas_preset_{ID}");
                break;

            default:
                throw new InvalidOperationException($"Invalid shader kind: '{_rawKind}'");
            }

            if (StencilDataHolder != null)
            {
                _stencilEnabled = true;
            }
        }
Example #6
0
 public Result Compile(string source, string fileName, ShaderKind shaderKind, string entryPoint = "main")
 {
     Includer?.Activate(Options);
     return(new Result(shaderc_compile_into_spv(_handle, source, (nuint)source.Length, (byte)shaderKind, fileName, entryPoint, Options.Handle)));
 }
Example #7
0
 public void SetHLSLRegisterSetAndBindingForStage(ShaderKind shaderKind, string reg, string set, string binding)
 {
     shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage(_handle, shaderKind, reg, set, binding);
 }
Example #8
0
 public void SetBindingBaseForStage(ShaderKind shaderKind, UniformKind kind, uint @base)
 {
     shaderc_compile_options_set_binding_base_for_stage(_handle, shaderKind, kind, @base);
 }
Example #9
0
 public static void shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage(nint options, ShaderKind shader_kind, string reg, string set, string binding)
 {
     shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage_(options, shader_kind, reg, set, binding);
 }
Example #10
0
 public static void shaderc_compile_options_set_binding_base_for_stage(nint options, ShaderKind shader_kind, UniformKind kind, uint _base)
 {
     shaderc_compile_options_set_binding_base_for_stage_(options, shader_kind, kind, _base);
 }
Example #11
0
 static extern IntPtr shaderc_compile_into_spv(IntPtr compiler, [MarshalAs(UnmanagedType.LPStr)] string source, int sourceLen, ShaderKind shader_kind, [MarshalAs(UnmanagedType.LPStr)] string input_file_name, [MarshalAs(UnmanagedType.LPStr)] string entry_point_name, IntPtr additional_options);
Example #12
0
 internal static extern void shaderc_compile_options_set_hlsl_register_set_and_binding_for_stage(IntPtr options, ShaderKind shader_kind, string reg, string set, string binding);
Example #13
0
 internal static extern void shaderc_compile_options_set_binding_base_for_stage(IntPtr options, ShaderKind shader_kind, UniformKind kind, UInt32 _base);
Example #14
0
 /// <summary>
 /// Takes a GLSL source string and the associated shader kind, input file
 /// name, compiles it according to the given additional_options.
 /// If the additional_options parameter is not null, then the compilation is modified by any options
 /// present.  May be safely called from multiple threads without explicit synchronization.
 /// If there was failure in allocating the compiler object, null will be returned.
 /// </summary>
 /// <returns>Compilation result</returns>
 /// <param name="source">the source code plain text</param>
 /// <param name="fileName">used as a tag to identify the source string in cases like emitting error messages. It
 /// doesn't have to be a 'file name'.</param>
 /// <param name="shaderKind">If the shader kind is not set to a specified kind, but shaderc_glslc_infer_from_source,
 /// the compiler will try to deduce the shader kind from the source string and a failure in deducing will generate an error. Currently only
 /// #pragma annotation is supported. If the shader kind is set to one of the default shader kinds, the compiler will fall back to the default shader
 /// kind in case it failed to deduce the shader kind from source string.</param>
 /// <param name="entry_point">defines the name of the entry point to associate with this GLSL source.</param>
 public Result Compile(string source, string fileName, ShaderKind shaderKind, string entry_point = "main")
 {
     return(new Result(NativeMethods.shaderc_compile_into_spv(handle, source, (ulong)source.Length, (byte)shaderKind, fileName, "main", Options.handle)));
 }