Subversion Repository Public Repository

Divide-Framework

This repository has no backups
This repository's network speed is throttled to 100KB/sec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
   Copyright (c) 2017 DIVIDE-Studio
   Copyright (c) 2009 Ionut Cava

   This file is part of DIVIDE Framework.

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software
   and associated documentation files (the "Software"), to deal in the Software
   without restriction,
   including without limitation the rights to use, copy, modify, merge, publish,
   distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so,
   subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED,
   INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
   PARTICULAR PURPOSE AND NONINFRINGEMENT.
   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
   DAMAGES OR OTHER LIABILITY,
   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
   IN CONNECTION WITH THE SOFTWARE
   OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 */

#ifndef _MATERIAL_H_
#define _MATERIAL_H_

#include "Utility/Headers/XMLParser.h"
#include "Utility/Headers/StateTracker.h"

#include "Core/Resources/Headers/Resource.h"
#include "Core/Resources/Headers/ResourceDescriptor.h"

#include "Platform/Video/Headers/RenderAPIEnums.h"
#include "Platform/Video/Headers/RenderStagePass.h"
#include "Platform/Video/Textures/Headers/Texture.h"

#include "Geometry/Material/Headers/ShaderProgramInfo.h"

namespace Divide {

class RenderStateBlock;
class ResourceDescriptor;

enum class BlendProperty : U32;
enum class ReflectorType : U32;

class Material : public CachedResource {
   public:
    enum class BumpMethod : U8 {
        NONE = 0,    //<Use phong
        NORMAL = 1,  //<Normal mapping
        PARALLAX = 2,
        RELIEF = 3,
        COUNT
    };

    /// How should each texture be added
    enum class TextureOperation : U8 {
        NONE = 0,
        MULTIPLY = 1,
        ADD = 2,
        SUBTRACT = 3,
        DIVIDE = 4,
        SMOOTH_ADD = 5,
        SIGNED_ADD = 6,
        DECAL = 7,
        REPLACE = 8,
        COUNT
    };

    enum class TranslucencySource : U8 {
        DIFFUSE = 0,
        DIFFUSE_MAP,
        OPACITY_MAP,
        COUNT
    };

    enum class TranslucencyType : U8 {
        FULL_TRANSPARENT = 0,
        TRANSLUCENT,
        COUNT
    };

    /// Not used yet but implemented for shading model selection in shaders
    /// This enum matches the ASSIMP one on a 1-to-1 basis
    enum class ShadingMode : U32 {
        FLAT = 0x1,
        PHONG = 0x2,
        BLINN_PHONG = 0x3,
        TOON = 0x4,
        // Use PBR for the following
        OREN_NAYAR = 0x5,
        COOK_TORRANCE = 0x6,
        COUNT
    };

    struct ExternalTexture {
        Texture_ptr _texture = nullptr;
        U8 _bindSlot = 0;
        bool _activeForDepth = false;
    };

    /// ShaderData stores information needed by the shader code to properly
    /// shade objects
    struct ColourData {
        FColour _diffuse;  /* diffuse component */
        FColour _specular; /* specular component*/
        FColour _emissive; /* emissive component*/
        F32 _shininess;    /* specular exponent */

        ColourData()
            : _diffuse(FColour(VECTOR3_UNIT / 1.5f, 1)),
              _specular(0.8f, 0.8f, 0.8f, 1.0f),
              _emissive(0.0f, 0.0f, 0.0f, 1.0f),
              _shininess(5)
        {
        }

        ColourData(const ColourData& other)
            : _diffuse(other._diffuse),
              _specular(other._specular),
              _emissive(other._emissive),
              _shininess(other._shininess)
        {
        }

        ColourData& operator=(const ColourData& other) {
            _diffuse.set(other._diffuse);
            _specular.set(other._specular);
            _emissive.set(other._emissive);
            _shininess = other._shininess;
            return *this;
        }
    };

   public:
    explicit Material(GFXDevice& context, ResourceCache& parentCache, size_t descriptorHash, const stringImpl& name);
    ~Material();

    /// Return a new instance of this material with the name composed of the
    /// base material's name and the give name suffix.
    /// clone calls CreateResource internally!)
    Material_ptr clone(const stringImpl& nameSuffix);
    bool unload();
    void update(const U64 deltaTimeUS);

    void setColourData(const ColourData& other);
    void setDiffuse(const FColour& value);
    void setSpecular(const FColour& value);
    void setEmissive(const vec3<F32>& value);
    void setHardwareSkinning(const bool state);
    void setOpacity(F32 value);
    void setShininess(F32 value);
    void setShadingMode(const ShadingMode& mode);
    // Should the shaders be computed on add? Should reflections be always parsed? Etc
    void setHighPriority(const bool state);

    void setDoubleSided(const bool state);
    bool setTexture(ShaderProgram::TextureUsage textureUsageSlot,
                    const Texture_ptr& tex,
                    const TextureOperation& op = TextureOperation::REPLACE);
    /// Add a texture <-> bind slot pair to be bound with the default textures
    /// on each "bindTexture" call
    void addExternalTexture(const Texture_ptr& texture, U8 slot, bool activeForDepth = false);

    /// Remove the custom texture assigned to the specified offset
    bool removeCustomTexture(U8 bindslot);

    /// Set the desired bump mapping method.
    void setBumpMethod(const BumpMethod& newBumpMethod);
    /// Shader modifiers add tokens to the end of the shader name.
    /// Add as many tokens as needed but separate them with a ".". i.e:
    /// "Tree.NoWind.Glow"
    void addShaderModifier(const RenderStagePass& renderStagePass, const stringImpl& shaderModifier);

    void addShaderModifier(const stringImpl& shaderModifier);

    /// Shader defines, separated by commas, are added to the generated shader
    /// The shader generator appends "#define " to the start of each define
    /// For example, to define max light count and max shadow casters add this string:
    ///"MAX_LIGHT_COUNT 4, MAX_SHADOW_CASTERS 2"
    /// The above strings becomes, in the shader:
    ///#define MAX_LIGHT_COUNT 4
    ///#define MAX_SHADOW_CASTERS 2
    void setShaderDefines(RenderPassType passType, const stringImpl& shaderDefines);
    void setShaderDefines(RenderStage renderStage, const stringImpl& shaderDefines);
    void setShaderDefines(const RenderStagePass& renderStagePass, const stringImpl& shaderDefines);

    void setShaderDefines(const stringImpl& shaderDefines);

    /// toggle multi-threaded shader loading on or off for this material
    void setShaderLoadThreaded(const bool state);

    /// Add the specified shader to the specified Stage Pass (stage and pass type)
    void setShaderProgram(const stringImpl& shader,
                          const RenderStagePass& renderStagePass,
                          const bool computeOnAdd);

    void setShaderProgram(const ShaderProgram_ptr& shader,
                          const RenderStagePass& renderStagePass);

    /// Add the specified shader to the specified render stage (for all pass types)
    void setShaderProgram(const stringImpl& shader,
                          RenderStage stage,
                          const bool computeOnAdd);

    void setShaderProgram(const ShaderProgram_ptr& shader,
                          RenderStage stage);

    /// Add the specified shader to the specified pass type (for all render stages)
    void setShaderProgram(const stringImpl& shader,
                          RenderPassType passType,
                          const bool computeOnAdd);

    void setShaderProgram(const ShaderProgram_ptr& shader,
                          RenderPassType passType);

    void setShaderProgram(const stringImpl& shader,
                          const bool computeOnAdd);

    void setShaderProgram(const ShaderProgram_ptr& shader);

    void setRenderStateBlock(size_t renderStateBlockHash,
                             I32 variant = -1);

    void setRenderStateBlock(size_t renderStateBlockHash,
                             RenderStage renderStage,
                             I32 variant = -1);

    void setRenderStateBlock(size_t renderStateBlockHash,
                             RenderPassType renderPassType,
                             I32 variant = -1);

    void setRenderStateBlock(size_t renderStateBlockHash,
                             const RenderStagePass& renderStagePass,
                              I32 variant = -1);

    void setParallaxFactor(F32 factor);

    void getSortKeys(const RenderStagePass& renderStagePass, I32& shaderKey, I32& textureKey) const;

    void getMaterialMatrix(mat4<F32>& retMatrix) const;

    F32 getParallaxFactor() const;

    size_t getRenderStateBlock(const RenderStagePass& renderStagePass, I32 variant = 0);

    std::weak_ptr<Texture> getTexture(ShaderProgram::TextureUsage textureUsage) const;

    ShaderProgramInfo& getShaderInfo(const RenderStagePass& renderStagePass);

    const TextureOperation& getTextureOperation() const;

    const ColourData&  getColourData()  const;
    const ShadingMode& getShadingMode() const;
    const BumpMethod&  getBumpMethod()  const;

    void getTextureData(TextureDataContainer& textureData);

    void rebuild();
    void clean();
    void updateTranslucency(bool requestRecomputeShaders = true);
    bool hasTranslucency() const;
    bool hasTransparency() const;

    void dumpToFile(bool state);
    bool isDirty() const;
    bool isDoubleSided() const;
    // Checks if the shader needed for the current stage is already constructed.
    // Returns false if the shader was already ready.
    bool computeShader(const RenderStagePass& renderStagePass, const bool computeOnAdd);

    bool canDraw(const RenderStagePass& renderStagePass);

    void updateReflectionIndex(ReflectorType type, I32 index);
    void updateRefractionIndex(ReflectorType type, I32 index);

    void defaultReflectionTexture(const Texture_ptr& reflectionPtr, U32 arrayIndex);
    void defaultRefractionTexture(const Texture_ptr& reflectionPtr, U32 arrayIndex);

    U32 defaultReflectionTextureIndex() const;
    U32 defaultRefractionTextureIndex() const;

   private:
    void getTextureData(ShaderProgram::TextureUsage slot,
                        TextureDataContainer& container);

    void recomputeShaders();
    void setShaderProgramInternal(const stringImpl& shader,
                                  const RenderStagePass& renderStagePass,
                                  const bool computeOnAdd);

    void setShaderProgramInternal(const ShaderProgram_ptr& shader,
                                  const RenderStagePass& renderStagePass);

    bool isExternalTexture(ShaderProgram::TextureUsage slot) const;

    ShaderProgramInfo& shaderInfo(const RenderStagePass& renderStagePass);

    const ShaderProgramInfo& shaderInfo(const RenderStagePass& renderStagePass) const;

    std::array<size_t, 3>& defaultRenderStates(const RenderStagePass& renderStagePass);

   private:
    GFXDevice& _context;
    ResourceCache& _parentCache;
    ShadingMode _shadingMode;
    /// use for special shader tokens, such as "Tree"
    std::array<stringImpl, to_base(RenderStagePass::count())> _shaderModifier;
    TranslucencySource _translucencySource;
    TranslucencyType _translucencyType;
    /// parallax/relief factor (higher value > more pronounced effect)
    F32 _parallaxFactor;
    bool _dirty;
    bool _dumpToFile;
    bool _translucencyCheck;
    bool _doubleSided;
    /// Use shaders that have bone transforms implemented
    bool _hardwareSkinning;
    std::array<ShaderProgramInfo, to_base(RenderStagePass::count())> _shaderInfo;
    std::array<std::array<size_t, 3>,  to_base(RenderStagePass::count())> _defaultRenderStates;

    bool _shaderThreadedLoad;
    bool _highPriority;
    /// use this map to add textures to the material
    std::array<Texture_ptr, to_base(ShaderProgram::TextureUsage::COUNT)> _textures;
    std::array<bool, to_base(ShaderProgram::TextureUsage::COUNT)> _textureExtenalFlag;

    vectorImpl<ExternalTexture> _externalTextures;

    /// use the below map to define texture operation
    TextureOperation _operation;
    BumpMethod _bumpMethod;
    ColourData _colourData;

    /// used to keep track of what GFXDevice::reflectionTarget we are using for this rendering pass
    I32 _reflectionIndex;
    I32 _refractionIndex;
    std::pair<Texture_ptr, U32> _defaultReflection;
    std::pair<Texture_ptr, U32> _defaultRefraction;
};

TYPEDEF_SMART_POINTERS_FOR_CLASS(Material);

};  // namespace Divide

#endif //_MATERIAL_H_

#include "Material.inl"

Commits for Divide-Framework/trunk/Source Code/Geometry/Material/Headers/Material.h

Diff revisions: vs.
Revision Author Commited Message
1004 Diff Diff IonutCava picture IonutCava Wed 20 Dec, 2017 17:16:32 +0000

[Ionut]
- More GUI cleanup (added multiple GUIButton events)
- Typedef vec4<U8> and vec4<F32> to UColour and FColour respectively for easier to read code.

968 Diff Diff IonutCava picture IonutCava Tue 07 Nov, 2017 17:32:31 +0000

[Ionut]
- Consistent naming between timing variables

926 Diff Diff IonutCava picture IonutCava Fri 04 Aug, 2017 14:06:17 +0000

[Ionut]
- Rename material custom textures to external textures and make sure we properly provide all needed textures for depth stages

913 Diff Diff IonutCava picture IonutCava Thu 06 Jul, 2017 15:57:35 +0000

[Ionut]
- All shaders that are assigned to a material should have SHADOW_PASS defined for shadow shaders and DEPTH_PASS for all depth pass shaders.
- Add a helper function to map a value from one range to another

905 Diff Diff IonutCava picture IonutCava Mon 26 Jun, 2017 15:56:21 +0000

[Ionut]
Untested code

- Rework RenderBin/RenderPass/RenderQueue system to allow per-bin type rendering decision
— Added to allow implementation of Weighted, Blended Order-Independent Transparency later on
- Better detection of transparency and translucency in textures and materials. Used to separate Alpha-Discard based geometry from OIT translucent geometry

895 Diff Diff IonutCava picture IonutCava Wed 21 Jun, 2017 21:10:26 +0000

[IonutCava]
- Reorder and cleanup OpenGL backend a bit.
- Small code cleanup
- Some small profile-guided optimizations

890 Diff Diff IonutCava picture IonutCava Sun 14 May, 2017 20:54:59 +0000

[Ionut]
- Add pre-compiled header support
- Initial code for per-stage tessellation computation

875 Diff Diff IonutCava picture IonutCava Tue 14 Mar, 2017 16:59:38 +0000

[IonutCava]
- Cleanup and optimizations

872 Diff Diff IonutCava picture IonutCava Mon 06 Mar, 2017 23:23:20 +0000

[IonutCava]
- Optimise static casts

867 IonutCava picture IonutCava Mon 20 Feb, 2017 17:01:12 +0000

[IonutCava]
- Rename to_XNN and to_const_XNN functions to reflect the target data type they are converting to.
- Cleanup Angle namespace and remove all “inDegrees” parameters from rotation functions. Use aliases to differentiate between Degree and Radian parameters.
- Profile guided optimizations.