3 ###############################################################################
10 ###############################################################################
12 sub OpenFileForWriting
14 my($fileName) = shift;
16 my $fh = FileHandle->new;
17 if (!$fh->open("> $fileName"))
19 die "Can't open $fileName for writing: $!\n";
24 ###############################################################################
32 ###############################################################################
34 sub GenerateHeaderFileHeader
36 my($headerFile) = shift;
38 print $headerFile <<"H_HEADER_END";
40 // Automatically Generated on $time
43 #ifndef __DALI_GENERATED_SHADERS_H_
44 #define __DALI_GENERATED_SHADERS_H_
54 ###############################################################################
56 sub GenerateHeaderFileFooter
58 my($headerFile) = shift;
59 print $headerFile <<"H_FOOTER_END";
61 } // namespace Internal
64 #endif // __DALI_GENERATED_SHADERS_H_
68 ###############################################################################
70 sub GenerateSourceFileHeader
72 my($sourceFile) = shift;
74 print $sourceFile <<"CPP_HEADER_END";
76 // Automatically Generated on $time
87 ###############################################################################
89 sub GenerateSourceFileFooter
91 my($sourceFile) = shift;
92 print $sourceFile <<"CPP_FOOTER_END";
93 } // namespace Internal
98 ###############################################################################
102 sub GenerateStringsFromFile
107 my $shadername = $file;
108 $shadername =~ s/-([a-z])/uc($1)/eg;
109 $shadername =~ s/^([a-z])/uc($1)/e;
110 $shadername =~ s/\.txt$//;
115 open MEM, "$dir/$file" || die "Can't open $file for reading: $!\n";
121 if (/<VertexShader>/)
126 elsif (/<FragmentShader>/)
134 if (m!</VertexShader>!)
137 $shaders{$shadername}->{"vertex"} = $shader;
139 elsif( m!</FragmentShader>!)
142 $shaders{$shadername}->{"fragment"} = $shader;
148 # print $sourceFile "\"$_\\n\"\n";
155 ###############################################################################
160 opendir (DIR, $dir) || die "Can't open $dir for reading: $!\n";
161 my @shaderFiles = grep { /\.txt$/ && -f "$dir/$_" } readdir DIR;
166 ###############################################################################
170 my $sourceFile=shift;
173 $line =~ s!//.*$!!; # Strip out comments
174 $line =~ s!\s*$!!; # Strip out trailing space
175 if( $line !~ m!^\s*$! )
177 print $sourceFile "\"$line\\n\"\n";
183 my $sourceFile=shift;
186 $line =~ s!//.*$!!; # Strip out comments
187 $line =~ s!\s*$!!; # Strip out trailing space
188 if( $line !~ m!^\s*$! )
190 print $sourceFile "\"$line\\n\" \\\n";
195 sub PrintShaderProgramWithMacros
197 my $sourceFile=shift;
198 my $shadername=shift;
199 my $SHADERNAME=$shadername;
200 $SHADERNAME =~ s/([A-Z])/_$1/g;
201 substr($SHADERNAME,0,1)="";
202 $SHADERNAME = uc($SHADERNAME);
204 my $program_type=shift;
205 my $ProgramType=ucfirst($program_type);
206 my $PROGRAM_TYPE=uc($program_type);
209 my @lines=split(/\n/, $shaders{$shadername}->{$program_type} );
211 print $sourceFile "#define ${SHADERNAME}_PREFIX_${PROGRAM_TYPE} \\\n";
212 LINE: while( scalar(@lines) )
214 last LINE if $lines[0] =~ /main()/;
215 PrintMacroLine($sourceFile, shift(@lines));
217 print $sourceFile "\n\n";
219 print $sourceFile "#define ${SHADERNAME}_POSTFIX_${PROGRAM_TYPE} \\\n";
220 LINE: while( scalar(@lines) )
222 PrintMacroLine($sourceFile, shift(@lines));
224 print $sourceFile "\n\n";
228 print $sourceFile "extern const char* const Custom${shadername}Prefix${ProgramType};\n";
229 print $sourceFile "const char* const Custom${shadername}Prefix${ProgramType}(\n";
230 print $sourceFile " ${SHADERNAME}_PREFIX_${PROGRAM_TYPE}\n";
231 print $sourceFile ");\n\n";
233 print $sourceFile "extern const char* const Custom${shadername}Postfix${ProgramType};\n";
234 print $sourceFile "const char* const Custom${shadername}Postfix${ProgramType}(\n";
235 print $sourceFile " ${SHADERNAME}_POSTFIX_${PROGRAM_TYPE}\n";
236 print $sourceFile ");\n\n";
238 print $sourceFile "extern const char* const ${shadername}${ProgramType};\n";
239 print $sourceFile "const char* const ${shadername}${ProgramType}(\n";
240 print $sourceFile " ${SHADERNAME}_PREFIX_${PROGRAM_TYPE}\n";
241 print $sourceFile " ${SHADERNAME}_POSTFIX_${PROGRAM_TYPE}\n";
242 print $sourceFile ");\n\n";
246 sub PrintShaderProgram
248 my $sourceFile=shift;
249 my $shadername=shift;
250 my $program_type=shift;
252 my $ProgramType=ucfirst($program_type);
254 my @lines=split(/\n/, $shaders{$shadername}->{$program_type} );
256 print $sourceFile "const char* const ${shadername}${ProgramType}(\n";
257 for my $i (0..scalar(@lines)-1)
259 PrintSourceLine($sourceFile, $lines[$i]);
261 print $sourceFile ");\n\n";
265 print $sourceFile "const char* const Custom${shadername}Prefix${ProgramType}(\n";
267 while( scalar(@lines) )
269 last LINE if $lines[0] =~ /main()/;
270 PrintSourceLine($sourceFile, shift(@lines));
272 print $sourceFile ");\n\n";
274 print $sourceFile "const char* const Custom${shadername}Postfix${ProgramType}(\n";
276 while( scalar(@lines) )
278 PrintSourceLine($sourceFile, shift(@lines));
280 print $sourceFile ");\n\n";
285 sub PrintShaderSources
287 my($headerFile) = shift;
288 my($sourceFile) = shift;
291 # Strings are now in memory. Dump them back out again:
292 foreach $shadername (sort(keys(%shaders)))
294 print $headerFile "extern const char* const ${shadername}Vertex;\n";
295 print $headerFile "extern const char* const ${shadername}Fragment;\n";
298 if( $shadername !~ /TextDistanceField/ || $shadername eq "TextDistanceField" )
300 print $headerFile "extern const char* const Custom${shadername}PrefixVertex;\n";
301 print $headerFile "extern const char* const Custom${shadername}PostfixVertex;\n";
302 print $headerFile "extern const char* const Custom${shadername}PrefixFragment;\n";
303 print $headerFile "extern const char* const Custom${shadername}PostfixFragment;\n";
306 PrintShaderProgramWithMacros($sourceFile, $shadername, "vertex", $custom);
307 PrintShaderProgramWithMacros($sourceFile, $shadername, "fragment", $custom);
311 ###############################################################################
321 "shader-dir=s" => \$shaderDir,
322 "file-name=s" => \$fileName
325 pod2usage(1) if $optHelp;
326 pod2usage(-exitstatus => 0, -verbose => 2) if $optMan;
328 if ($shaderDir eq "" || $fileName eq "")
334 my $dir = $shaderDir;
335 my @shaderFiles = GetShaderFiles($dir);
337 my $headerFile = OpenFileForWriting("$fileName.h");
338 my $sourceFile = OpenFileForWriting("$fileName.cpp");
340 GenerateHeaderFileHeader($headerFile);
341 GenerateSourceFileHeader($sourceFile);
344 foreach $file (@shaderFiles)
346 GenerateStringsFromFile($file, $dir);
349 PrintShaderSources($headerFile, $sourceFile);
351 GenerateHeaderFileFooter($headerFile);
352 GenerateSourceFileFooter($sourceFile);
354 CloseFile($headerFile);
355 CloseFile($sourceFile);
358 ###############################################################################
364 generate-shader-strings.pl - Given a shader directory and a file name, this script generates a header and source file where all the shaders in the directory are stored as vertex and fragment shader const char arrays.
368 generate-shader-strings.pl -s=<shader-dir> -f=<file-name>
370 generate-shader-strings.pl -shader-dir=<shader-dir> -file-name=<file-name>
374 Given a shader directory and a file name, this script generates a header and source file where all the shaders in the directory are stored as vertex and fragment shader const char arrays.
376 The shader files in the specified directory should have the suffix ".txt" and the vertex and fragment shaders should be encapsulated within <VertexShader>*</VertexShader> and <FragmentShader>*</FragmentShader> respectively in this text file.
378 The generated source files will be called <file-name>.h and <file-name>.cpp.
384 =item B<-s|--shader-dir=<shader-directory>>
386 The directory the shader files should be loaded from.
388 =item B<-f|--file-name>
390 The name of the output files.