コード例 #1
0
 public ShaderInfo(VkShaderStageFlags _stageFlags, string _spirvPath, SpecializationInfo specializationInfo = null, string _entryPoint = "main")
 {
     StageFlags = _stageFlags;
     SpirvPath  = _spirvPath;
     EntryPoint = new FixedUtf8String(_entryPoint);
     this.SpecializationInfo = specializationInfo;
 }
コード例 #2
0
        public void PushConstants <T>(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, T[] data) where T : struct
        {
            GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);

            Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)(data.Length * Interop.SizeOf <T>()), handle.AddrOfPinnedObject());
            handle.Free();
        }
コード例 #3
0
ファイル: ShaderModule.cs プロジェクト: Gaiaxis/SharpGame
        public ShaderModule(VkShaderStageFlags stage, byte[] code)
        {
            Stage = stage;
            Code  = code;

            Build();
        }
コード例 #4
0
        public void PushConstants(CommandBuffer cmd)
        {
            int size = maxPushConstRange - minPushConstRange;

            if (size > 0)
            {
                VkShaderStageFlags shaderStage = VkShaderStageFlags.None;
                int minRange    = minPushConstRange;
                int currentSize = 0;
                for (int i = 0; i < pipelineLayout.PushConstant.Length; i++)
                {
                    if (i == 0)
                    {
                        shaderStage = pipelineLayout.PushConstant[0].stageFlags;
                    }

                    currentSize += pipelineLayout.PushConstant[i].size;

                    if ((pipelineLayout.PushConstant[i].stageFlags != shaderStage) || (i == pipelineLayout.PushConstant.Length - 1))
                    {
                        cmd.PushConstants(pipelineLayout, shaderStage, minRange, currentSize, pushConstBuffer + minRange);

                        shaderStage = pipelineLayout.PushConstant[i].stageFlags;
                        minRange   += currentSize;
                        currentSize = 0;
                    }
                }
            }
        }
コード例 #5
0
ファイル: VkFormats.cs プロジェクト: suprafun/veldrid
        internal static VkShaderStageFlags VdToVkShaderStages(ShaderStages stage)
        {
            VkShaderStageFlags ret = VkShaderStageFlags.None;

            if ((stage & ShaderStages.Vertex) == ShaderStages.Vertex)
            {
                ret |= VkShaderStageFlags.Vertex;
            }

            if ((stage & ShaderStages.Geometry) == ShaderStages.Geometry)
            {
                ret |= VkShaderStageFlags.Geometry;
            }

            if ((stage & ShaderStages.TessellationControl) == ShaderStages.TessellationControl)
            {
                ret |= VkShaderStageFlags.TessellationControl;
            }

            if ((stage & ShaderStages.TessellationEvaluation) == ShaderStages.TessellationEvaluation)
            {
                ret |= VkShaderStageFlags.TessellationEvaluation;
            }

            if ((stage & ShaderStages.Fragment) == ShaderStages.Fragment)
            {
                ret |= VkShaderStageFlags.Fragment;
            }

            return(ret);
        }
コード例 #6
0
        public override void RenderNode(CommandBuffer cmd, PipelineLayout pipelineLayout, Node node, Matrix4x4 currentTransform, bool shadowPass = false)
        {
            Matrix4x4          localMat = node.localMatrix * currentTransform;
            VkShaderStageFlags matStage = shadowPass ? VkShaderStageFlags.Geometry : VkShaderStageFlags.Vertex;

            cmd.PushConstant(pipelineLayout, matStage, localMat);

            if (node.Mesh != null)
            {
                foreach (Primitive p in node.Mesh.Primitives)
                {
                    if (!shadowPass)
                    {
                        cmd.PushConstant(pipelineLayout, VkShaderStageFlags.Fragment, (int)p.material, (uint)Marshal.SizeOf <Matrix4x4> ());
                        if (descriptorSets[p.material] != null)
                        {
                            cmd.BindDescriptorSet(pipelineLayout, descriptorSets[p.material], 2);
                        }
                    }
                    cmd.DrawIndexed(p.indexCount, 1, p.indexBase, p.vertexBase, 0);
                }
            }
            if (node.Children == null)
            {
                return;
            }
            foreach (Node child in node.Children)
            {
                RenderNode(cmd, pipelineLayout, child, localMat, shadowPass);
            }
        }
コード例 #7
0
 ShaderModule LoadShaderModelFromFile(VkShaderStageFlags shaderStage, string file, string[] defs)
 {
     using (File stream = FileSystem.Instance.GetFile(file))
     {
         return(LoadShaderModel(shaderStage, stream.ReadAllText(), defs));
     }
 }
コード例 #8
0
 public static extern void CmdPushConstants(
     VkCommandBuffer commandBuffer,
     VkPipelineLayout layout,
     VkShaderStageFlags stageFlags,
     uint offset,
     uint size,
     IntPtr pValues
     );
コード例 #9
0
 public VkDescriptorSetLayoutBinding(uint _binding, VkShaderStageFlags _stageFlags, VkDescriptorType _descriptorType, uint _descriptorCount = 1)
 {
     binding = _binding;
     this._descriptorType = (int)_descriptorType;
     descriptorCount      = _descriptorCount;
     this._stageFlags     = (uint)_stageFlags;
     _pImmutableSamplers  = IntPtr.Zero;
 }
コード例 #10
0
ファイル: Structs.cs プロジェクト: arakis/vk.net
 public VkDescriptorSetLayoutBinding(uint _binding, VkShaderStageFlags _stageFlags, VkDescriptorType _descriptorType, uint _descriptorCount = 1)
 {
     binding            = _binding;
     descriptorType     = _descriptorType;
     descriptorCount    = _descriptorCount;
     stageFlags         = _stageFlags;
     pImmutableSamplers = (IntPtr)0;
 }
コード例 #11
0
 public DescriptorSetLayoutBinding(uint binding, VkDescriptorType type, VkShaderStageFlags stageFlags, uint descriptorCount = 1)
 {
     descriptorType       = type;
     this.binding         = binding;
     this.descriptorCount = descriptorCount;
     this.stageFlags      = stageFlags;
     pImmutableSamplers   = null;
 }
コード例 #12
0
 public void PushConstants <T>(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, T data) where T : struct
 {
     unsafe
     {
         //can't take address of generic variable in c#
         IntPtr native = (IntPtr)Unsafe.AsPointer(ref data);
         Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)Interop.SizeOf <T>(), native);
     }
 }
コード例 #13
0
        /// <summary>
        /// Create a new ShaderInfo object by providing directly a VkShaderModule. Note
        /// that this module will not be own by this ShaderInfo, and so will not be
        /// destroyed on Dispose.
        /// </summary>
        public ShaderInfo(VkShaderStageFlags stageFlags, VkShaderModule module, SpecializationInfo specializationInfo = null, string entryPoint = "main")
        {
            EntryPoint = new FixedUtf8String(entryPoint);

            info.stage  = stageFlags;
            info.pName  = EntryPoint;
            info.module = module;
            info.pSpecializationInfo = (specializationInfo == null) ? IntPtr.Zero : specializationInfo.InfosPtr;
        }
コード例 #14
0
 public void PushConstants <T>(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, List <T> data) where T : struct
 {
     unsafe
     {
         int   size   = (int)Interop.SizeOf <T>();
         byte *native = stackalloc byte[size * data.Count];
         Interop.Copy(data, (IntPtr)native);
         Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)(size * data.Count), (IntPtr)native);
     }
 }
コード例 #15
0
 public void PushConstants(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, byte[] data)
 {
     unsafe
     {
         fixed(byte *ptr = data)
         {
             Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, (uint)data.Length, (IntPtr)ptr);
         }
     }
 }
コード例 #16
0
ファイル: ShaderUtil.cs プロジェクト: Gaiaxis/SharpGame
 public static ShaderModule CreateShaderModule(VkShaderStageFlags stage, string fileName, string funcName = "main")
 {
     using (File stream = FileSystem.Instance.GetFile(fileName))
     {
         var source       = stream.ReadAllText();
         var shaderModule = ShaderReader.CreateShaderModule(stage, source, fileName);
         shaderModule.Build();
         return(shaderModule);
     }
 }
コード例 #17
0
        protected VkPipelineShaderStageCreateInfo loadShader(string fileName, VkShaderStageFlags stage)
        {
            VkPipelineShaderStageCreateInfo shaderStage = VkPipelineShaderStageCreateInfo.New();

            shaderStage.stage  = stage;
            shaderStage.module = Tools.loadShader(fileName, device, stage);
            shaderStage.pName  = Strings.main; // todo : make param
            Debug.Assert(shaderStage.module.Handle != 0);
            shaderModules.Add(shaderStage.module);
            return(shaderStage);
        }
コード例 #18
0
        public static VkPushConstantRange pushConstantRange(
            VkShaderStageFlags stageFlags,
            uint size,
            uint offset)
        {
            VkPushConstantRange pushConstantRange = new VkPushConstantRange();

            pushConstantRange.stageFlags = stageFlags;
            pushConstantRange.offset     = offset;
            pushConstantRange.size       = size;
            return(pushConstantRange);
        }
コード例 #19
0
 public static ShaderInfo CreateShaderInfo(this shaderc.Compiler comp, Device dev, string shaderPath, shaderc.ShaderKind shaderKind,
                                           SpecializationInfo specializationInfo = null, string entryPoint = "main")
 {
     using (shaderc.Result res = comp.Compile(shaderPath, shaderKind)) {
         if (res.Status != shaderc.Status.Success)
         {
             throw new Exception($"Shaderc compilation failure: {res.ErrorMessage}");
         }
         VkShaderStageFlags stageFlags = Utils.ShaderKindToStageFlag(shaderKind);
         return(new ShaderInfo(dev, stageFlags, res.CodePointer, (UIntPtr)res.CodeLength, specializationInfo, entryPoint));
     }
 }
コード例 #20
0
 public DescriptorBindingInfo(
     uint index,
     VkDescriptorType descriptorType,
     uint descriptorCounts,
     VkShaderStageFlags shaderStageFlags
     )
 {
     Index            = index;
     DescriptorType   = descriptorType;
     DescriptorCounts = descriptorCounts;
     ShaderStageFlags = shaderStageFlags;
 }
コード例 #21
0
ファイル: Initializers.cs プロジェクト: gomson/vk
        public static VkDescriptorSetLayoutBinding descriptorSetLayoutBinding(
            VkDescriptorType type,
            VkShaderStageFlags stageFlags,
            uint binding,
            uint descriptorCount = 1)
        {
            VkDescriptorSetLayoutBinding setLayoutBinding = new VkDescriptorSetLayoutBinding();

            setLayoutBinding.descriptorType  = type;
            setLayoutBinding.stageFlags      = stageFlags;
            setLayoutBinding.binding         = binding;
            setLayoutBinding.descriptorCount = descriptorCount;
            return(setLayoutBinding);
        }
コード例 #22
0
ファイル: ShaderModule.cs プロジェクト: Gaiaxis/SharpGame
        public ShaderModule(VkShaderStageFlags stage, IntPtr CodePointer, uint CodeLength)
        {
            Stage = stage;
            unsafe
            {
                var sm = new VkShaderModuleCreateInfo
                {
                    sType    = VkStructureType.ShaderModuleCreateInfo,
                    pCode    = (uint *)CodePointer,
                    codeSize = new UIntPtr(CodeLength),
                };

                handle = Device.CreateShaderModule(ref sm);
            }
        }
コード例 #23
0
        private VkShaderStageFlags MapStageFlags(ShaderStages stages)
        {
            VkShaderStageFlags flags = VkShaderStageFlags.None;

            if ((stages & ShaderStages.Vertex) == ShaderStages.Vertex)
            {
                flags |= VkShaderStageFlags.Vertex;
            }
            if ((stages & ShaderStages.Geometry) == ShaderStages.Geometry)
            {
                flags |= VkShaderStageFlags.Geometry;
            }
            if ((stages & ShaderStages.Fragment) == ShaderStages.Fragment)
            {
                flags |= VkShaderStageFlags.Fragment;
            }

            return(flags);
        }
コード例 #24
0
ファイル: Structs.cs プロジェクト: LibVega/VVK
 public VkShaderStatisticsInfoAMD(
     VkShaderStageFlags shaderStageMask     = default,
     VkShaderResourceUsageAMD resourceUsage = default,
     uint numPhysicalVgprs       = default,
     uint numPhysicalSgprs       = default,
     uint numAvailableVgprs      = default,
     uint numAvailableSgprs      = default,
     uint computeWorkGroupSize_0 = default,
     uint computeWorkGroupSize_1 = default,
     uint computeWorkGroupSize_2 = default
     )
 {
     ShaderStageMask         = shaderStageMask;
     ResourceUsage           = resourceUsage;
     NumPhysicalVgprs        = numPhysicalVgprs;
     NumPhysicalSgprs        = numPhysicalSgprs;
     NumAvailableVgprs       = numAvailableVgprs;
     NumAvailableSgprs       = numAvailableSgprs;
     ComputeWorkGroupSize[0] = computeWorkGroupSize_0;
     ComputeWorkGroupSize[1] = computeWorkGroupSize_1;
     ComputeWorkGroupSize[2] = computeWorkGroupSize_2;
 }
コード例 #25
0
ファイル: Tools.cs プロジェクト: bitzhuwei/vk.other
        public static VkShaderModule loadShader(string fileName, VkDevice device, VkShaderStageFlags stage)
        {
            using (var fs = File.OpenRead(fileName))
            {
                var length = fs.Length;
            }
            byte[] shaderCode = File.ReadAllBytes(fileName);
            ulong  shaderSize = (ulong)shaderCode.Length;

            fixed(byte *scPtr = shaderCode)
            {
                // Create a new shader module that will be used for Pipeline creation
                VkShaderModuleCreateInfo moduleCreateInfo = VkShaderModuleCreateInfo.New();

                moduleCreateInfo.codeSize = new UIntPtr(shaderSize);
                moduleCreateInfo.pCode    = (uint *)scPtr;

                Util.CheckResult(vkCreateShaderModule(device, ref moduleCreateInfo, null, out VkShaderModule shaderModule));

                return(shaderModule);
            }
        }
コード例 #26
0
ファイル: VkFormats.cs プロジェクト: shawn8777/veldrid
        internal static VkShaderStageFlags VdToVkShaderStages(ShaderStages stage)
        {
            VkShaderStageFlags ret = VkShaderStageFlags.None;

            if ((stage & ShaderStages.Vertex) == ShaderStages.Vertex)
                ret |= VkShaderStageFlags.Vertex;

            if ((stage & ShaderStages.Geometry) == ShaderStages.Geometry)
                ret |= VkShaderStageFlags.Geometry;

            if ((stage & ShaderStages.TessellationControl) == ShaderStages.TessellationControl)
                ret |= VkShaderStageFlags.TessellationControl;

            if ((stage & ShaderStages.TessellationEvaluation) == ShaderStages.TessellationEvaluation)
                ret |= VkShaderStageFlags.TessellationEvaluation;

            if ((stage & ShaderStages.Fragment) == ShaderStages.Fragment)
                ret |= VkShaderStageFlags.Fragment;

            if ((stage & ShaderStages.Compute) == ShaderStages.Compute)
                ret |= VkShaderStageFlags.Compute;

            return ret;
        }
コード例 #27
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);
        }
コード例 #28
0
        ShaderModule LoadShaderModel(VkShaderStageFlags shaderStage, string code, string[] defs)
        {
            List <string> saveLines   = new List <string>();
            string        ver         = "";
            string        includeFile = "";
            StringReader  reader      = new StringReader(code);

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }

                string str = line.TrimStart();
                if (str.StartsWith("#version"))
                {
                    ver = line;
                    break;
                }

                if (str.StartsWith("#include"))
                {
                    var incFile = str.Substring(8);
                    if (string.IsNullOrEmpty(includeFile))
                    {
                        includeFile = incFile;
                    }

                    if (ReadInclude(incFile, saveLines, out ver))
                    {
                        break;
                    }
                }
                else
                {
                    saveLines.Add(line);
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine(ver);

            if (defs != null)
            {
                foreach (var def in defs)
                {
                    sb.Append("#define ").Append(def);
                }
            }

            foreach (var line in saveLines)
            {
                sb.AppendLine(line);
            }

            sb.Append(reader.ReadToEnd());

            code = sb.ToString();

            return(CreateShaderModule(shaderStage, code, includeFile));
        }
コード例 #29
0
 internal extern static void vkCmdPushConstants(IntPtr commandBuffer, UInt64 layout, VkShaderStageFlags stageFlags, UInt32 offset, UInt32 size, IntPtr pValues);
コード例 #30
0
 public void PushConstants(PipelineLayout layout, VkShaderStageFlags stageFlags, uint offset, uint size, IntPtr data)
 {
     Device.Commands.cmdPushConstants(commandBuffer, layout.Native, stageFlags, offset, size, data);
 }