From patchwork Thu Aug 18 15:38:12 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: alexandros.frantzis@linaro.org X-Patchwork-Id: 3514 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 4CAB423F27 for ; Thu, 18 Aug 2011 15:38:14 +0000 (UTC) Received: from mail-ew0-f52.google.com (mail-ew0-f52.google.com [209.85.215.52]) by fiordland.canonical.com (Postfix) with ESMTP id 2E5F7A18171 for ; Thu, 18 Aug 2011 15:38:14 +0000 (UTC) Received: by ewy28 with SMTP id 28so1122704ewy.11 for ; Thu, 18 Aug 2011 08:38:14 -0700 (PDT) Received: by 10.213.14.67 with SMTP id f3mr1795407eba.56.1313681893819; Thu, 18 Aug 2011 08:38:13 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.213.102.5 with SMTP id e5cs80391ebo; Thu, 18 Aug 2011 08:38:13 -0700 (PDT) Received: by 10.227.24.146 with SMTP id v18mr741696wbb.84.1313681893304; Thu, 18 Aug 2011 08:38:13 -0700 (PDT) Received: from indium.canonical.com (indium.canonical.com [91.189.90.7]) by mx.google.com with ESMTPS id ev1si5981624wbb.53.2011.08.18.08.38.12 (version=TLSv1/SSLv3 cipher=OTHER); Thu, 18 Aug 2011 08:38:13 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) client-ip=91.189.90.7; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of bounces@canonical.com designates 91.189.90.7 as permitted sender) smtp.mail=bounces@canonical.com Received: from ackee.canonical.com ([91.189.89.26]) by indium.canonical.com with esmtp (Exim 4.71 #1 (Debian)) id 1Qu4fc-0002vO-MT for ; Thu, 18 Aug 2011 15:38:12 +0000 Received: from ackee.canonical.com (localhost [127.0.0.1]) by ackee.canonical.com (Postfix) with ESMTP id A01DAE033E for ; Thu, 18 Aug 2011 15:38:12 +0000 (UTC) MIME-Version: 1.0 X-Launchpad-Project: glmark2 X-Launchpad-Branch: ~glmark2-dev/glmark2/trunk X-Launchpad-Message-Rationale: Subscriber X-Launchpad-Branch-Revision-Number: 125 X-Launchpad-Notification-Type: branch-revision To: Linaro Patch Tracker From: noreply@launchpad.net Subject: [Branch ~glmark2-dev/glmark2/trunk] Rev 125: Merge pulsar scene. Message-Id: <20110818153812.25735.33493.launchpad@ackee.canonical.com> Date: Thu, 18 Aug 2011 15:38:12 -0000 Reply-To: noreply@launchpad.net Sender: bounces@canonical.com Errors-To: bounces@canonical.com Precedence: bulk X-Generated-By: Launchpad (canonical.com); Revision="13697"; Instance="initZopeless config overlay" X-Launchpad-Hash: 7df4bae32e483544fef20ebcfc35dde95758906b Merge authors: Alexandros Frantzis (afrantzis) Marc Ordinas i Llopis (marcoil) Related merge proposals: https://code.launchpad.net/~marcoil/glmark2/pulsar/+merge/71831 proposed by: Marc Ordinas i Llopis (marcoil) review: Needs Fixing - Alexandros Frantzis (afrantzis) ------------------------------------------------------------ revno: 125 [merge] committer: Alexandros Frantzis branch nick: trunk timestamp: Thu 2011-08-18 18:19:08 +0300 message: Merge pulsar scene. added: data/shaders/pulsar-light.vert data/shaders/pulsar.vert src/scene-pulsar.cpp modified: src/android.cpp 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 === added file 'data/shaders/pulsar-light.vert' --- data/shaders/pulsar-light.vert 1970-01-01 00:00:00 +0000 +++ data/shaders/pulsar-light.vert 2011-08-17 09:21:24 +0000 @@ -0,0 +1,31 @@ +attribute vec3 position; +attribute vec3 normal; +attribute vec4 vtxcolor; +attribute vec2 texcoord; + +uniform mat4 ModelViewProjectionMatrix; +uniform mat4 NormalMatrix; + +varying vec4 Color; +varying vec2 TextureCoord; + +void main(void) +{ + // Transform the normal to eye coordinates + vec3 N = normalize(vec3(NormalMatrix * vec4(normal, 1.0))); + + // The LightSourcePosition is actually its direction for directional light + vec3 L = normalize(LightSourcePosition.xyz); + + // Multiply the diffuse value by the vertex color + // to get the actual color that we will use to draw this vertex with + float diffuse = max(abs(dot(N, L)), 0.0); + Color = diffuse * vtxcolor; + + // Set the texture coordinates as a varying + TextureCoord = texcoord; + + // Transform the position to clip coordinates + gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0); +} + === added file 'data/shaders/pulsar.vert' --- data/shaders/pulsar.vert 1970-01-01 00:00:00 +0000 +++ data/shaders/pulsar.vert 2011-08-17 09:21:24 +0000 @@ -0,0 +1,19 @@ +attribute vec3 position; +attribute vec4 vtxcolor; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 ModelViewProjectionMatrix; + +varying vec4 Color; +varying vec2 TextureCoord; + +void main(void) +{ + Color = vtxcolor; + TextureCoord = texcoord; + + // Transform the position to clip coordinates + gl_Position = ModelViewProjectionMatrix * vec4(position, 1.0); +} + === modified file 'src/android.cpp' --- src/android.cpp 2011-08-16 13:10:09 +0000 +++ src/android.cpp 2011-08-18 15:03:40 +0000 @@ -43,6 +43,7 @@ "bump:bump-render=normals", "effect2d:kernel=0,1,0;1,-4,1;0,1,0;", "effect2d:kernel=1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;", + "pulsar:quads=5:texture=false:light=false", "conditionals:vertex-steps=0:fragment-steps=0", "conditionals:vertex-steps=0:fragment-steps=5", "conditionals:vertex-steps=5:fragment-steps=0", @@ -83,6 +84,7 @@ Benchmark::register_scene(*new SceneLoop(*g_canvas)); Benchmark::register_scene(*new SceneBump(*g_canvas)); Benchmark::register_scene(*new SceneEffect2D(*g_canvas)); + Benchmark::register_scene(*new ScenePulsar(*g_canvas)); add_default_benchmarks(g_benchmarks); === modified file 'src/main.cpp' --- src/main.cpp 2011-08-09 11:13:43 +0000 +++ src/main.cpp 2011-08-18 15:03:40 +0000 @@ -52,6 +52,7 @@ "bump:bump-render=normals", "effect2d:kernel=0,1,0;1,-4,1;0,1,0;", "effect2d:kernel=1,1,1,1,1;1,1,1,1,1;1,1,1,1,1;", + "pulsar:quads=5:texture=false:light=false", "conditionals:vertex-steps=0:fragment-steps=0", "conditionals:vertex-steps=0:fragment-steps=5", "conditionals:vertex-steps=5:fragment-steps=0", @@ -229,6 +230,7 @@ Benchmark::register_scene(*new SceneLoop(canvas)); Benchmark::register_scene(*new SceneBump(canvas)); Benchmark::register_scene(*new SceneEffect2D(canvas)); + Benchmark::register_scene(*new ScenePulsar(canvas)); if (Options::list_scenes) { list_scenes(); === added file 'src/scene-pulsar.cpp' --- src/scene-pulsar.cpp 1970-01-01 00:00:00 +0000 +++ src/scene-pulsar.cpp 2011-08-18 13:43:31 +0000 @@ -0,0 +1,294 @@ +/* + * Copyright © 2008 Ben Smith + * 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 . + * + * Authors: + * Ben Smith (original glmark benchmark) + * Alexandros Frantzis (glmark2) + * Marc Ordinas i Llopis, Collabora Ltd. (pulsar scene) + */ +#include "scene.h" +#include "mat.h" +#include "stack.h" +#include "vec.h" +#include "log.h" +#include "program.h" +#include "shader-source.h" + +#include + +ScenePulsar::ScenePulsar(Canvas &pCanvas) : + Scene(pCanvas, "pulsar"), + mTexture(0) +{ + mOptions["quads"] = Scene::Option("quads", "5", "Number of quads to render"); + mOptions["texture"] = Scene::Option("texture", "false", "Enable texturing"); + mOptions["light"] = Scene::Option("light", "false", "Enable lighting"); + mOptions["random"] = Scene::Option("random", "false", "Enable random rotation speeds"); +} + +ScenePulsar::~ScenePulsar() +{ +} + +int ScenePulsar::load() +{ + mScale = LibMatrix::vec3(1.0, 1.0, 1.0); + + mRunning = false; + + return 1; +} + +void ScenePulsar::unload() +{ +} + +void ScenePulsar::setup() +{ + Scene::setup(); + + // Disable back-face culling + glDisable(GL_CULL_FACE); + // Enable alpha blending + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + // Create a rotation for each quad. + std::stringstream ss; + ss << mOptions["quads"].value; + ss >> mNumQuads; + + srand((unsigned)time(0)); + for (int i = 0; i < mNumQuads; i++) { + mRotations.push_back(LibMatrix::vec3()); + if (mOptions["random"].value == "true") { + mRotationSpeeds.push_back(LibMatrix::vec3(((float)rand() / (float)RAND_MAX) * 5.0, + ((float)rand() / (float)RAND_MAX) * 5.0, + 0.0)); + } + else { + float integral; + float x_rot = std::modf((i + 1) * M_PI, &integral); + float y_rot = std::modf((i + 1) * M_E, &integral); + mRotationSpeeds.push_back(LibMatrix::vec3(x_rot * 5.0, + y_rot * 5.0, + 0.0)); + } + } + + // Load shaders + std::string vtx_shader_filename; + std::string frg_shader_filename; + static const LibMatrix::vec4 lightPosition(-20.0f, 20.0f,-20.0f, 1.0f); + if (mOptions["light"].value == "true") { + vtx_shader_filename = GLMARK_DATA_PATH"/shaders/pulsar-light.vert"; + } else { + vtx_shader_filename = GLMARK_DATA_PATH"/shaders/pulsar.vert"; + } + + if (mOptions["texture"].value == "true") { + frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic-tex.frag"; + Texture::load(GLMARK_DATA_PATH"/textures/crate-base.png", &mTexture, + GL_NEAREST, GL_NEAREST, 0); + + } else { + frg_shader_filename = GLMARK_DATA_PATH"/shaders/light-basic.frag"; + } + + ShaderSource vtx_source(vtx_shader_filename); + ShaderSource frg_source(frg_shader_filename); + if (mOptions["light"].value == "true") { + // Load the light position constant + vtx_source.add_const("LightSourcePosition", lightPosition); + } + + if (!Scene::load_shaders_from_strings(mProgram, vtx_source.str(), + frg_source.str())) + { + return; + } + + create_and_setup_mesh(); + + mProgram.start(); + + mCurrentFrame = 0; + + mRunning = true; + mStartTime = Scene::get_timestamp_us() / 1000000.0; + mLastUpdateTime = mStartTime; +} + +void ScenePulsar::teardown() +{ + mProgram.stop(); + mProgram.release(); + + if (mOptions["texture"].value == "true") { + glDeleteTextures(1, &mTexture); + mTexture = 0; + } + + // Re-enable back-face culling + glEnable(GL_CULL_FACE); + // Disable alpha blending + glDisable(GL_BLEND); + + mPlaneMesh.reset(); + + Scene::teardown(); +} + +void ScenePulsar::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; + } + + for (int i = 0; i < mNumQuads; i++) { + mRotations[i] += mRotationSpeeds[i] * (dt * 60); + } + + mScale = LibMatrix::vec3(cos(elapsed_time / 3.60) * 10.0, sin(elapsed_time / 3.60) * 10.0, 1.0); + + mCurrentFrame++; +} + +void ScenePulsar::draw() +{ + if (mOptions["texture"].value == "true") { + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, mTexture); + } + + for (int i = 0; i < mNumQuads; i++) { + // Load the ModelViewProjectionMatrix uniform in the shader + LibMatrix::Stack4 model_view; + LibMatrix::mat4 model_view_proj(mCanvas.projection()); + model_view.scale(mScale.x(), mScale.y(), mScale.z()); + model_view.translate(0.0f, 0.0f, -10.0f); + model_view.rotate(mRotations[i].x(), 1.0f, 0.0f, 0.0f); + model_view.rotate(mRotations[i].y(), 0.0f, 1.0f, 0.0f); + model_view.rotate(mRotations[i].z(), 0.0f, 0.0f, 1.0f); + model_view_proj *= model_view.getCurrent(); + mProgram.loadUniformMatrix(model_view_proj, "ModelViewProjectionMatrix"); + + if (mOptions["light"].value == "true") { + // Load the NormalMatrix uniform in the shader. The NormalMatrix is the + // inverse transpose of the model view matrix. + LibMatrix::mat4 normal_matrix(model_view.getCurrent()); + normal_matrix.inverse().transpose(); + mProgram.loadUniformMatrix(normal_matrix, "NormalMatrix"); + } + + mPlaneMesh.render_vbo(); + } +} + +Scene::ValidationResult +ScenePulsar::validate() +{ + return ValidationUnknown; +} + +void ScenePulsar::create_and_setup_mesh() +{ + bool texture = mOptions["texture"].value == "true"; + bool light = mOptions["light"].value == "true"; + + struct PlaneMeshVertex { + LibMatrix::vec3 position; + LibMatrix::vec4 color; + LibMatrix::vec2 texcoord; + LibMatrix::vec3 normal; + }; + + PlaneMeshVertex plane_vertices[] = { + { + LibMatrix::vec3(-1.0, -1.0, 0.0), + LibMatrix::vec4(1.0, 0.0, 0.0, 0.4), + LibMatrix::vec2(0.0, 0.0), + LibMatrix::vec3(0.0, 0.0, 1.0) + }, + { + LibMatrix::vec3(-1.0, 1.0, 0.0), + LibMatrix::vec4(0.0, 1.0, 0.0, 0.4), + LibMatrix::vec2(0.0, 1.0), + LibMatrix::vec3(0.0, 0.0, 1.0) + }, + { + LibMatrix::vec3(1.0, 1.0, 0.0), + LibMatrix::vec4(0.0, 0.0, 1.0, 0.4), + LibMatrix::vec2(1.0, 1.0), + LibMatrix::vec3(0.0, 0.0, 1.0) + }, + { + LibMatrix::vec3(1.0, -1.0, 0.0), + LibMatrix::vec4(1.0, 1.0, 1.0, 1.0), + LibMatrix::vec2(1.0, 0.0), + LibMatrix::vec3(0.0, 0.0, 1.0) + } + }; + + unsigned int vertex_index[] = {0, 1, 2, 0, 2, 3}; + + // Set vertex format + std::vector vertex_format; + vertex_format.push_back(3); // Position + vertex_format.push_back(4); // Color + if (texture) + vertex_format.push_back(2); // Texcoord + if (light) + vertex_format.push_back(3); // Normal + + mPlaneMesh.set_vertex_format(vertex_format); + + // Build the plane mesh + for (size_t i = 0; i < sizeof(vertex_index) / sizeof(*vertex_index); i++) { + PlaneMeshVertex& vertex = plane_vertices[vertex_index[i]]; + + mPlaneMesh.next_vertex(); + mPlaneMesh.set_attrib(0, vertex.position); + mPlaneMesh.set_attrib(1, vertex.color); + if (texture) + mPlaneMesh.set_attrib(2, vertex.texcoord); + if (light) + mPlaneMesh.set_attrib(2 + ((int)texture), vertex.normal); + } + + mPlaneMesh.build_vbo(); + + // Set attribute locations + std::vector attrib_locations; + attrib_locations.push_back(mProgram.getAttribIndex("position")); + attrib_locations.push_back(mProgram.getAttribIndex("vtxcolor")); + if (texture) + attrib_locations.push_back(mProgram.getAttribIndex("texcoord")); + if (light) + attrib_locations.push_back(mProgram.getAttribIndex("normal")); + mPlaneMesh.set_attrib_locations(attrib_locations); +} + === modified file 'src/scene.h' --- src/scene.h 2011-08-16 07:37:52 +0000 +++ src/scene.h 2011-08-18 13:20:04 +0000 @@ -20,6 +20,7 @@ * Authors: * Ben Smith (original glmark benchmark) * Alexandros Frantzis (glmark2) + * Marc Ordinas i Llopis, Collabora Ltd. (pulsar scene) */ #ifndef GLMARK2_SCENE_H_ #define GLMARK2_SCENE_H_ @@ -300,4 +301,34 @@ Mesh mesh_; GLuint texture_; }; + +class ScenePulsar : public Scene +{ +public: + ScenePulsar(Canvas &pCanvas); + int load(); + void unload(); + void setup(); + void teardown(); + void update(); + void draw(); + ValidationResult validate(); + + ~ScenePulsar(); + +protected: + int mNumQuads; + Program mProgram; + + Mesh mPlaneMesh; + LibMatrix::vec3 mScale; + std::vector mRotations; + std::vector mRotationSpeeds; + + GLuint mTexture; + +private: + void create_and_setup_mesh(); +}; + #endif