Пример #1
0
        private SharpVulkan.ShaderModule CreateShaderModule(IntPtr program, ShaderCompiler.Stage stage)
        {
            var code       = ShaderCompiler.GetSpv(program, stage);
            var codeSize   = ShaderCompiler.GetSpvSize(program, stage);
            var createInfo = new SharpVulkan.ShaderModuleCreateInfo {
                StructureType = SharpVulkan.StructureType.ShaderModuleCreateInfo,
                CodeSize      = codeSize,
                Code          = code
            };

            return(context.Device.CreateShaderModule(ref createInfo));
        }
Пример #2
0
        private SharpVulkan.ShaderStageFlags GetVKShaderStageFlags(ShaderCompiler.Stage stage)
        {
            switch (stage)
            {
            case ShaderCompiler.Stage.Vertex:
                return(SharpVulkan.ShaderStageFlags.Vertex);

            case ShaderCompiler.Stage.Fragment:
                return(SharpVulkan.ShaderStageFlags.Fragment);

            default:
                throw new ArgumentException(nameof(stage));
            }
        }
Пример #3
0
        private static ShaderStageMask ConvertShaderStage(ShaderCompiler.Stage stage)
        {
            switch (stage)
            {
            case ShaderCompiler.Stage.Vertex:
                return(ShaderStageMask.Vertex);

            case ShaderCompiler.Stage.Fragment:
                return(ShaderStageMask.Fragment);

            default:
                throw new ArgumentException(nameof(stage));
            }
        }
Пример #4
0
        public static ShaderModule CreateShaderModule(VkShaderStageFlags shaderStage, string code, string includeFile)
        {
#if SHARP_SHADER_COMPILER
            ShaderCompiler.Stage stage = ShaderCompiler.Stage.Vertex;
            switch (shaderStage)
            {
            case VkShaderStageFlags.Vertex:
                stage = ShaderCompiler.Stage.Vertex;
                break;

            case VkShaderStageFlags.Fragment:
                stage = ShaderCompiler.Stage.Fragment;
                break;

            case VkShaderStageFlags.Geometry:
                stage = ShaderCompiler.Stage.Geometry;
                break;

            case VkShaderStageFlags.Compute:
                stage = ShaderCompiler.Stage.Compute;
                break;

            case VkShaderStageFlags.TessellationControl:
                stage = ShaderCompiler.Stage.TessControl;
                break;

            case VkShaderStageFlags.TessellationEvaluation:
                stage = ShaderCompiler.Stage.TessEvaluation;
                break;
            }

            var c = new ShaderCompiler();
            var o = new CompileOptions(IncludeHandler)
            {
                Language      = CompileOptions.InputLanguage.GLSL,
                Target        = CompileOptions.Environment.Vulkan,
                GenerateDebug = true,
            };

            var r = c.Preprocess(code, stage, o, "main");
            if (r.NumberOfErrors > 0)
            {
                Log.Error(r.ErrorMessage);
            }

            if (r.CompileStatus != CompileResult.Status.Success)
            {
                return(null);
            }

            var source = r.GetString();

            var res = c.Compile(source, stage, o, includeFile, "main");
            if (res.NumberOfErrors > 0)
            {
                Log.Error(res.ErrorMessage);
            }

            if (res.CompileStatus != CompileResult.Status.Success)
            {
                return(null);
            }

            uint len          = res.GetCode(out var codePointer);
            var  refl         = ReflectionShaderModule(source, codePointer, len / 4);
            var  shaderModule = new ShaderModule(shaderStage, codePointer, len)
            {
                ShaderReflection = refl
            };
#else
            shaderc.ShaderKind stage = shaderc.ShaderKind.VertexShader;
            switch (shaderStage)
            {
            case VkShaderStageFlags.Vertex:
                stage = shaderc.ShaderKind.VertexShader;
                break;

            case VkShaderStageFlags.Fragment:
                stage = shaderc.ShaderKind.FragmentShader;
                break;

            case VkShaderStageFlags.Geometry:
                stage = shaderc.ShaderKind.GeometryShader;
                break;

            case VkShaderStageFlags.Compute:
                stage = shaderc.ShaderKind.ComputeShader;
                break;

            case VkShaderStageFlags.TessControl:
                stage = shaderc.ShaderKind.TessControlShader;
                break;

            case VkShaderStageFlags.TessEvaluation:
                stage = shaderc.ShaderKind.TessEvaluationShader;
                break;
            }

            shaderc.Compiler.GetSpvVersion(out shaderc.SpirVVersion version, out uint revision);

            shaderc.Options o = new shaderc.Options() //new ShadercOptions()
            {
                //SourceLanguage = shaderc.SourceLanguage.Glsl,
                //TargetSpirVVersion = new shaderc.SpirVVersion(1,5),
                //Optimization = shaderc.OptimizationLevel.Performance
            };

            o.EnableDebugInfo();

            o.IncludeDirectories.Add(FileSystem.WorkSpace + "data/shaders/common");
            o.IncludeDirectories.Add(FileSystem.WorkSpace + "data/shaders/glsl");

            var c   = new shaderc.Compiler(o);
            var res = c.Compile(code, includeFile, stage);

            if (res.ErrorCount > 0)
            {
                Log.Error(res.ErrorMessage);
            }

            if (res.Status != Status.Success)
            {
                return(null);
            }

            var refl         = ReflectionShaderModule(code, res.CodePointer, res.CodeLength / 4);
            var shaderModule = new ShaderModule(shaderStage, res.CodePointer, res.CodeLength)
            {
                ShaderReflection = refl
            };
#endif


            return(shaderModule);
        }