gfxShader.cpp

Engine/source/gfx/gfxShader.cpp

More...

Public Functions

DefineEngineFunction(addGlobalShaderMacro , void , (const char *name, const char *value) , (nullAsType< const char * >()) , "Adds <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> global shader macro which will be merged with the script defined " "macros on every shader. The macro will replace the <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> of an existing " "macro of the same name. For the <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> macro <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> take effect all the shaders " "in the system need <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">reloaded.\n</a>" "@see resetLightManager, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">removeGlobalShaderMacro\n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Rendering\n</a>" )
DefineEngineFunction(removeGlobalShaderMacro , void , (const char *name) , "Removes an existing global macro by <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">name.\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">addGlobalShaderMacro\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Rendering\n</a>" )

Detailed Description

Public Functions

DefineEngineFunction(addGlobalShaderMacro , void , (const char *name, const char *value) , (nullAsType< const char * >()) , "Adds <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> global shader macro which will be merged with the script defined " "macros on every shader. The macro will replace the <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> of an existing " "macro of the same name. For the <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> macro <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> take effect all the shaders " "in the system need <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">reloaded.\n</a>" "@see resetLightManager, <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">removeGlobalShaderMacro\n</a>" " @ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Rendering\n</a>" )

DefineEngineFunction(removeGlobalShaderMacro , void , (const char *name) , "Removes an existing global macro by <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">name.\n</a>" "@see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">addGlobalShaderMacro\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Rendering\n</a>" )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "gfx/gfxShader.h"
 26
 27#include "shaderGen/conditionerFeature.h"
 28#include "core/volume.h"
 29#include "console/engineAPI.h"
 30
 31
 32Vector<GFXShaderMacro> GFXShader::smGlobalMacros;
 33bool GFXShader::smLogErrors = true;
 34bool GFXShader::smLogWarnings = true;
 35
 36
 37GFXShader::GFXShader()
 38   :  mPixVersion( 0.0f ),
 39      mReloadKey( 0 ),
 40      mInstancingFormat( NULL )
 41{
 42}
 43
 44GFXShader::~GFXShader()
 45{
 46   Torque::FS::RemoveChangeNotification( mVertexFile, this, &GFXShader::_onFileChanged );
 47   Torque::FS::RemoveChangeNotification( mPixelFile, this, &GFXShader::_onFileChanged );
 48
 49   SAFE_DELETE(mInstancingFormat);
 50}
 51
 52#ifndef TORQUE_OPENGL
 53bool GFXShader::init(   const Torque::Path &vertFile, 
 54                        const Torque::Path &pixFile, 
 55                        F32 pixVersion, 
 56                        const Vector<GFXShaderMacro> &macros )
 57{
 58   Vector<String> samplerNames;
 59   return init( vertFile, pixFile, pixVersion, macros, samplerNames );
 60}
 61#endif
 62
 63bool GFXShader::init(   const Torque::Path &vertFile, 
 64                        const Torque::Path &pixFile, 
 65                        F32 pixVersion, 
 66                        const Vector<GFXShaderMacro> &macros,
 67                        const Vector<String> &samplerNames,
 68                        GFXVertexFormat *instanceFormat)
 69{
 70   // Take care of instancing
 71   if (instanceFormat)
 72   {
 73      mInstancingFormat = new GFXVertexFormat;
 74      mInstancingFormat->copy(*instanceFormat);
 75   }
 76
 77   // Store the inputs for use in reloading.
 78   mVertexFile = vertFile;
 79   mPixelFile = pixFile;
 80   mPixVersion = pixVersion;
 81   mMacros = macros;
 82   mSamplerNamesOrdered = samplerNames;
 83
 84   // Before we compile the shader make sure the
 85   // conditioner features have been updated.
 86   ConditionerFeature::updateConditioners();
 87
 88   // Now do the real initialization.
 89   if ( !_init() )
 90      return false;
 91
 92   _updateDesc();
 93
 94   // Add file change notifications for reloads.
 95   Torque::FS::AddChangeNotification( mVertexFile, this, &GFXShader::_onFileChanged );
 96   Torque::FS::AddChangeNotification( mPixelFile, this, &GFXShader::_onFileChanged );
 97
 98   return true;
 99}
100
101bool GFXShader::reload()
102{
103   // Before we compile the shader make sure the
104   // conditioner features have been updated.
105   ConditionerFeature::updateConditioners();
106
107   mReloadKey++;
108
109   // Init does the work.
110   bool success = _init();
111   if ( success )
112      _updateDesc();
113
114   // Let anything that cares know that
115   // this shader has reloaded
116   mReloadSignal.trigger();
117
118   return success;
119}
120
121void GFXShader::_updateDesc()
122{
123   mDescription = String::ToString( "Files: %s, %s Pix Version: %0.2f\nMacros: ", 
124      mVertexFile.getFullPath().c_str(), mPixelFile.getFullPath().c_str(), mPixVersion );
125
126   GFXShaderMacro::stringize( smGlobalMacros, &mDescription );
127   GFXShaderMacro::stringize( mMacros, &mDescription );   
128}
129
130void GFXShader::addGlobalMacro( const String &name, const String &value )
131{
132   // Check to see if we already have this macro.
133   Vector<GFXShaderMacro>::iterator iter = smGlobalMacros.begin();
134   for ( ; iter != smGlobalMacros.end(); iter++ )
135   {
136      if ( iter->name == name )
137      {
138         if ( iter->value != value )
139            iter->value = value;
140         return;
141      }
142   }
143
144   // Add a new macro.
145   smGlobalMacros.increment();
146   smGlobalMacros.last().name = name;
147   smGlobalMacros.last().value = value;
148}
149
150bool GFXShader::removeGlobalMacro( const String &name )
151{
152   Vector<GFXShaderMacro>::iterator iter = smGlobalMacros.begin();
153   for ( ; iter != smGlobalMacros.end(); iter++ )
154   {
155      if ( iter->name == name )
156      {
157         smGlobalMacros.erase( iter );
158         return true;
159      }
160   }
161
162   return false;
163}
164
165void GFXShader::_unlinkBuffer( GFXShaderConstBuffer *buf )
166{   
167   Vector<GFXShaderConstBuffer*>::iterator iter = mActiveBuffers.begin();
168   for ( ; iter != mActiveBuffers.end(); iter++ )
169   {
170      if ( *iter == buf )
171      {
172         mActiveBuffers.erase_fast( iter );
173         return;
174      }
175   }
176
177   AssertFatal( false, "GFXShader::_unlinkBuffer - buffer was not found?" );
178}
179
180
181DefineEngineFunction( addGlobalShaderMacro, void, 
182   ( const char *name, const char *value ), ( nullAsType<const char*>() ),
183   "Adds a global shader macro which will be merged with the script defined "
184   "macros on every shader.  The macro will replace the value of an existing "
185   "macro of the same name.  For the new macro to take effect all the shaders "
186   "in the system need to be reloaded.\n"
187   "@see resetLightManager, removeGlobalShaderMacro\n"
188   "@ingroup Rendering\n" )
189{
190   GFXShader::addGlobalMacro( name, value );
191}
192
193DefineEngineFunction( removeGlobalShaderMacro, void, ( const char *name ),, 
194   "Removes an existing global macro by name.\n"
195   "@see addGlobalShaderMacro\n"
196   "@ingroup Rendering\n" )
197{
198   GFXShader::removeGlobalMacro( name );
199}
200