// ------------------------------------ #ifdef WIN32 #include #endif // WIN32 #ifdef __APPLE__ #define GL_GLEXT_LEGACY 1 #include #include #else #include #include #endif #include #include "BL_Shader.h" #include "BL_Material.h" #include "MT_assert.h" #include "MT_Matrix4x4.h" #include "MT_Matrix3x3.h" #include "KX_PyMath.h" #include "MEM_guardedalloc.h" #include "RAS_GLExtensionManager.h" //using namespace bgl; #define spit(x) std::cout << x << std::endl; const bool BL_Shader::Ok()const { return (mShader !=0 && mOk && mUse); } BL_Shader::BL_Shader(PyTypeObject *T) : PyObjectPlus(T), mShader(0), mVert(0), mFrag(0), mPass(1), mOk(0), mUse(0), vertProg(""), fragProg(""), mError(0), mLog(0) { // if !RAS_EXT_support._ARB_shader_objects this class will not be used for (int i=0; i 1){ PrintInfo(vertlen,tmpVert, &char_len); goto programError; } // check for compile errors bgl::blGetObjectParameterivARB(tmpVert, GL_OBJECT_COMPILE_STATUS_ARB, &vertstatus); if(!vertstatus) goto programError; // -- fragment shader ---------------- tmpFrag = bgl::blCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); bgl::blShaderSourceARB(tmpFrag, 1,(const char**)&fragProg, 0); bgl::blCompileShaderARB(tmpFrag); bgl::blGetObjectParameterivARB(tmpFrag, GL_OBJECT_INFO_LOG_LENGTH_ARB, &fraglen); if(fraglen >1 ){ PrintInfo(fraglen,tmpFrag, &char_len); goto programError; } bgl::blGetObjectParameterivARB(tmpFrag, GL_OBJECT_COMPILE_STATUS_ARB, &fragstatus); if(!fragstatus) goto programError; // -- program ------------------------ // set compiled vert/frag shader & link tmpProg = bgl::blCreateProgramObjectARB(); bgl::blAttachObjectARB(tmpProg, tmpVert); bgl::blAttachObjectARB(tmpProg, tmpFrag); bgl::blLinkProgramARB(tmpProg); bgl::blGetObjectParameterivARB(tmpProg, GL_OBJECT_INFO_LOG_LENGTH_ARB, &proglen); bgl::blGetObjectParameterivARB(tmpProg, GL_OBJECT_LINK_STATUS_ARB, &progstatus); if(!progstatus) goto programError; if(proglen > 0) { // print success PrintInfo(proglen,tmpProg, &char_len); if(char_len >0) spit(mLog); mError = 0; } // set mShader = tmpProg; mVert = tmpVert; mFrag = tmpFrag; mOk = 1; return true; programError: if(tmpVert) { bgl::blDeleteObjectARB(tmpVert); tmpVert=0; } if(tmpFrag) { bgl::blDeleteObjectARB(tmpFrag); tmpFrag=0; } if(tmpProg) { bgl::blDeleteObjectARB(tmpProg); tmpProg=0; } mOk = 0; mUse=0; mError = 1; spit("----------"); spit("GLSL Error "); if(mLog) spit(mLog); spit("--------------------"); return false; #else return false; #endif//GL_ARB_shader_objects } void BL_Shader::PrintInfo(int len, unsigned int handle, int* num) { #ifdef GL_ARB_shader_objects mLog = (char*)MEM_mallocN(sizeof(char)*len, "print_log"); //MT_assert(mLog, "Failed to create memory"); bgl::blGetInfoLogARB(handle, len, num, mLog); #endif//GL_ARB_shader_objects } char *BL_Shader::GetVertPtr() { return vertProg?vertProg:0; } char *BL_Shader::GetFragPtr() { return fragProg?fragProg:0; } void BL_Shader::SetVertPtr( char *vert ) { vertProg = vert; } void BL_Shader::SetFragPtr( char *frag ) { fragProg = frag; } unsigned int BL_Shader::GetProg() { return mShader; } unsigned int BL_Shader::GetVertexShader() { return mVert; } unsigned int BL_Shader::GetFragmentShader() { return mFrag; } const uSampler* BL_Shader::getSampler(int i) { MT_assert(i<=MAXTEX); return &mSampler[i]; } void BL_Shader::InitializeSampler( int type, int unit, int pass, unsigned int texture) { MT_assert(unit<=MAXTEX); mSampler[unit].glTexture = texture; mSampler[unit].loc =-1; mSampler[unit].pass=0; mSampler[unit].type=type; mSampler[unit].unit=unit; } PyObject* BL_Shader::_getattr(const STR_String& attr) { _getattr_up(PyObjectPlus); } PyMethodDef BL_Shader::Methods[] = { // creation KX_PYMETHODTABLE( BL_Shader, setSource ), KX_PYMETHODTABLE( BL_Shader, delSource ), KX_PYMETHODTABLE( BL_Shader, getVertexProg ), KX_PYMETHODTABLE( BL_Shader, getFragmentProg ), KX_PYMETHODTABLE( BL_Shader, setNumberOfPasses ), KX_PYMETHODTABLE( BL_Shader, validate), /// access functions KX_PYMETHODTABLE( BL_Shader, isValid), KX_PYMETHODTABLE( BL_Shader, setUniform1f ), KX_PYMETHODTABLE( BL_Shader, setUniform2f ), KX_PYMETHODTABLE( BL_Shader, setUniform3f ), KX_PYMETHODTABLE( BL_Shader, setUniform4f ), KX_PYMETHODTABLE( BL_Shader, setUniform1i ), KX_PYMETHODTABLE( BL_Shader, setUniform2i ), KX_PYMETHODTABLE( BL_Shader, setUniform3i ), KX_PYMETHODTABLE( BL_Shader, setUniform4i ), KX_PYMETHODTABLE( BL_Shader, setUniformfv ), KX_PYMETHODTABLE( BL_Shader, setUniformiv ), KX_PYMETHODTABLE( BL_Shader, setSampler ), KX_PYMETHODTABLE( BL_Shader, setUniformMatrix4 ), KX_PYMETHODTABLE( BL_Shader, setUniformMatrix3 ), {NULL,NULL} //Sentinel }; PyTypeObject BL_Shader::Type = { PyObject_HEAD_INIT(&PyType_Type) 0, "BL_Shader", sizeof(BL_Shader), 0, PyDestructor, 0, __getattr, __setattr, 0, __repr, 0 }; PyParentObject BL_Shader::Parents[] = { &PyObjectPlus::Type, &BL_Shader::Type, NULL }; KX_PYMETHODDEF_DOC( BL_Shader, setSource," setSource(vertexProgram, fragmentProgram)" ) { #ifdef GL_ARB_shader_objects if(mShader !=0 && mOk ) { // already set... Py_Return; } char *v,*f; int apply=0; if( PyArg_ParseTuple(args, "ssi", &v, &f, &apply) ) { vertProg = v; fragProg = f; if( LinkProgram() ) { bgl::blUseProgramObjectARB( mShader ); mUse = apply!=0; Py_Return; } vertProg = 0; fragProg = 0; mUse = 0; Py_Return; } return NULL; #else Py_Return; #endif } KX_PYMETHODDEF_DOC( BL_Shader, delSource, "delSource( )" ) { #ifdef GL_ARB_shader_objects bgl::blDeleteObjectARB(mShader); bgl::blDeleteObjectARB(mFrag); bgl::blDeleteObjectARB(mVert); mShader = 0; mFrag = 0; mVert = 0; vertProg = 0; fragProg = 0; mOk = 0; mUse = 0; bgl::blUseProgramObjectARB(0); #endif Py_Return; } KX_PYMETHODDEF_DOC( BL_Shader, isValid, "isValid()" ) { return PyInt_FromLong( ( mShader !=0 && mOk ) ); } KX_PYMETHODDEF_DOC( BL_Shader, getVertexProg ,"getVertexProg( )" ) { return PyString_FromString(vertProg?vertProg:""); } KX_PYMETHODDEF_DOC( BL_Shader, getFragmentProg ,"getFragmentProg( )" ) { return PyString_FromString(fragProg?fragProg:""); } KX_PYMETHODDEF_DOC( BL_Shader, validate, "validate()") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } if(mShader==0) { PyErr_Format(PyExc_TypeError, "invalid shader object"); return NULL; } int stat = 0; bgl::blValidateProgramARB(mShader); bgl::blGetObjectParameterivARB(mShader, GL_OBJECT_VALIDATE_STATUS_ARB, &stat); return PyInt_FromLong(0); #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setSampler, "setSampler(name, index)" ) { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; int index=-1; if(PyArg_ParseTuple(args, "si", &uniform, &index)) { if(mShader==0) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader, uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { if(index <= MAXTEX) { mSampler[index].loc = loc; }else { spit("Invalid texture sample index: " << index); } Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setNumberOfPasses, "setNumberOfPasses( max-pass )" ) { int pass = 1; if(!PyArg_ParseTuple(args, "i", &pass)) return NULL; mPass = 1; Py_Return; } /// access functions KX_PYMETHODDEF_DOC( BL_Shader, setUniform1f, "setUniform1f(name, fx)" ) { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; float value=0; if(PyArg_ParseTuple(args, "sf", &uniform, &value )) { if( mShader==0 ) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader, uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB( mShader ); bgl::blUniform1fARB( loc, value ); Py_Return; } } return NULL; #else Py_Return; #endif } KX_PYMETHODDEF_DOC( BL_Shader, setUniform2f , "setUniform2f(name, fx, fy)") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; float array[2]={ 0,0 }; if(PyArg_ParseTuple(args, "sff", &uniform, &array[0],&array[1] )) { if( mShader==0 ) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB( mShader ); bgl::blUniform2fARB(loc, array[0],array[1] ); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniform3f, "setUniform3f(name, fx,fy,fz) ") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; float array[3]={0,0,0}; if(PyArg_ParseTuple(args, "sfff", &uniform, &array[0],&array[1],&array[2])) { if(mShader==0) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB(mShader); bgl::blUniform3fARB(loc, array[0],array[1],array[2]); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniform4f, "setUniform4f(name, fx,fy,fz, fw) ") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; float array[4]={0,0,0,0}; if(PyArg_ParseTuple(args, "sffff", &uniform, &array[0],&array[1],&array[2], &array[3])) { if(mShader==0) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB(mShader); bgl::blUniform4fARB(loc, array[0],array[1],array[2], array[3]); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniform1i, "setUniform1i(name, ix)" ) { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; int value=0; if(PyArg_ParseTuple(args, "si", &uniform, &value )) { if( mShader==0 ) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader, uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB( mShader ); bgl::blUniform1iARB( loc, value ); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniform2i , "setUniform2i(name, ix, iy)") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; int array[2]={ 0,0 }; if(PyArg_ParseTuple(args, "sii", &uniform, &array[0],&array[1] )) { if( mShader==0 ) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB( mShader ); bgl::blUniform2iARB(loc, array[0],array[1] ); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniform3i, "setUniform3i(name, ix,iy,iz) ") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; int array[3]={0,0,0}; if(PyArg_ParseTuple(args, "siii", &uniform, &array[0],&array[1],&array[2])) { if(mShader==0) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB(mShader); bgl::blUniform3iARB(loc, array[0],array[1],array[2]); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniform4i, "setUniform4i(name, ix,iy,iz, iw) ") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char *uniform=""; int array[4]={0,0,0, 0}; if(PyArg_ParseTuple(args, "siiii", &uniform, &array[0],&array[1],&array[2], &array[3] )) { if(mShader==0) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { bgl::blUseProgramObjectARB(mShader); bgl::blUniform4iARB(loc, array[0],array[1],array[2], array[3]); Py_Return; } } return NULL; #else Py_Return; #endif//GL_ARB_shader_objects } KX_PYMETHODDEF_DOC( BL_Shader, setUniformfv , "setUniformfv( float (list2 or list3 or list4) )") { #ifdef GL_ARB_shader_objects if(mError) { Py_INCREF(Py_None); return Py_None; } char*uniform = ""; PyObject *listPtr =0; float array_data[4] = {0.f,0.f,0.f,0.f}; if(PyArg_ParseTuple(args, "sO", &uniform, &listPtr)) { if(mShader==0) { PyErr_Format(PyExc_ValueError, "invalid shader object"); return NULL; } int loc= bgl::blGetUniformLocationARB(mShader , uniform); if( loc==-1 ) { spit("Invalid uniform value: " << uniform << "."); Py_Return; }else { if(PySequence_Check(listPtr)) { unsigned int list_size = PySequence_Size(listPtr); for(unsigned int i=0; (i