Commit af51a4f8 authored by Ken Martin's avatar Ken Martin

bunch of optimization more to come

Change-Id: Ie6e63c47b03e2aeb8ee1137e0ca333c98b86f651
parent 858756b8
......@@ -27,6 +27,12 @@
#include "vtkRenderer.h"
#include "vtkPDBReader.h"
#include "vtkTimerLog.h"
#include "vtkSmartPointer.h"
#include "vtkCamera.h"
int TestPDBBallAndStick(int argc, char *argv[])
{
char* fileName =
......@@ -65,6 +71,29 @@ int TestPDBBallAndStick(int argc, char *argv[])
ren->GetActiveCamera()->Zoom(1.7);
ren->SetBackground(0.0, 0.0, 0.0);
win->SetSize(450, 450);
vtkSmartPointer<vtkTimerLog> timer = vtkSmartPointer<vtkTimerLog>::New();
int numRenders = 85;
timer->StartTimer();
win->Render();
timer->StopTimer();
double firstRender = timer->GetElapsedTime();
cerr << "first render time: " << firstRender << endl;
timer->StartTimer();
for (int i = 0; i < numRenders; ++i)
{
ren->GetActiveCamera()->Azimuth(1);
ren->GetActiveCamera()->Elevation(1);
win->Render();
}
timer->StopTimer();
double elapsed = timer->GetElapsedTime();
cerr << "interactive render time: " << elapsed / numRenders << endl;
win->Render();
// Finally render the scene and compare the image to a reference image
......
......@@ -30,6 +30,7 @@ set(Module_SRCS
vtkVBOPolyDataMapper.cxx
vtkOpenGL2RenderWindow.cxx
vtkOpenGL2Renderer.cxx
vtkOpenGL2ShaderCache.cxx
vtkOpenGL2Texture.cxx
vtkOpenGL2TextureUnitManager.cxx
${CMAKE_CURRENT_BINARY_DIR}/${vtk-module}ObjectFactory.cxx
......
......@@ -30,7 +30,6 @@ uniform float specularPower;
// passed from the vertex shader
varying vec4 vertexVC;
varying vec4 vertexWC;
// optional color passed in from the vertex shader, vertexColor
//VTK::Color::Dec
......
......@@ -28,8 +28,6 @@ uniform vec3 specularColor; // intensity weighted color
uniform float specularPower;
// camera and actor matrix values
uniform mat4 WCVCMatrix; // world to view matrix
uniform mat4 MCWCMatrix; // model to world matrix
uniform mat4 MCVCMatrix; // combined Model to View transform
uniform mat4 VCDCMatrix; // the camera's projection matrix
uniform mat3 normalMatrix; // transform model coordinate directions to view coordinates
......@@ -41,7 +39,6 @@ uniform vec3 lightDirectionVC[6]; // normalized
// passed from the vertex shader
varying vec4 vertexVC;
varying vec4 vertexWC;
// optional color passed in from the vertex shader, vertexColor
//VTK::Color::Dec
......
......@@ -29,8 +29,6 @@ uniform vec3 specularColor; // intensity weighted color
uniform float specularPower;
// camera and actor matrix values
uniform mat4 WCVCMatrix; // world to view matrix
uniform mat4 MCWCMatrix; // model to world matrix
uniform mat4 MCVCMatrix; // combined Model to View transform
uniform mat4 VCDCMatrix; // the camera's projection matrix
uniform mat3 normalMatrix; // transform model coordinate directions to view coordinates
......
......@@ -32,8 +32,6 @@ uniform vec3 specularColor; // intensity weighted color
uniform float specularPower;
// camera and actor matrix values
uniform mat4 WCVCMatrix; // world to view matrix
uniform mat4 MCWCMatrix; // model to world matrix
uniform mat4 MCVCMatrix; // combined Model to View transform
uniform mat4 VCDCMatrix; // the camera's projection matrix
uniform mat3 normalMatrix; // transform model coordinate directions to view coordinates
......
......@@ -31,7 +31,6 @@ uniform vec3 specularColor; // intensity weighted color
uniform float specularPower;
// camera and actor matrix values
uniform mat4 WCVCMatrix; // world to view matrix
uniform mat4 MCWCMatrix; // model to world matrix
uniform mat4 MCVCMatrix; // combined Model to View transform
uniform mat4 VCDCMatrix; // the camera's projection matrix
......
......@@ -69,11 +69,14 @@ protected:
class vtkColorMapper;
vtkNew<vtkColorMapper> ColorMapper;
class vtkOpenGL2Glyph3DMapperEntry;
class vtkOpenGL2Glyph3DMapperArray;
vtkOpenGL2Glyph3DMapperArray *SourceMappers; // array of mappers
vtkOpenGL2Glyph3DMapperArray *GlyphValues; // array of value for datasets
vtkWeakPointer<vtkWindow> LastWindow; // Window used for previous render.
vtkVBOPolyDataMapper *Mapper;
vtkTimeStamp PainterUpdateTime;
private:
......
......@@ -36,6 +36,9 @@
#include "vtkgluPickMatrix.h"
#include "vtkOpenGLError.h"
#include "vtkOpenGL2RenderWindow.h"
#include "vtkOpenGL2ShaderCache.h"
#include <cmath>
// Bring in our shader symbols.
......@@ -55,6 +58,9 @@ public:
vtkgl::CellBO tris;
vtkgl::CellBO triStrips;
vtkTimeStamp ShaderSourceTime;
vtkTimeStamp propertiesTime;
vtkTimeStamp shaderBuildTime;
......@@ -75,13 +81,11 @@ vtkOpenGL2PolyDataMapper2D::~vtkOpenGL2PolyDataMapper2D()
delete this->Internal;
}
//-----------------------------------------------------------------------------
void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
vtkViewport* viewport, vtkActor2D *actor)
void vtkOpenGL2PolyDataMapper2D::BuildShader(std::string &VSSource, std::string &FSSource, vtkViewport* ren, vtkActor2D *actor)
{
std::string VSSource = vtkglPolyData2DVS;
std::string FSSource = vtkglPolyData2DFS;
VSSource = vtkglPolyData2DVS;
FSSource = vtkglPolyData2DFS;
// Build our shader if necessary.
if (this->Colors && this->Colors->GetNumberOfComponents())
......@@ -141,47 +145,42 @@ void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
"//VTK::TCoord::Impl","");
}
//cout << "VS: " << VSSource << endl;
}
// only recompile if the shader has changed
if (cellBO.vs.GetSource().size() == 0 ||
cellBO.vs.GetSource().compare(VSSource) ||
cellBO.fs.GetSource().compare(FSSource))
{
cellBO.vs.SetSource(VSSource);
cellBO.vs.SetType(vtkgl::Shader::Vertex);
cellBO.fs.SetSource(FSSource);
cellBO.fs.SetType(vtkgl::Shader::Fragment);
if (!cellBO.vs.Compile())
{
vtkErrorMacro(<< cellBO.vs.GetError());
}
if (!cellBO.fs.Compile())
{
vtkErrorMacro(<< cellBO.fs.GetError());
}
if (!cellBO.program.AttachShader(cellBO.vs))
{
vtkErrorMacro(<< cellBO.program.GetError());
}
if (!cellBO.program.AttachShader(cellBO.fs))
{
vtkErrorMacro(<< cellBO.program.GetError());
}
if (!cellBO.program.Link())
{
vtkErrorMacro(<< "Links failed: " << cellBO.program.GetError());
}
cellBO.buildTime.Modified();
//-----------------------------------------------------------------------------
void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
vtkViewport* viewport, vtkActor2D *actor)
{
vtkOpenGL2RenderWindow *renWin = vtkOpenGL2RenderWindow::SafeDownCast(viewport->GetVTKWindow());
// has something changed that would require us to recreate the shader?
// candidates are
// property modified (representation interpolation and lighting)
// input modified
// light complexity changed
if (this->Internal->ShaderSourceTime < this->GetMTime() ||
this->Internal->ShaderSourceTime < actor->GetMTime() ||
this->Internal->ShaderSourceTime < this->GetInput()->GetMTime())
{
std::string VSSource;
std::string FSSource;
this->BuildShader(VSSource,FSSource,viewport,actor);
cellBO.CachedProgram = renWin->GetShaderCache()->ReadyShader(VSSource.c_str(), FSSource.c_str());
this->Internal->ShaderSourceTime.Modified();
cellBO.vao.Initialize(); // reset the VAO as the shader has changed
}
else
{
renWin->GetShaderCache()->ReadyShader(cellBO.CachedProgram);
}
// Now to update the VAO too, if necessary.
vtkgl::VBOLayout &layout = this->Internal->layout;
if (this->VBOUpdateTime > cellBO.attributeUpdateTime)
{
cellBO.program.Bind();
cellBO.vao.Bind();
if (!cellBO.vao.AddAttributeArray(cellBO.program, this->Internal->vbo,
if (!cellBO.vao.AddAttributeArray(cellBO.CachedProgram->Program, this->Internal->vbo,
"vertexWC", layout.VertexOffset,
layout.Stride, VTK_FLOAT, 3, false))
{
......@@ -189,7 +188,7 @@ void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
}
if (layout.TCoordComponents)
{
if (!cellBO.vao.AddAttributeArray(cellBO.program, this->Internal->vbo,
if (!cellBO.vao.AddAttributeArray(cellBO.CachedProgram->Program, this->Internal->vbo,
"tcoordMC", layout.TCoordOffset,
layout.Stride, VTK_FLOAT, layout.TCoordComponents, false))
{
......@@ -198,7 +197,7 @@ void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
}
if (layout.ColorComponents != 0)
{
if (!cellBO.vao.AddAttributeArray(cellBO.program, this->Internal->vbo,
if (!cellBO.vao.AddAttributeArray(cellBO.CachedProgram->Program, this->Internal->vbo,
"diffuseColor", layout.ColorOffset,
layout.Stride, VTK_UNSIGNED_CHAR,
layout.ColorComponents, true))
......@@ -209,16 +208,9 @@ void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
cellBO.attributeUpdateTime.Modified();
}
if (!cellBO.program.Bind())
{
vtkErrorMacro(<< cellBO.program.GetError());
return;
}
if (layout.TCoordComponents)
{
cellBO.program.SetUniformValue("texture1", 0);
cellBO.CachedProgram->Program.SetUniformValue("texture1", 0);
}
this->SetPropertyShaderParameters(cellBO, viewport, actor);
......@@ -230,7 +222,7 @@ void vtkOpenGL2PolyDataMapper2D::UpdateShader(vtkgl::CellBO &cellBO,
void vtkOpenGL2PolyDataMapper2D::SetPropertyShaderParameters(
vtkgl::CellBO &cellBO, vtkViewport*, vtkActor2D *actor)
{
vtkgl::ShaderProgram &program = cellBO.program;
vtkgl::ShaderProgram &program = cellBO.CachedProgram->Program;
// Query the actor for some of the properties that can be applied.
float opacity = static_cast<float>(actor->GetProperty()->GetOpacity());
......@@ -247,7 +239,7 @@ void vtkOpenGL2PolyDataMapper2D::SetPropertyShaderParameters(
void vtkOpenGL2PolyDataMapper2D::SetCameraShaderParameters(
vtkgl::CellBO &cellBO, vtkViewport* viewport, vtkActor2D *actor)
{
vtkgl::ShaderProgram &program = cellBO.program;
vtkgl::ShaderProgram &program = cellBO.CachedProgram->Program;
// Get the position of the actor
int size[2];
......@@ -565,7 +557,6 @@ void vtkOpenGL2PolyDataMapper2D::RenderOverlay(vtkViewport* viewport,
}
this->Internal->points.vao.Release();
this->Internal->points.program.Release();
this->Internal->vbo.Release();
vtkOpenGLCheckErrorMacro("failed after RenderOverlay");
......
......@@ -46,6 +46,9 @@ protected:
vtkOpenGL2PolyDataMapper2D();
~vtkOpenGL2PolyDataMapper2D();
// Description:
// Build the shader source code
virtual void BuildShader(std::string &VertexCode, std::string &fragmentCode, vtkViewport *ren, vtkActor2D *act);
// Description:
// Determine what shader to use and compile/link it
......
......@@ -12,7 +12,9 @@
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include <GL/glew.h>
#include "vtkOpenGL2RenderWindow.h"
#include <cassert>
#include "vtkFloatArray.h"
#include "vtkgl.h"
......@@ -28,6 +30,7 @@
#include "vtkOpenGLError.h"
#include "vtkOpenGL2Texture.h"
#include "vtkUnsignedCharArray.h"
#include "vtkOpenGL2ShaderCache.h"
#include "vtkOpenGL2TextureUnitManager.h"
#include "vtkStdString.h"
#include <sstream>
......@@ -61,6 +64,10 @@ int vtkOpenGL2RenderWindow::GetGlobalMaximumNumberOfMultiSamples()
// ----------------------------------------------------------------------------
vtkOpenGL2RenderWindow::vtkOpenGL2RenderWindow()
{
this->Initialized = false;
this->ShaderCache = vtkOpenGL2ShaderCache::New();
this->ExtensionManager = NULL;
this->HardwareSupport = NULL;
this->TextureUnitManager=0;
......@@ -109,6 +116,7 @@ vtkOpenGL2RenderWindow::~vtkOpenGL2RenderWindow()
this->SetTextureUnitManager(0);
this->SetExtensionManager(0);
this->SetHardwareSupport(0);
this->ShaderCache->UnRegister(this);
}
// ----------------------------------------------------------------------------
......@@ -388,6 +396,25 @@ void vtkOpenGL2RenderWindow::OpenGLInitState()
void vtkOpenGL2RenderWindow::OpenGLInitContext()
{
// When a new OpenGL context is created, force an update
if (!this->Initialized)
{
GLenum result = glewInit();
bool m_valid = (result == GLEW_OK);
if (!m_valid)
{
vtkErrorMacro("GLEW could not be initialized.");
return;
}
if (!GLEW_VERSION_2_1)
{
vtkErrorMacro("GL version 2.1 is not supported by your graphics driver.");
//m_valid = false;
return;
}
this->Initialized = true;
}
// of the extension manager by calling modified on it.
vtkOpenGLExtensionManager *extensions = this->GetExtensionManager();
extensions->Modified();
......@@ -412,6 +439,7 @@ void vtkOpenGL2RenderWindow::OpenGLInitContext()
extensions->LoadCorePromotedExtension("GL_EXT_blend_func_separate");
}
}
}
void vtkOpenGL2RenderWindow::PrintSelf(ostream& os, vtkIndent indent)
......
......@@ -24,12 +24,14 @@
#include "vtkRenderingOpenGL2Module.h" // For export macro
#include "vtkRenderWindow.h"
#include "vtkOpenGL.h" // Needed for GLuint.
class vtkIdList;
class vtkOpenGLExtensionManager;
class vtkOpenGLHardwareSupport;
class vtkOpenGL2TextureUnitManager;
class vtkOpenGL2ShaderCache;
class vtkStdString;
class VTKRENDERINGOPENGL2_EXPORT vtkOpenGL2RenderWindow : public vtkRenderWindow
......@@ -175,6 +177,10 @@ public:
// already been set up.
vtkOpenGLExtensionManager* GetExtensionManager();
// Description:
// Returns an Shader Cache object
vtkGetObjectMacro(ShaderCache,vtkOpenGL2ShaderCache);
// Description:
// Returns an Hardware Support object. A new one will be created if one
// hasn't already been set up.
......@@ -196,6 +202,8 @@ protected:
vtkOpenGL2RenderWindow();
~vtkOpenGL2RenderWindow();
vtkOpenGL2ShaderCache *ShaderCache;
long OldMonitorSetting;
vtkIdList *TextureResourceIds;
......@@ -265,6 +273,8 @@ protected:
vtkOpenGL2TextureUnitManager *TextureUnitManager;
bool Initialized; // ensure glewinit has been called
private:
vtkOpenGL2RenderWindow(const vtkOpenGL2RenderWindow&); // Not implemented.
void operator=(const vtkOpenGL2RenderWindow&); // Not implemented.
......
......@@ -76,8 +76,6 @@ public:
vtkOpenGL2Renderer::vtkOpenGL2Renderer()
{
this->Initialized = false;
this->PickInfo = new vtkGLPickInfo;
this->PickInfo->PickBuffer = 0;
this->PickInfo->PickedId = 0;
......@@ -188,27 +186,6 @@ void vtkOpenGL2Renderer::DeviceRender(void)
{
vtkTimerLog::MarkStartEvent("OpenGL Dev Render");
// FIXME: This should be moved to the renderwindow probably
if (!this->Initialized)
{
GLenum result = glewInit();
bool m_valid = (result == GLEW_OK);
if (!m_valid)
{
vtkErrorMacro("GLEW could not be initialized.");
return;
}
if (!GLEW_VERSION_2_1)
{
vtkErrorMacro("GL version 2.1 is not supported by your graphics driver.");
//m_valid = false;
return;
}
this->Initialized = true;
}
if(this->Pass!=0)
{
vtkRenderState s(this);
......
......@@ -164,8 +164,6 @@ protected:
friend class vtkRenderPass;
vtkRenderPass *Pass;
bool Initialized; // Hack - ensure glewinit has been called - move to window.
private:
vtkOpenGL2Renderer(const vtkOpenGL2Renderer&); // Not implemented.
void operator=(const vtkOpenGL2Renderer&); // Not implemented.
......
This diff is collapsed.
......@@ -36,6 +36,12 @@ public:
// Implemented by sub classes. Actual rendering is done here.
virtual void RenderPiece(vtkRenderer *ren, vtkActor *act);
// Description:
// Implemented by sub classes. Actual rendering is done here.
virtual void RenderPieceStart(vtkRenderer *ren, vtkActor *act);
virtual void RenderPieceDraw(vtkRenderer *ren, vtkActor *act);
virtual void RenderPieceFinish(vtkRenderer *ren, vtkActor *act);
// Description:
// Release any graphics resources that are being consumed by this mapper.
// The parameter window could be used to determine which graphic
......@@ -84,6 +90,10 @@ protected:
// Determine what shader to use and compile/link it
virtual void UpdateShader(vtkgl::CellBO &cellBO, vtkRenderer *ren, vtkActor *act);
// Description:
// Build the shader source code
virtual void BuildShader(std::string &VertexCode, std::string &fragmentCode, int lightComplexity, vtkRenderer *ren, vtkActor *act);
// Description:
// Update the scene when necessary.
void UpdateVBO(vtkActor *act);
......
......@@ -24,6 +24,8 @@
#include <vector>
#include "vtkOpenGL2ShaderCache.h"
class vtkCellArray;
class vtkPoints;
class vtkDataArray;
......@@ -51,11 +53,7 @@ size_t CreateMultiIndexBuffer(vtkCellArray *cells, BufferObject &indexBuffer,
// Store the shaders, program, and ibo in a common struct.
struct CellBO
{
Shader vs;
Shader fs;
const char* vsFile;
const char* fsFile;
ShaderProgram program;
vtkOpenGL2ShaderCache::CachedShaderProgram *CachedProgram;
BufferObject ibo;
VertexArrayObject vao;
size_t indexCount;
......
......@@ -99,7 +99,7 @@ public:
bool supported;
typedef std::map< GLuint, std::vector<VertexAttributes> > AttributeMap;
AttributeMap atttributes;
AttributeMap attributes;
};
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
......@@ -127,7 +127,7 @@ void VertexArrayObject::Bind()
else if (this->d->IsReady())
{
Private::AttributeMap::const_iterator it;
for (it = this->d->atttributes.begin(); it != this->d->atttributes.end();
for (it = this->d->attributes.begin(); it != this->d->attributes.end();
++it)
{
std::vector<VertexAttributes>::const_iterator attrIt;
......@@ -152,7 +152,7 @@ void VertexArrayObject::Release()
else if (this->d->IsReady())
{
Private::AttributeMap::const_iterator it;
for (it = this->d->atttributes.begin(); it != this->d->atttributes.end();
for (it = this->d->attributes.begin(); it != this->d->attributes.end();
++it)
{
std::vector<VertexAttributes>::const_iterator attrIt;
......@@ -164,6 +164,21 @@ void VertexArrayObject::Release()
}
}
void VertexArrayObject::Initialize()
{
this->Release();
Private::AttributeMap::iterator it;
for (it = this->d->attributes.begin(); it != this->d->attributes.end();
++it)
{
it->second.clear();
}
this->d->attributes.clear();
this->d->handleProgram = 0;
}
bool VertexArrayObject::AddAttributeArray(ShaderProgram &program,
BufferObject &buffer,
const std::string &name,
......@@ -214,8 +229,8 @@ bool VertexArrayObject::AddAttributeArray(ShaderProgram &program,
if (!this->d->supported)
{
GLuint handleBuffer = buffer.GetHandle();
Private::AttributeMap::iterator it = this->d->atttributes.find(handleBuffer);
if (it != this->d->atttributes.end())
Private::AttributeMap::iterator it = this->d->attributes.find(handleBuffer);
if (it != this->d->attributes.end())
{
std::vector<VertexAttributes> &attribsVector = it->second;
std::vector<VertexAttributes>::iterator it2;
......@@ -234,7 +249,7 @@ bool VertexArrayObject::AddAttributeArray(ShaderProgram &program,
{
std::vector<VertexAttributes> attribsVector;
attribsVector.push_back(attribs);
this->d->atttributes[handleBuffer] = attribsVector;
this->d->attributes[handleBuffer] = attribsVector;
}
}
......@@ -260,7 +275,7 @@ bool VertexArrayObject::RemoveAttributeArray(const std::string &name)
if (!this->d->supported)
{
Private::AttributeMap::iterator it;
for (it = this->d->atttributes.begin(); it != this->d->atttributes.end();
for (it = this->d->attributes.begin(); it != this->d->attributes.end();
++it)
{
std::vector<VertexAttributes>::iterator attrIt;
......
......@@ -43,6 +43,8 @@ public:
void Release();
void Initialize();
bool AddAttributeArray(ShaderProgram &program, BufferObject &buffer,
const std::string &name, int offset, size_t stride,
int elementType, int elementTupleSize, bool normalize);
......
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