Implement the first builtin function: exp().
authorEric Anholt <eric@anholt.net>
Sat, 27 Mar 2010 01:20:30 +0000 (18:20 -0700)
committerIan Romanick <ian.d.romanick@intel.com>
Mon, 29 Mar 2010 19:48:45 +0000 (12:48 -0700)
Makefile.am
ast_to_hir.cpp
builtin_function.cpp [new file with mode: 0644]
ir.h

index 48b4b3921c061fab1d6a1251dc08c96d702c25ff..9f5efa9e5d4b38b4caabd04140bd25163025e1a4 100644 (file)
@@ -26,7 +26,7 @@ bin_PROGRAMS = glsl
 glsl_SOURCES = symbol_table.c hash_table.c glsl_types.cpp \
        glsl_parser.ypp glsl_lexer.lpp glsl_parser_extras.cpp \
        ast_expr.cpp ast_to_hir.cpp ast_function.cpp ast_type.cpp \
-       ir.cpp hir_field_selection.cpp \
+       ir.cpp hir_field_selection.cpp builtin_function.cpp \
        ir_print_visitor.cpp ir_variable.cpp ir_function.cpp
 
 BUILT_SOURCES = glsl_parser.h builtin_types.h glsl_parser.cpp glsl_lexer.cpp
index d6aa3bce5797be766e625fb8d0df89d5dc5265c9..ec1562f8cb28c66b4b43fb0ea7b5cbecd9956036 100644 (file)
@@ -63,6 +63,7 @@ _mesa_ast_to_hir(exec_list *instructions, struct _mesa_glsl_parse_state *state)
 
    _mesa_glsl_initialize_variables(instructions, state);
    _mesa_glsl_initialize_constructors(instructions, state);
+   _mesa_glsl_initialize_functions(instructions, state);
 
    state->current_function = NULL;
 
diff --git a/builtin_function.cpp b/builtin_function.cpp
new file mode 100644 (file)
index 0000000..fb168ac
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * 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 <stdlib.h>
+#include "glsl_symbol_table.h"
+#include "glsl_parser_extras.h"
+#include "glsl_types.h"
+#include "ir.h"
+
+static void
+generate_exp(exec_list *instructions,
+            ir_variable **declarations,
+            const glsl_type *type)
+{
+   ir_dereference *const retval = new ir_dereference(declarations[16]);
+   ir_dereference *const arg = new ir_dereference(declarations[0]);
+   ir_rvalue *result;
+
+   result = new ir_expression(ir_unop_exp, type, arg, NULL);
+
+   ir_instruction *inst = new ir_assignment(retval, result, NULL);
+   instructions->push_tail(inst);
+}
+
+void
+generate_function_instance(ir_function *f,
+                          const char *name,
+                          exec_list *instructions,
+                          void (*generate)(exec_list *instructions,
+                                           ir_variable **declarations,
+                                           const glsl_type *type),
+                          const glsl_type *type)
+{
+   ir_variable *declarations[17];
+
+   ir_function_signature *const sig = new ir_function_signature(type);
+   f->signatures.push_tail(sig);
+
+   ir_label *const label = new ir_label(name);
+   instructions->push_tail(label);
+   sig->definition = label;
+
+   ir_variable *var = new ir_variable(type, "arg");
+
+   var->mode = ir_var_in;
+   sig->parameters.push_tail(var);
+
+   var = new ir_variable(type, "arg");
+
+   declarations[0] = var;
+
+   ir_variable *retval = new ir_variable(type, "__retval");
+   instructions->push_tail(retval);
+
+   declarations[16] = retval;
+
+   generate(instructions, declarations, type);
+}
+
+void
+make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions,
+                     const char *name,
+                     void (*generate)(exec_list *instructions,
+                                      ir_variable **declarations,
+                                      const glsl_type *type))
+{
+   ir_function *const f = new ir_function(name);
+   const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1);
+   const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1);
+   const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1);
+
+   bool added = symtab->add_function(name, f);
+   assert(added);
+
+   generate_function_instance(f, name, instructions, generate, glsl_type::float_type);
+   generate_function_instance(f, name, instructions, generate, vec2_type);
+   generate_function_instance(f, name, instructions, generate, vec3_type);
+   generate_function_instance(f, name, instructions, generate, vec4_type);
+}
+
+void
+generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions)
+{
+   /* FINISHME: radians() */
+   /* FINISHME: degrees() */
+   /* FINISHME: sin() */
+   /* FINISHME: cos() */
+   /* FINISHME: tan() */
+   /* FINISHME: asin() */
+   /* FINISHME: acos() */
+   /* FINISHME: atan(y,x) */
+   /* FINISHME: atan(y/x) */
+   /* FINISHME: pow() */
+   make_gentype_function(symtab, instructions, "exp", generate_exp);
+   /* FINISHME: log() */
+   /* FINISHME: exp2() */
+   /* FINISHME: log2() */
+   /* FINISHME: sqrt() */
+   /* FINISHME: inversesqrt() */
+   /* FINISHME: abs() */
+   /* FINISHME: sign() */
+   /* FINISHME: floor() */
+   /* FINISHME: ceil() */
+   /* FINISHME: fract() */
+   /* FINISHME: mod(x, float y) */
+   /* FINISHME: mod(x, y) */
+   /* FINISHME: min() */
+   /* FINISHME: max() */
+   /* FINISHME: clamp() */
+   /* FINISHME: clamp() */
+   /* FINISHME: mix() */
+   /* FINISHME: mix() */
+   /* FINISHME: step() */
+   /* FINISHME: step() */
+   /* FINISHME: smoothstep() */
+   /* FINISHME: smoothstep() */
+   /* FINISHME: floor() */
+   /* FINISHME: step() */
+   /* FINISHME: length() */
+   /* FINISHME: distance() */
+   /* FINISHME: dot() */
+   /* FINISHME: cross() */
+   /* FINISHME: normalize() */
+   /* FINISHME: ftransform() */
+   /* FINISHME: faceforward() */
+   /* FINISHME: reflect() */
+   /* FINISHME: refract() */
+   /* FINISHME: matrixCompMult() */
+   /* FINISHME: lessThan() */
+   /* FINISHME: lessThanEqual() */
+   /* FINISHME: greaterThan() */
+   /* FINISHME: greaterThanEqual() */
+   /* FINISHME: equal() */
+   /* FINISHME: notEqual() */
+   /* FINISHME: any() */
+   /* FINISHME: all() */
+   /* FINISHME: not() */
+   /* FINISHME: texture*() */
+   /* FINISHME: shadow*() */
+   /* FINISHME: dFd[xy]() */
+   /* FINISHME: fwidth() */
+}
+
+void
+_mesa_glsl_initialize_functions(exec_list *instructions,
+                               struct _mesa_glsl_parse_state *state)
+{
+   generate_110_functions(state->symbols, instructions);
+}
diff --git a/ir.h b/ir.h
index 7e2f304b41d0055762d87fc49f85c858aa8fed3b..bad5111b5a8d03e19ab090cefc3af493891bd28b 100644 (file)
--- a/ir.h
+++ b/ir.h
@@ -521,4 +521,8 @@ extern void
 _mesa_glsl_initialize_variables(exec_list *instructions,
                                struct _mesa_glsl_parse_state *state);
 
+extern void
+_mesa_glsl_initialize_functions(exec_list *instructions,
+                               struct _mesa_glsl_parse_state *state);
+
 #endif /* IR_H */