public ShaderVariable[] GetPushConstants() { ShaderVariable[] pushConstants = new ShaderVariable[0]; IEnumerable <string> fields = _segmentCollection.GetPushConstantFields(); if (fields != null) { pushConstants = ShaderVariableParser.Parse(fields).ToArray(); for (int i = 0; i < pushConstants.Length; i++) { pushConstants[i].ShaderStage = ShaderStage; } } return(pushConstants); }
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); }