conditionerFeature.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "shaderGen/conditionerFeature.h"
  24. #include "shaderGen/shaderOp.h"
  25. #include "shaderGen/featureMgr.h"
  26. #include "gfx/gfxDevice.h"
  27. #include "gfx/gfxStringEnumTranslate.h"
  28. #include "core/stream/fileStream.h"
  29. #include "materials/shaderData.h"
  30. #include "core/util/safeDelete.h"
  31. const String ConditionerFeature::ConditionerIncludeFileName = "autogenConditioners.h";
  32. bool ConditionerFeature::smDirtyConditioners = true;
  33. Vector<ConditionerFeature*> ConditionerFeature::smConditioners;
  34. ConditionerFeature::ConditionerFeature( const GFXFormat bufferFormat )
  35. : mBufferFormat(bufferFormat)
  36. {
  37. dMemset( mMethodDependency, 0, sizeof( mMethodDependency ) );
  38. smConditioners.push_back( this );
  39. smDirtyConditioners = true;
  40. }
  41. ConditionerFeature::~ConditionerFeature()
  42. {
  43. for( U32 i = 0; i < NumMethodTypes; i++ )
  44. SAFE_DELETE( mMethodDependency[i] );
  45. smConditioners.remove( this );
  46. smDirtyConditioners = true;
  47. }
  48. LangElement *ConditionerFeature::assignOutput( Var *unconditionedOutput, ShaderFeature::OutputTarget outputTarget /* = ShaderFeature::DefaultTarget*/ )
  49. {
  50. LangElement *assign;
  51. MultiLine *meta = new MultiLine;
  52. meta->addStatement( new GenOp( avar( "\r\n\r\n // output buffer format: %s\r\n", GFXStringTextureFormat[getBufferFormat()] ) ) );
  53. // condition the output
  54. Var *conditionedOutput = _conditionOutput( unconditionedOutput, meta );
  55. // search for color var
  56. Var *color = (Var*) LangElement::find( getOutputTargetVarName(outputTarget) );
  57. if ( !color )
  58. {
  59. // create color var
  60. color = new Var;
  61. if(GFX->getAdapterType() == OpenGL)
  62. {
  63. color->setName( getOutputTargetVarName(outputTarget) );
  64. color->setType( "vec4" );
  65. color->setStructName( "OUT" );
  66. assign = new GenOp( "@ = vec4(@)", color, conditionedOutput );
  67. }
  68. else
  69. {
  70. color->setType( "fragout" );
  71. color->setName( getOutputTargetVarName(outputTarget) );
  72. color->setStructName( "OUT" );
  73. assign = new GenOp( "@ = @", color, conditionedOutput );
  74. }
  75. }
  76. else
  77. {
  78. if (GFX->getAdapterType() == OpenGL)
  79. assign = new GenOp( "@ = vec4(@)", color, conditionedOutput);
  80. else
  81. assign = new GenOp( "@ = @", color, conditionedOutput );
  82. }
  83. meta->addStatement( new GenOp( " @;\r\n", assign ) );
  84. return meta;
  85. }
  86. Var *ConditionerFeature::_conditionOutput( Var *unconditionedOutput, MultiLine *meta )
  87. {
  88. meta->addStatement( new GenOp( " // generic conditioner: no conditioning performed\r\n" ) );
  89. return unconditionedOutput;
  90. }
  91. Var *ConditionerFeature::_unconditionInput( Var *conditionedInput, MultiLine *meta )
  92. {
  93. meta->addStatement( new GenOp( " // generic conditioner: no conditioning performed\r\n" ) );
  94. return conditionedInput;
  95. }
  96. const String &ConditionerFeature::getShaderMethodName( MethodType methodType )
  97. {
  98. if ( mConditionMethodName.isEmpty() )
  99. {
  100. const U32 hash = getName().getHashCaseInsensitive();
  101. mUnconditionMethodName = avar("autogen%s_%08x", "Uncondition", hash );
  102. mConditionMethodName = avar("autogen%s_%08x", "Condition", hash );
  103. }
  104. return methodType == UnconditionMethod ? mUnconditionMethodName : mConditionMethodName;
  105. }
  106. ConditionerMethodDependency* ConditionerFeature::getConditionerMethodDependency( MethodType methodType )
  107. {
  108. if ( mMethodDependency[methodType] == NULL )
  109. mMethodDependency[methodType] = new ConditionerMethodDependency( this, methodType );
  110. return mMethodDependency[methodType];
  111. }
  112. void ConditionerFeature::_print( Stream *stream )
  113. {
  114. _printMethod( ConditionMethod, getShaderMethodName( ConditionMethod ), *stream );
  115. LangElement::deleteElements();
  116. _printMethod( UnconditionMethod, getShaderMethodName( UnconditionMethod ), *stream );
  117. LangElement::deleteElements();
  118. }
  119. void ConditionerFeature::_updateConditioners()
  120. {
  121. smDirtyConditioners = false;
  122. String includePath = "shadergen:/" + ConditionerIncludeFileName;
  123. FileStream stream;
  124. if ( !stream.open( includePath, Torque::FS::File::Write ) )
  125. return;
  126. for ( U32 i=0; i < smConditioners.size(); i++ )
  127. smConditioners[i]->_print( &stream );
  128. }
  129. Var *ConditionerFeature::printMethodHeader( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
  130. {
  131. Var *methodVar = new Var;
  132. methodVar->setName(methodName);
  133. DecOp *methodDecl = new DecOp(methodVar);
  134. const bool isCondition = (methodType == ConditionerFeature::ConditionMethod);
  135. Var *paramVar = new Var;
  136. paramVar->setName(avar("%sconditioned%sput", isCondition ? "un" : "", isCondition ? "Out" : "In"));
  137. DecOp *paramDecl = new DecOp(paramVar);
  138. if(GFX->getAdapterType() == OpenGL)
  139. {
  140. methodVar->setType("vec4");
  141. paramVar->setType("vec4");
  142. }
  143. else
  144. {
  145. methodVar->setType("inline float4");
  146. paramVar->setType("in float4");
  147. }
  148. // Method header and opening bracket
  149. meta->addStatement( new GenOp( "@(@)\r\n", methodDecl, paramDecl ) );
  150. meta->addStatement( new GenOp( "{\r\n" ) );
  151. return paramVar;
  152. }
  153. void ConditionerFeature::printMethodFooter( MethodType methodType, Var *retVar, Stream &stream, MultiLine *meta )
  154. {
  155. // Return and closing bracket
  156. meta->addStatement( new GenOp( "\r\n return @;\r\n", retVar ) );
  157. meta->addStatement( new GenOp( "}\r\n" ) );
  158. }
  159. void ConditionerFeature::_printMethod( MethodType methodType, const String &methodName, Stream &stream )
  160. {
  161. MultiLine *meta = new MultiLine;
  162. printHeaderComment( methodType, methodName, stream, meta );
  163. Var *paramVar = printMethodHeader( methodType, methodName, stream, meta );
  164. Var *unconditionedInput = NULL;
  165. if( methodType == UnconditionMethod )
  166. unconditionedInput = _unconditionInput( paramVar, meta );
  167. else
  168. unconditionedInput = _conditionOutput( paramVar, meta );
  169. printMethodFooter( methodType, unconditionedInput, stream, meta );
  170. printFooterComment( methodType, methodName, stream, meta );
  171. meta->print(stream);
  172. }
  173. void ConditionerFeature::printHeaderComment( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
  174. {
  175. meta->addStatement( new GenOp( "//------------------------------------------------------------------------------\r\n" ) );
  176. meta->addStatement( new GenOp( avar( "// Autogenerated '%s' %s Method\r\n", getName().c_str(),
  177. methodType == ConditionMethod ? "Condition" : "Uncondition" ) ) );
  178. meta->addStatement( new GenOp( "//------------------------------------------------------------------------------\r\n" ) );
  179. }
  180. void ConditionerFeature::printFooterComment( MethodType methodType, const String &methodName, Stream &stream, MultiLine *meta )
  181. {
  182. meta->addStatement( new GenOp( "\r\n\r\n" ) );
  183. }
  184. void ConditionerMethodDependency::print( Stream &s ) const
  185. {
  186. mConditioner->_printMethod(mMethodType, mConditioner->getShaderMethodName(mMethodType), s);
  187. }
  188. void ConditionerMethodDependency::createMethodMacro( const String &methodName, Vector<GFXShaderMacro> &macros )
  189. {
  190. GFXShaderMacro conditionerMethodMacro;
  191. conditionerMethodMacro.name = methodName;
  192. conditionerMethodMacro.value = mConditioner->getShaderMethodName(mMethodType);
  193. macros.push_back(conditionerMethodMacro);
  194. }