Example #1
0
 // Can only construct from this assembly and friend assemblies
 internal Uniform(string name, ShaderType type, uint?arrSize, uint loc, UniformBlock block, uint idx, uint off)
 {
     Name      = name;
     Type      = type;
     ArraySize = arrSize.GetValueOrDefault(1);
     IsArray   = arrSize.HasValue;
     Location  = block?.Location ?? loc;
     Block     = block;
     Index     = idx;
     Offset    = off;
 }
Example #2
0
        public static ShaderInfo LoadFrom(BinaryReader reader, Version cVer, Version sVer)
        {
            if (cVer > TOOL_VERSION)
            {
                throw new InvalidOperationException($"The file version {cVer} cannot be loaded by this version of the library ({TOOL_VERSION}).");
            }

            ShaderInfo info = new ShaderInfo();

            info.CompilerVersion = cVer;
            info.SourceVersion   = sVer;

            // Load the stages
            var stages = reader.ReadByte();

            info.Stages = (ShaderStages)stages;

            // Load the uniforms (note that this assumes that the uniforms are sorted when written)
            var ucount = reader.ReadByte();
            var cont   = reader.ReadBoolean();
            var bcount = reader.ReadByte();

            info._blocks.AddRange(reader.ReadBytes(bcount).Select(b => new UniformBlock(b)));
            while (ucount > 0)
            {
                var slen  = reader.ReadByte();
                var uname = Encoding.ASCII.GetString(reader.ReadBytes(slen));
                var type  = (ShaderType)reader.ReadByte();
                var asize = reader.ReadByte();
                var extra = reader.ReadByte();
                var loc   = reader.ReadByte();
                var idx   = reader.ReadByte();

                UniformBlock b   = (idx != 0xFF) ? info._blocks.Find(blk => blk.Location == loc) : null;
                var          uni = new Uniform(uname, type, asize == 0 ? (uint?)null : asize, loc, b, idx, b?.Size ?? 0);
                if (type == ShaderType.SubpassInput)
                {
                    uni.SubpassIndex = extra;
                }
                else if (type.IsImageType())
                {
                    uni.ImageFormat = (ImageFormat)extra;
                }
                b?.AddMember(uni);
                info._uniforms.Add(uni);

                --ucount;
            }

            // Load the attributes
            var acount = reader.ReadByte();

            while (acount > 0)
            {
                var slen  = reader.ReadByte();
                var aname = Encoding.ASCII.GetString(reader.ReadBytes(slen));
                var type  = (ShaderType)reader.ReadByte();
                var asize = reader.ReadByte();
                var loc   = reader.ReadByte();

                info._attributes.Add(new VertexAttribute(aname, type, asize == 0 ? (uint?)null : asize, loc));

                --acount;
            }

            // Load the outputs
            var ocount = reader.ReadByte();

            for (uint i = 0; i < ocount; ++i)
            {
                var slen  = reader.ReadByte();
                var oname = Encoding.ASCII.GetString(reader.ReadBytes(slen));
                var type  = (ShaderType)reader.ReadByte();

                info._outputs.Add(new FragmentOutput(oname, type, i));
            }

            // Load the specialization constants
            var scount = reader.ReadByte();

            while (scount > 0)
            {
                var slen  = reader.ReadByte();
                var aname = Encoding.ASCII.GetString(reader.ReadBytes(slen));
                var type  = (ShaderType)reader.ReadByte();
                var idx   = reader.ReadByte();

                info._specializations.Add(new SpecConstant(aname, type, idx));

                --scount;
            }

            // Return
            info.Sort();
            return(info);
        }