Commit 60e514da authored by zmoelnig's avatar zmoelnig
Browse files

use integer keys rather than strings to speed up GemState again

git-svn-id: https://pd-gem.svn.sourceforge.net/svnroot/pd-gem/trunk/Gem@3853 49b2a0ca-c541-4d49-a7ef-d785e4371512
parent 36a7b7f2
......@@ -368,12 +368,12 @@ void GemMan :: fillGemState(GemState &state)
{
if (s_lightState) {
// GemState['lighting'] = 1;
state.set(gensym("gl.lighting"), true);
state.set(gensym("gl.smooth"), true);
state.set(GemState::_GL_LIGHTING, true);
state.set(GemState::_GL_SMOOTH, true);
}
gem::GLStack*stacks=NULL;
state.get("gl.stacks", stacks);
state.get(GemState::_GL_STACKS, stacks);
if(stacks)stacks->reset();
}
......@@ -520,7 +520,7 @@ void GemMan :: render(void *)
else
tickTime = static_cast<float>(clock_gettimesince(m_lastRenderTime));
currentState.set("timing.tick", tickTime);
currentState.set(GemState::_TIMING_TICK, tickTime);
m_lastRenderTime = clock_getsystime();
......@@ -588,7 +588,7 @@ void GemMan :: render(void *)
// render right view
fillGemState(currentState);
tickTime=0;
currentState.set("timing.tick", tickTime);
currentState.set(GemState::_TIMING_TICK, tickTime);
renderChain(chain1, &currentState);
glMatrixMode(GL_MODELVIEW);
......@@ -697,7 +697,7 @@ void GemMan :: render(void *)
// render right view
fillGemState(currentState);
tickTime=0;
currentState.set("timing.tick", tickTime);
currentState.set(GemState::_TIMING_TICK, tickTime);
renderChain(chain1, &currentState);
glMatrixMode(GL_MODELVIEW);
......@@ -760,7 +760,7 @@ void GemMan :: render(void *)
// render right view
fillGemState(currentState);
tickTime=0;
currentState.set("timing.tick", tickTime);
currentState.set(GemState::_TIMING_TICK, tickTime);
renderChain(chain1, &currentState);
glMatrixMode(GL_MODELVIEW);
......
......@@ -199,7 +199,7 @@ void GemPixDualObj :: stopRendering()
/////////////////////////////////////////////////////////
void GemPixDualObj :: rightRender(GemState *statePtr)
{
if (!statePtr || !statePtr->get("pix", m_pixRight) || !m_pixRight) {
if (!statePtr || !statePtr->get(GemState::_PIX, m_pixRight) || !m_pixRight) {
m_pixRightValid = 0;
m_pixRight = 0;
return;
......
......@@ -55,7 +55,7 @@ void GemPixObj :: render(GemState *state){
// the data is restored in the <postrender> call,
// so that the objects can rely on their (buffered) images
pixBlock*image=NULL;
if (!state || !state->get("pix", image))return;
if (!state || !state->get(GemState::_PIX, image))return;
if(!image ||
!&image->image) return;
cachedPixBlock.newimage=image->newimage;
......@@ -124,13 +124,13 @@ void GemPixObj :: render(GemState *state){
}
}
}
state->set("pix", image);
state->set(GemState::_PIX, image);
}
//////////
// get the original state back
void GemPixObj :: postrender(GemState *state){
state->set("pix", orgPixBlock);
state->set(GemState::_PIX, orgPixBlock);
}
/////////////////////////////////////////////////////////
......
......@@ -63,8 +63,8 @@ void GemShape :: SetVertex(GemState* state,float x, float y, float z, float tx,
int numCoords = 0;
int numUnits = 0;
state->get("gl.tex.numcoords", numCoords);
state->get("gl.tex.units", numUnits);
state->get(GemState::_GL_TEX_NUMCOORDS, numCoords);
state->get(GemState::_GL_TEX_UNITS, numUnits);
if (numCoords) {
......@@ -90,8 +90,8 @@ void GemShape :: SetVertex(GemState* state,float x, float y, float z,
int numCoords = 0;
int numUnits = 0;
state->get("gl.tex.numcoords", numCoords);
state->get("gl.tex.units", numUnits);
state->get(GemState::_GL_TEX_NUMCOORDS, numCoords);
state->get(GemState::_GL_TEX_UNITS, numUnits);
......@@ -196,10 +196,10 @@ void GemShape :: render(GemState *state)
m_texCoords=NULL;
m_lighting=false;
state->get("gl.tex.coords", m_texCoords);
state->get("gl.tex.type", m_texType);
state->get("gl.tex.numcoords", m_texNum);
state->get("gl.lighting", m_lighting);
state->get(GemState::_GL_TEX_COORDS, m_texCoords);
state->get(GemState::_GL_TEX_TYPE, m_texType);
state->get(GemState::_GL_TEX_NUMCOORDS, m_texNum);
state->get(GemState::_GL_LIGHTING, m_lighting);
renderShape(state);
......
......@@ -31,7 +31,7 @@
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#define CATCH_ANY(y) catch(gem::bad_any_cast&x) { ::verbose(3, "%s:%d [%s] %s:: %s", __FILE__, __LINE__, __FUNCTION__, (y), x.what().c_str()); }
#define CATCH_ANY(y) catch(gem::bad_any_cast&x) { ::verbose(3, "%s:%d [%s] %d:: %s", __FILE__, __LINE__, __FUNCTION__, (y), x.what().c_str()); }
using namespace gem;
......@@ -58,7 +58,7 @@ class GemStateData {
protected:
// dictionary for setting values
std::map <std::string, any> data;
std::map <key_t, any> data;
std::auto_ptr<GLStack>stacks;
};
......@@ -90,20 +90,20 @@ GemState :: GemState()
stackDepth[GemMan::STACKPROJECTION]=
1; // 1 is the current matrix
set("dirty", (dirty=false));
set("gl.displaylist", (inDisplayList=false));
set(_DIRTY, (dirty=false));
set(_GL_DISPLAYLIST, (inDisplayList=false));
set("gl.lighting", (lighting=false));
set("gl.smooth", (smooth=false));
set("gl.tex.type", (texture=0));
// set("pix", (image=0));
set("gl.tex.numcoords", (numTexCoords=0)); // LATER get rid of this
set("gl.tex.coords", (texCoords=0)); // LATER make this a std::vector
set("gl.tex.units", (multiTexUnits=0));
set("timing.tick", (tickTime=50.f));
set("gl.drawtype", (drawType=0));
set(_GL_LIGHTING, (lighting=false));
set(_GL_SMOOTH, (smooth=false));
set(_GL_TEX_TYPE, (texture=0));
// set(_PIX, (image=0));
set(_GL_TEX_NUMCOORDS, (numTexCoords=0)); // LATER get rid of this
set(_GL_TEX_COORDS, (texCoords=0)); // LATER make this a std::vector
set(_GL_TEX_UNITS, (multiTexUnits=0));
set(_TIMING_TICK, (tickTime=50.f));
set(_GL_DRAWTYPE, (drawType=0));
set("gl.stacks", data->stacks.get());
set(_GL_STACKS, data->stacks.get());
/*
set("vertex.array.vertex", 0);
......@@ -154,12 +154,12 @@ void GemState :: reset() {
if(GemMan::windowExists()) {
GLStack *stacks;
get("gl.stacks", stacks);
get(GemState::_GL_STACKS, stacks);
stacks->reset();
}
set("pix", (image=0));
set("gl.tex.numcoords", (numTexCoords=0));
set(GemState::_PIX, (image=0));
set(GemState::_GL_TEX_NUMCOORDS, (numTexCoords=0));
}
......@@ -187,26 +187,27 @@ float GemState::texCoordY(int num) const {
/* get a named property */
bool GemState::get(const std::string key, any&value) {
std::map<std::string,any>::iterator it = data->data.find(key);
bool GemState::get(const key_t key, any&value) {
std::map<int,any>::iterator it =
data->data.find(key);
if(it==data->data.end()) {
if(key=="pix") { value=image; return true; }
if(key=="gl.tex.numcoords") { value=numTexCoords; return true; }
if(key==_PIX) { value=image; return true; }
if(key==_GL_TEX_NUMCOORDS) { value=numTexCoords; return true; }
return false;
if(key=="dirty") { value=dirty; return true; }
if(key=="gl.displaylist") { value=inDisplayList; return true; }
if(key==_DIRTY) { value=dirty; return true; }
if(key==_GL_DISPLAYLIST) { value=inDisplayList; return true; }
if(key=="gl.lighting") { value=lighting; return true; }
if(key=="gl.smooth") { value=smooth; return true; }
if(key=="gl.tex.type") { value=texture; return true; }
if(key=="gl.tex.coords") { if(!texCoords)return false; value=texCoords; return true; }
if(key=="gl.tex.units") { value=multiTexUnits; return true; }
if(key=="timing.tick") { value=tickTime; return true; }
if(key=="gl.drawtype") { value=drawType; return true; }
if(key==_GL_LIGHTING) { value=lighting; return true; }
if(key==_GL_SMOOTH) { value=smooth; return true; }
if(key==_GL_TEX_TYPE) { value=texture; return true; }
if(key==_GL_TEX_COORDS) { if(!texCoords)return false; value=texCoords; return true; }
if(key==_GL_TEX_UNITS) { value=multiTexUnits; return true; }
if(key==_TIMING_TICK) { value=tickTime; return true; }
if(key==_GL_DRAWTYPE) { value=drawType; return true; }
#if 0
//if(key=="gl.stacks") { value=stackDepth[4]; return true; }
//if(key==GemState::_GL_STACKS) { value=stackDepth[4]; return true; }
if(key=="vertex.dirty") { value=VertexDirty; return true; }
if(key=="*VertexArray") { value=*VertexArray; return true; }
......@@ -228,7 +229,7 @@ bool GemState::get(const std::string key, any&value) {
}
/* set a named property */
bool GemState::set(const std::string key, any value) {
bool GemState::set(const key_t key, any value) {
if(value.empty()) {
data->data.erase(key);
return false;
......@@ -236,24 +237,29 @@ bool GemState::set(const std::string key, any value) {
/* wrapper for DEPRECATED access to member variables */
if(1) {
if(key=="dirty") { try { dirty=gem::any_cast<bool>(value); } CATCH_ANY(key.c_str()); }
else if(key=="pix") { try { image=gem::any_cast<pixBlock*>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.tex.numcoords") { try { numTexCoords=gem::any_cast<int>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.tex.coords") { try { texCoords=gem::any_cast<TexCoord*>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.lighting") { try { lighting=gem::any_cast<bool>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.smooth") { try { smooth=gem::any_cast<bool>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.tex.type") { try { texture=gem::any_cast<int>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.tex.units") { try { multiTexUnits=gem::any_cast<int>(value); } CATCH_ANY(key.c_str()); }
else if(key=="timing.tick") { try { tickTime=gem::any_cast<float>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.drawtype") { try { drawType=gem::any_cast<GLenum>(value); } CATCH_ANY(key.c_str()); }
else if(key=="gl.displaylist") { try { inDisplayList=gem::any_cast<bool>(value); } CATCH_ANY(key.c_str()); }
try {
switch(key) {
case(_DIRTY): dirty=gem::any_cast<bool>(value); break;
case(_PIX): image=gem::any_cast<pixBlock*>(value); break;
case(_GL_TEX_NUMCOORDS): numTexCoords=gem::any_cast<int>(value); break;
case(_GL_TEX_COORDS): texCoords=gem::any_cast<TexCoord*>(value); break;
case(_GL_LIGHTING): lighting=gem::any_cast<bool>(value); break;
case(_GL_SMOOTH): smooth=gem::any_cast<bool>(value); break;
case(_GL_TEX_TYPE): texture=gem::any_cast<int>(value); break;
case(_GL_TEX_UNITS): multiTexUnits=gem::any_cast<int>(value); break;
case(_TIMING_TICK): tickTime=gem::any_cast<float>(value); break;
case(_GL_DRAWTYPE): drawType=gem::any_cast<GLenum>(value); break;
case(_GL_DISPLAYLIST): inDisplayList=gem::any_cast<bool>(value); break;
default: break;
}
} CATCH_ANY(key);
}
data->data[key]=value;
return true;
}
/* remove a named property */
bool GemState::remove(const std::string key) {
bool GemState::remove(const key_t key) {
return (0!=data->data.erase(key));
}
......@@ -58,6 +58,26 @@ class GemStateData;
class GEM_EXTERN GemState
{
public:
typedef enum {
_DIRTY,
_TIMING_TICK,
_PIX,
_GL_STACKS,
_GL_DISPLAYLIST,
_GL_LIGHTING,
_GL_SMOOTH,
_GL_DRAWTYPE,
_GL_TEX_TYPE,
_GL_TEX_COORDS,
_GL_TEX_NUMCOORDS,
_GL_TEX_UNITS,
_LAST
} key_t;
//////////
// Has something changed since the last time?
......@@ -173,11 +193,10 @@ class GEM_EXTERN GemState
* the value of the 2nd argument is set accordingly and <code>true</code> is returned
* if the key does not exist (or the type is wrong) the value is not touched and <code>false</code> is returned instead
*/
virtual bool get(const std::string key, gem::any&value);
virtual bool get(const t_symbol*key, gem::any&value) { return get(key->s_name, value); }
virtual bool get(const key_t key, gem::any&value);
template<class T>
bool get(const std::string key, T&value) {
bool get(const key_t key, T&value) {
try {
gem::any val;
if(!get(key,val)) {
......@@ -187,22 +206,15 @@ class GEM_EXTERN GemState
value=gem::any_cast<T>(val);
return true;
} catch (gem::bad_any_cast&x) {
::verbose(3, "%s:%d [%s] %s :: %s", __FILE__, __LINE__, __FUNCTION__, key.c_str(), x.what().c_str());
::verbose(3, "%s:%d [%s] %d :: %s", __FILE__, __LINE__, __FUNCTION__, key, x.what().c_str());
// type problem
}
return false;
};
template<class T>
bool get(const t_symbol*key, T&value) {
return get(key->s_name, value);
}
/* set a named property */
virtual bool set(const std::string key, gem::any value);
virtual bool set(const t_symbol*key, gem::any value) { return set(key->s_name, value); }
virtual bool set(const key_t key, gem::any value);
/* remove a named property */
virtual bool remove(const std::string key);
virtual bool remove(const t_symbol*key) { return remove(key->s_name);}
virtual bool remove(const key_t key);
// Copy assignment
GemState& operator=(const GemState&);
......
......@@ -100,7 +100,7 @@ void gemframebuffer :: render(GemState *state)
{
gem::GLStack*stacks=NULL;
if(state) {
state->get("gl.stacks", stacks);
state->get(GemState::_GL_STACKS, stacks);
}
if(!m_width || !m_height) {
......@@ -156,7 +156,7 @@ void gemframebuffer :: postrender(GemState *state)
t_float w, h;
gem::GLStack*stacks=NULL;
if(state) {
state->get("gl.stacks", stacks);
state->get(GemState::_GL_STACKS, stacks);
}
glActiveTexture(GL_TEXTURE0_ARB + m_texunit);
......
......@@ -88,10 +88,10 @@ void gemhead :: renderGL(GemState *state)
{
state->reset();
// set the state dirty flag
state->set("dirty", m_cache->dirty);
state->set(GemState::_DIRTY, m_cache->dirty);
state->VertexDirty=m_cache->vertexDirty;
state->get("gl.stacks", stacks);
state->get(GemState::_GL_STACKS, stacks);
if(stacks)stacks->push();
}
......@@ -108,7 +108,7 @@ void gemhead :: renderGL(GemState *state)
m_cache->dirty = false;
m_cache->vertexDirty=false;
state->get("gl.stacks", stacks);
state->get(GemState::_GL_STACKS, stacks);
if(stacks)stacks->pop();
}
......
......@@ -112,16 +112,16 @@ void gemlist :: trigger()
GemState state;
if(m_lightState) {
state.set("gl.lighting", true);
state.set("gl.smooth", true);
state.set(GemState::_GL_LIGHTING, true);
state.set(GemState::_GL_SMOOTH, true);
}
if(m_drawType) {
state.set("gl.drawtype", m_drawType);
state.set(GemState::_GL_DRAWTYPE, m_drawType);
}
if(m_tickTime>=0.f) {
state.set("timing.tick", m_tickTime);
state.set(GemState::_TIMING_TICK, m_tickTime);
}
// GemMan::fillGemState(state);
......
......@@ -58,9 +58,9 @@ void curve :: render(GemState *state)
int texType=0;
int texNum=0;
bool lighting=false;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
if(m_drawType==GL_DEFAULT_GEM)m_drawType=GL_LINE_STRIP;
glNormal3f(0.0f, 0.0f, 1.0f);
......
......@@ -66,10 +66,10 @@ void cylinder :: render(GemState *state)
int texType=0;
int texNum=0;
bool lighting=false;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get("gl.lighting", lighting);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
state->get(GemState::_GL_LIGHTING, lighting);
GLfloat xsize = 1.0, xsize0 = 0.0;
......
......@@ -97,10 +97,10 @@ void disk :: render(GemState *state)
int texType=0;
int texNum=0;
bool lighting=false;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get("gl.lighting", lighting);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
state->get(GemState::_GL_LIGHTING, lighting);
GLboolean normals = (lighting);
......
......@@ -324,9 +324,9 @@ void imageVert :: render(GemState *state)
int texType=0;
bool dl = false;
state->get("pix", img);
state->get("gl.tex.type", texType);
state->get("gl.displaylist", dl);
state->get(GemState::_PIX, img);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_DISPLAYLIST, dl);
// always want to render
if (!img) return;
......
......@@ -75,9 +75,9 @@ void sphere :: createSphere(GemState *state)
TexCoord*texCoords=NULL;
int texNum=0;
int texType=0;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
if(m_drawType==GL_DEFAULT_GEM)m_drawType=GL_FILL;
......@@ -243,10 +243,10 @@ void sphere :: render(GemState *state)
int texType=0;
int texNum=0;
bool lighting=false;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get("gl.lighting", lighting);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
state->get(GemState::_GL_LIGHTING, lighting);
GLfloat xsize = 1.0, xsize0 = 0.0;
GLfloat ysize = 1.0, ysize0 = 0.0;
......
......@@ -177,10 +177,10 @@ void sphere3d :: render(GemState *state)
int texType=0;
int texNum=0;
bool lighting=false;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get("gl.lighting", lighting);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
state->get(GemState::_GL_LIGHTING, lighting);
glPushMatrix();
glScalef(m_size, m_size, m_size);
......
......@@ -147,10 +147,10 @@ void teapot :: render(GemState *state)
int texType=0;
int texNum=0;
bool lighting=false;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get("gl.lighting", lighting);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
state->get(GemState::_GL_LIGHTING, lighting);
if (texType && texNum>=4) {
m_texCoords[0][0]=texCoords[0].s;m_texCoords[0][1]=texCoords[0].t;
......
......@@ -91,10 +91,10 @@ void torus :: render(GemState *state)
bool lighting = false;
int texNum=0;
int texType=0;
state->get("gl.tex.coords", texCoords);
state->get("gl.tex.type", texType);
state->get("gl.tex.numcoords", texNum);
state->get("gl.lighting", lighting);
state->get(GemState::_GL_TEX_COORDS, texCoords);
state->get(GemState::_GL_TEX_TYPE, texType);
state->get(GemState::_GL_TEX_NUMCOORDS, texNum);
state->get(GemState::_GL_LIGHTING, lighting);
GLenum type = m_drawType;
switch(m_drawType){
......
......@@ -67,7 +67,7 @@ separator :: separator(int argc, t_atom*argv)
m_active[GLStack::TEXTURE ]=true;
m_active[GLStack::PROJECTION]=true;
}
m_state.set("gl.tex.coords", static_cast<TexCoord*>(NULL));
m_state.set(GemState::_GL_TEX_COORDS, static_cast<TexCoord*>(NULL));
}
/////////////////////////////////////////////////////////
......@@ -77,7 +77,7 @@ separator :: separator(int argc, t_atom*argv)
separator :: ~separator()
{
TexCoord*tc=NULL;
m_state.get("gl.tex.coords", tc);
m_state.get(GemState::_GL_TEX_COORDS, tc);
if(tc)
delete [] tc;
}
......@@ -93,7 +93,7 @@ void separator :: render(GemState *state)
if(!state)return;
using namespace gem;
GLStack*stacks=NULL;
state->get("gl.stacks", stacks);
state->get(GemState::_GL_STACKS, stacks);
// push the current matrix stacks
if(stacks) {
#define PUSHGLSTACK(type) if(m_active[type])m_pushed[type]=stacks->push(type)
......@@ -107,20 +107,20 @@ void separator :: render(GemState *state)
# warning use GemState copy rather than manually copying each state!
#endif
SEPARATOR_STATEASSIGN(state, &m_state, bool, "gl.lighting");
SEPARATOR_STATEASSIGN(state, &m_state, bool, "gl.smooth");
SEPARATOR_STATEASSIGN(state, &m_state, int, "gl.tex.type");
SEPARATOR_STATEASSIGN(state, &m_state, int, "gl.tex.numcoords");
SEPARATOR_STATEASSIGN(state, &m_state, pixBlock*, "pix");
SEPARATOR_STATEASSIGN(state, &m_state, bool, GemState::_GL_LIGHTING);
SEPARATOR_STATEASSIGN(state, &m_state, bool, GemState