91ef8917b0ae6da55661030c83e88fa72c8dfca2
[mesa.git] / src / glsl / builtins / tools / generate_builtins.pl
1 #!/usr/bin/env perl
2
3 sub process_version {
4 my ($version) = @_;
5 my @vars;
6 print "/* $version builtins */\n\n";
7
8 my @files = <builtins/$version/*>;
9 foreach $file (@files) {
10 push(@vars, process_file($file));
11 }
12
13 print "static const char *functions_for_$version [] = {\n";
14 foreach $var (@vars) {
15 print " $var,\n";
16 }
17 print "};\n\n"
18 }
19
20 sub process_file {
21 my ($file) = @_;
22
23 # Change from builtins/110/foo to builtins_110_foo
24 my $var = $file; $var =~ s!/!_!g;
25
26 print "static const char *$var = {\n";
27 open SRC, "<", "$file" or die $!;
28 while (<SRC>) {
29 s/\\/\\\\/g;
30 s/\"/\\\"/g;
31 s/\n/\\n/g;
32 print " \"$_\"\n";
33 }
34 print "};\n\n";
35 close SRC or die $!;
36 return $var;
37 }
38
39 print << 'EOF';
40 /* DO NOT MODIFY - automatically generated by generate_builtins.pl */
41 /*
42 * Copyright © 2010 Intel Corporation
43 *
44 * Permission is hereby granted, free of charge, to any person obtaining a
45 * copy of this software and associated documentation files (the "Software"),
46 * to deal in the Software without restriction, including without limitation
47 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
48 * and/or sell copies of the Software, and to permit persons to whom the
49 * Software is furnished to do so, subject to the following conditions:
50 *
51 * The above copyright notice and this permission notice (including the next
52 * paragraph) shall be included in all copies or substantial portions of the
53 * Software.
54 *
55 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
58 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
60 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
61 * DEALINGS IN THE SOFTWARE.
62 */
63
64 #include <stdio.h>
65 #include "main/compiler.h"
66 #include "glsl_parser_extras.h"
67 #include "ir_reader.h"
68 #include "program.h"
69
70 extern "C" struct gl_shader *
71 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
72
73 gl_shader *
74 read_builtins(GLenum target, const char **functions, unsigned count)
75 {
76 gl_shader *sh = _mesa_new_shader(NULL, 0, target);
77 struct _mesa_glsl_parse_state *st =
78 new(sh) _mesa_glsl_parse_state(NULL, target, sh);
79
80 st->language_version = 130;
81 st->ARB_texture_rectangle_enable = true;
82 st->EXT_texture_array_enable = true;
83 _mesa_glsl_initialize_types(st);
84
85 sh->ir = new(sh) exec_list;
86 sh->symbols = st->symbols;
87
88 for (unsigned i = 0; i < count; i++) {
89 _mesa_glsl_read_ir(st, sh->ir, functions[i]);
90
91 if (st->error) {
92 printf("error reading builtin: %.35s ...\n", functions[i]);
93 delete st;
94 talloc_free(sh);
95 return NULL;
96 }
97 }
98
99 reparent_ir(sh->ir, sh);
100 delete st;
101
102 return sh;
103 }
104
105 EOF
106
107 @versions = sort(<builtins/[1-9A-Z]*>);
108 foreach $version (@versions) {
109 $version =~ s!builtins/!!g;
110 process_version($version);
111 }
112
113 print << 'EOF';
114 void *builtin_mem_ctx = NULL;
115
116 void
117 _mesa_glsl_release_functions(void)
118 {
119 talloc_free(builtin_mem_ctx);
120 }
121
122 void
123 _mesa_glsl_initialize_functions(exec_list *instructions,
124 struct _mesa_glsl_parse_state *state)
125 {
126 if (builtin_mem_ctx == NULL)
127 builtin_mem_ctx = talloc_init("GLSL built-in functions");
128
129 state->num_builtins_to_link = 0;
130 EOF
131
132 foreach $version_xs (@versions) {
133 $check = "";
134 if ($version_xs =~ /_vs/) {
135 $check = "state->target == vertex_shader && ";
136 } elsif ($version_xs =~ /_fs/) {
137 $check = "state->target == fragment_shader && ";
138 }
139 $version = $version_xs;
140 $version =~ s/_[vf]s//g;
141
142 if ($version =~ /^[1-9][0-9][0-9]/) {
143 $check = "${check}state->language_version >= $version";
144 } else {
145 # Not a version...an extension name
146 $check = "${check}state->${version}_enable";
147 }
148 print " if ($check) {\n";
149 print " static gl_shader *sh = NULL;\n";
150 print "\n";
151 print " if (sh == NULL) {\n";
152 print " sh = read_builtins(GL_VERTEX_SHADER, functions_for_$version_xs,\n";
153 print " Elements(functions_for_$version_xs));\n";
154 print " talloc_steal(builtin_mem_ctx, sh);\n";
155 print " }\n";
156 print "\n";
157 print " import_prototypes(sh->ir, instructions, state->symbols, state);\n";
158 print " state->builtins_to_link[state->num_builtins_to_link] = sh;\n";
159 print " state->num_builtins_to_link++;\n";
160 print " }\n";
161 print "\n";
162 }
163
164 print "}\n";