123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "shaderGen/GLSL/shaderCompGLSL.h"
- #include "shaderGen/shaderComp.h"
- #include "shaderGen/langElement.h"
- #include "gfx/gfxDevice.h"
- #include "gfx/gl/gfxGLVertexAttribLocation.h"
- Var * AppVertConnectorGLSL::getElement( RegisterType type,
- U32 numElements,
- U32 numRegisters )
- {
- switch( type )
- {
- case RT_POSITION:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "vPosition" );
- return newVar;
- }
- case RT_NORMAL:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "vNormal" );
- return newVar;
- }
- case RT_BINORMAL:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "vBinormal" );
- return newVar;
- }
- case RT_COLOR:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "vColor" );
- return newVar;
- }
- case RT_TANGENT:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "vTangent" );
- return newVar;
- }
- case RT_TANGENTW:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "vTangentW" );
- return newVar;
- }
- case RT_TEXCOORD:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
-
- char out[32];
- dSprintf( (char*)out, sizeof(out), "vTexCoord%d", mCurTexElem );
- newVar->setConnectName( out );
- newVar->constNum = mCurTexElem;
- newVar->arraySize = numElements;
- if ( numRegisters != -1 )
- mCurTexElem += numRegisters;
- else
- mCurTexElem += numElements;
- return newVar;
- }
- case RT_BLENDINDICES:
- {
- Var *newVar = new Var;
- newVar->constNum = mCurBlendIndicesElem;
- mElementList.push_back(newVar);
- char out[32];
- const U32 blendIndicesOffset = Torque::GL_VertexAttrib_BlendIndex0 - Torque::GL_VertexAttrib_TexCoord0;
- dSprintf((char*)out, sizeof(out), "vTexCoord%d", blendIndicesOffset + mCurBlendIndicesElem);
- mCurBlendIndicesElem += 1;
- newVar->setConnectName(out);
- return newVar;
- }
- case RT_BLENDWEIGHT:
- {
- Var *newVar = new Var;
- newVar->constNum = mCurBlendWeightsElem;
- mElementList.push_back(newVar);
- char out[32];
- const U32 blendWeightsOffset = Torque::GL_VertexAttrib_BlendWeight0 - Torque::GL_VertexAttrib_TexCoord0;
- dSprintf((char*)out, sizeof(out), "vTexCoord%d", blendWeightsOffset + mCurBlendWeightsElem);
- mCurBlendWeightsElem += 1;
- newVar->setConnectName(out);
- return newVar;
- }
- default:
- break;
- }
-
- return NULL;
- }
- void AppVertConnectorGLSL::sortVars()
- {
- // Not required in GLSL
- }
- void AppVertConnectorGLSL::setName( const char *newName )
- {
- dStrcpy( (char*)mName, newName, 32 );
- }
- void AppVertConnectorGLSL::reset()
- {
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- mElementList[i] = NULL;
- }
- mElementList.setSize( 0 );
- mCurTexElem = 0;
- }
- void AppVertConnectorGLSL::print( Stream &stream, bool isVertexShader )
- {
- if(!isVertexShader)
- return;
- U8 output[256];
- // print struct
- dSprintf( (char*)output, sizeof(output), "struct VertexData\r\n" );
- stream.write( dStrlen((char*)output), output );
- dSprintf( (char*)output, sizeof(output), "{\r\n" );
- stream.write( dStrlen((char*)output), output );
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- Var *var = mElementList[i];
-
- if( var->arraySize == 1)
- {
- dSprintf( (char*)output, sizeof(output), " %s %s;\r\n", var->type, (char*)var->name );
- stream.write( dStrlen((char*)output), output );
- }
- else
- {
- dSprintf( (char*)output, sizeof(output), " %s %s[%d];\r\n", var->type, (char*)var->name, var->arraySize );
- stream.write( dStrlen((char*)output), output );
- }
- }
- dSprintf( (char*)output, sizeof(output), "} IN;\r\n\r\n" );
- stream.write( dStrlen((char*)output), output );
- // print in elements
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- Var *var = mElementList[i];
-
- for(int j = 0; j < var->arraySize; ++j)
- {
- const char *name = j == 0 ? var->connectName : avar("vTexCoord%d", var->constNum + j) ;
- dSprintf( (char*)output, sizeof(output), "in %s %s;\r\n", var->type, name );
- stream.write( dStrlen((char*)output), output );
- }
- dSprintf( (char*)output, sizeof(output), "#define IN_%s IN.%s\r\n", var->name, var->name ); // TODO REMOVE
- stream.write( dStrlen((char*)output), output );
- }
- const char* newLine ="\r\n";
- stream.write( dStrlen((char*)newLine), newLine );
- }
- Var * VertPixelConnectorGLSL::getElement( RegisterType type,
- U32 numElements,
- U32 numRegisters )
- {
- switch( type )
- {
- case RT_POSITION:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "POSITION" );
- return newVar;
- }
- case RT_NORMAL:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "NORMAL" );
- return newVar;
- }
- case RT_COLOR:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "COLOR" );
- return newVar;
- }
- /*case RT_BINORMAL:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "BINORMAL" );
- return newVar;
- }
- case RT_TANGENT:
- {
- Var *newVar = new Var;
- mElementList.push_back( newVar );
- newVar->setConnectName( "TANGENT" );
- return newVar;
- } */
- case RT_TEXCOORD:
- case RT_BINORMAL:
- case RT_TANGENT:
- {
- Var *newVar = new Var;
- newVar->arraySize = numElements;
- char out[32];
- dSprintf( (char*)out, sizeof(out), "TEXCOORD%d", mCurTexElem );
- newVar->setConnectName( out );
- if ( numRegisters != -1 )
- mCurTexElem += numRegisters;
- else
- mCurTexElem += numElements;
-
- mElementList.push_back( newVar );
- return newVar;
- }
- default:
- break;
- }
- return NULL;
- }
- void VertPixelConnectorGLSL::sortVars()
- {
- // Not needed in GLSL
- }
- void VertPixelConnectorGLSL::setName( const char *newName )
- {
- dStrcpy( (char*)mName, newName, 32 );
- }
- void VertPixelConnectorGLSL::reset()
- {
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- mElementList[i] = NULL;
- }
- mElementList.setSize( 0 );
- mCurTexElem = 0;
- }
- void VertPixelConnectorGLSL::print( Stream &stream, bool isVerterShader )
- {
- // print out elements
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- U8 output[256];
- Var *var = mElementList[i];
- if(!String::compare((const char*)var->name, "gl_Position"))
- continue;
- if(var->arraySize <= 1)
- dSprintf((char*)output, sizeof(output), "%s %s _%s_;\r\n", (isVerterShader ? "out" : "in"), var->type, var->connectName);
- else
- dSprintf((char*)output, sizeof(output), "%s %s _%s_[%d];\r\n", (isVerterShader ? "out" : "in"),var->type, var->connectName, var->arraySize);
- stream.write( dStrlen((char*)output), output );
- }
- printStructDefines(stream, !isVerterShader);
- }
- void VertPixelConnectorGLSL::printOnMain( Stream &stream, bool isVerterShader )
- {
- if(isVerterShader)
- return;
- const char *newLine = "\r\n";
- const char *header = " //-------------------------\r\n";
- stream.write( dStrlen((char*)newLine), newLine );
- stream.write( dStrlen((char*)header), header );
- // print out elements
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- U8 output[256];
- Var *var = mElementList[i];
- if(!String::compare((const char*)var->name, "gl_Position"))
- continue;
-
- dSprintf((char*)output, sizeof(output), " %s IN_%s = _%s_;\r\n", var->type, var->name, var->connectName);
- stream.write( dStrlen((char*)output), output );
- }
- stream.write( dStrlen((char*)header), header );
- stream.write( dStrlen((char*)newLine), newLine );
- }
- void AppVertConnectorGLSL::printOnMain( Stream &stream, bool isVerterShader )
- {
- if(!isVerterShader)
- return;
- const char *newLine = "\r\n";
- const char *header = " //-------------------------\r\n";
- stream.write( dStrlen((char*)newLine), newLine );
- stream.write( dStrlen((char*)header), header );
- // print out elements
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- Var *var = mElementList[i];
- U8 output[256];
- if(var->arraySize <= 1)
- {
- dSprintf((char*)output, sizeof(output), " IN.%s = %s;\r\n", var->name, var->connectName);
- stream.write( dStrlen((char*)output), output );
- }
- else
- {
- for(int j = 0; j < var->arraySize; ++j)
- {
- const char *name = j == 0 ? var->connectName : avar("vTexCoord%d", var->constNum + j) ;
- dSprintf((char*)output, sizeof(output), " IN.%s[%d] = %s;\r\n", var->name, j, name );
- stream.write( dStrlen((char*)output), output );
- }
- }
- }
- stream.write( dStrlen((char*)header), header );
- stream.write( dStrlen((char*)newLine), newLine );
- }
- Vector<String> initDeprecadedDefines()
- {
- Vector<String> vec;
- vec.push_back( "isBack");
- return vec;
- }
- void VertPixelConnectorGLSL::printStructDefines( Stream &stream, bool in )
- {
- const char* connectionDir = in ? "IN" : "OUT";
- static Vector<String> deprecatedDefines = initDeprecadedDefines();
- const char *newLine = "\r\n";
- const char *header = "// Struct defines\r\n";
- stream.write( dStrlen((char*)newLine), newLine );
- stream.write( dStrlen((char*)header), header );
- // print out elements
- for( U32 i=0; i<mElementList.size(); i++ )
- {
- U8 output[256];
- Var *var = mElementList[i];
- if(!String::compare((const char*)var->name, "gl_Position"))
- continue;
-
- if(!in)
- {
- dSprintf((char*)output, sizeof(output), "#define %s_%s _%s_\r\n", connectionDir, var->name, var->connectName);
- stream.write( dStrlen((char*)output), output );
- continue;
- }
- }
- stream.write( dStrlen((char*)newLine), newLine );
- }
- void VertexParamsDefGLSL::print( Stream &stream, bool isVerterShader )
- {
- // find all the uniform variables and print them out
- for( U32 i=0; i<LangElement::elementList.size(); i++)
- {
- Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
- if( var )
- {
- if( var->uniform )
- {
- U8 output[256];
- if(var->arraySize <= 1)
- dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s;\r\n", var->type, var->name);
- else
- dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s[%d];\r\n", var->type, var->name, var->arraySize);
- stream.write( dStrlen((char*)output), output );
- }
- }
- }
- const char *closer = "\r\n\r\nvoid main()\r\n{\r\n";
- stream.write( dStrlen(closer), closer );
- }
- void PixelParamsDefGLSL::print( Stream &stream, bool isVerterShader )
- {
- // find all the uniform variables and print them out
- for( U32 i=0; i<LangElement::elementList.size(); i++)
- {
- Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
- if( var )
- {
- if( var->uniform )
- {
- U8 output[256];
- if(var->arraySize <= 1)
- dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s;\r\n", var->type, var->name);
- else
- dSprintf((char*)output, sizeof(output), "uniform %-8s %-15s[%d];\r\n", var->type, var->name, var->arraySize);
- stream.write( dStrlen((char*)output), output );
- }
- }
- }
- const char *closer = "\r\nvoid main()\r\n{\r\n";
- stream.write( dStrlen(closer), closer );
- for( U32 i=0; i<LangElement::elementList.size(); i++)
- {
- Var *var = dynamic_cast<Var*>(LangElement::elementList[i]);
- if( var )
- {
- if( var->uniform && !var->sampler)
- {
- U8 output[256];
- if(var->arraySize <= 1)
- dSprintf((char*)output, sizeof(output), " %s %s = %s;\r\n", var->type, var->name, var->name);
- else
- dSprintf((char*)output, sizeof(output), " %s %s[%d] = %s;\r\n", var->type, var->name, var->arraySize, var->name);
- stream.write( dStrlen((char*)output), output );
- }
- }
- }
- }
|