Coverage Report

Created: 2025-11-11 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/OgreMain/include/OgreMaterialSerializer.h
Line
Count
Source
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
    (Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
6
7
Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
#ifndef __MaterialSerializer_H__
29
#define __MaterialSerializer_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreTextureUnitState.h"
33
#include "OgreGpuProgram.h"
34
#include "OgreStringVector.h"
35
#include "OgreHeaderPrefix.h"
36
37
namespace Ogre {
38
39
    /** \addtogroup Core
40
    *  @{
41
    */
42
    /** \addtogroup Materials
43
    *  @{
44
    */
45
    /** Class for serializing Materials to a .material script.*/
46
    class _OgreExport MaterialSerializer : public SerializerAlloc
47
    {   
48
    public:
49
50
        // Material serialize event.
51
        enum SerializeEvent
52
        {
53
            MSE_PRE_WRITE,
54
            MSE_WRITE_BEGIN,
55
            MSE_WRITE_END,
56
            MSE_POST_WRITE
57
        };
58
59
        /** Class that allows listening in on the various stages of material serialization process.
60
        Sub-classing it enable extending the attribute set of any part in the material.
61
        */
62
        class Listener
63
        {
64
        public:
65
0
            virtual ~Listener() {}
66
            
67
            /** Called when material section event raised.                  
68
            @param ser The MaterialSerializer instance that writes the given material.
69
            @param event The current section writing stage.
70
            @param skip May set to true by sub-class instances in order to skip the following section write.
71
            This parameter relevant only when stage equals MSE_PRE_WRITE. 
72
            @param mat The material that is being written.          
73
            */
74
            virtual void materialEventRaised(MaterialSerializer* ser, 
75
                SerializeEvent event, bool& skip, const Material* mat)
76
0
                        { (void)ser; (void)event; (void)skip; (void)mat; }
77
            
78
            /** Called when technique section event raised.             
79
            @param ser The MaterialSerializer instance that writes the given material.
80
            @param event The current section writing stage.
81
            @param skip May set to true by sub-class instances in order to skip the following section write.
82
            This parameter relevant only when stage equals MSE_PRE_WRITE. 
83
            @param tech The technique that is being written.        
84
            */
85
            virtual void techniqueEventRaised(MaterialSerializer* ser, 
86
                SerializeEvent event, bool& skip, const Technique* tech)
87
0
                        { (void)ser; (void)event; (void)skip; (void)tech; }
88
        
89
            /** Called when pass section event raised.                  
90
            @param ser The MaterialSerializer instance that writes the given material.
91
            @param event The current section writing stage.
92
            @param skip May set to true by sub-class instances in order to skip the following section write.
93
            This parameter relevant only when stage equals MSE_PRE_WRITE. 
94
            @param pass The pass that is being written.     
95
            */
96
            virtual void passEventRaised(MaterialSerializer* ser, 
97
                SerializeEvent event, bool& skip, const Pass* pass)
98
0
                        { (void)ser; (void)event; (void)skip; (void)pass; }
99
100
            /** Called when GPU program reference section event raised.             
101
            @param ser The MaterialSerializer instance that writes the given material.
102
            @param event The current section writing stage.
103
            @param skip May set to true by sub-class instances in order to skip the following section write.
104
            This parameter relevant only when stage equals MSE_PRE_WRITE. 
105
            @param attrib The GPU program reference description (vertex_program_ref, fragment_program_ref, etc).        
106
            @param program The program being written.
107
            @param params The program parameters.
108
            @param defaultParams The default program parameters.
109
            */
110
            void gpuProgramRefEventRaised(MaterialSerializer* ser, 
111
                SerializeEvent event, bool& skip,
112
                const String& attrib, 
113
                const GpuProgramPtr& program, 
114
                const GpuProgramParametersSharedPtr& params,
115
                GpuProgramParameters* defaultParams)
116
0
                        {
117
0
                            (void)ser;
118
0
                            (void)event;
119
0
                            (void)skip;
120
0
                            (void)attrib;
121
0
                            (void)program;
122
0
                            (void)params;
123
0
                            (void)defaultParams;
124
0
                        }
125
126
            /** Called when texture unit state section event raised.                    
127
            @param ser The MaterialSerializer instance that writes the given material.
128
            @param event The current section writing stage.
129
            @param skip May set to true by sub-class instances in order to skip the following section write.
130
            This parameter relevant only when stage equals MSE_PRE_WRITE. 
131
            @param textureUnit The texture unit state that is being written.        
132
            */
133
            virtual void textureUnitStateEventRaised(MaterialSerializer* ser, 
134
                SerializeEvent event, bool& skip, const TextureUnitState* textureUnit)
135
0
                        {
136
0
                            (void)ser;
137
0
                            (void)event;
138
0
                            (void)skip;
139
0
                            (void)textureUnit;
140
0
                        }           
141
        };
142
143
    private:
144
        /** Internal method for saving a program definition which has been
145
            built up.
146
        */
147
        void finishProgramDefinition(void);
148
149
        /// Listeners list of this Serializer.
150
        typedef std::vector<Listener*>         ListenerList;
151
        typedef ListenerList::iterator          ListenerListIterator;
152
        typedef ListenerList::const_iterator    ListenerListConstIterator;
153
        ListenerList mListeners;
154
155
156
        void writeMaterial(const MaterialPtr& pMat, const String& materialName = "");
157
        void writeTechnique(const Technique* pTech);
158
        void writePass(const Pass* pPass);
159
        void writeVertexProgramRef(const Pass* pPass);
160
        void writeTesselationHullProgramRef(const Pass* pPass);
161
        void writeTesselationDomainProgramRef(const Pass* pPass);
162
        void writeGeometryProgramRef(const Pass* pPass);
163
        void writeFragmentProgramRef(const Pass* pPass);
164
        void writeGpuProgramRef(const String& attrib, const GpuProgramPtr& program, const GpuProgramParametersSharedPtr& params);
165
        void writeGpuPrograms(void);
166
        void writeGPUProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
167
                                       const unsigned short level = 4, const bool useMainBuffer = true);
168
        void writeNamedGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
169
                                            const unsigned short level = 4, const bool useMainBuffer = true);
170
        void writeLowLevelGpuProgramParameters(const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
171
                                               const unsigned short level = 4, const bool useMainBuffer = true);
172
        void writeGpuProgramParameter(
173
            const String& commandName, const String& identifier, 
174
            const GpuProgramParameters::AutoConstantEntry* autoEntry, 
175
            const GpuProgramParameters::AutoConstantEntry* defaultAutoEntry, 
176
            bool isFloat, bool isDouble, bool isInt, bool isUnsignedInt, bool isRegister,
177
            size_t physicalIndex, size_t physicalSize,
178
            const GpuProgramParametersSharedPtr& params, GpuProgramParameters* defaultParams,
179
            const unsigned short level, const bool useMainBuffer);
180
        void writeTextureUnit(const TextureUnitState *pTex);
181
        void writeSceneBlendFactor(const SceneBlendFactor c_src, const SceneBlendFactor c_dest, 
182
                                   const SceneBlendFactor a_src, const SceneBlendFactor a_dest);
183
        void writeSceneBlendFactor(const SceneBlendFactor sbf_src, const SceneBlendFactor sbf_dest);
184
        void writeSceneBlendFactor(const SceneBlendFactor sbf);
185
        void writeCompareFunction(const CompareFunction cf);
186
        void writeColourValue(const ColourValue &colour, bool writeAlpha = false);
187
        void writeLayerBlendOperationEx(const LayerBlendOperationEx op);
188
        void writeLayerBlendSource(const LayerBlendSource lbs);
189
        
190
        void writeRotationEffect(float speed);
191
        void writeTransformEffect(const TextureUnitState::TextureEffect& effect, const TextureUnitState *pTex);
192
        void writeScrollEffect(float scrollAnimU, float scrollAnimV);
193
        void writeEnvironmentMapEffect(int type);
194
195
        String convertFiltering(FilterOptions fo);
196
197
        
198
        /** Internal methods that invokes registered listeners callback.
199
        @see Listener::materialEventRaised.
200
        */
201
        void fireMaterialEvent(SerializeEvent event, bool& skip, const Material* mat);
202
203
        /** Internal methods that invokes registered listeners callback.
204
        @see Listener::techniqueEventRaised.
205
        */
206
        void fireTechniqueEvent(SerializeEvent event, bool& skip, const Technique* tech);
207
        
208
        /** Internal methods that invokes registered listeners callback.
209
        @see Listener::passEventRaised.
210
        */
211
        void firePassEvent(SerializeEvent event, bool& skip, const Pass* pass);
212
        
213
        /** Internal methods that invokes registered listeners callback.
214
        @see Listener::gpuProgramRefEventRaised.
215
        */
216
        void fireGpuProgramRefEvent(SerializeEvent event, bool& skip,
217
            const String& attrib, 
218
            const GpuProgramPtr& program, 
219
            const GpuProgramParametersSharedPtr& params,
220
            GpuProgramParameters* defaultParams);
221
    
222
223
        /** Internal methods that invokes registered listeners callback.
224
        @see Listener::textureUnitStateEventRaised.
225
        */
226
        void fireTextureUnitStateEvent(SerializeEvent event, bool& skip, const TextureUnitState* textureUnit);
227
        
228
   public:      
229
        /** default constructor*/
230
        MaterialSerializer();
231
        /** default destructor*/
232
0
        virtual ~MaterialSerializer() {}
233
234
        /** Queue an in-memory Material to the internal buffer for export.
235
        @param pMat Material pointer
236
        @param clearQueued If true, any materials already queued will be removed
237
        @param exportDefaults If true, attributes which are defaulted will be
238
            included in the script exported, otherwise they will be omitted
239
        @param materialName Allow exporting the given material under a different name.
240
            In case of empty string the original material name will be used.
241
        */
242
        void queueForExport(const MaterialPtr& pMat, bool clearQueued = false, 
243
            bool exportDefaults = false, const String& materialName = "");
244
        /** Exports queued material(s) to a named material script file.
245
        @param filename the file name of the material script to be exported
246
        @param includeProgDef If true, vertex program and fragment program 
247
            definitions will be written at the top of the material script
248
        @param programFilename the file name of the vertex / fragment program 
249
            script to be exported. This is only used if there are program definitions
250
            to be exported and includeProgDef is false 
251
            when calling queueForExport.
252
        */
253
        void exportQueued(const String& filename, const bool includeProgDef = false, const String& programFilename = "");
254
        /** Exports a single in-memory Material to the named material script file.
255
        @param pMat Material pointer
256
        @param filename the file name of the material script to be exported
257
        @param exportDefaults if true then exports all values including defaults
258
        @param includeProgDef if true includes Gpu shader program definitions in the
259
            export material script otherwise if false then program definitions will
260
            be exported to a separate file with name programFilename if
261
            programFilename is not empty
262
        @param programFilename the file name of the vertex / fragment program 
263
            script to be exported. This is only used if includeProgDef is false.
264
        @param materialName Allow exporting the given material under a different name.
265
            In case of empty string the original material name will be used.
266
        */
267
        void exportMaterial(const MaterialPtr& pMat, const String& filename, bool exportDefaults = false,
268
            const bool includeProgDef = false, const String& programFilename = "", 
269
            const String& materialName = "");
270
        /** Returns a string representing the parsed material(s) */
271
        const String &getQueuedAsString() const;
272
        /** Clears the internal buffer */
273
        void clearQueue();
274
275
        /** Register a listener to this Serializer.
276
        @see MaterialSerializer::Listener
277
        */
278
        void addListener(Listener* listener);
279
280
        /** Remove a listener from this Serializer.
281
        @see MaterialSerializer::Listener
282
        */
283
        void removeListener(Listener* listener);
284
285
    private:
286
        String mBuffer;
287
        String mGpuProgramBuffer;
288
        typedef std::set<String> GpuProgramDefinitionContainer;
289
        typedef GpuProgramDefinitionContainer::iterator GpuProgramDefIterator;
290
        GpuProgramDefinitionContainer mGpuProgramDefinitionContainer;
291
        bool mDefaults;
292
        
293
    public:
294
        void beginSection(unsigned short level, const bool useMainBuffer = true)
295
0
        {
296
0
            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
297
0
            buffer += "\n";
298
0
            for (unsigned short i = 0; i < level; ++i)
299
0
            {
300
0
                buffer += "\t";
301
0
            }
302
0
            buffer += "{";
303
0
        }
304
        void endSection(unsigned short level, const bool useMainBuffer = true)
305
0
        {
306
0
            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
307
0
            buffer += "\n";
308
0
            for (unsigned short i = 0; i < level; ++i)
309
0
            {
310
0
                buffer += "\t";
311
0
            }
312
0
            buffer += "}";
313
0
        }
314
315
        void writeAttribute(unsigned short level, const String& att, const bool useMainBuffer = true)
316
0
        {
317
0
            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
318
0
            buffer += "\n";
319
0
            for (unsigned short i = 0; i < level; ++i)
320
0
            {
321
0
                buffer += "\t";
322
0
            }
323
0
            buffer += att;
324
0
        }
325
326
        void writeValue(const String& val, const bool useMainBuffer = true)
327
0
        {
328
0
            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
329
0
            buffer += (" " + val);
330
0
        }
331
332
        String quoteWord(const String& val)
333
0
        {
334
0
            if (val.find_first_of("{}$: \t") != String::npos)
335
0
                return ("\"" + val + "\"");
336
0
            else return val;
337
0
        }
338
339
340
        void writeComment(unsigned short level, const String& comment, const bool useMainBuffer = true)
341
0
        {
342
0
            String& buffer = (useMainBuffer ? mBuffer : mGpuProgramBuffer);
343
0
            buffer += "\n";
344
0
            for (unsigned short i = 0; i < level; ++i)
345
0
            {
346
0
                buffer += "\t";
347
0
            }
348
0
            buffer += "// " + comment;
349
0
        }
350
351
352
353
    };
354
    /** @} */
355
    /** @} */
356
}
357
358
#include "OgreHeaderSuffix.h"
359
360
#endif