Coverage Report

Created: 2025-08-28 06:22

/src/ogre/OgreMain/include/OgreGpuProgram.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 __GpuProgram_H_
29
#define __GpuProgram_H_
30
31
// Precompiler options
32
#include "OgrePrerequisites.h"
33
#include "OgreResource.h"
34
#include "OgreGpuProgramParams.h"
35
#include "OgreHeaderPrefix.h"
36
#include "OgreVector.h"
37
#include "OgreSharedPtr.h"
38
39
namespace Ogre {
40
41
    /** \addtogroup Core
42
     *  @{
43
     */
44
    /** \addtogroup Resources
45
     *  @{
46
     */
47
    /** Enumerates the types of programs which can run on the GPU. */
48
    enum GpuProgramType : uint8
49
    {
50
        GPT_VERTEX_PROGRAM = 0,
51
        GPT_FRAGMENT_PROGRAM,
52
        GPT_GEOMETRY_PROGRAM,
53
        GPT_DOMAIN_PROGRAM,
54
        GPT_HULL_PROGRAM,
55
        GPT_MESH_PROGRAM, // aliases with vertex
56
        GPT_COMPUTE_PROGRAM, // aliases with fragment
57
        GPT_TASK_PROGRAM
58
    };
59
    enum {
60
        GPT_COUNT = GPT_TASK_PROGRAM + 1,
61
        /// max programs that can be active in a pipeline (e.g compute is separate)
62
        GPT_PIPELINE_COUNT = GPT_HULL_PROGRAM + 1
63
    };
64
65
    /** Defines a program which runs on the GPU such as a vertex or fragment program.
66
67
        This class defines the low-level program in assembler code, the sort used to
68
        directly assemble into machine instructions for the GPU to execute. By nature,
69
        this means that the assembler source is rendersystem specific, which is why this
70
        is an abstract class - real instances are created through the RenderSystem.
71
        If you wish to use higher level shading languages like HLSL and Cg, you need to
72
        use the HighLevelGpuProgram class instead.
73
    */
74
    class _OgreExport GpuProgram : public Resource
75
    {
76
    protected:
77
    /// The name of the file to load source from (may be blank)
78
    String mFilename;
79
    /// The assembler source of the program (may be blank until file loaded)
80
    String mSource;
81
    /// Syntax code e.g. arbvp1, vs_2_0 etc
82
    String mSyntaxCode;
83
    /// The type of the program
84
    GpuProgramType mType;
85
    /// Whether we need to load source from file or not
86
    bool mLoadFromFile;
87
    /// Does this (vertex) program include Instancing?
88
    bool mInstancing;
89
    /// Does this (vertex) program include skeletal animation?
90
    bool mSkeletalAnimation;
91
    /// Does this (vertex) program include morph animation?
92
    bool mMorphAnimation;
93
    /// Does this (vertex) program require support for vertex texture fetch?
94
    bool mVertexTextureFetch;
95
    /// Does this (geometry) program require adjacency information?
96
    bool mNeedsAdjacencyInfo;
97
    /// Did we encounter a compilation error?
98
    bool mCompileError;
99
    bool mLoadedManualNamedConstants;
100
    /// The default parameters for use with this object
101
    GpuProgramParametersSharedPtr mDefaultParams;
102
    /** Record of logical to physical buffer maps. Mandatory for low-level
103
        programs or high-level programs which set their params the same way.
104
        This is a shared pointer because if the program is recompiled and the parameters
105
        change, this definition will alter, but previous params may reference the old def. */
106
    GpuLogicalBufferStructPtr mLogicalToPhysical;
107
    /** Parameter name -> ConstantDefinition map, shared instance used by all parameter objects.
108
        This is a shared pointer because if the program is recompiled and the parameters
109
        change, this definition will alter, but previous params may reference the old def.
110
    */
111
    GpuNamedConstantsPtr mConstantDefs;
112
    /// File from which to load named constants manually
113
    String mManualNamedConstantsFile;
114
    /// Does this (vertex) program include pose animation (count of number of poses supported)
115
    ushort mPoseAnimation;
116
117
    /** Internal method for setting up the basic parameter definitions for a subclass.
118
119
        Because StringInterface holds a dictionary of parameters per class, subclasses need to
120
        call this to ask the base class to add it's parameters to their dictionary as well.
121
        Can't do this in the constructor because that runs in a non-virtual context.
122
        @par
123
        The subclass must have called it's own createParamDictionary before calling this method.
124
    */
125
    virtual void setupBaseParamDictionary(void);
126
127
    /** Internal method returns whether required capabilities for this program is supported.
128
     */
129
    bool isRequiredCapabilitiesSupported(void) const;
130
131
    // catches errors during prepare
132
    void safePrepare();
133
134
    void prepareImpl() override;
135
136
    void loadImpl(void) override;
137
138
    void postLoadImpl() override;
139
140
    /// Create the internal params logical & named mapping structures
141
    void createParameterMappingStructures(bool recreateIfExists = true);
142
    /// Create the internal params logical mapping structures
143
    void createLogicalParameterMappingStructures(bool recreateIfExists = true);
144
    /// Create the internal params named mapping structures
145
    void createNamedParameterMappingStructures(bool recreateIfExists = true);
146
147
    public:
148
149
    GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
150
               const String& group, bool isManual = false, ManualResourceLoader* loader = 0);
151
152
    static const String getProgramTypeName(GpuProgramType programType);
153
154
0
    virtual ~GpuProgram() {}
155
156
    /** Sets the filename of the source assembly for this program.
157
158
        Setting this will have no effect until you (re)load the program.
159
    */
160
    void setSourceFile(const String& filename);
161
162
    /** Sets the source assembly for this program from an in-memory string.
163
164
        Setting this will have no effect until you (re)load the program.
165
    */
166
    void setSource(const String& source);
167
168
    /** Gets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
169
0
    const String& getSyntaxCode(void) const { return mSyntaxCode; }
170
171
    /** Sets the syntax code for this program e.g. arbvp1, fp20, vs_1_1 etc */
172
    void setSyntaxCode(const String& syntax);
173
174
    /** Gets the name of the file used as source for this program. */
175
0
    const String& getSourceFile(void) const { return mFilename; }
176
    /** Gets the assembler source for this program. */
177
0
    virtual const String& getSource(void) const { return mSource; }
178
    /// Set the program type (only valid before load)
179
    void setType(GpuProgramType t);
180
    /// Get the program type
181
0
    GpuProgramType getType(void) const { return mType; }
182
183
    /** Returns the GpuProgram which should be bound to the pipeline.
184
185
        This method is simply to allow some subclasses of GpuProgram to delegate
186
        the program which is bound to the pipeline to a delegate, if required. */
187
0
    virtual GpuProgram* _getBindingDelegate(void) { return this; }
188
189
    /** Returns whether this program can be supported on the current renderer and hardware. */
190
    virtual bool isSupported(void) const;
191
192
    /** Creates a new parameters object compatible with this program definition.
193
194
        It is recommended that you use this method of creating parameters objects
195
        rather than going direct to GpuProgramManager, because this method will
196
        populate any implementation-specific extras (like named parameters) where
197
        they are appropriate.
198
    */
199
    virtual GpuProgramParametersSharedPtr createParameters(void);
200
201
    /// @name GPU offloading features
202
    /// @{
203
    /** Sets whether a vertex program can do instancing.
204
205
        If this is set to true, OGRE will provide an instance buffer holding the
206
        World Matrix of each instance to the vertex program.
207
    */
208
0
    void setInstancingIncluded(bool included) { mInstancing = included; }
209
210
    /** Returns whether a vertex program can do instancing.
211
    */
212
0
    bool isInstancingIncluded(void) const { return mInstancing; }
213
214
    /** Sets whether a vertex program can do skeletal animation.
215
216
        If this is set to true, OGRE will not blend the geometry according to
217
        skeletal animation, it will expect the vertex program to do it.
218
    */
219
0
    virtual void setSkeletalAnimationIncluded(bool included) { mSkeletalAnimation = included; }
220
221
    /** Returns whether a vertex program can do skeletal animation.
222
223
        If this returns true, OGRE will not blend the geometry according to
224
        skeletal animation, it will expect the vertex program to do it.
225
    */
226
0
    virtual bool isSkeletalAnimationIncluded(void) const { return mSkeletalAnimation; }
227
228
    /** Sets whether a vertex program can do morph animation.
229
230
        If this is set to true, OGRE will not blend the geometry according to
231
        morph animation, it will expect the vertex program to do it.
232
    */
233
    virtual void setMorphAnimationIncluded(bool included)
234
0
    { mMorphAnimation = included; }
235
236
    /** Sets whether a vertex program can do pose animation.
237
238
        If this is set to true, OGRE will not blend the geometry according to
239
        pose animation, it will expect the vertex program to do it.
240
        @param poseCount The number of simultaneous poses the program can blend
241
    */
242
    virtual void setPoseAnimationIncluded(ushort poseCount)
243
0
    { mPoseAnimation = poseCount; }
244
245
    /** Returns whether a vertex program can do morph animation.
246
247
        If this returns true, OGRE will not blend the geometry according to
248
        morph animation, it will expect the vertex program to do it.
249
    */
250
0
    virtual bool isMorphAnimationIncluded(void) const { return mMorphAnimation; }
251
252
    /** Returns whether a vertex program can do pose animation.
253
254
        If this returns true, OGRE will not blend the geometry according to
255
        pose animation, it will expect the vertex program to do it.
256
    */
257
0
    virtual bool isPoseAnimationIncluded(void) const { return mPoseAnimation > 0; }
258
    /** Returns the number of simultaneous poses the vertex program can
259
        blend, for use in pose animation.
260
    */
261
0
    virtual ushort getNumberOfPosesIncluded(void) const { return mPoseAnimation; }
262
    /// @}
263
264
    /** Sets whether this vertex program requires support for vertex
265
        texture fetch from the hardware.
266
    */
267
0
    virtual void setVertexTextureFetchRequired(bool r) { mVertexTextureFetch = r; }
268
    /** Returns whether this vertex program requires support for vertex
269
        texture fetch from the hardware.
270
    */
271
0
    virtual bool isVertexTextureFetchRequired(void) const { return mVertexTextureFetch; }
272
273
    /** Get a reference to the default parameters which are to be used for all
274
        uses of this program.
275
276
        A program can be set up with a list of default parameters, which can save time when
277
        using a program many times in a material with roughly the same settings. By
278
        retrieving the default parameters and populating it with the most used options,
279
        any new parameter objects created from this program afterwards will automatically include
280
        the default parameters; thus users of the program need only change the parameters
281
        which are unique to their own usage of the program.
282
    */
283
    virtual const GpuProgramParametersPtr& getDefaultParameters(void);
284
285
    /** Returns true if default parameters have been set up.
286
     */
287
0
    virtual bool hasDefaultParameters(void) const { return mDefaultParams.get() != 0; }
288
289
    /** Returns whether a vertex program wants light and material states to be passed
290
        through fixed pipeline low level API rendering calls (default false, subclasses can override)
291
292
        Most vertex programs do not need this material information, however GLSL
293
        shaders can refer to this material and lighting state so enable this option
294
    */
295
0
    virtual bool getPassSurfaceAndLightStates(void) const { return false; }
296
297
    /** Returns whether a fragment program wants fog state to be passed
298
        through fixed pipeline low level API rendering calls (default true, subclasses can override)
299
300
        On DirectX, shader model 2 and earlier continues to have fixed-function fog
301
        applied to it, so fog state is still passed (you should disable fog on the
302
        pass if you want to perform fog in the shader). In OpenGL it is also
303
        common to be able to access the fixed-function fog state inside the shader.
304
    */
305
0
    virtual bool getPassFogStates(void) const { return true; }
306
307
    /** Returns whether a vertex program wants transform state to be passed
308
        through fixed pipeline low level API rendering calls
309
310
        Most vertex programs do not need fixed-function transform information, however GLSL
311
        shaders can refer to this state so enable this option
312
    */
313
0
    virtual bool getPassTransformStates(void) const { return false; }
314
315
    /** Returns a string that specifies the language of the gpu programs as specified
316
        in a material script. ie: asm, cg, hlsl, glsl
317
    */
318
    virtual const String& getLanguage(void) const;
319
320
    /** Did this program encounter a compile error when loading?
321
     */
322
0
    virtual bool hasCompileError(void) const { return mCompileError; }
323
324
    /** Reset a compile error if it occurred, allowing the load to be retried
325
     */
326
0
    virtual void resetCompileError(void) { mCompileError = false; }
327
328
    /** Allows you to manually provide a set of named parameter mappings
329
        to a program which would not be able to derive named parameters itself.
330
331
        You may wish to use this if you have assembler programs that were compiled
332
        from a high-level source, and want the convenience of still being able
333
        to use the named parameters from the original high-level source.
334
        @see setManualNamedConstantsFile
335
    */
336
    void setManualNamedConstants(const GpuNamedConstants& namedConstants);
337
338
    /** Specifies the name of a file from which to load named parameters mapping
339
        for a program which would not be able to derive named parameters itself.
340
341
        You may wish to use this if you have assembler programs that were compiled
342
        from a high-level source, and want the convenience of still being able
343
        to use the named parameters from the original high-level source. This
344
        method will make a low-level program search in the resource group of the
345
        program for the named file from which to load parameter names from.
346
        The file must be in the format produced by GpuNamedConstants::save.
347
    */
348
    void setManualNamedConstantsFile(const String& paramDefFile);
349
350
    /** Gets the name of a file from which to load named parameters mapping
351
        for a program which would not be able to derive named parameters itself.
352
    */
353
0
    const String& getManualNamedConstantsFile() const { return mManualNamedConstantsFile; }
354
    /** Get the full list of named constants.
355
        @note
356
        Only available if this parameters object has named parameters, which means either
357
        a high-level program which loads them, or a low-level program which has them
358
        specified manually.
359
    */
360
0
    virtual const GpuNamedConstants& getConstantDefinitions() { return *mConstantDefs.get(); }
361
362
    size_t calculateSize(void) const override;
363
364
    /// internal method to get the microcode cache id
365
    uint32 _getHash(uint32 seed = 0) const;
366
367
    protected:
368
    /// Virtual method which must be implemented by subclasses, load from mSource
369
    virtual void loadFromSource(void) = 0;
370
371
    };
372
373
0
    inline String to_string(const GpuProgramType& v) { return GpuProgram::getProgramTypeName(v); }
374
    /** @} */
375
    /** @} */
376
}
377
378
#include "OgreHeaderSuffix.h"
379
380
#endif