Commit b7922209 authored by Christopher Tenter's avatar Christopher Tenter

- remove shader modifier limit closes #2306

- instanced render objects

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@19916 383ad7c9-94d9-4d36-a494-682f7c89f535
parent d0db9b1a
......@@ -884,7 +884,10 @@ void IRenderer::drawObject(ACG::RenderObject* _obj)
glPolygonMode(GL_FRONT_AND_BACK, _obj->fillMode);
if (noIndices) {
glDrawArrays(_obj->primitiveMode, _obj->indexOffset, _obj->numIndices);
if (_obj->numInstances <= 0)
glDrawArrays(_obj->primitiveMode, _obj->indexOffset, _obj->numIndices);
else
glDrawArraysInstanced(_obj->primitiveMode, _obj->indexOffset, _obj->numIndices, _obj->numInstances);
}
else
{
......@@ -898,8 +901,12 @@ void IRenderer::drawObject(ACG::RenderObject* _obj)
default: indexSize = 1; break;
}
glDrawElements(_obj->primitiveMode, _obj->numIndices, _obj->indexType,
((const char*)_obj->sysmemIndexBuffer) + _obj->indexOffset * indexSize);
if (_obj->numInstances <= 0)
glDrawElements(_obj->primitiveMode, _obj->numIndices, _obj->indexType,
((const char*)_obj->sysmemIndexBuffer) + _obj->indexOffset * indexSize);
else
glDrawElementsInstanced(_obj->primitiveMode, _obj->numIndices, _obj->indexType,
((const char*)_obj->sysmemIndexBuffer) + _obj->indexOffset * indexSize, _obj->numInstances);
}
}
}
......@@ -907,7 +914,7 @@ void IRenderer::drawObject(ACG::RenderObject* _obj)
void IRenderer::renderObject(ACG::RenderObject* _obj,
GLSL::Program* _prog,
bool _constRenderStates,
unsigned int _shaderModifiers)
const std::vector<unsigned int>* _shaderModifiers)
{
// select shader from cache
GLSL::Program* prog = _prog ? _prog : ACG::ShaderCache::getInstance()->getProgram(&_obj->shaderDesc, _shaderModifiers);
......@@ -1059,11 +1066,7 @@ QString IRenderer::dumpCurrentRenderObjectsToString(ACG::RenderObject** _list, b
QTextStream outStrm(&objectString);
std::vector<ACG::ShaderModifier*>::iterator it;
unsigned int usage = 0;
if (_modifiers) {
for (it = _modifiers->begin(); it != _modifiers->end(); ++it)
usage |= (*it)->getID();
}
for (int i = 0; i < getNumRenderObjects(); ++i)
{
const RenderObject* obj = _list ? _list[i] : &renderObjects_[i];
......@@ -1077,10 +1080,7 @@ QString IRenderer::dumpCurrentRenderObjectsToString(ACG::RenderObject** _list, b
outStrm << obj->shaderDesc.toString();
ShaderProgGenerator progGen(&(obj->shaderDesc), usage);
if (!usage)
progGen.generateShaders();
ShaderProgGenerator progGen(&(obj->shaderDesc), _modifiers);
outStrm << "\n---------------------vertex-shader--------------------\n\n";
for (int i = 0; i < progGen.getVertexShaderCode().size(); ++i)
......
......@@ -200,7 +200,7 @@ protected:
*
* Shader modifiers can be combined and applied to this pass.
*/
virtual void renderObject(ACG::RenderObject* _obj, GLSL::Program* _prog = 0, bool _constRenderStates = false, unsigned int _shaderModifiers = 0);
virtual void renderObject(ACG::RenderObject* _obj, GLSL::Program* _prog = 0, bool _constRenderStates = false, const std::vector<unsigned int>* _shaderModifiers = 0);
/** \brief Binding VBOs (First state function)
*
......
......@@ -177,6 +177,7 @@ RenderObject::RenderObject()
vertexArrayObject(0),
vertexBuffer(0), indexBuffer(0), sysmemIndexBuffer(0),
primitiveMode(GL_TRIANGLES), numIndices(0), indexOffset(0), indexType(GL_UNSIGNED_INT),
numInstances(0),
vertexDecl(0),
culling(true), blending(false), alphaTest(false),
depthTest(true), depthWrite(true),
......
......@@ -193,6 +193,13 @@ struct ACGDLLEXPORT RenderObject
*/
GLenum indexType;
/**\ brief Instancing
*
* Number of instances to render.
* numinstances <= 0, disables instancing.
*/
GLsizei numInstances;
/// Defines the vertex buffer layout, ignored if VAO is provided
const VertexDeclaration* vertexDecl;
......@@ -336,7 +343,7 @@ public:
// opengl style helper function interface:
// provided for easier setup of RenderObjects,
// usage is not necessary
void glBindBuffer(GLenum target, GLuint buffer)
inline void glBindBuffer(GLenum target, GLuint buffer)
{
switch (target)
{
......@@ -345,19 +352,28 @@ public:
}
}
void glDrawArrays(GLenum mode, GLint first, GLsizei count)
inline void glDrawArrays(GLenum mode, GLint first, GLsizei count)
{
this->glDrawArraysInstanced(mode, first, count, 0);
}
inline void glDrawArraysInstanced(GLenum mode, GLint first, GLsizei count, GLsizei primcount)
{
indexBuffer = 0;
sysmemIndexBuffer = 0;
primitiveMode = mode;
indexOffset = first;
numIndices = count;
numInstances = primcount;
}
inline void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
{
this->glDrawElementsInstanced(mode, count, type, indices, 0);
}
void glDrawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices)
inline void glDrawElementsInstanced(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei primcount)
{
primitiveMode = mode;
numIndices = count;
......@@ -366,12 +382,12 @@ public:
sysmemIndexBuffer = indices;
}
void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
inline void glColorMask(GLboolean r, GLboolean g, GLboolean b, GLboolean a)
{
colorWriteMask[0] = r; colorWriteMask[1] = g; colorWriteMask[2] = b; colorWriteMask[3] = a;
}
void glAlphaFunc(GLenum func, float ref)
inline void glAlphaFunc(GLenum func, float ref)
{
alphaFunc = func;
alphaRef = ref;
......
......@@ -91,16 +91,22 @@ ShaderCache* ACG::ShaderCache::getInstance()
}
GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc )
{
std::vector<unsigned int> dummy;
return getProgram(_desc, dummy);
}
//***********************************************************************
// TODO implement binary search eventually (if cache access is getting too slow)
// - modify compareShaderGenDescs s.t. it defines an order
// or generate a hash key from ShaderGenDesc
GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc, unsigned int _usage )
GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc, const std::vector<unsigned int>& _mods )
{
CacheEntry newEntry;
newEntry.desc = *_desc;
newEntry.usage = _usage;
newEntry.mods = _mods;
if (!_desc->fragmentTemplateFile.isEmpty())
{
......@@ -147,7 +153,7 @@ GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc, unsigne
}
// glsl program not in cache, generate shaders
ShaderProgGenerator progGen(_desc, _usage);
ShaderProgGenerator progGen(_desc, _mods);
if (!dbgOutputDir_.isEmpty())
{
......@@ -163,7 +169,11 @@ GLSL::Program* ACG::ShaderCache::getProgram( const ShaderGenDesc* _desc, unsigne
QTextStream outStrm(&fileOut);
outStrm << _desc->toString();
outStrm << "\nusage: " << _usage << "\n";
outStrm << "\nmods: ";
for (size_t i = 0; i < _mods.size(); ++i)
outStrm << _mods[i] << (i+1 < _mods.size() ? ", " : "");
outStrm << "\n";
outStrm << "\n---------------------vertex-shader--------------------\n\n";
......@@ -280,7 +290,6 @@ GLSL::Program* ACG::ShaderCache::getProgram( const char* _vertexShaderFile,
QStringList* _macros, bool _verbose )
{
CacheEntry newEntry;
newEntry.usage = 0;
// store filenames and timestamps in new entry
......@@ -435,7 +444,6 @@ GLSL::Program* ACG::ShaderCache::getProgram( const char* _vertexShaderFile, cons
GLSL::Program* ACG::ShaderCache::getComputeProgram(const char* _computeShaderFile, QStringList* _macros /* = 0 */, bool _verbose /* = true */)
{
CacheEntry newEntry;
newEntry.usage = 0;
// store filenames and timestamps in new entry
// use vertex shader filename as compute shader
......@@ -560,7 +568,7 @@ bool ACG::ShaderCache::compareTimeStamp(const CacheEntry* _a, const CacheEntry*
int ACG::ShaderCache::compareShaderGenDescs( const CacheEntry* _a, const CacheEntry* _b)
{
if (_a->usage != _b->usage)
if (_a->mods != _b->mods)
return -1;
const ShaderGenDesc* a = &_a->desc;
......
......@@ -86,10 +86,30 @@ public:
/** \brief Query a dynamically generated program from cache
*
* @param _desc Shader description
* @param _usage Additional usage identifier (combination of shader modifier ids)
* @param _mods Combination of active shader modifier ids
* @return The program (Either from cache or newly compiled and linked)
*/
GLSL::Program* getProgram(const ShaderGenDesc* _desc, unsigned int _usage = 0);
GLSL::Program* getProgram(const ShaderGenDesc* _desc, const std::vector<unsigned int>& _mods);
/** \brief Query a dynamically generated program from cache
*
* @param _desc Shader description
* @param _mods Combination of active shader modifier ids
* @return The program (Either from cache or newly compiled and linked)
*/
GLSL::Program* getProgram(const ShaderGenDesc* _desc, const std::vector<unsigned int>* _mods)
{
return _mods ? getProgram(_desc, *_mods) : getProgram(_desc);
}
/** \brief Query a dynamically generated program from cache
*
* @param _desc Shader description
* @param _mods Combination of active shader modifier ids
* @return The program (Either from cache or newly compiled and linked)
*/
GLSL::Program* getProgram(const ShaderGenDesc* _desc);
/** \brief Query a static shader program from cache
*
......@@ -161,7 +181,7 @@ protected:
struct CacheEntry
{
ShaderGenDesc desc;
unsigned int usage;
std::vector<unsigned int> mods;
// string-pointer in ShaderGenDesc may not be permanent,
// so copy string data here
......
This diff is collapsed.
......@@ -47,6 +47,7 @@
#include <string>
#include <list>
#include <map>
#include <vector>
#include <ACG/GL/gl.hh>
#include <ACG/Config/ACGDefines.hh>
......@@ -844,7 +845,16 @@ public:
*/
unsigned int getID() {return modifierID_;}
operator unsigned int() const {return modifierID_;}
// operator unsigned int() const {return modifierID_;}
operator std::vector<unsigned int>() const {return std::vector<unsigned int>(1,modifierID_);}
std::vector<unsigned int> operator | (const std::vector<unsigned int>& _v) const
{
std::vector<unsigned int> r(1 + _v.size(), modifierID_);
for (size_t i = 0; i < _v.size(); ++i)
r[i + 1] = _v[i];
return r;
}
private:
unsigned int modifierID_;
......@@ -871,9 +881,47 @@ public:
/**
@param _desc description-set of shader properties.
@param _modifierFlags bitflag of modifier-IDs that should be used for the generation.
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, unsigned int _modifierFlags = 0);
ShaderProgGenerator(const ShaderGenDesc* _desc);
/**
@param _desc description-set of shader properties.
@param _modifierIDs array of modifier-IDs that should be used for the generation.
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, const std::vector<unsigned int>& _modifierIDs);
/**
@param _desc description-set of shader properties.
@param _modifierIDs array of modifier-IDs that should be used for the generation.
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, const std::vector<unsigned int>* _modifierIDs);
/**
@param _desc description-set of shader properties.
@param _modifierIDs array of modifier-IDs that should be used for the generation.
@param _numModifiers number of modifiers in _modifierIDs
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, const unsigned int* _modifierIDs, unsigned int _numModifiers);
/**
@param _desc description-set of shader properties.
@param _modifiers array of modifiers that should be used for the generation.
@param _numModifiers number of modifiers in _modifierIDs
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, ShaderModifier* const* _modifiers, unsigned int _numModifiers);
/**
@param _desc description-set of shader properties.
@param _modifiers array of modifiers that should be used for the generation.
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, const std::vector<ShaderModifier*>& _modifiers);
/**
@param _desc description-set of shader properties.
@param _modifiers array of modifiers that should be used for the generation.
*/
ShaderProgGenerator(const ShaderGenDesc* _desc, const std::vector<ShaderModifier*>* _modifiers);
virtual ~ShaderProgGenerator(void);
......@@ -945,6 +993,10 @@ private:
*/
void scanShaderTemplate(QStringList& _templateSrc, QString _templateFilename, QStringList* _outLayoutDirectives = 0);
/** \brief Called in constructor
*/
void init(const ShaderGenDesc* _desc, ShaderModifier* const* _modifiers, unsigned int _numActiveMods);
void init(const ShaderGenDesc* _desc, const unsigned int* _modifiers, unsigned int _numActiveMods);
void buildVertexShader();
void buildTessControlShader();
......@@ -1007,12 +1059,12 @@ private:
QStringList fragmentTemplate_;
ShaderGenDesc desc_;
unsigned int usage_;
std::vector<ShaderModifier*> activeMods_;
/// registered shader modifier
static int numModifiers_;
static ShaderModifier* modifiers_[32];
static int numRegisteredModifiers_;
static std::vector<ShaderModifier*> registeredModifiers_;
/// path + filename to shader templates
QString vertexShaderFile_;
......
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