Coverage Report

Created: 2025-07-11 06:36

/src/ogre/OgreMain/include/OgreRenderSystemCapabilitiesSerializer.h
Line
Count
Source (jump to first uncovered line)
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 __RenderSystemCapabilitiesSerializer_H__
29
#define __RenderSystemCapabilitiesSerializer_H__
30
31
#include "OgrePrerequisites.h"
32
#include "OgreRenderSystemCapabilities.h"
33
#include "OgreHeaderPrefix.h"
34
35
36
namespace Ogre {
37
38
39
    /** \addtogroup Core
40
    *  @{
41
    */
42
    /** \addtogroup RenderSystem
43
    *  @{
44
    */
45
    /** Class for serializing RenderSystemCapabilities to / from a .rendercaps script.*/
46
    class _OgreExport RenderSystemCapabilitiesSerializer : public RenderSysAlloc
47
    {
48
49
    public:
50
        /** default constructor*/
51
        RenderSystemCapabilitiesSerializer();
52
53
        /** Writes a RenderSystemCapabilities object to a data stream */
54
        void writeScript(const RenderSystemCapabilities* caps, const String &name, const String& filename);
55
        
56
        /** Writes a RenderSystemCapabilities object to a string */
57
        String writeString(const RenderSystemCapabilities* caps, const String &name);
58
59
        /** Parses a RenderSystemCapabilities script file passed as a stream.
60
            Adds it to RenderSystemCapabilitiesManager::_addRenderSystemCapabilities
61
        */
62
        void parseScript(DataStreamPtr& stream);
63
64
    private:
65
        void write(const RenderSystemCapabilities* caps, const String &name, std::ostream &file);
66
67
        enum CapabilityKeywordType {UNDEFINED_CAPABILITY_TYPE = 0, SET_STRING_METHOD, SET_INT_METHOD, SET_BOOL_METHOD, SET_REAL_METHOD,
68
                                SET_CAPABILITY_ENUM_BOOL, ADD_SHADER_PROFILE_STRING};
69
        // determines what keyword is what type of capability. For example:
70
        // "automipmap" and "pbuffer" are both activated with setCapability (passing RSC_AUTOMIPMAP and RSC_PBUFFER respectivelly)
71
        // while "max_num_multi_render_targets" is an integer and has it's own method: setMaxMultiNumRenderTargets
72
        // we need to know these types to automatically parse each capability
73
        typedef std::map<String, CapabilityKeywordType> KeywordTypeMap;
74
        KeywordTypeMap mKeywordTypeMap;
75
76
        typedef void (RenderSystemCapabilities::*SetStringMethod)(const String&);
77
        // maps capability keywords to setCapability(String& cap) style methods
78
        typedef std::map<String, SetStringMethod> SetStringMethodDispatchTable;
79
        SetStringMethodDispatchTable mSetStringMethodDispatchTable;
80
81
        // SET_INT_METHOD parsing tables
82
        typedef void (RenderSystemCapabilities::*SetIntMethod)(ushort);
83
        typedef std::map<String, SetIntMethod> SetIntMethodDispatchTable;
84
        SetIntMethodDispatchTable mSetIntMethodDispatchTable;
85
86
        // SET_BOOL_METHOD parsing tables
87
        typedef void (RenderSystemCapabilities::*SetBoolMethod)(bool);
88
        typedef std::map<String, SetBoolMethod> SetBoolMethodDispatchTable;
89
        SetBoolMethodDispatchTable mSetBoolMethodDispatchTable;
90
91
        // SET_REAL_METHOD parsing tables
92
        typedef void (RenderSystemCapabilities::*SetRealMethod)(Real);
93
        typedef std::map<String, SetRealMethod> SetRealMethodDispatchTable;
94
        SetRealMethodDispatchTable mSetRealMethodDispatchTable;
95
96
        typedef std::map<String, Capabilities> CapabilitiesMap;
97
        CapabilitiesMap mCapabilitiesMap;
98
99
        inline void addCapabilitiesMapping(const String& name, Capabilities cap)
100
43
        {
101
43
            mCapabilitiesMap.emplace(name, cap);
102
43
        }
103
104
105
        // capabilities lines for parsing are collected along with their line numbers for debugging
106
        typedef std::vector<std::pair<String, int> > CapabilitiesLinesList;
107
        // the set of states that the parser can be in
108
        enum ParseAction {PARSE_HEADER, FIND_OPEN_BRACE, COLLECT_LINES};
109
110
        int mCurrentLineNumber;
111
        String* mCurrentLine;
112
        DataStreamPtr mCurrentStream;
113
114
        RenderSystemCapabilities* mCurrentCapabilities;
115
116
        inline void addKeywordType(const String& keyword, CapabilityKeywordType type)
117
31
        {
118
31
            mKeywordTypeMap.emplace(keyword, type);
119
31
        }
120
121
        CapabilityKeywordType getKeywordType(const String& keyword) const
122
0
        {
123
0
            KeywordTypeMap::const_iterator it = mKeywordTypeMap.find(keyword);
124
0
            if (it != mKeywordTypeMap.end())
125
0
                return (*it).second;
126
127
            // default
128
0
            return SET_CAPABILITY_ENUM_BOOL;
129
0
        }
130
131
        inline void addSetStringMethod(const String& keyword, SetStringMethod method)
132
4
        {
133
4
            mSetStringMethodDispatchTable.emplace(keyword, method);
134
4
        }
135
136
        inline void callSetStringMethod(String& keyword, String& val)
137
0
        {
138
0
            SetStringMethodDispatchTable::iterator methodIter = mSetStringMethodDispatchTable.find(keyword);
139
0
            if (methodIter != mSetStringMethodDispatchTable.end())
140
0
            {
141
0
                            SetStringMethod m = (*methodIter).second;
142
0
                (mCurrentCapabilities->*m)(val);
143
0
            }
144
0
            else
145
0
            {
146
0
                logParseError("undefined keyword: " + keyword);
147
0
            }
148
0
        }
149
150
151
        inline void addSetIntMethod(const String& keyword, SetIntMethod method)
152
10
        {
153
10
            mSetIntMethodDispatchTable.emplace(keyword, method);
154
10
        }
155
156
        inline void callSetIntMethod(String& keyword, ushort val)
157
0
        {
158
0
            SetIntMethodDispatchTable::iterator methodIter = mSetIntMethodDispatchTable.find(keyword);
159
0
            if (methodIter != mSetIntMethodDispatchTable.end())
160
0
            {
161
0
                            SetIntMethod m = (*methodIter).second;
162
0
                (mCurrentCapabilities->*m)(val);
163
0
            }
164
0
            else
165
0
            {
166
0
                logParseError("undefined keyword: " + keyword);
167
0
            }  
168
0
        }
169
170
171
        inline void addSetBoolMethod(const String& keyword, SetBoolMethod method)
172
1
        {
173
1
            mSetBoolMethodDispatchTable.emplace(keyword, method);
174
1
        }
175
176
        inline void callSetBoolMethod(String& keyword, bool val)
177
0
        {
178
0
            SetBoolMethodDispatchTable::iterator methodIter = mSetBoolMethodDispatchTable.find(keyword);
179
0
            if (methodIter != mSetBoolMethodDispatchTable.end())
180
0
            {
181
0
                            SetBoolMethod m = (*methodIter).second;
182
0
                (mCurrentCapabilities->*m)(val);
183
0
            }
184
0
            else
185
0
            {
186
0
                logParseError("undefined keyword: " + keyword);
187
0
                        }
188
0
        }
189
190
191
        inline void addSetRealMethod(const String& keyword, SetRealMethod method)
192
1
        {
193
1
            mSetRealMethodDispatchTable.emplace(keyword, method);
194
1
        }
195
196
        inline void callSetRealMethod(String& keyword, Real val)
197
0
        {
198
0
            SetRealMethodDispatchTable::iterator methodIter = mSetRealMethodDispatchTable.find(keyword);
199
0
            if (methodIter != mSetRealMethodDispatchTable.end())
200
0
            {
201
0
                            SetRealMethod m = (*methodIter).second;
202
0
                (mCurrentCapabilities->*m)(val);
203
0
            }
204
0
            else
205
0
            {
206
0
                logParseError("undefined keyword: " + keyword);
207
0
                        }
208
0
        }
209
210
        inline void addShaderProfile(String& val)
211
0
        {
212
0
            mCurrentCapabilities->addShaderProfile(val);
213
0
        }
214
215
        inline void setCapabilityEnumBool(String& name, bool val)
216
0
        {
217
            // check for errors
218
0
            if(mCapabilitiesMap.find(name) == mCapabilitiesMap.end())
219
0
            {
220
0
                logParseError("Undefined capability: " + name);
221
0
                return;
222
0
            }
223
            // only set true capabilities, we can't unset false
224
0
            if(val)
225
0
            {
226
0
                Capabilities cap = mCapabilitiesMap[name];
227
0
                mCurrentCapabilities->setCapability(cap);
228
0
            }
229
0
        }
230
231
        void initialiaseDispatchTables();
232
233
        void parseCapabilitiesLines(CapabilitiesLinesList& linesList);
234
235
        void logParseError(const String& error) const;
236
237
    };
238
    /** @} */
239
    /** @} */
240
241
}
242
243
#include "OgreHeaderSuffix.h"
244
245
#endif