=== added file 'data/shaders/function-call.all'
--- data/shaders/function-call.all	1970-01-01 00:00:00 +0000
+++ data/shaders/function-call.all	2011-07-07 13:16:12 +0000
@@ -0,0 +1,1 @@
+    d = process(d);

=== added file 'data/shaders/function-step-low.all'
--- data/shaders/function-step-low.all	1970-01-01 00:00:00 +0000
+++ data/shaders/function-step-low.all	2011-07-07 13:16:12 +0000
@@ -0,0 +1,1 @@
+    d = fract(3.0 * d);

=== added file 'data/shaders/function-step-medium.all'
--- data/shaders/function-step-medium.all	1970-01-01 00:00:00 +0000
+++ data/shaders/function-step-medium.all	2011-07-07 13:16:12 +0000
@@ -0,0 +1,2 @@
+    d = fract(3.0 * d) * fract(3.0 * d);
+    d = fract(sqrt(d));

=== added file 'data/shaders/function.frag'
--- data/shaders/function.frag	1970-01-01 00:00:00 +0000
+++ data/shaders/function.frag	2011-07-07 13:16:12 +0000
@@ -0,0 +1,20 @@
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+varying vec4 dummy;
+
+float process(float d)
+{
+$PROCESS$
+    return d;
+}
+
+void main(void)
+{
+    float d = fract(gl_FragCoord.x * gl_FragCoord.y * 0.0001);
+
+$MAIN$
+
+    gl_FragColor = vec4(d, d, d, 1.0);
+}

=== added file 'data/shaders/function.vert'
--- data/shaders/function.vert	1970-01-01 00:00:00 +0000
+++ data/shaders/function.vert	2011-07-07 13:16:12 +0000
@@ -0,0 +1,30 @@
+attribute vec3 position;
+
+uniform mat4 ModelViewProjectionMatrix;
+
+// Removing this varying causes an inexplicable performance regression
+// with r600g... Keeping it for now.
+varying vec4 dummy;
+
+float process(float d)
+{
+    $PROCESS$
+    return d;
+}
+
+void main(void)
+{
+    dummy = vec4(1.0);
+
+    float d = fract(position.x);
+
+    $MAIN$
+
+    vec4 pos = vec4(position.x,
+                    position.y + 0.1 * d * fract(position.x),
+                    position.z, 1.0);
+
+    // Transform the position to clip coordinates
+    gl_Position = ModelViewProjectionMatrix * pos;
+}
+

=== modified file 'src/main.cpp'
--- src/main.cpp	2011-07-06 10:10:13 +0000
+++ src/main.cpp	2011-07-07 13:40:51 +0000
@@ -50,6 +50,8 @@
     "conditionals:vertex-steps=0:fragment-steps=0",
     "conditionals:vertex-steps=0:fragment-steps=5",
     "conditionals:vertex-steps=5:fragment-steps=0",
+    "function:fragment-steps=5:fragment-complexity=low",
+    "function:fragment-steps=5:fragment-complexity=medium",
     NULL
 };
 
@@ -210,6 +212,7 @@
     Benchmark::register_scene(*new SceneTexture(canvas));
     Benchmark::register_scene(*new SceneShading(canvas));
     Benchmark::register_scene(*new SceneConditionals(canvas));
+    Benchmark::register_scene(*new SceneFunction(canvas));
 
     if (Options::list_scenes) {
         list_scenes();

=== added file 'src/scene-function.cpp'
--- src/scene-function.cpp	1970-01-01 00:00:00 +0000
+++ src/scene-function.cpp	2011-07-07 13:35:24 +0000
@@ -0,0 +1,178 @@
+/*
+ * Copyright © 2010-2011 Linaro Limited
+ *
+ * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
+ *
+ * glmark2 is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * glmark2.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *  Alexandros Frantzis (glmark2)
+ */
+#include "scene.h"
+#include "mat.h"
+#include "stack.h"
+#include "vec.h"
+#include "log.h"
+
+#include <sstream>
+
+static const std::string shader_file_base(GLMARK_DATA_PATH"/shaders/function");
+
+static const std::string vtx_file(shader_file_base + ".vert");
+static const std::string frg_file(shader_file_base + ".frag");
+static const std::string call_file(shader_file_base + "-call.all");
+static const std::string step_low_file(shader_file_base + "-step-low.all");
+static const std::string step_medium_file(shader_file_base + "-step-medium.all");
+
+SceneFunction::SceneFunction(Canvas &pCanvas) :
+    SceneGrid(pCanvas, "function")
+{
+    mOptions["fragment-steps"] = Scene::Option("fragment-steps", "1",
+            "The number of computational steps in the fragment shader");
+    mOptions["fragment-function"] = Scene::Option("fragment-function", "true",
+            "Whether each computational step includes a function call");
+    mOptions["vertex-steps"] = Scene::Option("vertex-steps", "1",
+            "The number of computational steps in the vertex shader");
+    mOptions["vertex-function"] = Scene::Option("vertex-function", "true",
+            "Whether each computational step includes an if-else clause");
+    mOptions["vertex-complexity"] = Scene::Option("vertex-complexity", "low",
+            "The complexity of each computational step in the vertex shader");
+    mOptions["fragment-complexity"] = Scene::Option("fragment-complexity", "low",
+            "The complexity of each computational step in the fragment shader");
+}
+
+SceneFunction::~SceneFunction()
+{
+}
+
+static std::string &
+replace_string(std::string &str, const std::string &remove, const std::string &insert)
+{
+    std::string::size_type pos = 0;
+
+    while ((pos = str.find(remove, pos)) != std::string::npos) {
+        str.replace(pos, remove.size(), insert);
+        pos++;
+    }
+
+    return str;
+}
+
+static std::string
+get_vertex_shader_source(int steps, bool function, std::string &complexity)
+{
+    std::string vtx_string, step_low_string, step_medium_string, call_string;
+
+    if (!gotSource(vtx_file, vtx_string) ||
+        !gotSource(step_low_file, step_low_string) ||
+        !gotSource(step_medium_file, step_medium_string) ||
+        !gotSource(call_file, call_string))
+    {
+        return "";
+    }
+
+    std::stringstream ss_main;
+    std::string process_string;
+
+    if (complexity == "low")
+        process_string = step_low_string;
+    else if (complexity == "medium")
+        process_string = step_medium_string;
+
+    for (int i = 0; i < steps; i++) {
+        if (function)
+            ss_main << call_string;
+        else
+            ss_main << process_string;
+    }
+
+    replace_string(vtx_string, "$PROCESS$", function ? process_string : "");
+    replace_string(vtx_string, "$MAIN$", ss_main.str());
+
+    return vtx_string;
+}
+
+static std::string
+get_fragment_shader_source(int steps, bool function, std::string &complexity)
+{
+    std::string frg_string, step_low_string, step_medium_string, call_string;
+
+    if (!gotSource(frg_file, frg_string) ||
+        !gotSource(step_low_file, step_low_string) ||
+        !gotSource(step_medium_file, step_medium_string) ||
+        !gotSource(call_file, call_string))
+    {
+        return "";
+    }
+
+    std::stringstream ss_main;
+    std::string process_string;
+
+    if (complexity == "low")
+        process_string = step_low_string;
+    else if (complexity == "medium")
+        process_string = step_medium_string;
+
+    for (int i = 0; i < steps; i++) {
+        if (function)
+            ss_main << call_string;
+        else
+            ss_main << process_string;
+    }
+
+    replace_string(frg_string, "$PROCESS$", function ? process_string : "");
+    replace_string(frg_string, "$MAIN$", ss_main.str());
+
+    return frg_string;
+}
+
+void SceneFunction::setup()
+{
+    SceneGrid::setup();
+
+    /* Parse options */
+    bool vtx_function = mOptions["vertex-function"].value == "true";
+    bool frg_function = mOptions["fragment-function"].value == "true";
+    std::string vtx_complexity = mOptions["vertex-complexity"].value;
+    std::string frg_complexity = mOptions["fragment-complexity"].value;
+    int vtx_steps = 0;
+    int frg_steps = 0;
+
+    std::stringstream ss;
+
+    ss << mOptions["vertex-steps"].value;
+    ss >> vtx_steps;
+    ss.clear();
+    ss << mOptions["fragment-steps"].value;
+    ss >> frg_steps;
+
+    /* Load shaders */
+    std::string vtx_shader(get_vertex_shader_source(vtx_steps, vtx_function,
+                                                    vtx_complexity));
+    std::string frg_shader(get_fragment_shader_source(frg_steps, frg_function,
+                                                      frg_complexity));
+
+    if (!Scene::load_shaders_from_strings(mProgram, vtx_shader, frg_shader))
+        return;
+
+    mProgram.start();
+
+    std::vector<GLint> attrib_locations;
+    attrib_locations.push_back(mProgram.getAttribIndex("position"));
+    mMesh.set_attrib_locations(attrib_locations);
+
+    mRunning = true;
+    mStartTime = Scene::get_timestamp_us() / 1000000.0;
+    mLastUpdateTime = mStartTime;
+}

=== added file 'src/scene-grid.cpp'
--- src/scene-grid.cpp	1970-01-01 00:00:00 +0000
+++ src/scene-grid.cpp	2011-07-07 13:35:24 +0000
@@ -0,0 +1,139 @@
+/*
+ * Copyright © 2010-2011 Linaro Limited
+ *
+ * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
+ *
+ * glmark2 is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * glmark2.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors:
+ *  Alexandros Frantzis (glmark2)
+ */
+#include "scene.h"
+#include "mat.h"
+#include "stack.h"
+#include "vec.h"
+#include "log.h"
+
+#include <sstream>
+
+SceneGrid::SceneGrid(Canvas &pCanvas, const std::string &name) :
+    Scene(pCanvas, name)
+{
+    mOptions["grid-size"] = Scene::Option("grid-size", "32",
+            "The number of squares per side of the grid (controls the number of vertices)");
+    mOptions["grid-length"] = Scene::Option("grid-length", "5.0",
+            "The length of each side of the grid (normalized) (controls the area drawn to)");
+}
+
+SceneGrid::~SceneGrid()
+{
+}
+
+int SceneGrid::load()
+{
+    mRotationSpeed = 36.0f;
+    mRunning = false;
+
+    return 1;
+}
+
+void SceneGrid::unload()
+{
+}
+
+void SceneGrid::setup()
+{
+    Scene::setup();
+
+    int grid_size = 0;
+    double grid_length = 0;
+
+    std::stringstream ss;
+
+    ss << mOptions["grid-size"].value;
+    ss >> grid_size;
+    ss.clear();
+    ss << mOptions["grid-length"].value;
+    ss >> grid_length;
+
+    /* Create and configure the grid mesh */
+    std::vector<int> vertex_format;
+    vertex_format.push_back(3);
+    mMesh.set_vertex_format(vertex_format);
+
+    /* 
+     * The spacing needed in order for the area of the requested grid
+     * to be the same as the area of a grid with size 32 and spacing 0.02.
+     */
+    double spacing = grid_length * (1 - 4.38 / 5.0) / (grid_size - 1.0);
+
+    mMesh.make_grid(grid_size, grid_size, grid_length, grid_length,
+                    grid_size > 1 ? spacing : 0);
+    mMesh.build_vbo();
+
+    std::vector<GLint> attrib_locations;
+    attrib_locations.push_back(mProgram.getAttribIndex("position"));
+    mMesh.set_attrib_locations(attrib_locations);
+
+    mCurrentFrame = 0;
+    mRotation = 0.0f;
+}
+
+void SceneGrid::teardown()
+{
+    mProgram.stop();
+    mProgram.release();
+    mMesh.reset();
+
+    Scene::teardown();
+}
+
+void SceneGrid::update()
+{
+    double current_time = Scene::get_timestamp_us() / 1000000.0;
+    double dt = current_time - mLastUpdateTime;
+    double elapsed_time = current_time - mStartTime;
+
+    mLastUpdateTime = current_time;
+
+    if (elapsed_time >= mDuration) {
+        mAverageFPS = mCurrentFrame / elapsed_time;
+        mRunning = false;
+    }
+
+    mRotation += mRotationSpeed * dt;
+
+    mCurrentFrame++;
+}
+
+void SceneGrid::draw()
+{
+    // Load the ModelViewProjectionMatrix uniform in the shader
+    LibMatrix::Stack4 model_view;
+    LibMatrix::mat4 model_view_proj(mCanvas.projection());
+
+    model_view.translate(0.0f, 0.0f, -5.0f);
+    model_view.rotate(mRotation, 0.0f, 0.0f, 1.0f);
+    model_view_proj *= model_view.getCurrent();
+
+    mProgram.loadUniformMatrix(model_view_proj, "ModelViewProjectionMatrix");
+
+    mMesh.render_vbo();
+}
+
+Scene::ValidationResult
+SceneGrid::validate()
+{
+    return Scene::ValidationUnknown;
+}

=== modified file 'src/scene.h'
--- src/scene.h	2011-07-04 13:22:56 +0000
+++ src/scene.h	2011-07-07 13:35:24 +0000
@@ -205,6 +205,28 @@
     float mRotationSpeed;
 };
 
+class SceneGrid : public Scene
+{
+public:
+    SceneGrid(Canvas &pCanvas, const std::string &name);
+    virtual int load();
+    virtual void unload();
+    virtual void setup();
+    virtual void teardown();
+    virtual void update();
+    virtual void draw();
+    virtual ValidationResult validate();
+
+    ~SceneGrid();
+
+protected:
+    Program mProgram;
+
+    Mesh mMesh;
+    float mRotation;
+    float mRotationSpeed;
+};
+
 class SceneConditionals : public Scene
 {
 public:
@@ -226,4 +248,13 @@
     float mRotation;
     float mRotationSpeed;
 };
+
+class SceneFunction : public SceneGrid
+{
+public:
+    SceneFunction(Canvas &pCanvas);
+    void setup();
+
+    ~SceneFunction();
+};
 #endif

