示例#1
0
        public DescriptorSet CreateDescriptorSet(int setIndex, int binding, BufferviewWithMemory bufferviewWithMemory)
        {
            ValidateDiscriporPool();

            DescriptorSetLayout descriptorSetLayout = DescriptorSetLayouts[setIndex];
            ShaderUniformSet    shaderUniformSet    = ShaderUniformSets[setIndex];

            DescriptorSet descriptorSet = VContext.Instance.device.AllocateDescriptorSets(
                new DescriptorSetAllocateInfo()
            {
                DescriptorPool = VContext.Instance.descriptorPool,
                SetLayouts     = new DescriptorSetLayout[]
                {
                    descriptorSetLayout
                }
            }
                ).First();

            ShaderUniform shaderUniformWithCorrectBinding = shaderUniformSet.ShaderUniforms.Where(su => su.Binding == binding).First();

            VContext.Instance.device.UpdateDescriptorSet
            (
                new WriteDescriptorSet()
            {
                DescriptorType = shaderUniformWithCorrectBinding.DescriptorType,
                DstBinding     = (uint)shaderUniformWithCorrectBinding.Binding,
                DstSet         = descriptorSet,
                BufferInfo     = new DescriptorBufferInfo[]
                {
                    new DescriptorBufferInfo()
                    {
                        Buffer = bufferviewWithMemory.Buffer,
                        Offset = 0,
                        Range  = bufferviewWithMemory.Size,
                    }
                },
                TexelBufferView = new BufferView[]
                {
                    bufferviewWithMemory.Bufferview
                },
            },
                null
            );

            return(descriptorSet);
        }
示例#2
0
        public DescriptorSet CreateDescriptorSet(int setIndex, int binding, ImageView imageView, ImageLayout imageLayout = ImageLayout.ColorAttachmentOptimal)
        {
            ValidateDiscriporPool();

            DescriptorSetLayout descriptorSetLayout = DescriptorSetLayouts[setIndex];
            ShaderUniformSet    shaderUniformSet    = ShaderUniformSets[setIndex];

            DescriptorSet descriptorSet = VContext.Instance.device.AllocateDescriptorSets
                                          (
                new DescriptorSetAllocateInfo()
            {
                DescriptorPool     = VContext.Instance.descriptorPool,
                DescriptorSetCount = 1,
                SetLayouts         = new DescriptorSetLayout[]
                {
                    descriptorSetLayout
                }
            }
                                          ).First();


            ShaderUniform shaderUniformWithCorrectBinding = shaderUniformSet.ShaderUniforms.Where(su => su.Binding == binding).First();

            VContext.Instance.device.UpdateDescriptorSet(
                new WriteDescriptorSet()
            {
                DescriptorType = shaderUniformWithCorrectBinding.DescriptorType,
                DstBinding     = (uint)shaderUniformWithCorrectBinding.Binding,
                DstSet         = descriptorSet,

                ImageInfo = new DescriptorImageInfo[]
                {
                    new DescriptorImageInfo()
                    {
                        ImageLayout = imageLayout,
                        ImageView   = imageView,
                        Sampler     = VContext.Instance.sampler
                    }
                }
            },
                null
                );

            return(descriptorSet);
        }
示例#3
0
        public Dictionary <int, ShaderUniformSet> GetDescriptorSetLayouts()
        {
            Dictionary <int, ShaderUniformSet> sets = new Dictionary <int, ShaderUniformSet>();

            IEnumerable <DescriptorSegment> descriptorSegments = _segmentCollection.GetSegments <DescriptorSegment>();

            for (int i = 0; i < descriptorSegments.Count(); i++)
            {
            }

            /*
             * if (!sets.ContainsKey(set))
             * {
             *  sets.Add(set, new ShaderUniformSet(set));
             * }
             * sets[set].ShaderUniforms.Add(shaderUniform);*/

            MatchCollection matches = Regex.Matches(code, @"layout *\( *(( *std140 *| *set *= *\d*) *, *){0,1} *binding *= *\d*\) *(readonly){0,1} *(buffer|uniform) ");

            for (int i = 0; i < matches.Count; i++)
            {
                Match match = matches[i];

                int    set      = 0;
                string theMatch = match.ToString();
                if (theMatch.Contains("set"))
                {
                    set = int.Parse(theMatch.Substring(theMatch.IndexOf("set"), theMatch.IndexOf(",") - theMatch.IndexOf("set")).Replace("set", string.Empty).Replace("=", string.Empty).Trim());
                }
                int binding = int.Parse(theMatch.Substring(theMatch.IndexOf("binding"), theMatch.IndexOf(")") - theMatch.IndexOf("binding")).Replace("binding", string.Empty).Replace("=", string.Empty).Trim());

                string bufferOrUniformSegment = Regex.Match(theMatch, @"(buffer |uniform )").ToString();

                string codeSegment = SegmentParser.GetWholeCodeSegment(code, match.Index);

                DescriptorType descriptorType;
                string         name;

                uint   size = 0;
                string segmentAfterBufferOrUniform = codeSegment.Substring(codeSegment.IndexOf(bufferOrUniformSegment, StringComparison.InvariantCultureIgnoreCase) + bufferOrUniformSegment.Length);
                if (codeSegment.Contains("{"))
                {
                    descriptorType = Constants.GlslToCustomDescriptorTypeConverter.Convert(bufferOrUniformSegment.Trim());

                    int openingIndex = segmentAfterBufferOrUniform.IndexOf("{");
                    int closingIndex = segmentAfterBufferOrUniform.IndexOf("}");

                    name = Util.KillDuplicateWhiteSpaces(segmentAfterBufferOrUniform.Substring(0, openingIndex));

                    string pushconstantSegment  = segmentAfterBufferOrUniform.Substring(openingIndex + 1, closingIndex - openingIndex - 1);
                    IEnumerable <string> fields = pushconstantSegment.Split(';').Select(s => s.Trim());

                    List <ShaderVariable> variables = ShaderVariableParser.Parse(fields);

                    size = (uint)variables.Sum(v => v.Size);
                }
                else
                {
                    string[] typeAndName = Util.KillDuplicateWhiteSpaces(segmentAfterBufferOrUniform).Split(' ');

                    string glslType = typeAndName[0];
                    descriptorType = Constants.GlslToDescriptorType[glslType];

                    name = typeAndName[1];
                }


                ShaderUniform shaderUniform = new ShaderUniform()
                {
                    Name           = name,
                    Binding        = binding,
                    DescriptorType = descriptorType,
                    StageFlags     = ShaderStage,
                    Size           = size,
                };


                if (!sets.ContainsKey(set))
                {
                    sets.Add(set, new ShaderUniformSet(set));
                }
                sets[set].ShaderUniforms.Add(shaderUniform);
            }

            matches = Regex.Matches(code, @"layout *\(.*\) *uniform *image2D *");
            for (int i = 0; i < matches.Count; i++)
            {
                Match match = matches[i];

                int    set      = 0;
                string theMatch = match.ToString();
                if (theMatch.Contains("set"))
                {
                    set = int.Parse(theMatch.Substring(theMatch.IndexOf("set"), theMatch.IndexOf(",") - theMatch.IndexOf("set")).Replace("set", string.Empty).Replace("=", string.Empty).Trim());
                }
                int binding = int.Parse(theMatch.Substring(theMatch.IndexOf("binding"), Math.Min(theMatch.LastIndexOf(","), theMatch.IndexOf(")")) - theMatch.IndexOf("binding")).Replace("binding", string.Empty).Replace("=", string.Empty).Trim());

                string bufferOrUniformSegment = Regex.Match(theMatch, @"(buffer |uniform )").ToString();

                string codeSegment = SegmentParser.GetWholeCodeSegment(code, match.Index);

                DescriptorType descriptorType;
                string         name;

                string segmentAfterBufferOrUniform = codeSegment.Substring(codeSegment.IndexOf(bufferOrUniformSegment, StringComparison.InvariantCultureIgnoreCase) + bufferOrUniformSegment.Length);
                if (codeSegment.Contains("{"))
                {
                    descriptorType = Constants.GlslToCustomDescriptorTypeConverter.Convert(bufferOrUniformSegment.Trim());

                    int openingIndex = segmentAfterBufferOrUniform.IndexOf("{");
                    int closingIndex = segmentAfterBufferOrUniform.IndexOf("}");

                    name = Util.KillDuplicateWhiteSpaces(segmentAfterBufferOrUniform.Substring(0, openingIndex));

                    string pushconstantSegment  = segmentAfterBufferOrUniform.Substring(openingIndex + 1, closingIndex - openingIndex - 1);
                    IEnumerable <string> fields = pushconstantSegment.Split(';').Select(s => s.Trim());

                    List <ShaderVariable> variables = ShaderVariableParser.Parse(fields);
                }
                else
                {
                    string[] typeAndName = Util.KillDuplicateWhiteSpaces(segmentAfterBufferOrUniform).Split(' ');

                    string glslType = typeAndName[0];
                    descriptorType = Constants.GlslToDescriptorType[glslType];

                    name = typeAndName[1];
                }


                ShaderUniform shaderUniform = new ShaderUniform()
                {
                    Name           = name,
                    Binding        = binding,
                    DescriptorType = descriptorType,
                    StageFlags     = ShaderStage,
                };


                if (!sets.ContainsKey(set))
                {
                    sets.Add(set, new ShaderUniformSet(set));
                }
                sets[set].ShaderUniforms.Add(shaderUniform);
            }

            return(sets);
        }