gfxShader.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "gfx/gfxShader.h"
  24. #include "shaderGen/conditionerFeature.h"
  25. #include "core/volume.h"
  26. #include "console/engineAPI.h"
  27. Vector<GFXShaderMacro> GFXShader::smGlobalMacros;
  28. bool GFXShader::smLogErrors = true;
  29. bool GFXShader::smLogWarnings = true;
  30. GFXShader::GFXShader()
  31. : mPixVersion( 0.0f ),
  32. mReloadKey( 0 )
  33. {
  34. }
  35. GFXShader::~GFXShader()
  36. {
  37. Torque::FS::RemoveChangeNotification( mVertexFile, this, &GFXShader::_onFileChanged );
  38. Torque::FS::RemoveChangeNotification( mPixelFile, this, &GFXShader::_onFileChanged );
  39. }
  40. bool GFXShader::init( const Torque::Path &vertFile,
  41. const Torque::Path &pixFile,
  42. F32 pixVersion,
  43. const Vector<GFXShaderMacro> &macros )
  44. {
  45. // Store the inputs for use in reloading.
  46. mVertexFile = vertFile;
  47. mPixelFile = pixFile;
  48. mPixVersion = pixVersion;
  49. mMacros = macros;
  50. // Before we compile the shader make sure the
  51. // conditioner features have been updated.
  52. ConditionerFeature::updateConditioners();
  53. // Now do the real initialization.
  54. if ( !_init() )
  55. return false;
  56. _updateDesc();
  57. // Add file change notifications for reloads.
  58. Torque::FS::AddChangeNotification( mVertexFile, this, &GFXShader::_onFileChanged );
  59. Torque::FS::AddChangeNotification( mPixelFile, this, &GFXShader::_onFileChanged );
  60. return true;
  61. }
  62. bool GFXShader::reload()
  63. {
  64. // Before we compile the shader make sure the
  65. // conditioner features have been updated.
  66. ConditionerFeature::updateConditioners();
  67. mReloadKey++;
  68. // Init does the work.
  69. bool success = _init();
  70. if ( success )
  71. _updateDesc();
  72. // Let anything that cares know that
  73. // this shader has reloaded
  74. mReloadSignal.trigger();
  75. return success;
  76. }
  77. void GFXShader::_updateDesc()
  78. {
  79. mDescription = String::ToString( "Files: %s, %s Pix Version: %0.2f\nMacros: ",
  80. mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), mPixVersion );
  81. GFXShaderMacro::stringize( smGlobalMacros, &mDescription );
  82. GFXShaderMacro::stringize( mMacros, &mDescription );
  83. }
  84. void GFXShader::addGlobalMacro( const String &name, const String &value )
  85. {
  86. // Check to see if we already have this macro.
  87. Vector<GFXShaderMacro>::iterator iter = smGlobalMacros.begin();
  88. for ( ; iter != smGlobalMacros.end(); iter++ )
  89. {
  90. if ( iter->name == name )
  91. {
  92. if ( iter->value != value )
  93. iter->value = value;
  94. return;
  95. }
  96. }
  97. // Add a new macro.
  98. smGlobalMacros.increment();
  99. smGlobalMacros.last().name = name;
  100. smGlobalMacros.last().value = value;
  101. }
  102. bool GFXShader::removeGlobalMacro( const String &name )
  103. {
  104. Vector<GFXShaderMacro>::iterator iter = smGlobalMacros.begin();
  105. for ( ; iter != smGlobalMacros.end(); iter++ )
  106. {
  107. if ( iter->name == name )
  108. {
  109. smGlobalMacros.erase( iter );
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. void GFXShader::_unlinkBuffer( GFXShaderConstBuffer *buf )
  116. {
  117. Vector<GFXShaderConstBuffer*>::iterator iter = mActiveBuffers.begin();
  118. for ( ; iter != mActiveBuffers.end(); iter++ )
  119. {
  120. if ( *iter == buf )
  121. {
  122. mActiveBuffers.erase_fast( iter );
  123. return;
  124. }
  125. }
  126. AssertFatal( false, "GFXShader::_unlinkBuffer - buffer was not found?" );
  127. }
  128. DefineEngineFunction( addGlobalShaderMacro, void,
  129. ( const char *name, const char *value ), ( NULL ),
  130. "Adds a global shader macro which will be merged with the script defined "
  131. "macros on every shader. The macro will replace the value of an existing "
  132. "macro of the same name. For the new macro to take effect all the shaders "
  133. "in the system need to be reloaded.\n"
  134. "@see resetLightManager, removeGlobalShaderMacro\n"
  135. "@ingroup Rendering\n" )
  136. {
  137. GFXShader::addGlobalMacro( name, value );
  138. }
  139. DefineEngineFunction( removeGlobalShaderMacro, void, ( const char *name ),,
  140. "Removes an existing global macro by name.\n"
  141. "@see addGlobalShaderMacro\n"
  142. "@ingroup Rendering\n" )
  143. {
  144. GFXShader::removeGlobalMacro( name );
  145. }