Commit 712e3f4d by Geoff Lang

Port the MultipleRenderTarget sample to the new sample framework.

Renamed to MultipleDrawBuffers and added shader loading from file. BUG=angle:521 Change-Id: Iffeb589a95eee4409d3624c56fe3a5e4a7d5be63 Reviewed-on: https://chromium-review.googlesource.com/188724Reviewed-by: 's avatarShannon Woods <shannonwoods@chromium.org> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 8dd592bf
......@@ -173,6 +173,10 @@
<Project>{130A534E-6BA3-DB9C-B0D2-5ACE5CCB2632}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\samples\multiple_draw_buffers.vcxproj">
<Project>{58BE89D7-25D5-CC84-EDBF-412C12C59709}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>
<ProjectReference Include="..\samples\simple_texture_2d.vcxproj">
<Project>{0BD6333E-E82C-7665-C386-CDA40096413D}</Project>
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
......
......@@ -18,6 +18,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "All", "All.vcxproj", "{D048
{37E1BB43-65DC-A25A-042E-30B88E6C75A2} = {37E1BB43-65DC-A25A-042E-30B88E6C75A2}
{8C20B90A-FBCB-B63F-435A-3299A1C38B2C} = {8C20B90A-FBCB-B63F-435A-3299A1C38B2C}
{130A534E-6BA3-DB9C-B0D2-5ACE5CCB2632} = {130A534E-6BA3-DB9C-B0D2-5ACE5CCB2632}
{58BE89D7-25D5-CC84-EDBF-412C12C59709} = {58BE89D7-25D5-CC84-EDBF-412C12C59709}
{0BD6333E-E82C-7665-C386-CDA40096413D} = {0BD6333E-E82C-7665-C386-CDA40096413D}
{93B7F18A-947E-69A1-9CD8-D1E5144E792A} = {93B7F18A-947E-69A1-9CD8-D1E5144E792A}
{459E5678-892C-4EF9-6ED8-0D9E5272B4FF} = {459E5678-892C-4EF9-6ED8-0D9E5272B4FF}
......@@ -85,6 +86,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multi_texture", "..\samples
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multiple_draw_buffers", "..\samples\multiple_draw_buffers.vcxproj", "{58BE89D7-25D5-CC84-EDBF-412C12C59709}"
ProjectSection(ProjectDependencies) = postProject
{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
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "particle_system", "..\samples\particle_system.vcxproj", "{8C20B90A-FBCB-B63F-435A-3299A1C38B2C}"
ProjectSection(ProjectDependencies) = postProject
{297C46B0-9263-A9EA-82B1-EA221E7D7C7F} = {297C46B0-9263-A9EA-82B1-EA221E7D7C7F}
......@@ -201,6 +209,10 @@ Global
{46160987-0221-9E14-3B88-80F9FCFCFFBF}.Debug|Win32.Build.0 = Debug|Win32
{46160987-0221-9E14-3B88-80F9FCFCFFBF}.Release|Win32.ActiveCfg = Release|Win32
{46160987-0221-9E14-3B88-80F9FCFCFFBF}.Release|Win32.Build.0 = Release|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Debug|Win32.ActiveCfg = Debug|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Debug|Win32.Build.0 = Debug|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Release|Win32.ActiveCfg = Release|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Release|Win32.Build.0 = Release|Win32
{6BC75FF2-C11A-C393-F9BB-BF29FF29D736}.Debug|Win32.ActiveCfg = Debug|Win32
{6BC75FF2-C11A-C393-F9BB-BF29FF29D736}.Debug|Win32.Build.0 = Debug|Win32
{6BC75FF2-C11A-C393-F9BB-BF29FF29D736}.Release|Win32.ActiveCfg = Release|Win32
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="angle">
<UniqueIdentifier>{2039F76D-4C9A-B8A0-444A-4766C6F6104A}</UniqueIdentifier>
</Filter>
<Filter Include="angle\multiple_draw_buffers">
<UniqueIdentifier>{D0BAF5AC-C79C-4569-5154-A04FD87C707E}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="..\..\samples\samples.gyp"/>
<None Include="..\..\samples\angle\multiple_draw_buffers\multiple_draw_buffers_copy_fs.glsl">
<Filter>angle\multiple_draw_buffers</Filter>
</None>
<None Include="..\..\samples\angle\multiple_draw_buffers\multiple_draw_buffers_vs.glsl">
<Filter>angle\multiple_draw_buffers</Filter>
</None>
<ClCompile Include="..\..\samples\angle\multiple_draw_buffers\MultipleDrawBuffers.cpp">
<Filter>angle\multiple_draw_buffers</Filter>
</ClCompile>
<None Include="..\..\samples\angle\multiple_draw_buffers\multiple_draw_buffers_fs.glsl">
<Filter>angle\multiple_draw_buffers</Filter>
</None>
</ItemGroup>
</Project>
......@@ -59,6 +59,13 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multi_texture", "multi_text
{7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81} = {7FBD6F69-B9A4-69F1-A12B-8DACB3F8CD81}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "multiple_draw_buffers", "multiple_draw_buffers.vcxproj", "{58BE89D7-25D5-CC84-EDBF-412C12C59709}"
ProjectSection(ProjectDependencies) = postProject
{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
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "particle_system", "particle_system.vcxproj", "{8C20B90A-FBCB-B63F-435A-3299A1C38B2C}"
ProjectSection(ProjectDependencies) = postProject
{297C46B0-9263-A9EA-82B1-EA221E7D7C7F} = {297C46B0-9263-A9EA-82B1-EA221E7D7C7F}
......@@ -175,6 +182,10 @@ Global
{46160987-0221-9E14-3B88-80F9FCFCFFBF}.Debug|Win32.Build.0 = Debug|Win32
{46160987-0221-9E14-3B88-80F9FCFCFFBF}.Release|Win32.ActiveCfg = Release|Win32
{46160987-0221-9E14-3B88-80F9FCFCFFBF}.Release|Win32.Build.0 = Release|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Debug|Win32.ActiveCfg = Debug|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Debug|Win32.Build.0 = Debug|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Release|Win32.ActiveCfg = Release|Win32
{58BE89D7-25D5-CC84-EDBF-412C12C59709}.Release|Win32.Build.0 = Release|Win32
{6BC75FF2-C11A-C393-F9BB-BF29FF29D736}.Debug|Win32.ActiveCfg = Debug|Win32
{6BC75FF2-C11A-C393-F9BB-BF29FF29D736}.Debug|Win32.Build.0 = Debug|Win32
{6BC75FF2-C11A-C393-F9BB-BF29FF29D736}.Release|Win32.ActiveCfg = Release|Win32
......
//
// 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_Texture2D.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 "path_utils.h"
#include "texture_utils.h"
#include <iostream>
class MultipleDrawBuffersSample : public SampleApplication
{
public:
MultipleDrawBuffersSample::MultipleDrawBuffersSample()
: SampleApplication("MultipleDrawBuffers", 1280, 720)
{
}
virtual bool initialize()
{
// Check EXT_draw_buffers is supported
char *extensionString = (char*)glGetString(GL_EXTENSIONS);
if (strstr(extensionString, "GL_EXT_draw_buffers") != NULL)
{
// Retrieve the address of glDrawBuffersEXT from EGL
mDrawBuffers = (PFNGLDRAWBUFFERSEXTPROC)eglGetProcAddress("glDrawBuffersEXT");
}
else
{
mDrawBuffers = glDrawBuffers;
}
if (!mDrawBuffers)
{
std::cerr << "Unable to load glDrawBuffers[EXT] entry point.";
return false;
}
mMRTProgram = CompileProgramFromFiles(GetExecutableDirectory() + "/multiple_draw_buffers_vs.glsl",
GetExecutableDirectory() + "/multiple_draw_buffers_fs.glsl");
if (!mMRTProgram)
{
return false;
}
mCopyProgram = CompileProgramFromFiles(GetExecutableDirectory() + "/multiple_draw_buffers_vs.glsl",
GetExecutableDirectory() + "/multiple_draw_buffers_copy_fs.glsl");
if (!mCopyProgram)
{
return false;
}
// Get the attribute locations
mPositionLoc = glGetAttribLocation(mCopyProgram, "a_position");
mTexCoordLoc = glGetAttribLocation(mCopyProgram, "a_texCoord");
// Get the sampler location
mSamplerLoc = glGetUniformLocation(mCopyProgram, "s_texture");
// Load the texture
mTexture = CreateSimpleTexture2D();
// Initialize the user framebuffer
glGenFramebuffers(1, &mFramebuffer);
glGenTextures(mFramebufferAttachmentCount, mFramebufferTextures);
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
for (size_t i = 0; i < mFramebufferAttachmentCount; i++)
{
// Create textures for the four color attachments
glBindTexture(GL_TEXTURE_2D, mFramebufferTextures[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindow()->getWidth(), getWindow()->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0_EXT + i, GL_TEXTURE_2D, mFramebufferTextures[i], 0);
}
glBindTexture(GL_TEXTURE_2D, 0);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
return true;
}
virtual void destroy()
{
glDeleteProgram(mCopyProgram);
glDeleteProgram(mMRTProgram);
glDeleteTextures(1, &mTexture);
glDeleteTextures(mFramebufferAttachmentCount, mFramebufferTextures);
glDeleteFramebuffers(1, &mFramebuffer);
}
virtual void draw()
{
GLfloat vertices[] =
{
-0.8f, 0.8f, 0.0f, // Position 0
0.0f, 0.0f, // TexCoord 0
-0.8f, -0.8f, 0.0f, // Position 1
0.0f, 1.0f, // TexCoord 1
0.8f, -0.8f, 0.0f, // Position 2
1.0f, 1.0f, // TexCoord 2
0.8f, 0.8f, 0.0f, // Position 3
1.0f, 0.0f // TexCoord 3
};
GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
GLenum drawBuffers[mFramebufferAttachmentCount] =
{
GL_COLOR_ATTACHMENT0_EXT,
GL_COLOR_ATTACHMENT1_EXT,
GL_COLOR_ATTACHMENT2_EXT,
GL_COLOR_ATTACHMENT3_EXT
};
// Enable drawing to the four color attachments of the user framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, mFramebuffer);
mDrawBuffers(mFramebufferAttachmentCount, drawBuffers);
// Set the viewport
size_t width = getWindow()->getWidth();
size_t height = getWindow()->getHeight();
glViewport(0, 0, width, height);
// Clear the color buffer
glClear(GL_COLOR_BUFFER_BIT);
// Use the program object
glUseProgram(mMRTProgram);
// Load the vertex position
glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices);
glEnableVertexAttribArray(mPositionLoc);
// Load the texture coordinate
glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices + 3);
glEnableVertexAttribArray(mTexCoordLoc);
// Bind the texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, mTexture);
// Set the sampler texture unit to 0
glUniform1i(mSamplerLoc, 0);
// Draw the textured quad to the four render targets
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
// Enable the default framebuffer and single textured drawing
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(mCopyProgram);
// Draw the four textured quads to a separate region in the viewport
glBindTexture(GL_TEXTURE_2D, mFramebufferTextures[0]);
glViewport(0, 0, width / 2, height / 2);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
glBindTexture(GL_TEXTURE_2D, mFramebufferTextures[1]);
glViewport(width / 2, 0, width / 2, height / 2);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
glBindTexture(GL_TEXTURE_2D, mFramebufferTextures[2]);
glViewport(0, height / 2, width / 2, height / 2);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
glBindTexture(GL_TEXTURE_2D, mFramebufferTextures[3]);
glViewport(width / 2, height / 2, width / 2, height / 2);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
}
private:
// Handle to a program object
GLuint mMRTProgram;
GLuint mCopyProgram;
// Attribute locations
GLint mPositionLoc;
GLint mTexCoordLoc;
// Sampler location
GLint mSamplerLoc;
// Texture handle
GLuint mTexture;
// Framebuffer object handle
GLuint mFramebuffer;
// Framebuffer color attachments
static const size_t mFramebufferAttachmentCount = 4;
GLuint mFramebufferTextures[mFramebufferAttachmentCount];
// Loaded draw buffer entry points
PFNGLDRAWBUFFERSEXTPROC mDrawBuffers;
};
int main(int argc, char **argv)
{
MultipleDrawBuffersSample app;
return app.run();
}
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main()
{
vec4 color = texture2D(s_texture, v_texCoord);
gl_FragColor = color;
}
#extension GL_EXT_draw_buffers : enable
precision mediump float;
varying vec2 v_texCoord;
uniform sampler2D s_texture;
void main()
{
vec4 color = texture2D(s_texture, v_texCoord);
gl_FragData[0] = color;
gl_FragData[1] = vec4(1.0, 1.0, 1.0, 1.0) - color.brga;
gl_FragData[2] = vec4(0.2, 1.0, 0.5, 1.0) * color.gbra;
gl_FragData[3] = color.rrra;
}
attribute vec4 a_position;
attribute vec2 a_texCoord;
varying vec2 v_texCoord;
void main()
{
gl_Position = a_position;
v_texCoord = a_texCoord;
}
......@@ -8,6 +8,27 @@
#include <vector>
#include <iostream>
#include <fstream>
static std::string ReadFileToString(const std::string &source)
{
std::ifstream stream(source);
if (!stream)
{
std::cerr << "Failed to load shader file: " << source;
return "";
}
std::string result;
stream.seekg(0, std::ios::end);
result.reserve(stream.tellg());
stream.seekg(0, std::ios::beg);
result.assign((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
return result;
}
GLuint CompileShader(GLenum type, const std::string &source)
{
......@@ -37,6 +58,17 @@ GLuint CompileShader(GLenum type, const std::string &source)
return shader;
}
GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath)
{
std::string source = ReadFileToString(sourcePath);
if (source.empty())
{
return 0;
}
return CompileShader(type, source);
}
GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
{
GLuint program = glCreateProgram();
......@@ -79,3 +111,15 @@ GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource)
return program;
}
GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath)
{
std::string vsSource = ReadFileToString(vsPath);
std::string fsSource = ReadFileToString(fsPath);
if (vsSource.empty() || fsSource.empty())
{
return 0;
}
return CompileProgram(vsSource, fsSource);
}
......@@ -21,6 +21,9 @@
#define SHADER_SOURCE(...) #__VA_ARGS__
GLuint CompileShader(GLenum type, const std::string &source);
GLuint CompileShaderFromFile(GLenum type, const std::string &sourcePath);
GLuint CompileProgram(const std::string &vsSource, const std::string &fsSource);
GLuint CompileProgramFromFiles(const std::string &vsPath, const std::string &fsPath);
#endif // SAMPLE_UTIL_SHADER_UTILS_H
......@@ -149,6 +149,20 @@
},
{
'target_name': 'multiple_draw_buffers',
'type': 'executable',
'dependencies': [ 'sample_util' ],
'sources': [ '<!@(python <(angle_path)/enumerate_files.py angle/multiple_draw_buffers -types *.cpp *.h *.glsl)' ],
'copies':
[
{
'destination': '<(PRODUCT_DIR)',
'files': [ '<!@(python <(angle_path)/enumerate_files.py angle/multiple_draw_buffers -types *.glsl)' ],
}
]
},
{
'target_name': 'simple_texture_2d',
'type': 'executable',
'dependencies': [ 'es_util' ],
......
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