shaderGen.h
Engine/source/shaderGen/shaderGen.h
Classes:
class
class
Abstract factory for created (and initializating, if necessary) shader components.
class
Base class used by shaderGen to be API agnostic.
Detailed Description
Public Defines
SHADERGEN() <>::instance()
Returns the ShaderGen singleton.
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#ifndef _SHADERGEN_H_ 24#define _SHADERGEN_H_ 25 26#ifndef _LANG_ELEMENT_H_ 27#include "shaderGen/langElement.h" 28#endif 29#ifndef _SHADERFEATURE_H_ 30#include "shaderGen/shaderFeature.h" 31#endif 32#ifndef _SHADERCOMP_H_ 33#include "shaderGen/shaderComp.h" 34#endif 35#ifndef _GFXDEVICE_H_ 36#include "gfx/gfxDevice.h" 37#endif 38#ifndef _AUTOPTR_H_ 39#include "core/util/autoPtr.h" 40#endif 41#ifndef _TSINGLETON_H_ 42#include "core/util/tSingleton.h" 43#endif 44#ifndef _VOLUME_H_ 45#include "core/volume.h" 46#endif 47#ifndef _MATERIALFEATUREDATA_H_ 48#include "materials/materialFeatureData.h" 49#endif 50 51 52/// Base class used by shaderGen to be API agnostic. Subclasses implement the various methods 53/// in an API specific way. 54class ShaderGenPrinter 55{ 56public: 57 virtual ~ShaderGenPrinter() {} 58 59 /// Prints a simple header, including the engine name, language type, and 60 /// the fact that the shader was procedurally generated 61 virtual void printShaderHeader(Stream& stream) = 0; 62 63 /// Prints a comment block specifying the beginning of the main() function (or equivalent) 64 virtual void printMainComment(Stream& stream) = 0; 65 66 /// Prints the final line of the vertex shader, e.g. return OUT; }, }, END 67 virtual void printVertexShaderCloser(Stream& stream) = 0; 68 69 /// Prints the output struct for the pixel shader. Probably only used in HLSL/Cg. 70 virtual void printPixelShaderOutputStruct(Stream& stream, const MaterialFeatureData &featureData) = 0; 71 72 /// Prints the final line of the pixel shader. 73 virtual void printPixelShaderCloser(Stream& stream) = 0; 74 75 // Prints a line into the shader adding the proper terminator. 76 virtual void printLine(Stream& stream, const String& line) = 0; 77}; 78 79/// Abstract factory for created (and initializating, if necessary) shader components. 80class ShaderGenComponentFactory 81{ 82public: 83 virtual ~ShaderGenComponentFactory() {} 84 85 /// Creates and initializes a vertex input connector with the specified flags 86 virtual ShaderComponent* createVertexInputConnector( const GFXVertexFormat &vertexFormat ) = 0; 87 88 /// Creates and names a vertex/pixel connector 89 virtual ShaderComponent* createVertexPixelConnector() = 0; 90 91 /// Creates an instance of VertexParamsDef 92 virtual ShaderComponent* createVertexParamsDef() = 0; 93 94 /// Creates an instance of PixelParamsDef 95 virtual ShaderComponent* createPixelParamsDef() = 0; 96}; 97 98//************************************************************************** 99/*! 100 The ShaderGen class takes shader feature data (usually created by 101 MatInstance) and creates a vertex/pixel shader pair in text files 102 to be later compiled by a shader manager. 103 104 It accomplishes this task by creating a group of shader "components" and 105 "features" that output bits of high level shader code. Shader components 106 translate to structures in HLSL that indicate incoming vertex data, 107 data that is output from the vertex shader to the pixel shader, and data 108 such as constants and textures that are passed directly to the shader 109 from the app. 110 111 Shader features are separable shader functions that can be turned on or 112 off. Examples would be bumpmapping and specular highlights. See 113 MaterialFeatureData for the current list of features supported. 114 115 ShaderGen processes all of the features that are present for a desired 116 shader, and then prints them out to the respective vertex or pixel 117 shader file. 118 119 For more information on shader features and components see the 120 ShaderFeature and ShaderComponent classes. 121*/ 122//************************************************************************** 123 124 125//************************************************************************** 126// Shader generator 127//************************************************************************** 128class ShaderGen 129{ 130public: 131 virtual ~ShaderGen(); 132 133 /// Parameter 1 is the ShaderGen instance to initialize. 134 typedef Delegate<void (ShaderGen*)> ShaderGenInitDelegate; 135 136 /// Register an initialization delegate for adapterType. This should setPrinter/ComponentFactory/etc, and register 137 /// shader features. 138 void registerInitDelegate(GFXAdapterType adapterType, ShaderGenInitDelegate& initDelegate); 139 140 /// Signal used to notify systems to register features. 141 typedef Signal<void(GFXAdapterType type)> FeatureInitSignal; 142 143 /// Returns the signal used to notify systems to register features. 144 FeatureInitSignal& getFeatureInitSignal() { return mFeatureInitSignal; } 145 146 /// vertFile and pixFile are filled in by this function. They point to 147 /// the vertex and pixel shader files. pixVersion is also filled in by 148 /// this function. 149 /// @param assignNum used to assign a specific number as the filename 150 void generateShader( const MaterialFeatureData &featureData, 151 char *vertFile, 152 char *pixFile, 153 F32 *pixVersion, 154 const GFXVertexFormat *vertexFormat, 155 const char* cacheName, 156 Vector<GFXShaderMacro> ¯os ); 157 158 // Returns a shader that implements the features listed by dat. 159 GFXShader* getShader( const MaterialFeatureData &dat, const GFXVertexFormat *vertexFormat, const Vector<GFXShaderMacro> *macros, const Vector<String> &samplers ); 160 161 // This will delete all of the procedural shaders that we have. Used to regenerate shaders when 162 // the ShaderFeatures have changed (due to lighting system change, or new plugin) 163 virtual void flushProceduralShaders(); 164 165 void setPrinter(ShaderGenPrinter* printer) { mPrinter = printer; } 166 void setComponentFactory(ShaderGenComponentFactory* factory) { mComponentFactory = factory; } 167 void setFileEnding(String ending) { mFileEnding = ending; } 168 169protected: 170 171 friend class ManagedSingleton<ShaderGen>; 172 173 // Shader generation 174 MaterialFeatureData mFeatureData; 175 const GFXVertexFormat *mVertexFormat; 176 177 Vector< ShaderComponent*> mComponents; 178 179 AutoPtr<ShaderGenPrinter> mPrinter; 180 AutoPtr<ShaderGenComponentFactory> mComponentFactory; 181 182 String mFileEnding; 183 184 /// The currently processing output. 185 MultiLine *mOutput; 186 GFXVertexFormat mInstancingFormat; 187 188 /// Init 189 bool mInit; 190 ShaderGenInitDelegate mInitDelegates[GFXAdapterType_Count]; 191 FeatureInitSignal mFeatureInitSignal; 192 bool mRegisteredWithGFX; 193 Torque::FS::FileSystemRef mMemFS; 194 195 /// Map of cache string -> shaders 196 typedef Map<String, GFXShaderRef> ShaderMap; 197 ShaderMap mProcShaders; 198 199 ShaderGen(); 200 201 bool _handleGFXEvent(GFXDevice::GFXDeviceEventType event); 202 203 /// Causes the init delegate to be called. 204 void initShaderGen(); 205 206 void _init(); 207 void _uninit(); 208 209 /// Creates all the various shader components that will be filled in when 210 /// the shader features are processed. 211 void _createComponents(); 212 213 void _printFeatureList(Stream &stream); 214 215 /// print out the processed features to the file stream 216 void _printFeatures( Stream &stream ); 217 218 void _printDependencies( Stream &stream ); 219 220 void _processPixFeatures( Vector<GFXShaderMacro> ¯os, bool macrosOnly = false ); 221 void _printPixShader( Stream &stream ); 222 223 void _processVertFeatures( Vector<GFXShaderMacro> ¯os, bool macrosOnly = false ); 224 void _printVertShader( Stream &stream ); 225 226 // For ManagedSingleton. 227 static const char* getSingletonName() { return "ShaderGen"; } 228}; 229 230 231/// Returns the ShaderGen singleton. 232#define SHADERGEN ManagedSingleton<ShaderGen>::instance() 233 234#endif // _SHADERGEN_H_ 235
