diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 104: Merge implementation of shader loop benchmarking scene.

Message ID 20110708135719.6253.51745.launchpad@loganberry.canonical.com
State Accepted
Headers show

Commit Message

alexandros.frantzis@linaro.org July 8, 2011, 1:57 p.m. UTC
Merge authors:
  Alexandros Frantzis (afrantzis)
------------------------------------------------------------
revno: 104 [merge]
committer: Alexandros Frantzis <alexandros.frantzis@linaro.org>
branch nick: trunk
timestamp: Fri 2011-07-08 16:55:40 +0300
message:
  Merge implementation of shader loop benchmarking scene.
added:
  data/shaders/loop-step-loop.all
  data/shaders/loop-step-simple.all
  data/shaders/loop.frag
  data/shaders/loop.vert
  src/scene-loop.cpp
modified:
  src/main.cpp
  src/scene.h


--
lp:glmark2
https://code.launchpad.net/~glmark2-dev/glmark2/trunk

You are subscribed to branch lp:glmark2.
To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription
diff mbox

Patch

=== added file 'data/shaders/loop-step-loop.all'
--- data/shaders/loop-step-loop.all	1970-01-01 00:00:00 +0000
+++ data/shaders/loop-step-loop.all	2011-07-08 12:48:36 +0000
@@ -0,0 +1,2 @@ 
+    for (int i = 0; i < $NLOOPS$; i++)
+        d = fract(3.0 * d);

=== added file 'data/shaders/loop-step-simple.all'
--- data/shaders/loop-step-simple.all	1970-01-01 00:00:00 +0000
+++ data/shaders/loop-step-simple.all	2011-07-08 12:48:36 +0000
@@ -0,0 +1,1 @@ 
+    d = fract(3.0 * d);

=== added file 'data/shaders/loop.frag'
--- data/shaders/loop.frag	1970-01-01 00:00:00 +0000
+++ data/shaders/loop.frag	2011-07-08 12:48:36 +0000
@@ -0,0 +1,16 @@ 
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+varying vec4 dummy;
+uniform int FragmentLoops;
+
+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/loop.vert'
--- data/shaders/loop.vert	1970-01-01 00:00:00 +0000
+++ data/shaders/loop.vert	2011-07-08 12:48:36 +0000
@@ -0,0 +1,26 @@ 
+attribute vec3 position;
+
+uniform mat4 ModelViewProjectionMatrix;
+uniform int VertexLoops;
+
+// Removing this varying causes an inexplicable performance regression
+// with r600g... Keeping it for now.
+varying vec4 dummy;
+
+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-07 13:40:51 +0000
+++ src/main.cpp	2011-07-08 13:46:32 +0000
@@ -52,6 +52,9 @@ 
     "conditionals:vertex-steps=5:fragment-steps=0",
     "function:fragment-steps=5:fragment-complexity=low",
     "function:fragment-steps=5:fragment-complexity=medium",
+    "loop:vertex-steps=5:fragment-steps=5:fragment-loop=false",
+    "loop:vertex-steps=5:fragment-steps=5:fragment-uniform=false",
+    "loop:vertex-steps=5:fragment-steps=5:fragment-uniform=true",
     NULL
 };
 
@@ -213,6 +216,7 @@ 
     Benchmark::register_scene(*new SceneShading(canvas));
     Benchmark::register_scene(*new SceneConditionals(canvas));
     Benchmark::register_scene(*new SceneFunction(canvas));
+    Benchmark::register_scene(*new SceneLoop(canvas));
 
     if (Options::list_scenes) {
         list_scenes();

=== added file 'src/scene-loop.cpp'
--- src/scene-loop.cpp	1970-01-01 00:00:00 +0000
+++ src/scene-loop.cpp	2011-07-08 13:00:36 +0000
@@ -0,0 +1,183 @@ 
+/*
+ * 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/loop");
+
+static const std::string vtx_file(shader_file_base + ".vert");
+static const std::string frg_file(shader_file_base + ".frag");
+static const std::string step_simple_file(shader_file_base + "-step-simple.all");
+static const std::string step_loop_file(shader_file_base + "-step-loop.all");
+
+SceneLoop::SceneLoop(Canvas &pCanvas) :
+    SceneGrid(pCanvas, "loop")
+{
+    mOptions["fragment-steps"] = Scene::Option("fragment-steps", "1",
+            "The number of computational steps in the fragment shader");
+    mOptions["fragment-loop"] = Scene::Option("fragment-function", "true",
+            "Whether to execute the steps in the vertex shader using a for loop");
+    mOptions["vertex-steps"] = Scene::Option("vertex-steps", "1",
+            "The number of computational steps in the vertex shader");
+    mOptions["vertex-loop"] = Scene::Option("vertex-function", "true",
+            "Whether to execute the steps in the vertex shader using a for loop");
+    mOptions["vertex-uniform"] = Scene::Option("vertex-uniform", "true",
+            "Whether to use a uniform in the vertex shader for the number of loop iterations to perform (i.e. vertex-steps)");
+    mOptions["fragment-uniform"] = Scene::Option("fragment-uniform", "true",
+            "Whether to use a uniform in the fragment shader for the number of loop iterations to perform (i.e. fragment-steps)");
+}
+
+SceneLoop::~SceneLoop()
+{
+}
+
+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_fragment_shader_source(int steps, bool loop, bool uniform)
+{
+    std::string frg_string, step_simple_string, step_loop_string;
+
+    if (!gotSource(frg_file, frg_string) ||
+        !gotSource(step_simple_file, step_simple_string) ||
+        !gotSource(step_loop_file, step_loop_string))
+    {
+        return "";
+    }
+
+    std::stringstream ss_main;
+
+    if (loop) {
+        if (uniform) {
+            replace_string(step_loop_string, "$NLOOPS$", "FragmentLoops");
+        }
+        else {
+            std::stringstream ss_steps;
+            ss_steps << steps;
+            replace_string(step_loop_string, "$NLOOPS$", ss_steps.str());
+        }
+        ss_main << step_loop_string;
+    }
+    else {
+        for (int i = 0; i < steps; i++)
+            ss_main << step_simple_string;
+    }
+
+    replace_string(frg_string, "$MAIN$", ss_main.str());
+
+    return frg_string;
+}
+
+static std::string
+get_vertex_shader_source(int steps, bool loop, bool uniform)
+{
+    std::string vtx_string, step_simple_string, step_loop_string;
+
+    if (!gotSource(vtx_file, vtx_string) ||
+        !gotSource(step_simple_file, step_simple_string) ||
+        !gotSource(step_loop_file, step_loop_string))
+    {
+        return "";
+    }
+
+    std::stringstream ss_main;
+
+    if (loop) {
+        if (uniform) {
+            replace_string(step_loop_string, "$NLOOPS$", "VertexLoops");
+        }
+        else {
+            std::stringstream ss_steps;
+            ss_steps << steps;
+            replace_string(step_loop_string, "$NLOOPS$", ss_steps.str());
+        }
+        ss_main << step_loop_string;
+    }
+    else {
+        for (int i = 0; i < steps; i++)
+            ss_main << step_simple_string;
+    }
+
+    replace_string(vtx_string, "$MAIN$", ss_main.str());
+
+    return vtx_string;
+}
+
+
+void SceneLoop::setup()
+{
+    SceneGrid::setup();
+
+    /* Parse options */
+    bool vtx_loop = mOptions["vertex-loop"].value == "true";
+    bool frg_loop = mOptions["fragment-loop"].value == "true";
+    bool vtx_uniform = mOptions["vertex-uniform"].value == "true";
+    bool frg_uniform = mOptions["fragment-uniform"].value == "true";
+    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_loop,
+                                                    vtx_uniform));
+    std::string frg_shader(get_fragment_shader_source(frg_steps, frg_loop,
+                                                      frg_uniform));
+
+    if (!Scene::load_shaders_from_strings(mProgram, vtx_shader, frg_shader))
+        return;
+
+    mProgram.start();
+
+    mProgram.loadUniformScalar(vtx_steps, "VertexLoops");
+    mProgram.loadUniformScalar(frg_steps, "FragmentLoops");
+
+    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;
+}

=== modified file 'src/scene.h'
--- src/scene.h	2011-07-07 13:45:19 +0000
+++ src/scene.h	2011-07-08 12:48:36 +0000
@@ -244,4 +244,13 @@ 
 
     ~SceneFunction();
 };
+
+class SceneLoop : public SceneGrid
+{
+public:
+    SceneLoop(Canvas &pCanvas);
+    void setup();
+
+    ~SceneLoop();
+};
 #endif