diff options
author | gman@chromium.org <gman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-15 22:15:44 +0000 |
---|---|---|
committer | gman@chromium.org <gman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-15 22:15:44 +0000 |
commit | e19228473a76277a68697115fa1ab8724f3f848a (patch) | |
tree | 23959d937bf329d041788345ea67555d9025277a /gpu | |
parent | 44a63205dee865aed6f6daad8170c1b695267fe1 (diff) | |
download | chromium_src-e19228473a76277a68697115fa1ab8724f3f848a.zip chromium_src-e19228473a76277a68697115fa1ab8724f3f848a.tar.gz chromium_src-e19228473a76277a68697115fa1ab8724f3f848a.tar.bz2 |
Make Occlusion Query Sample
BUG=88601
TEST=this is the test
Make Occlusion Query test
Review URL: http://codereview.chromium.org/9699048
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127009 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gpu')
-rw-r--r-- | gpu/demos/demos.gyp | 11 | ||||
-rw-r--r-- | gpu/demos/framework/window.cc | 15 | ||||
-rw-r--r-- | gpu/demos/framework/window.h | 7 | ||||
-rw-r--r-- | gpu/demos/framework/window_linux.cc | 4 | ||||
-rw-r--r-- | gpu/demos/occlusion_query/occlusion_query.cc | 312 |
5 files changed, 342 insertions, 7 deletions
diff --git a/gpu/demos/demos.gyp b/gpu/demos/demos.gyp index 802b328e..5b2fdb4 100644 --- a/gpu/demos/demos.gyp +++ b/gpu/demos/demos.gyp @@ -218,6 +218,17 @@ 'compressed_textures/compressed_textures.cc', ], }, + { + 'target_name': 'occlusion_query_exe', + 'type': 'executable', + 'dependencies': [ + 'gpu_demo_framework_exe', + '../../third_party/gles2_book/gles2_book.gyp:es_util', + ], + 'sources': [ + 'occlusion_query/occlusion_query.cc', + ], + }, ], 'conditions': [ ['enable_pepper_demos==1', { diff --git a/gpu/demos/framework/window.cc b/gpu/demos/framework/window.cc index c20a55a..87ae536 100644 --- a/gpu/demos/framework/window.cc +++ b/gpu/demos/framework/window.cc @@ -36,6 +36,13 @@ Window::Window() } Window::~Window() { + demo_.reset(); + + // must free client before service. + gles2_implementation_.reset(); + transfer_buffer_.reset(); + gles2_cmd_helper_.reset(); + if (decoder_.get()) { decoder_->Destroy(); } @@ -108,15 +115,15 @@ bool Window::CreateRenderContext(gfx::AcceleratedWidget hwnd) { transfer_buffer_.reset(new gpu::TransferBuffer(gles2_cmd_helper_.get())); ::gles2::Initialize(); - GLES2Implementation* gles2_implementation = new GLES2Implementation( + gles2_implementation_.reset(new GLES2Implementation( gles2_cmd_helper_.get(), transfer_buffer_.get(), false, - true); + true)); - ::gles2::SetGLContext(gles2_implementation); + ::gles2::SetGLContext(gles2_implementation_.get()); - if (!gles2_implementation->Initialize( + if (!gles2_implementation_->Initialize( kTransferBufferSize, kTransferBufferSize, kTransferBufferSize)) { diff --git a/gpu/demos/framework/window.h b/gpu/demos/framework/window.h index fe8a39a..956f60f1 100644 --- a/gpu/demos/framework/window.h +++ b/gpu/demos/framework/window.h @@ -18,6 +18,12 @@ namespace gpu { class TransferBuffer; +namespace gles2 { + +class GLES2Implementation; + +} // namespace gles2 + namespace demos { class Demo; @@ -56,6 +62,7 @@ class Window { scoped_refptr<gfx::GLSurface> surface_; scoped_ptr<gpu::gles2::GLES2CmdHelper> gles2_cmd_helper_; scoped_ptr<gpu::TransferBuffer> transfer_buffer_; + scoped_ptr<gpu::gles2::GLES2Implementation> gles2_implementation_; DISALLOW_COPY_AND_ASSIGN(Window); }; diff --git a/gpu/demos/framework/window_linux.cc b/gpu/demos/framework/window_linux.cc index d805d3d..552bb97 100644 --- a/gpu/demos/framework/window_linux.cc +++ b/gpu/demos/framework/window_linux.cc @@ -21,9 +21,7 @@ gboolean OnExpose(GtkWidget* widget, GdkEventExpose* event, gpointer data) { Window* window = static_cast<Window*>(data); window->OnPaint(); - // TODO(alokp): Figure out why this is crashing. Animation will not work - // until then. - //gtk_widget_queue_draw(widget); + gtk_widget_queue_draw(widget); return FALSE; } } // namespace diff --git a/gpu/demos/occlusion_query/occlusion_query.cc b/gpu/demos/occlusion_query/occlusion_query.cc new file mode 100644 index 0000000..4cab2e8 --- /dev/null +++ b/gpu/demos/occlusion_query/occlusion_query.cc @@ -0,0 +1,312 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> +#include <math.h> +#include <stdio.h> +#include <string> + +// Some tests for occlusion queries. +#include "gpu/demos/framework/demo_factory.h" +#include "gpu/demos/gles2_book/example.h" + +namespace gpu { +namespace demos { + +namespace { + +class OcclusionQueryTest; + +struct STUserData { + OcclusionQueryTest* demo; +}; + +class OcclusionQueryTest : public gles2_book::Example<STUserData> { + public: + OcclusionQueryTest() + : elasped_sec_(0), + program_(0), + matrix_loc_(0), + color_loc_(0), + vbo_(0), + query_(0), + clock_(0), + last_query_status_(0) { + test_ = this; + RegisterCallbacks(stInit, stUpdate, stDraw, stShutDown); + } + + const wchar_t* Title() const { + return L"Occlusion Query Test"; + } + + bool IsAnimated() { + return true; + } + + private: + int Init(ESContext* esContext); + void Update(ESContext* esContext, float elapsed_sec); + void Draw(ESContext* esContext); + void ShutDown(ESContext* esContext); + + void InitShaders(); + void DrawRect(float x, float z, float scale, float* color); + + static int stInit(ESContext* esContext) { + static_cast<STUserData*>(esContext->userData)->demo = test_; + test_ = NULL; + return static_cast<STUserData*>(esContext->userData)->demo->Init(esContext); + } + + static void stUpdate (ESContext* esContext, float elapsed_sec) { + static_cast<STUserData*>(esContext->userData)->demo->Update( + esContext, elapsed_sec); + } + + static void stShutDown (ESContext* esContext) { + static_cast<STUserData*>(esContext->userData)->demo->ShutDown( + esContext); + } + + static void stDraw (ESContext* esContext) { + static_cast<STUserData*>(esContext->userData)->demo->Draw( + esContext); + } + + // This is the test being created. Because the GLES2 book's framework + // has no way to pass anything into the init funciton this is a hacky + // workaround. + static OcclusionQueryTest* test_; + + float elasped_sec_; + GLuint program_; + GLint matrix_loc_; + GLint color_loc_; + GLuint vbo_; + GLuint query_; + float clock_; + GLuint last_query_status_; + + static float red_[4]; + static float green_[4]; + static float blue_[4]; +}; + +OcclusionQueryTest* OcclusionQueryTest::test_; +float OcclusionQueryTest::red_[4] = { 1, 0, 0, 1, }; +float OcclusionQueryTest::green_[4] = { 0, 1, 0, 1, }; +float OcclusionQueryTest::blue_[4] = { 0, 0, 1, 1, }; + +void CheckGLError(const char* func_name, int line_no) { +#ifndef NDEBUG + GLenum error = GL_NO_ERROR; + while ((error = glGetError()) != GL_NO_ERROR) { + fprintf(stderr, "GL ERROR in %s at line %d : 0x%04x\n", + func_name, line_no, error); + } +#endif +} + +GLuint LoadShader(GLenum type, const char* shaderSrc) { + CheckGLError("LoadShader", __LINE__); + GLuint shader = glCreateShader(type); + if (shader == 0) { + return 0; + } + // Load the shader source + glShaderSource(shader, 1, &shaderSrc, NULL); + // Compile the shader + glCompileShader(shader); + // Check the compile status + GLint value = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &value); + if (value == 0) { + char buffer[1024]; + GLsizei length = 0; + glGetShaderInfoLog(shader, sizeof(buffer), &length, buffer); + std::string log(buffer, length); + fprintf(stderr, "Error compiling shader: %s\n", log.c_str()); + glDeleteShader(shader); + return 0; + } + return shader; +} + +void OcclusionQueryTest::InitShaders() { + static const char* v_shader_str = + "uniform mat4 worldMatrix;\n" + "attribute vec3 g_Position;\n" + "void main()\n" + "{\n" + " gl_Position = worldMatrix *\n" + " vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n" + "}\n"; + static const char* f_shader_str = + "precision mediump float;" + "uniform vec4 color;\n" + "void main()\n" + "{\n" + " gl_FragColor = color;\n" + "}\n"; + + CheckGLError("InitShaders", __LINE__); + GLuint vertex_shader = LoadShader(GL_VERTEX_SHADER, v_shader_str); + GLuint fragment_shader = LoadShader(GL_FRAGMENT_SHADER, f_shader_str); + // Create the program object + GLuint program = glCreateProgram(); + if (program == 0) { + fprintf(stderr, "Creating program failed\n"); + return; + } + glAttachShader(program, vertex_shader); + glAttachShader(program, fragment_shader); + // Bind g_Position to attribute 0 + glBindAttribLocation(program, 0, "g_Position"); + // Link the program + glLinkProgram(program); + // Check the link status + GLint linked = 0; + glGetProgramiv(program, GL_LINK_STATUS, &linked); + if (linked == 0) { + char buffer[1024]; + GLsizei length = 0; + glGetProgramInfoLog(program, sizeof(buffer), &length, buffer); + std::string log(buffer, length); + fprintf(stderr, "Error linking program: %s\n", log.c_str()); + glDeleteProgram(program); + return; + } + program_ = program; + matrix_loc_ = glGetUniformLocation(program_, "worldMatrix"); + color_loc_ = glGetUniformLocation(program_, "color"); + glGenBuffers(1, &vbo_); + glBindBuffer(GL_ARRAY_BUFFER, vbo_); + static float vertices[] = { + 1, 1, 0.0, + -1, 1, 0.0, + -1, -1, 0.0, + 1, 1, 0.0, + -1, -1, 0.0, + 1, -1, 0.0, + }; + glBufferData(GL_ARRAY_BUFFER, + sizeof(vertices), + NULL, + GL_STATIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); + CheckGLError("InitShaders", __LINE__); + + glGenQueriesEXT(1, &query_); + glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, query_); + glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT); + CheckGLError("InitShaders", __LINE__); +} + +} // anonymous namespace. + +int OcclusionQueryTest::Init(ESContext* esContext) { + CheckGLError("GLFromCPPInit", __LINE__); + glClearColor(0.0f, 0.1f, 0.2f, 1.0f); + InitShaders(); + CheckGLError("GLFromCPPInit", __LINE__); + return 1; +} + +void OcclusionQueryTest::Update(ESContext* esContext, float elapsed_sec) { + elasped_sec_ = elapsed_sec; +} + +static void SetMatrix(float x, float z, float scale, float* matrix) { + matrix[0] = scale; + matrix[1] = 0.0f; + matrix[2] = 0.0f; + matrix[3] = 0.0f; + + matrix[4] = 0.0f; + matrix[5] = scale; + matrix[6] = 0.0f; + matrix[7] = 0.0f; + + matrix[8] = 0.0f; + matrix[9] = 0.0f; + matrix[10] = scale; + matrix[11] = 0.0f; + + matrix[12] = x; + matrix[13] = 0.0f; + matrix[14] = z; + matrix[15] = 1.0f; +} + +void OcclusionQueryTest::DrawRect(float x, float z, float scale, float* color) { + GLfloat matrix[16]; + + SetMatrix(x, z, scale, matrix); + + // Set up the model matrix + glUniformMatrix4fv(matrix_loc_, 1, GL_FALSE, matrix); + + glUniform4fv(color_loc_, 1, color); + CheckGLError("GLFromCPPDraw", __LINE__); + + glDrawArrays(GL_TRIANGLES, 0, 6); + CheckGLError("GLFromCPPDraw", __LINE__); +} + +void OcclusionQueryTest::Draw(ESContext* esContext) { + CheckGLError("GLFromCPPDraw", __LINE__); + clock_ += elasped_sec_; + + // Note: the viewport is automatically set up to cover the entire Canvas. + // Clear the color buffer + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_DEPTH_TEST); + CheckGLError("GLFromCPPDraw", __LINE__); + // Use the program object + glUseProgram(program_); + CheckGLError("GLFromCPPDraw", __LINE__); + + // Load the vertex data + glBindBuffer(GL_ARRAY_BUFFER, vbo_); + glEnableVertexAttribArray(0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); + CheckGLError("GLFromCPPDraw", __LINE__); + + DrawRect(sinf(clock_), 0.0f, 0.50f, + last_query_status_ ? green_ : red_); + + bool started_query = false; + GLuint result = 0; + glGetQueryObjectuivEXT(query_, GL_QUERY_RESULT_AVAILABLE_EXT, &result); + CheckGLError("GLFromCPPDraw", __LINE__); + if (result) { + glGetQueryObjectuivEXT(query_, GL_QUERY_RESULT_EXT, &last_query_status_); + CheckGLError("GLFromCPPDraw", __LINE__); + glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, query_); + CheckGLError("GLFromCPPDraw", __LINE__); + started_query = true; + } + + DrawRect(-0.125f, 0.1f, 0.25f, blue_); + + if (started_query) { + glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT); + CheckGLError("GLFromCPPDraw", __LINE__); + } + + glFlush(); +} + +void OcclusionQueryTest::ShutDown(ESContext* esContext) { +} + +Demo* CreateDemo() { + return new OcclusionQueryTest(); +} + +} // namespace demos +} // namespace gpu + + |