summaryrefslogtreecommitdiff
path: root/src/glsl/builtins/tools/generate_builtins.pl
diff options
context:
space:
mode:
Diffstat (limited to 'src/glsl/builtins/tools/generate_builtins.pl')
-rwxr-xr-xsrc/glsl/builtins/tools/generate_builtins.pl167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/glsl/builtins/tools/generate_builtins.pl b/src/glsl/builtins/tools/generate_builtins.pl
new file mode 100755
index 0000000000..61d511da1d
--- /dev/null
+++ b/src/glsl/builtins/tools/generate_builtins.pl
@@ -0,0 +1,167 @@
+#!/usr/bin/env perl
+
+sub process_version {
+ my ($version) = @_;
+ my @vars;
+ print "/* $version builtins */\n\n";
+
+ my @files = <builtins/$version/*>;
+ foreach $file (@files) {
+ push(@vars, process_file($file));
+ }
+
+ print "static const char *functions_for_$version [] = {\n";
+ foreach $var (@vars) {
+ print " $var,\n";
+ }
+ print "};\n\n"
+}
+
+sub process_file {
+ my ($file) = @_;
+
+ # Change from builtins/110/foo to builtins_110_foo
+ my $var = $file; $var =~ s!/!_!g;
+
+ print "static const char *$var = {\n";
+ open SRC, "<", "$file" or die $!;
+ while (<SRC>) {
+ s/\\/\\\\/g;
+ s/\"/\\\"/g;
+ s/\n/\\n/g;
+ print " \"$_\"\n";
+ }
+ print "};\n\n";
+ close SRC or die $!;
+ return $var;
+}
+
+print << 'EOF';
+/* DO NOT MODIFY - automatically generated by generate_builtins.pl */
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include "glsl_parser_extras.h"
+#include "ir_reader.h"
+#include "program.h"
+
+extern "C" struct gl_shader *
+_mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
+
+gl_shader *
+read_builtins(GLenum target, const char **functions, unsigned count)
+{
+ gl_shader *sh = _mesa_new_shader(NULL, 0, target);
+ struct _mesa_glsl_parse_state *st =
+ new(sh) _mesa_glsl_parse_state(NULL, target, sh);
+
+ st->language_version = 130;
+ st->ARB_texture_rectangle_enable = true;
+ st->EXT_texture_array_enable = true;
+ _mesa_glsl_initialize_types(st);
+
+ sh->ir = new(sh) exec_list;
+ sh->symbols = st->symbols;
+
+ for (unsigned i = 0; i < count; i++) {
+ _mesa_glsl_read_ir(st, sh->ir, functions[i]);
+
+ if (st->error) {
+ printf("error reading builtin: %.35s ...\n", functions[i]);
+ delete st;
+ talloc_free(sh);
+ return NULL;
+ }
+ }
+
+ reparent_ir(sh->ir, sh);
+ delete st;
+
+ return sh;
+}
+
+EOF
+
+@versions = sort(<builtins/[1-9A-Z]*>);
+foreach $version (@versions) {
+ $version =~ s!builtins/!!g;
+ process_version($version);
+}
+
+print << 'EOF';
+#ifndef Elements
+#define Elements(x) (sizeof(x)/sizeof(*(x)))
+#endif
+
+void *builtin_mem_ctx = NULL;
+
+void
+_mesa_glsl_release_functions(void)
+{
+ talloc_free(builtin_mem_ctx);
+}
+
+void
+_mesa_glsl_initialize_functions(exec_list *instructions,
+ struct _mesa_glsl_parse_state *state)
+{
+ if (builtin_mem_ctx == NULL)
+ builtin_mem_ctx = talloc_init("GLSL built-in functions");
+
+ state->num_builtins_to_link = 0;
+EOF
+
+foreach $version_xs (@versions) {
+ $check = "";
+ if ($version_xs =~ /_vs/) {
+ $check = "state->target == vertex_shader && ";
+ } elsif ($version_xs =~ /_fs/) {
+ $check = "state->target == fragment_shader && ";
+ }
+ $version = $version_xs;
+ $version =~ s/_[vf]s//g;
+
+ if ($version =~ /^[1-9][0-9][0-9]/) {
+ $check = "${check}state->language_version >= $version";
+ } else {
+ # Not a version...an extension name
+ $check = "${check}state->${version}_enable";
+ }
+ print " if ($check) {\n";
+ print " static gl_shader *sh = NULL;\n";
+ print "\n";
+ print " if (sh == NULL) {\n";
+ print " sh = read_builtins(GL_VERTEX_SHADER, functions_for_$version_xs,\n";
+ print " Elements(functions_for_$version_xs));\n";
+ print " talloc_steal(builtin_mem_ctx, sh);\n";
+ print " }\n";
+ print "\n";
+ print " import_prototypes(sh->ir, instructions, state->symbols, state);\n";
+ print " state->builtins_to_link[state->num_builtins_to_link] = sh;\n";
+ print " state->num_builtins_to_link++;\n";
+ print " }\n";
+ print "\n";
+}
+
+print "}\n";