Commit 3aff0add authored by Andreas Neu's avatar Andreas Neu
Browse files

Added State::enableVertexBuffer(), State::disableVertexBuffer() and enableFrameBuffer(),

so that the overhead is taken out of the State::render() method.
Furthermore State::update now builds an attribute list ordered by the array-buffer-id in order to
avoid multiple bind calls of the same array buffer.
parent 6cd625e1
......@@ -32,14 +32,15 @@ class State
private:
struct AttributeMapping
{
int idInVertexBuffer;
int idInShaderProgram;
int arrayBufferID;
int vertexBufferAttribute;
int shaderProgramAttribute;
};
struct FragmentDataMapping
{
int idInFrameBuffer;
int idInShaderProgram;
int frameBufferColorAttachment;
int shaderProgramFragmentData;
};
// ===================================================================================================== \/
// ============================================================================================ TYPEDEFS \/
......@@ -83,6 +84,9 @@ public:
// ============================================================================================ METHODS \/
// ==================================================================================================== \/
public:
void enableVertexBuffer();
void enableFrameBuffer();
void disableVertexBuffer();
void update (void);
void render (void);
......
......@@ -73,6 +73,11 @@ public:
public:
int_t getAttributeIndexByName(const std::string& _name) const;
inline int_t getArrayBufferIDByAttributeIndex(AttributeVec::size_type _indexInArray) const
{
return mAttributes[_indexInArray].bufferID;
}
inline void setAttributePointer(AttributeVec::size_type _indexInArray, GLuint _indexInShader) const
{
mArrayBuffers[mAttributes[_indexInArray].bufferID]->setAttributePointer(
......
......@@ -14,6 +14,48 @@ using namespace ACGL::Base;
using namespace ACGL::Utils;
using namespace ACGL::OpenGL;
void State::enableFrameBuffer()
{
mpFrameBuffer->bind();
glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);
}
void State::enableVertexBuffer()
{
if(mpVertexBuffer->getElementArrayBuffer())
mpVertexBuffer->getElementArrayBuffer()->bind();
int currentArrayBufferID = -1;
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
{
if(mAttributeMappings[k].arrayBufferID != currentArrayBufferID)
{
mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].arrayBufferID]->bind();
currentArrayBufferID = mAttributeMappings[k].arrayBufferID;
}
mpVertexBuffer->setAttributePointer(mAttributeMappings[k].vertexBufferAttribute, mAttributeMappings[k].shaderProgramAttribute);
glEnableVertexAttribArray(mAttributeMappings[k].shaderProgramAttribute);
openGLRareError();
}
}
void State::disableVertexBuffer()
{
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
{
mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].arrayBufferID]->bind();
glDisableVertexAttribArray(mAttributeMappings[k].shaderProgramAttribute);
openGLRareError();
}
}
void State::render(void)
{
mpVertexBuffer->draw();
}
void State::update (void)
{
ShaderProgram::ConstSharedShaderVec shaders = mpShaderProgram->getShaders();
......@@ -25,10 +67,22 @@ void State::update (void)
for(Shader::AttributeVec::size_type k = 0; k < attributeVec.size(); k++)
{
AttributeMapping mapping;
mapping.idInShaderProgram = mpShaderProgram->getAttributeLocation(attributeVec[k]);
mapping.idInVertexBuffer = mpVertexBuffer->getAttributeIndexByName(attributeVec[k]);
mAttributeMappings.push_back(mapping);
mapping.shaderProgramAttribute = mpShaderProgram->getAttributeLocation(attributeVec[k]);
mapping.vertexBufferAttribute = mpVertexBuffer->getAttributeIndexByName(attributeVec[k]);
mapping.arrayBufferID = mpVertexBuffer->getArrayBufferIDByAttributeIndex(mapping.vertexBufferAttribute);
bool inserted = false;
for (AttributeMappingVec::iterator it = mAttributeMappings.begin(); it < mAttributeMappings.end() && !inserted; it++)
{
if((*it).arrayBufferID > mapping.arrayBufferID)
{
mAttributeMappings.insert(it, mapping);
inserted = true;
}
}
if(!inserted)
mAttributeMappings.push_back(mapping);
}
const Shader::FragmentDataVec& fragmentDataVec = shaders[shader]->getFragmentData();
......@@ -36,8 +90,8 @@ void State::update (void)
for(Shader::FragmentDataVec::size_type k = 0; k < fragmentDataVec.size(); k++)
{
FragmentDataMapping mapping;
mapping.idInShaderProgram = mpShaderProgram->getFragmentDataLocation(fragmentDataVec[k]);
mapping.idInFrameBuffer = mpFrameBuffer->getColorAttachmentIndexByName(fragmentDataVec[k]);
mapping.shaderProgramFragmentData = mpShaderProgram->getFragmentDataLocation(fragmentDataVec[k]);
mapping.frameBufferColorAttachment = mpFrameBuffer->getColorAttachmentIndexByName(fragmentDataVec[k]);
mFragmentDataMappings.push_back(mapping);
}
......@@ -45,34 +99,5 @@ void State::update (void)
mpBuffers = new GLuint[mFragmentDataMappings.size()];
for(FragmentDataMappingVec::size_type k = 0; k < mFragmentDataMappings.size(); k++)
mpBuffers[k] = GL_COLOR_ATTACHMENT0 + mFragmentDataMappings[k].idInFrameBuffer;
mpBuffers[k] = GL_COLOR_ATTACHMENT0 + mFragmentDataMappings[k].frameBufferColorAttachment;
}
void State::render(void)
{
//Setting the draw buffers of the framebuffer
mpFrameBuffer->bind();
glDrawBuffers(mFragmentDataMappings.size(), mpBuffers);
if(mpVertexBuffer->getElementArrayBuffer())
mpVertexBuffer->getElementArrayBuffer()->bind();
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
{
mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].idInVertexBuffer]->bind();
mpVertexBuffer->setAttributePointer(mAttributeMappings[k].idInVertexBuffer, mAttributeMappings[k].idInShaderProgram);
glEnableVertexAttribArray(mAttributeMappings[k].idInShaderProgram);
openGLRareError();
}
mpVertexBuffer->draw();
for(AttributeMappingVec::size_type k = 0; k < mAttributeMappings.size(); k++)
{
mpVertexBuffer->getArrayBuffers()[mAttributeMappings[k].idInVertexBuffer]->bind();
glDisableVertexAttribArray(mAttributeMappings[k].idInShaderProgram);
openGLRareError();
}
}
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