Creates definition list by declaration.
		/// <summary>
		/// 
		/// </summary>
		/// <param name="buildContext"></param>
		public override void Process ( AssetSource assetFile, BuildContext buildContext )
		{
			//
			//	Get combinations :
			//
			string shaderSource	=	File.ReadAllText( assetFile.FullSourcePath );

			var ubershaderDecl	=	shaderSource.Split( new[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries )
									.Select( line0 => line0.Trim() )
									.Where( line1 => line1.StartsWith("$ubershader") )
									.ToList();

			var defineList = new List<string>();

			foreach ( var comb in ubershaderDecl ) {
				var ue = new UbershaderEnumerator( comb, "$ubershader" );
				defineList.AddRange( ue.DefineList );
			}

			

			//
			//	Start listing builder :
			//	
			ListingPath	=	buildContext.GetTempFileName( assetFile.KeyPath, ".html" );
			var htmlBuilder = new StringBuilder();

			htmlBuilder.AppendFormat("<pre>");
			htmlBuilder.AppendLine("<b>Ubershader assembly listing</b>");
			htmlBuilder.AppendLine("");
			htmlBuilder.AppendLine("<b>Source:</b> <i>" + assetFile.KeyPath + "</i>" );
			htmlBuilder.AppendLine("");

			//	insert includes here bit later:
			var includeInsert = htmlBuilder.Length;

			htmlBuilder.AppendLine("<b>Declarations:</b>");

			foreach ( var comb in ubershaderDecl ) {
				htmlBuilder.AppendLine("  <i>" + comb + "</i>");
			}
			htmlBuilder.AppendLine("");

			

			var usdb = new List<UsdbEntry>();

			var include = new IncludeHandler(buildContext);


			//
			//	Build all :
			//
			foreach ( var defines in defineList ) {

				var id		=	defineList.IndexOf( defines );

				var psbc	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".PS.dxbc" );
				var vsbc	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".VS.dxbc" );
				var gsbc	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".GS.dxbc" );
				var hsbc	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".HS.dxbc" );
				var dsbc	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".DS.dxbc" );
				var csbc	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".CS.dxbc" );
															  
				var pshtm	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".PS.html" );
				var vshtm	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".VS.html" );
				var gshtm	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".GS.html" );
				var hshtm	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".HS.html" );
				var dshtm	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".DS.html" );
				var cshtm	=	buildContext.GetTempFileName( assetFile.KeyPath, "." + id.ToString("D8") + ".CS.html" );

				var ps = Compile( buildContext, include, shaderSource, assetFile.FullSourcePath, "ps_5_0", PSEntryPoint, defines, psbc, pshtm );
				var vs = Compile( buildContext, include, shaderSource, assetFile.FullSourcePath, "vs_5_0", VSEntryPoint, defines, vsbc, vshtm );
				var gs = Compile( buildContext, include, shaderSource, assetFile.FullSourcePath, "gs_5_0", GSEntryPoint, defines, gsbc, gshtm );
				var hs = Compile( buildContext, include, shaderSource, assetFile.FullSourcePath, "hs_5_0", HSEntryPoint, defines, hsbc, hshtm );
				var ds = Compile( buildContext, include, shaderSource, assetFile.FullSourcePath, "ds_5_0", DSEntryPoint, defines, dsbc, dshtm );
				var cs = Compile( buildContext, include, shaderSource, assetFile.FullSourcePath, "cs_5_0", CSEntryPoint, defines, csbc, cshtm );
				

				htmlBuilder.AppendFormat( (vs.Length==0) ? ".. " : "<a href=\"{0}\">vs</a> ",	Path.GetFileName(vshtm) );
				htmlBuilder.AppendFormat( (ps.Length==0) ? ".. " : "<a href=\"{0}\">ps</a> ",	Path.GetFileName(pshtm) );
				htmlBuilder.AppendFormat( (hs.Length==0) ? ".. " : "<a href=\"{0}\">hs</a> ",	Path.GetFileName(hshtm) );
				htmlBuilder.AppendFormat( (ds.Length==0) ? ".. " : "<a href=\"{0}\">ds</a> ",	Path.GetFileName(dshtm) );
				htmlBuilder.AppendFormat( (gs.Length==0) ? ".. " : "<a href=\"{0}\">gs</a> ",	Path.GetFileName(gshtm) );
				htmlBuilder.AppendFormat( (cs.Length==0) ? ".. " : "<a href=\"{0}\">cs</a> ",	Path.GetFileName(cshtm) );

				htmlBuilder.Append( "[" + defines + "]<br>" );

				usdb.Add( new UsdbEntry( defines, ps, vs, gs, hs, ds, cs ) );
			}


			htmlBuilder.Insert( includeInsert, 
				"<b>Includes:</b>\r\n" 
				+ string.Join("", include.Includes.Select(s=>"  <i>" + s + "</i>\r\n") )
				+ "\r\n");


			File.WriteAllText( buildContext.GetTempFileName(assetFile.KeyPath, ".html"), htmlBuilder.ToString() );


			//
			//	Write ubershader :
			//
			using ( var fs = assetFile.OpenTargetStream(include.Includes) ) {

				using ( var bw = new BinaryWriter( fs ) ) {

					bw.WriteFourCC( Ubershader.UbershaderSignature );

					//	params :

					//	bytecodes :
					bw.Write( usdb.Count );

					foreach ( var entry in usdb ) {

						bw.Write( entry.Defines );

						bw.WriteFourCC( Ubershader.PSBytecodeSignature );
						bw.Write( entry.PSBytecode.Length );
						bw.Write( entry.PSBytecode );

						bw.WriteFourCC( Ubershader.VSBytecodeSignature );
						bw.Write( entry.VSBytecode.Length );
						bw.Write( entry.VSBytecode );

						bw.WriteFourCC( Ubershader.GSBytecodeSignature );
						bw.Write( entry.GSBytecode.Length );
						bw.Write( entry.GSBytecode );

						bw.WriteFourCC( Ubershader.HSBytecodeSignature );
						bw.Write( entry.HSBytecode.Length );
						bw.Write( entry.HSBytecode );

						bw.WriteFourCC( Ubershader.DSBytecodeSignature );
						bw.Write( entry.DSBytecode.Length );
						bw.Write( entry.DSBytecode );

						bw.WriteFourCC( Ubershader.CSBytecodeSignature );
						bw.Write( entry.CSBytecode.Length );
						bw.Write( entry.CSBytecode );
					}
				}
			}
		}
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="buildContext"></param>
        public override void Process(AssetSource assetFile, BuildContext buildContext)
        {
            //
            //	Get combinations :
            //
            string shaderSource = File.ReadAllText(assetFile.FullSourcePath);

            var ubershaderDecl = shaderSource.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)
                                 .Select(line0 => line0.Trim())
                                 .Where(line1 => line1.StartsWith("$ubershader"))
                                 .ToList();

            var defineList = new List <string>();

            foreach (var comb in ubershaderDecl)
            {
                var ue = new UbershaderEnumerator(comb, "$ubershader");
                defineList.AddRange(ue.DefineList);
            }



            //
            //	Start listing builder :
            //
            ListingPath = buildContext.GetTempFileFullPath(assetFile.KeyPath, ".html");
            var htmlBuilder = new StringBuilder();

            htmlBuilder.AppendFormat("<pre>");
            htmlBuilder.AppendLine("<b>Ubershader assembly listing</b>");
            htmlBuilder.AppendLine("");
            htmlBuilder.AppendLine("<b>Source:</b> <i>" + assetFile.KeyPath + "</i>");
            htmlBuilder.AppendLine("");

            //	insert includes here bit later:
            var includeInsert = htmlBuilder.Length;

            htmlBuilder.AppendLine("<b>Declarations:</b>");

            foreach (var comb in ubershaderDecl)
            {
                htmlBuilder.AppendLine("  <i>" + comb + "</i>");
            }
            htmlBuilder.AppendLine("");



            var usdb = new List <UsdbEntry>();

            var include = new IncludeHandler(buildContext);


            //
            //	Build all :
            //
            foreach (var defines in defineList)
            {
                var id = defineList.IndexOf(defines);

                var psbc = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".PS.dxbc");
                var vsbc = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".VS.dxbc");
                var gsbc = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".GS.dxbc");
                var hsbc = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".HS.dxbc");
                var dsbc = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".DS.dxbc");
                var csbc = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".CS.dxbc");

                var pshtm = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".PS.html");
                var vshtm = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".VS.html");
                var gshtm = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".GS.html");
                var hshtm = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".HS.html");
                var dshtm = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".DS.html");
                var cshtm = buildContext.GetTempFileFullPath(assetFile.KeyPath, "." + id.ToString("D8") + ".CS.html");

                var ps = Compile(buildContext, include, shaderSource, assetFile.FullSourcePath, "ps_5_0", PSEntryPoint, defines, psbc, pshtm);
                var vs = Compile(buildContext, include, shaderSource, assetFile.FullSourcePath, "vs_5_0", VSEntryPoint, defines, vsbc, vshtm);
                var gs = Compile(buildContext, include, shaderSource, assetFile.FullSourcePath, "gs_5_0", GSEntryPoint, defines, gsbc, gshtm);
                var hs = Compile(buildContext, include, shaderSource, assetFile.FullSourcePath, "hs_5_0", HSEntryPoint, defines, hsbc, hshtm);
                var ds = Compile(buildContext, include, shaderSource, assetFile.FullSourcePath, "ds_5_0", DSEntryPoint, defines, dsbc, dshtm);
                var cs = Compile(buildContext, include, shaderSource, assetFile.FullSourcePath, "cs_5_0", CSEntryPoint, defines, csbc, cshtm);


                htmlBuilder.AppendFormat((vs.Length == 0) ? ".. " : "<a href=\"{0}\">vs</a> ", Path.GetFileName(vshtm));
                htmlBuilder.AppendFormat((ps.Length == 0) ? ".. " : "<a href=\"{0}\">ps</a> ", Path.GetFileName(pshtm));
                htmlBuilder.AppendFormat((hs.Length == 0) ? ".. " : "<a href=\"{0}\">hs</a> ", Path.GetFileName(hshtm));
                htmlBuilder.AppendFormat((ds.Length == 0) ? ".. " : "<a href=\"{0}\">ds</a> ", Path.GetFileName(dshtm));
                htmlBuilder.AppendFormat((gs.Length == 0) ? ".. " : "<a href=\"{0}\">gs</a> ", Path.GetFileName(gshtm));
                htmlBuilder.AppendFormat((cs.Length == 0) ? ".. " : "<a href=\"{0}\">cs</a> ", Path.GetFileName(cshtm));

                htmlBuilder.Append("[" + defines + "]<br>");

                usdb.Add(new UsdbEntry(defines, ps, vs, gs, hs, ds, cs));
            }


            htmlBuilder.Insert(includeInsert,
                               "<b>Includes:</b>\r\n"
                               + string.Join("", include.Includes.Select(s => "  <i>" + s + "</i>\r\n"))
                               + "\r\n");


            buildContext.WriteReport(assetFile, htmlBuilder.ToString());


            //
            //	Write ubershader :
            //
            using (var fs = assetFile.OpenTargetStream(include.Includes)) {
                using (var bw = new BinaryWriter(fs)) {
                    bw.WriteFourCC(UbershaderSignature);

                    //	params :

                    //	bytecodes :
                    bw.Write(usdb.Count);

                    foreach (var entry in usdb)
                    {
                        bw.Write(entry.Defines);

                        bw.WriteFourCC(PSBytecodeSignature);
                        bw.Write(entry.PSBytecode.Length);
                        bw.Write(entry.PSBytecode);

                        bw.WriteFourCC(VSBytecodeSignature);
                        bw.Write(entry.VSBytecode.Length);
                        bw.Write(entry.VSBytecode);

                        bw.WriteFourCC(GSBytecodeSignature);
                        bw.Write(entry.GSBytecode.Length);
                        bw.Write(entry.GSBytecode);

                        bw.WriteFourCC(HSBytecodeSignature);
                        bw.Write(entry.HSBytecode.Length);
                        bw.Write(entry.HSBytecode);

                        bw.WriteFourCC(DSBytecodeSignature);
                        bw.Write(entry.DSBytecode.Length);
                        bw.Write(entry.DSBytecode);

                        bw.WriteFourCC(CSBytecodeSignature);
                        bw.Write(entry.CSBytecode.Length);
                        bw.Write(entry.CSBytecode);
                    }
                }
            }
        }