1 // 2 // Copyright (c) 2008-2017 the Urho3D project. 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 deal 6 // in the Software without restriction, including without limitation the rights 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 // 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 FROM, 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 20 // THE SOFTWARE. 21 // 22 23 #pragma once 24 25 #include "../Graphics/GraphicsDefs.h" 26 #include "../Resource/Resource.h" 27 28 namespace Urho3D 29 { 30 31 class ShaderVariation; 32 33 /// Lighting mode of a pass. 34 enum PassLightingMode 35 { 36 LIGHTING_UNLIT = 0, 37 LIGHTING_PERVERTEX, 38 LIGHTING_PERPIXEL 39 }; 40 41 /// %Material rendering pass, which defines shaders and render state. 42 class URHO3D_API Pass : public RefCounted 43 { 44 public: 45 /// Construct. 46 Pass(const String& passName); 47 /// Destruct. 48 ~Pass(); 49 50 /// Set blend mode. 51 void SetBlendMode(BlendMode mode); 52 /// Set culling mode override. By default culling mode is read from the material instead. Set the illegal culling mode MAX_CULLMODES to disable override again. 53 void SetCullMode(CullMode mode); 54 /// Set depth compare mode. 55 void SetDepthTestMode(CompareMode mode); 56 /// Set pass lighting mode, affects what shader variations will be attempted to be loaded. 57 void SetLightingMode(PassLightingMode mode); 58 /// Set depth write on/off. 59 void SetDepthWrite(bool enable); 60 /// Set alpha-to-coverage on/off. 61 void SetAlphaToCoverage(bool enable); 62 /// Set whether requires desktop level hardware. 63 void SetIsDesktop(bool enable); 64 /// Set vertex shader name. 65 void SetVertexShader(const String& name); 66 /// Set pixel shader name. 67 void SetPixelShader(const String& name); 68 /// Set vertex shader defines. Separate multiple defines with spaces. 69 void SetVertexShaderDefines(const String& defines); 70 /// Set pixel shader defines. Separate multiple defines with spaces. 71 void SetPixelShaderDefines(const String& defines); 72 /// Set vertex shader define excludes. Use to mark defines that the shader code will not recognize, to prevent compiling redundant shader variations. 73 void SetVertexShaderDefineExcludes(const String& excludes); 74 /// Set pixel shader define excludes. Use to mark defines that the shader code will not recognize, to prevent compiling redundant shader variations. 75 void SetPixelShaderDefineExcludes(const String& excludes); 76 /// Reset shader pointers. 77 void ReleaseShaders(); 78 /// Mark shaders loaded this frame. 79 void MarkShadersLoaded(unsigned frameNumber); 80 81 /// Return pass name. GetName()82 const String& GetName() const { return name_; } 83 84 /// Return pass index. This is used for optimal render-time pass queries that avoid map lookups. GetIndex()85 unsigned GetIndex() const { return index_; } 86 87 /// Return blend mode. GetBlendMode()88 BlendMode GetBlendMode() const { return blendMode_; } 89 90 /// Return culling mode override. If pass is not overriding culling mode (default), the illegal mode MAX_CULLMODES is returned. GetCullMode()91 CullMode GetCullMode() const { return cullMode_; } 92 93 /// Return depth compare mode. GetDepthTestMode()94 CompareMode GetDepthTestMode() const { return depthTestMode_; } 95 96 /// Return pass lighting mode. GetLightingMode()97 PassLightingMode GetLightingMode() const { return lightingMode_; } 98 99 /// Return last shaders loaded frame number. GetShadersLoadedFrameNumber()100 unsigned GetShadersLoadedFrameNumber() const { return shadersLoadedFrameNumber_; } 101 102 /// Return depth write mode. GetDepthWrite()103 bool GetDepthWrite() const { return depthWrite_; } 104 105 /// Return alpha-to-coverage mode. GetAlphaToCoverage()106 bool GetAlphaToCoverage() const { return alphaToCoverage_; } 107 108 /// Return whether requires desktop level hardware. IsDesktop()109 bool IsDesktop() const { return isDesktop_; } 110 111 /// Return vertex shader name. GetVertexShader()112 const String& GetVertexShader() const { return vertexShaderName_; } 113 114 /// Return pixel shader name. GetPixelShader()115 const String& GetPixelShader() const { return pixelShaderName_; } 116 117 /// Return vertex shader defines. GetVertexShaderDefines()118 const String& GetVertexShaderDefines() const { return vertexShaderDefines_; } 119 120 /// Return pixel shader defines. GetPixelShaderDefines()121 const String& GetPixelShaderDefines() const { return pixelShaderDefines_; } 122 123 /// Return vertex shader define excludes. GetVertexShaderDefineExcludes()124 const String& GetVertexShaderDefineExcludes() const { return vertexShaderDefineExcludes_; } 125 126 /// Return pixel shader define excludes. GetPixelShaderDefineExcludes()127 const String& GetPixelShaderDefineExcludes() const { return pixelShaderDefineExcludes_; } 128 129 /// Return vertex shaders. GetVertexShaders()130 Vector<SharedPtr<ShaderVariation> >& GetVertexShaders() { return vertexShaders_; } 131 132 /// Return pixel shaders. GetPixelShaders()133 Vector<SharedPtr<ShaderVariation> >& GetPixelShaders() { return pixelShaders_; } 134 135 /// Return vertex shaders with extra defines from the renderpath. 136 Vector<SharedPtr<ShaderVariation> >& GetVertexShaders(const StringHash& extraDefinesHash); 137 /// Return pixel shaders with extra defines from the renderpath. 138 Vector<SharedPtr<ShaderVariation> >& GetPixelShaders(const StringHash& extraDefinesHash); 139 /// Return the effective vertex shader defines, accounting for excludes. Called internally by Renderer. 140 String GetEffectiveVertexShaderDefines() const; 141 /// Return the effective pixel shader defines, accounting for excludes. Called internally by Renderer. 142 String GetEffectivePixelShaderDefines() const; 143 144 private: 145 /// Pass index. 146 unsigned index_; 147 /// Blend mode. 148 BlendMode blendMode_; 149 /// Culling mode. 150 CullMode cullMode_; 151 /// Depth compare mode. 152 CompareMode depthTestMode_; 153 /// Lighting mode. 154 PassLightingMode lightingMode_; 155 /// Last shaders loaded frame number. 156 unsigned shadersLoadedFrameNumber_; 157 /// Depth write mode. 158 bool depthWrite_; 159 /// Alpha-to-coverage mode. 160 bool alphaToCoverage_; 161 /// Require desktop level hardware flag. 162 bool isDesktop_; 163 /// Vertex shader name. 164 String vertexShaderName_; 165 /// Pixel shader name. 166 String pixelShaderName_; 167 /// Vertex shader defines. 168 String vertexShaderDefines_; 169 /// Pixel shader defines. 170 String pixelShaderDefines_; 171 /// Vertex shader define excludes. 172 String vertexShaderDefineExcludes_; 173 /// Pixel shader define excludes. 174 String pixelShaderDefineExcludes_; 175 /// Vertex shaders. 176 Vector<SharedPtr<ShaderVariation> > vertexShaders_; 177 /// Pixel shaders. 178 Vector<SharedPtr<ShaderVariation> > pixelShaders_; 179 /// Vertex shaders with extra defines from the renderpath. 180 HashMap<StringHash, Vector<SharedPtr<ShaderVariation> > > extraVertexShaders_; 181 /// Pixel shaders with extra defines from the renderpath. 182 HashMap<StringHash, Vector<SharedPtr<ShaderVariation> > > extraPixelShaders_; 183 /// Pass name. 184 String name_; 185 }; 186 187 /// %Material technique. Consists of several passes. 188 class URHO3D_API Technique : public Resource 189 { 190 URHO3D_OBJECT(Technique, Resource); 191 192 friend class Renderer; 193 194 public: 195 /// Construct. 196 Technique(Context* context); 197 /// Destruct. 198 ~Technique(); 199 /// Register object factory. 200 static void RegisterObject(Context* context); 201 202 /// Load resource from stream. May be called from a worker thread. Return true if successful. 203 virtual bool BeginLoad(Deserializer& source); 204 205 /// Set whether requires desktop level hardware. 206 void SetIsDesktop(bool enable); 207 /// Create a new pass. 208 Pass* CreatePass(const String& passName); 209 /// Remove a pass. 210 void RemovePass(const String& passName); 211 /// Reset shader pointers in all passes. 212 void ReleaseShaders(); 213 /// Clone the technique. Passes will be deep copied to allow independent modification. 214 SharedPtr<Technique> Clone(const String& cloneName = String::EMPTY) const; 215 216 /// Return whether requires desktop level hardware. IsDesktop()217 bool IsDesktop() const { return isDesktop_; } 218 219 /// Return whether technique is supported by the current hardware. IsSupported()220 bool IsSupported() const { return !isDesktop_ || desktopSupport_; } 221 222 /// Return whether has a pass. HasPass(unsigned passIndex)223 bool HasPass(unsigned passIndex) const { return passIndex < passes_.Size() && passes_[passIndex].Get() != 0; } 224 225 /// Return whether has a pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead. 226 bool HasPass(const String& passName) const; 227 228 /// Return a pass, or null if not found. GetPass(unsigned passIndex)229 Pass* GetPass(unsigned passIndex) const { return passIndex < passes_.Size() ? passes_[passIndex].Get() : 0; } 230 231 /// Return a pass by name, or null if not found. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead. 232 Pass* GetPass(const String& passName) const; 233 234 /// Return a pass that is supported for rendering, or null if not found. GetSupportedPass(unsigned passIndex)235 Pass* GetSupportedPass(unsigned passIndex) const 236 { 237 Pass* pass = passIndex < passes_.Size() ? passes_[passIndex].Get() : 0; 238 return pass && (!pass->IsDesktop() || desktopSupport_) ? pass : 0; 239 } 240 241 /// Return a supported pass by name. This overload should not be called in time-critical rendering loops; use a pre-acquired pass index instead. 242 Pass* GetSupportedPass(const String& passName) const; 243 244 /// Return number of passes. 245 unsigned GetNumPasses() const; 246 /// Return all pass names. 247 Vector<String> GetPassNames() const; 248 /// Return all passes. 249 PODVector<Pass*> GetPasses() const; 250 251 /// Return a clone with added shader compilation defines. Called internally by Material. 252 SharedPtr<Technique> CloneWithDefines(const String& vsDefines, const String& psDefines); 253 254 /// Return a pass type index by name. Allocate new if not used yet. 255 static unsigned GetPassIndex(const String& passName); 256 257 /// Index for base pass. Initialized once GetPassIndex() has been called for the first time. 258 static unsigned basePassIndex; 259 /// Index for alpha pass. Initialized once GetPassIndex() has been called for the first time. 260 static unsigned alphaPassIndex; 261 /// Index for prepass material pass. Initialized once GetPassIndex() has been called for the first time. 262 static unsigned materialPassIndex; 263 /// Index for deferred G-buffer pass. Initialized once GetPassIndex() has been called for the first time. 264 static unsigned deferredPassIndex; 265 /// Index for per-pixel light pass. Initialized once GetPassIndex() has been called for the first time. 266 static unsigned lightPassIndex; 267 /// Index for lit base pass. Initialized once GetPassIndex() has been called for the first time. 268 static unsigned litBasePassIndex; 269 /// Index for lit alpha pass. Initialized once GetPassIndex() has been called for the first time. 270 static unsigned litAlphaPassIndex; 271 /// Index for shadow pass. Initialized once GetPassIndex() has been called for the first time. 272 static unsigned shadowPassIndex; 273 274 private: 275 /// Require desktop GPU flag. 276 bool isDesktop_; 277 /// Cached desktop GPU support flag. 278 bool desktopSupport_; 279 /// Passes. 280 Vector<SharedPtr<Pass> > passes_; 281 /// Cached clones with added shader compilation defines. 282 HashMap<Pair<StringHash, StringHash>, SharedPtr<Technique> > cloneTechniques_; 283 284 /// Pass index assignments. 285 static HashMap<String, unsigned> passIndices; 286 }; 287 288 } 289