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