Commit e977efc4 by Geoff Lang

Port the PostSubBuffer sample to the new sample framework.

Add a swap method on the SampleApplication class so that samples can override the default eglSwapBuffers function call. BUG=angle:521 Change-Id: I3f0ba6ed182b368e6f5fe502de67d1a14267a857 Reviewed-on: https://chromium-review.googlesource.com/188740Reviewed-by: 's avatarShannon Woods <shannonwoods@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent e9b02245
......@@ -102,7 +102,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "particle_system", "..\sampl
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "post_sub_buffer", "..\samples\post_sub_buffer.vcxproj", "{CBE3D362-43EA-60FC-D5AC-490039CA449A}"
ProjectSection(ProjectDependencies) = postProject
{80E5C3D9-93C2-943E-176F-C9A903E27BC9} = {80E5C3D9-93C2-943E-176F-C9A903E27BC9}
{297C46B0-9263-A9EA-82B1-EA221E7D7C7F} = {297C46B0-9263-A9EA-82B1-EA221E7D7C7F}
{FBAEE4F6-562A-588F-01F9-72DCABB3B061} = {FBAEE4F6-562A-588F-01F9-72DCABB3B061}
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
EndProjectSection
......
......@@ -37,12 +37,12 @@
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
<BufferSecurityCheck>true</BufferSecurityCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<DisableSpecificWarnings>4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<DisableSpecificWarnings>4201;4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<FunctionLevelLinking>true</FunctionLevelLinking>
<MinimalRebuild>false</MinimalRebuild>
<Optimization>Disabled</Optimization>
......@@ -67,17 +67,17 @@
<TargetMachine>MachineX86</TargetMachine>
</Link>
<ResourceCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Culture>0x0409</Culture>
</ResourceCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalOptions>/MP %(AdditionalOptions)</AdditionalOptions>
<BufferSecurityCheck>true</BufferSecurityCheck>
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
<DisableSpecificWarnings>4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<DisableSpecificWarnings>4201;4100;4127;4189;4239;4244;4245;4512;4702;4530;4718;%(DisableSpecificWarnings)</DisableSpecificWarnings>
<FunctionLevelLinking>true</FunctionLevelLinking>
<MinimalRebuild>false</MinimalRebuild>
<Optimization>MaxSpeed</Optimization>
......@@ -102,7 +102,7 @@
<TargetMachine>MachineX86</TargetMachine>
</Link>
<ResourceCompile>
<AdditionalIncludeDirectories>..\..\samples\gles2_book\Common;..\..\include;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..\..\include;..\..\samples\angle\sample_util;C:\Program Files (x86)\Windows Kits\8.0\Include\shared;C:\Program Files (x86)\Windows Kits\8.0\Include\um;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Culture>0x0409</Culture>
</ResourceCompile>
</ItemDefinitionGroup>
......@@ -110,11 +110,11 @@
<None Include="..\..\samples\samples.gyp"/>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\samples\gles2_book\PostSubBuffer\PostSubBuffer.c"/>
<ClCompile Include="..\..\samples\angle\post_sub_buffer\PostSubBuffer.cpp"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="es_util.vcxproj">
<Project>{80E5C3D9-93C2-943E-176F-C9A903E27BC9}</Project>
<ProjectReference Include="sample_util.vcxproj">
<Project>{297C46B0-9263-A9EA-82B1-EA221E7D7C7F}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\src\libEGL.vcxproj">
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="gles2_book">
<UniqueIdentifier>{01DCD872-C931-E1FA-4F9F-EBFA79587BB6}</UniqueIdentifier>
<Filter Include="angle">
<UniqueIdentifier>{2039F76D-4C9A-B8A0-444A-4766C6F6104A}</UniqueIdentifier>
</Filter>
<Filter Include="gles2_book\PostSubBuffer">
<UniqueIdentifier>{17D314C7-49C3-F5A0-BCF4-1020B498EFB0}</UniqueIdentifier>
<Filter Include="angle\post_sub_buffer">
<UniqueIdentifier>{662934E1-22DD-9C2B-5005-9C74A1B6424E}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="..\..\samples\samples.gyp"/>
<ClCompile Include="..\..\samples\gles2_book\PostSubBuffer\PostSubBuffer.c">
<Filter>gles2_book\PostSubBuffer</Filter>
<ClCompile Include="..\..\samples\angle\post_sub_buffer\PostSubBuffer.cpp">
<Filter>angle\post_sub_buffer</Filter>
</ClCompile>
</ItemGroup>
</Project>
......@@ -75,7 +75,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "particle_system", "particle
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "post_sub_buffer", "post_sub_buffer.vcxproj", "{CBE3D362-43EA-60FC-D5AC-490039CA449A}"
ProjectSection(ProjectDependencies) = postProject
{80E5C3D9-93C2-943E-176F-C9A903E27BC9} = {80E5C3D9-93C2-943E-176F-C9A903E27BC9}
{297C46B0-9263-A9EA-82B1-EA221E7D7C7F} = {297C46B0-9263-A9EA-82B1-EA221E7D7C7F}
{FBAEE4F6-562A-588F-01F9-72DCABB3B061} = {FBAEE4F6-562A-588F-01F9-72DCABB3B061}
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
EndProjectSection
......
//
// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Based on Simple_VertexShader.c from
// Book: OpenGL(R) ES 2.0 Programming Guide
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
// ISBN-10: 0321502795
// ISBN-13: 9780321502797
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780321563835
// http://www.opengles-book.com
#include "SampleApplication.h"
#include "shader_utils.h"
#include "texture_utils.h"
#include "geometry_utils.h"
#include "Vector.h"
#include "Matrix.h"
#include <iostream>
class PostSubBufferSample : public SampleApplication
{
public:
PostSubBufferSample::PostSubBufferSample()
: SampleApplication("PostSubBuffer", 1280, 720)
{
}
virtual bool initialize()
{
mPostSubBufferNV = (PFNEGLPOSTSUBBUFFERNVPROC)eglGetProcAddress("eglPostSubBufferNV");
if (!mPostSubBufferNV)
{
std::cerr << "Could not load eglPostSubBufferNV.";
return false;
}
const std::string vs = SHADER_SOURCE
(
uniform mat4 u_mvpMatrix;
attribute vec4 a_position;
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = u_mvpMatrix * a_position;
v_texcoord = a_texcoord;
}
);
const std::string fs = SHADER_SOURCE
(
precision mediump float;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);
}
);
mProgram = CompileProgram(vs, fs);
if (!mProgram)
{
return false;
}
// Get the attribute locations
mPositionLoc = glGetAttribLocation(mProgram, "a_position");
mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");
// Get the uniform locations
mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");
// Generate the geometry data
GenerateCubeGeometry(0.5f, &mCube);
// Set an initial rotation
mRotation = 45.0f;
// Clear the whole window surface to blue.
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
SampleApplication::swap();
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
return true;
}
virtual void destroy()
{
glDeleteProgram(mProgram);
}
virtual void step(float dt, double totalTime)
{
mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);
Matrix4 perspectiveMatrix = Matrix4::perspective(60.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(),
1.0f, 20.0f);
Matrix4 modelMatrix = Matrix4::translate(Vector3(0.0f, 0.0f, -2.0f)) *
Matrix4::rotate(mRotation, Vector3(1.0f, 0.0f, 1.0f));
Matrix4 viewMatrix = Matrix4::identity();
Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;
// Load the matrices
glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
}
virtual void draw()
{
// Set the viewport
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(mProgram);
// Load the vertex position
glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());
glEnableVertexAttribArray(mPositionLoc);
// Load the texcoord data
glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());
glEnableVertexAttribArray(mTexcoordLoc);
// Draw the cube
glDrawElements(GL_TRIANGLES, mCube.indices.size(), GL_UNSIGNED_SHORT, mCube.indices.data());
}
virtual void swap()
{
// Instead of letting the application call eglSwapBuffers, call eglPostSubBufferNV here instead
size_t windowWidth = getWindow()->getWidth();
size_t windowHeight = getWindow()->getHeight();
EGLDisplay display = getWindow()->getDisplay();
EGLSurface surface = getSurface();
mPostSubBufferNV(display, surface, 60, 60, windowWidth - 120, windowHeight - 120);
}
private:
// Handle to a program object
GLuint mProgram;
// Attribute locations
GLint mPositionLoc;
GLint mTexcoordLoc;
// Uniform locations
GLuint mMVPMatrixLoc;
// Current rotation
float mRotation;
// Geometry data
CubeGeometry mCube;
// eglPostSubBufferNV entry point
PFNEGLPOSTSUBBUFFERNVPROC mPostSubBufferNV;
};
int main(int argc, char **argv)
{
PostSubBufferSample app;
return app.run();
}
......@@ -47,11 +47,31 @@ void SampleApplication::draw()
{
}
void SampleApplication::swap()
{
eglSwapBuffers(mWindow->getDisplay(), mSurface);
}
Window *SampleApplication::getWindow() const
{
return mWindow.get();
}
EGLConfig SampleApplication::getConfig() const
{
return mConfig;
}
EGLSurface SampleApplication::getSurface() const
{
return mSurface;
}
EGLContext SampleApplication::getContext() const
{
return mContext;
}
int SampleApplication::run()
{
if (!mWindow->initialize(mName, mWidth, mHeight) || !initializeGL())
......@@ -95,8 +115,7 @@ int SampleApplication::run()
}
draw();
eglSwapBuffers(mWindow->getDisplay(), mSurface);
swap();
mWindow->messageLoop();
......@@ -141,7 +160,13 @@ bool SampleApplication::initializeGL()
return false;
}
mSurface = eglCreateWindowSurface(mWindow->getDisplay(), mConfig, mWindow->getNativeWindow(), NULL);
const EGLint surfaceAttributes[] =
{
EGL_POST_SUB_BUFFER_SUPPORTED_NV, EGL_TRUE,
EGL_NONE, EGL_NONE,
};
mSurface = eglCreateWindowSurface(mWindow->getDisplay(), mConfig, mWindow->getNativeWindow(), surfaceAttributes);
if (mSurface == EGL_NO_SURFACE)
{
eglGetError(); // Clear error and try again
......
......@@ -13,6 +13,8 @@
#include <GLES3/gl3ext.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "Window.h"
#include "Timer.h"
......@@ -34,7 +36,12 @@ class SampleApplication
virtual void step(float dt, double totalTime);
virtual void draw();
virtual void swap();
Window *getWindow() const;
EGLConfig getConfig() const;
EGLSurface getSurface() const;
EGLContext getContext() const;
bool popEvent(Event *event);
......
// Based on a sample from:
//
// Book: OpenGL(R) ES 2.0 Programming Guide
// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
// ISBN-10: 0321502795
// ISBN-13: 9780321502797
// Publisher: Addison-Wesley Professional
// URLs: http://safari.informit.com/9780321563835
// http://www.opengles-book.com
//
// PostSubBuffer.c
//
// This is a simple example that draws a rotating cube in perspective
// using a vertex shader to transform the object, posting only a subrectangle
// to the window surface.
//
#include <stdlib.h>
#include "esUtil.h"
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240
typedef struct
{
// Handle to a program object
GLuint programObject;
// Attribute locations
GLint positionLoc;
// Uniform locations
GLint mvpLoc;
// Vertex daata
GLfloat *vertices;
GLushort *indices;
int numIndices;
// Rotation angle
GLfloat angle;
// MVP matrix
ESMatrix mvpMatrix;
} UserData;
///
// Initialize the shader and program object
//
int Init ( ESContext *esContext )
{
UserData *userData = esContext->userData;
GLbyte vShaderStr[] =
"uniform mat4 u_mvpMatrix; \n"
"attribute vec4 a_position; \n"
"void main() \n"
"{ \n"
" gl_Position = u_mvpMatrix * a_position; \n"
"} \n";
GLbyte fShaderStr[] =
"precision mediump float; \n"
"void main() \n"
"{ \n"
" gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 ); \n"
"} \n";
// Load the shaders and get a linked program object
userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
// Get the attribute locations
userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
// Get the uniform locations
userData->mvpLoc = glGetUniformLocation( userData->programObject, "u_mvpMatrix" );
// Generate the vertex data
userData->numIndices = esGenCube( 1.0, &userData->vertices,
NULL, NULL, &userData->indices );
// Starting rotation angle for the cube
userData->angle = 45.0f;
// Clear the whole window surface.
glClearColor ( 0.0f, 0.0f, 1.0f, 0.0f );
glClear ( GL_COLOR_BUFFER_BIT );
eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
return TRUE;
}
///
// Update MVP matrix based on time
//
void Update ( ESContext *esContext, float deltaTime )
{
UserData *userData = (UserData*) esContext->userData;
ESMatrix perspective;
ESMatrix modelview;
float aspect;
// Compute a rotation angle based on time to rotate the cube
userData->angle += ( deltaTime * 40.0f );
if( userData->angle >= 360.0f )
userData->angle -= 360.0f;
// Compute the window aspect ratio
aspect = (GLfloat) esContext->width / (GLfloat) esContext->height;
// Generate a perspective matrix with a 60 degree FOV
esMatrixLoadIdentity( &perspective );
esPerspective( &perspective, 60.0f, aspect, 1.0f, 20.0f );
// Generate a model view matrix to rotate/translate the cube
esMatrixLoadIdentity( &modelview );
// Translate away from the viewer
esTranslate( &modelview, 0.0, 0.0, -2.0 );
// Rotate the cube
esRotate( &modelview, userData->angle, 1.0, 0.0, 1.0 );
// Compute the final MVP by multiplying the
// modevleiw and perspective matrices together
esMatrixMultiply( &userData->mvpMatrix, &modelview, &perspective );
}
///
// Draw a triangle using the shader pair created in Init()
//
void Draw ( ESContext *esContext )
{
UserData *userData = esContext->userData;
// Set the viewport
glViewport ( 0, 0, esContext->width, esContext->height );
// Clear the color buffer
glClear ( GL_COLOR_BUFFER_BIT );
// Use the program object
glUseProgram ( userData->programObject );
// Load the vertex position
glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
GL_FALSE, 3 * sizeof(GLfloat), userData->vertices );
glEnableVertexAttribArray ( userData->positionLoc );
// Load the MVP matrix
glUniformMatrix4fv( userData->mvpLoc, 1, GL_FALSE, (GLfloat*) &userData->mvpMatrix.m[0][0] );
// Draw the cube
glDrawElements ( GL_TRIANGLES, userData->numIndices, GL_UNSIGNED_SHORT, userData->indices );
eglPostSubBufferNV ( esContext->eglDisplay, esContext->eglSurface, 60, 60, WINDOW_WIDTH - 120, WINDOW_HEIGHT - 120 );
}
///
// Cleanup
//
void ShutDown ( ESContext *esContext )
{
UserData *userData = esContext->userData;
if ( userData->vertices != NULL )
{
free ( userData->vertices );
}
if ( userData->indices != NULL )
{
free ( userData->indices );
}
// Delete program object
glDeleteProgram ( userData->programObject );
}
int main ( int argc, char *argv[] )
{
ESContext esContext;
UserData userData;
esInitContext ( &esContext );
esContext.userData = &userData;
esCreateWindow ( &esContext, TEXT("Simple Vertex Shader"), WINDOW_WIDTH, WINDOW_HEIGHT, ES_WINDOW_RGB | ES_WINDOW_POST_SUB_BUFFER_SUPPORTED );
if ( !Init ( &esContext ) )
return 0;
esRegisterDrawFunc ( &esContext, Draw );
esRegisterUpdateFunc ( &esContext, Update );
esMainLoop ( &esContext );
ShutDown ( &esContext );
}
......@@ -200,8 +200,8 @@
{
'target_name': 'post_sub_buffer',
'type': 'executable',
'dependencies': [ 'es_util' ],
'sources': [ '<!@(python <(angle_path)/enumerate_files.py gles2_book/PostSubBuffer -types *.c *.h)' ],
'dependencies': [ 'sample_util' ],
'sources': [ '<!@(python <(angle_path)/enumerate_files.py angle/post_sub_buffer -types *.cpp *.h)' ],
},
],
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment