private void WriteProgramDependencies(StreamWriter stream, Program program) { stream.WriteLine("//-----------------------------------------------------------------------------"); stream.WriteLine("// PROGRAM DEPENDENCIES"); stream.WriteLine("//-----------------------------------------------------------------------------"); for (int i = 0; i < program.DependencyCount; i++) { string curDependency = program.GetDependency(i); stream.WriteLine("#include " + '\"' + curDependency + "." + TargetLanguage + '\"'); } }
private void WriteProgramDependencies( StreamWriter stream, Program program ) { stream.WriteLine( "//-----------------------------------------------------------------------------" ); stream.WriteLine( "// PROGRAM DEPENDENCIES" ); stream.WriteLine( "//-----------------------------------------------------------------------------" ); for ( int i = 0; i < program.DependencyCount; i++ ) { string curDependency = program.GetDependency( i ); stream.WriteLine( "#include " + '\"' + curDependency + "." + TargetLanguage + '\"' ); } }
private void WriteProgramDependencies(StreamWriter stream, Program program) { for (int i = 0; i < program.DependencyCount; i++) { string curDependency = program.GetDependency(i); CacheDependencyFunctions(curDependency); } stream.WriteLine("//-----------------------------------------------------------------------------"); stream.WriteLine("// PROGRAM DEPENDENCIES"); stream.WriteLine(); var forwardDecl = new List <FunctionInvocation>(); var functionList = program.Functions; int itFunction = 0; Function curFunction = functionList[0]; var atomInstances = curFunction.AtomInstances; int itAtom = 0; int itAtomEnd = atomInstances.Count; //Now iterate over all function atoms for ( ; itAtom != itAtomEnd; itAtom++) { //Skip non function invocation atom if (atomInstances[itAtom] is FunctionInvocation == false) { continue; } var funcInvoc = atomInstances[itAtom] as FunctionInvocation; forwardDecl.Add(funcInvoc); // Now look into that function for other non-builtin functions and add them to the declaration list // Look for non-builtin functions // Do so by assuming that these functions do not have several variations. // Also, because GLSL is C based, functions must be defined before they are used // so we can make the assumption that we already have this function cached. // // If we find a function, look it up in the map and write it out DiscoverFunctionDependencies(funcInvoc, forwardDecl); } //Now remove duplicate declarations forwardDecl.Sort(); forwardDecl = forwardDecl.Distinct(new FunctionInvocation.FunctionInvocationComparer()).ToList(); for (int i = 0; i < program.DependencyCount; i++) { string curDependency = program.GetDependency(i); foreach (var key in this.definesMap.Keys) { if (this.definesMap[key] == curDependency) { stream.Write(this.definesMap[key]); stream.Write("\n"); } } } // Parse the source shader and write out only the needed functions foreach (var it in forwardDecl) { var invoc = new FunctionInvocation(string.Empty, 0, 0, string.Empty); string body = string.Empty; //find the function in the cache foreach (var key in this.functionCacheMap.Keys) { if (!(it == key)) { continue; } invoc = key; body = this.functionCacheMap[key]; break; } if (invoc.FunctionName.Length > 0) { //Write out the funciton name from the cached FunctionInvocation stream.Write(invoc.ReturnType); stream.Write(" "); stream.Write(invoc.FunctionName); stream.Write("("); int itOperand = 0; int itOperandEnd = invoc.OperandList.Count; while (itOperand != itOperandEnd) { Operand op = invoc.OperandList[itOperand]; Operand.OpSemantic opSemantic = op.Semantic; string paramName = op.Parameter.Name; int opMask = op.Mask; GpuProgramParameters.GpuConstantType gpuType = GpuProgramParameters.GpuConstantType.Unknown; switch (opSemantic) { case Operand.OpSemantic.In: stream.Write("in "); break; case Operand.OpSemantic.Out: stream.Write("out "); break; case Operand.OpSemantic.InOut: stream.Write("inout "); break; default: break; } //Swizzle masks are onluy defined for types like vec2, vec3, vec4 if (opMask == (int)Operand.OpMask.All) { gpuType = op.Parameter.Type; } else { gpuType = Operand.GetGpuConstantType(opMask); } //We need a valid type otherwise glsl compilation will not work if (gpuType == GpuProgramParameters.GpuConstantType.Unknown) { throw new Core.AxiomException("Cannot convert Operand.OpMask to GpuConstantType"); } stream.Write(this.gpuConstTypeMap[gpuType] + " " + paramName); itOperand++; //Prepare for the next operand if (itOperand != itOperandEnd) { stream.Write(", "); } } stream.WriteLine(); stream.WriteLine("{"); stream.WriteLine(body); stream.WriteLine("}"); stream.WriteLine(); } } }
private void BindSubShaders(Program program, GpuProgram gpuProgram) { if (program.DependencyCount > 0) { // Get all attached shaders so we do not attach shaders twice. // maybe GLSLProgram should take care of that ( prevent add duplicate shaders ) string attachedShaders = string.Empty; //TODO: gpuProgram.GetParameter("attach"); string subSharedDef = string.Empty; for (int i = 0; i < program.DependencyCount; i++) { // Here we append _VS and _FS to the library shaders (so max each lib shader // is compiled twice once as vertex and once as fragment shader) string subShaderName = program.GetDependency(i); if (program.Type == GpuProgramType.Vertex) { subShaderName += "_VS"; } else { subShaderName += "_FS"; } //Check if the library shader already compiled if (!HighLevelGpuProgramManager.Instance.ResourceExists(subShaderName)) { //Create the library shader HighLevelGpuProgram subGpuProgram = HighLevelGpuProgramManager.Instance.CreateProgram(subShaderName, ResourceGroupManager. DefaultResourceGroupName, TargetLanguage, program.Type); //Set the source name string sourceName = program.GetDependency(i) + "." + TargetLanguage; subGpuProgram.SourceFile = sourceName; //If we have compiler errors than stop processing if (subGpuProgram.HasCompileError) { throw new AxiomException("Could not compile shader library from the source file: " + sourceName); } this.libraryPrograms.Add(subShaderName); } //Check if the lib shader already attached to this shader if (attachedShaders.Contains(subShaderName)) { subSharedDef += subShaderName + " "; } } //Check if we have something to attach if (subSharedDef.Length > 0) { var nvpl = new Axiom.Collections.NameValuePairList(); nvpl.Add("attach", subSharedDef); gpuProgram.SetParameters(nvpl); } } }
private void BindSubShaders( Program program, GpuProgram gpuProgram ) { if ( program.DependencyCount > 0 ) { // Get all attached shaders so we do not attach shaders twice. // maybe GLSLProgram should take care of that ( prevent add duplicate shaders ) string attachedShaders = string.Empty; //TODO: gpuProgram.GetParameter("attach"); string subSharedDef = string.Empty; for ( int i = 0; i < program.DependencyCount; i++ ) { // Here we append _VS and _FS to the library shaders (so max each lib shader // is compiled twice once as vertex and once as fragment shader) string subShaderName = program.GetDependency( i ); if ( program.Type == GpuProgramType.Vertex ) { subShaderName += "_VS"; } else { subShaderName += "_FS"; } //Check if the library shader already compiled if ( !HighLevelGpuProgramManager.Instance.ResourceExists( subShaderName ) ) { //Create the library shader HighLevelGpuProgram subGpuProgram = HighLevelGpuProgramManager.Instance.CreateProgram( subShaderName, ResourceGroupManager. DefaultResourceGroupName, TargetLanguage, program.Type ); //Set the source name string sourceName = program.GetDependency( i ) + "." + TargetLanguage; subGpuProgram.SourceFile = sourceName; //If we have compiler errors than stop processing if ( subGpuProgram.HasCompileError ) { throw new AxiomException( "Could not compile shader library from the source file: " + sourceName ); } this.libraryPrograms.Add( subShaderName ); } //Check if the lib shader already attached to this shader if ( attachedShaders.Contains( subShaderName ) ) { subSharedDef += subShaderName + " "; } } //Check if we have something to attach if ( subSharedDef.Length > 0 ) { var nvpl = new Axiom.Collections.NameValuePairList(); nvpl.Add( "attach", subSharedDef ); gpuProgram.SetParameters( nvpl ); } } }
private void WriteProgramDependencies( StreamWriter stream, Program program ) { for ( int i = 0; i < program.DependencyCount; i++ ) { string curDependency = program.GetDependency( i ); CacheDependencyFunctions( curDependency ); } stream.WriteLine( "//-----------------------------------------------------------------------------" ); stream.WriteLine( "// PROGRAM DEPENDENCIES" ); stream.WriteLine(); var forwardDecl = new List<FunctionInvocation>(); var functionList = program.Functions; int itFunction = 0; Function curFunction = functionList[ 0 ]; var atomInstances = curFunction.AtomInstances; int itAtom = 0; int itAtomEnd = atomInstances.Count; //Now iterate over all function atoms for ( ; itAtom != itAtomEnd; itAtom++ ) { //Skip non function invocation atom if ( atomInstances[ itAtom ] is FunctionInvocation == false ) { continue; } var funcInvoc = atomInstances[ itAtom ] as FunctionInvocation; forwardDecl.Add( funcInvoc ); // Now look into that function for other non-builtin functions and add them to the declaration list // Look for non-builtin functions // Do so by assuming that these functions do not have several variations. // Also, because GLSL is C based, functions must be defined before they are used // so we can make the assumption that we already have this function cached. // // If we find a function, look it up in the map and write it out DiscoverFunctionDependencies( funcInvoc, forwardDecl ); } //Now remove duplicate declarations forwardDecl.Sort(); forwardDecl = forwardDecl.Distinct( new FunctionInvocation.FunctionInvocationComparer() ).ToList(); for ( int i = 0; i < program.DependencyCount; i++ ) { string curDependency = program.GetDependency( i ); foreach ( var key in this.definesMap.Keys ) { if ( this.definesMap[ key ] == curDependency ) { stream.Write( this.definesMap[ key ] ); stream.Write( "\n" ); } } } // Parse the source shader and write out only the needed functions foreach ( var it in forwardDecl ) { var invoc = new FunctionInvocation( string.Empty, 0, 0, string.Empty ); string body = string.Empty; //find the function in the cache foreach ( var key in this.functionCacheMap.Keys ) { if ( !( it == key ) ) { continue; } invoc = key; body = this.functionCacheMap[ key ]; break; } if ( invoc.FunctionName.Length > 0 ) { //Write out the funciton name from the cached FunctionInvocation stream.Write( invoc.ReturnType ); stream.Write( " " ); stream.Write( invoc.FunctionName ); stream.Write( "(" ); int itOperand = 0; int itOperandEnd = invoc.OperandList.Count; while ( itOperand != itOperandEnd ) { Operand op = invoc.OperandList[ itOperand ]; Operand.OpSemantic opSemantic = op.Semantic; string paramName = op.Parameter.Name; int opMask = op.Mask; GpuProgramParameters.GpuConstantType gpuType = GpuProgramParameters.GpuConstantType.Unknown; switch ( opSemantic ) { case Operand.OpSemantic.In: stream.Write( "in " ); break; case Operand.OpSemantic.Out: stream.Write( "out " ); break; case Operand.OpSemantic.InOut: stream.Write( "inout " ); break; default: break; } //Swizzle masks are onluy defined for types like vec2, vec3, vec4 if ( opMask == (int)Operand.OpMask.All ) { gpuType = op.Parameter.Type; } else { gpuType = Operand.GetGpuConstantType( opMask ); } //We need a valid type otherwise glsl compilation will not work if ( gpuType == GpuProgramParameters.GpuConstantType.Unknown ) { throw new Core.AxiomException( "Cannot convert Operand.OpMask to GpuConstantType" ); } stream.Write( this.gpuConstTypeMap[ gpuType ] + " " + paramName ); itOperand++; //Prepare for the next operand if ( itOperand != itOperandEnd ) { stream.Write( ", " ); } } stream.WriteLine(); stream.WriteLine( "{" ); stream.WriteLine( body ); stream.WriteLine( "}" ); stream.WriteLine(); } } }