source: OGRE/trunk/ogrenew/OgreMain/src/OgreGpuProgram.cpp @ 692

Revision 692, 54.8 KB checked in by mattausch, 18 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://ogre.sourceforge.net/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25#include "OgreStableHeaders.h"
26#include "OgreGpuProgram.h"
27#include "OgreHighLevelGpuProgram.h"
28#include "OgreGpuProgramManager.h"
29#include "OgreVector3.h"
30#include "OgreVector4.h"
31#include "OgreAutoParamDataSource.h"
32#include "OgreLight.h"
33#include "OgreRoot.h"
34#include "OgreRenderSystem.h"
35#include "OgreRenderSystemCapabilities.h"
36#include "OgreStringConverter.h"
37
38namespace Ogre
39{
40    //-----------------------------------------------------------------------------
41    GpuProgram::CmdType GpuProgram::msTypeCmd;
42    GpuProgram::CmdSyntax GpuProgram::msSyntaxCmd;
43    GpuProgram::CmdSkeletal GpuProgram::msSkeletalCmd;
44
45
46    GpuProgramParameters::AutoConstantDefinition GpuProgramParameters::AutoConstantDictionary[] = {
47        AutoConstantDefinition(ACT_WORLD_MATRIX,                  "world_matrix",                16, ET_REAL, ACDT_NONE),
48        AutoConstantDefinition(ACT_INVERSE_WORLD_MATRIX,          "inverse_world_matrix",        16, ET_REAL, ACDT_NONE),
49        AutoConstantDefinition(ACT_TRANSPOSE_WORLD_MATRIX,             "transpose_world_matrix",            16, ET_REAL, ACDT_NONE),
50        AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_WORLD_MATRIX, "inverse_transpose_world_matrix", 16, ET_REAL, ACDT_NONE),
51
52        AutoConstantDefinition(ACT_WORLD_MATRIX_ARRAY_3x4,        "world_matrix_array_3x4",      12, ET_REAL, ACDT_NONE),
53        AutoConstantDefinition(ACT_WORLD_MATRIX_ARRAY,            "world_matrix_array",          16, ET_REAL, ACDT_NONE),
54
55        AutoConstantDefinition(ACT_VIEW_MATRIX,                   "view_matrix",                 16, ET_REAL, ACDT_NONE),
56        AutoConstantDefinition(ACT_INVERSE_VIEW_MATRIX,           "inverse_view_matrix",         16, ET_REAL, ACDT_NONE),
57        AutoConstantDefinition(ACT_TRANSPOSE_VIEW_MATRIX,              "transpose_view_matrix",             16, ET_REAL, ACDT_NONE),
58        AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_VIEW_MATRIX,       "inverse_transpose_view_matrix",     16, ET_REAL, ACDT_NONE),
59
60        AutoConstantDefinition(ACT_PROJECTION_MATRIX,             "projection_matrix",           16, ET_REAL, ACDT_NONE),
61        AutoConstantDefinition(ACT_INVERSE_PROJECTION_MATRIX,          "inverse_projection_matrix",         16, ET_REAL, ACDT_NONE),
62        AutoConstantDefinition(ACT_TRANSPOSE_PROJECTION_MATRIX,        "transpose_projection_matrix",       16, ET_REAL, ACDT_NONE),
63        AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_PROJECTION_MATRIX, "inverse_transpose_projection_matrix", 16, ET_REAL, ACDT_NONE),
64
65        AutoConstantDefinition(ACT_VIEWPROJ_MATRIX,               "viewproj_matrix",             16, ET_REAL, ACDT_NONE),
66        AutoConstantDefinition(ACT_INVERSE_VIEWPROJ_MATRIX,       "inverse_viewproj_matrix",     16, ET_REAL, ACDT_NONE),
67        AutoConstantDefinition(ACT_TRANSPOSE_VIEWPROJ_MATRIX,          "transpose_viewproj_matrix",         16, ET_REAL, ACDT_NONE),
68        AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_VIEWPROJ_MATRIX,   "inverse_transpose_viewproj_matrix", 16, ET_REAL, ACDT_NONE),
69
70        AutoConstantDefinition(ACT_WORLDVIEW_MATRIX,              "worldview_matrix",            16, ET_REAL, ACDT_NONE),
71        AutoConstantDefinition(ACT_INVERSE_WORLDVIEW_MATRIX,      "inverse_worldview_matrix",    16, ET_REAL, ACDT_NONE),
72        AutoConstantDefinition(ACT_TRANSPOSE_WORLDVIEW_MATRIX,         "transpose_worldview_matrix",        16, ET_REAL, ACDT_NONE),
73        AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX, "inverse_transpose_worldview_matrix", 16, ET_REAL, ACDT_NONE),
74
75        AutoConstantDefinition(ACT_WORLDVIEWPROJ_MATRIX,          "worldviewproj_matrix",        16, ET_REAL, ACDT_NONE),
76        AutoConstantDefinition(ACT_INVERSE_WORLDVIEWPROJ_MATRIX,       "inverse_worldviewproj_matrix",      16, ET_REAL, ACDT_NONE),
77        AutoConstantDefinition(ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX,     "transpose_worldviewproj_matrix",    16, ET_REAL, ACDT_NONE),
78        AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_WORLDVIEWPROJ_MATRIX, "inverse_transpose_worldviewproj_matrix", 16, ET_REAL, ACDT_NONE),
79
80        AutoConstantDefinition(ACT_RENDER_TARGET_FLIPPING,          "render_target_flipping",         1, ET_REAL, ACDT_NONE),
81
82        AutoConstantDefinition(ACT_FOG_COLOUR,                    "fog_colour",                   4, ET_REAL, ACDT_NONE),
83        AutoConstantDefinition(ACT_FOG_PARAMS,                    "fog_params",                   4, ET_REAL, ACDT_NONE),
84
85        AutoConstantDefinition(ACT_AMBIENT_LIGHT_COLOUR,          "ambient_light_colour",         4, ET_REAL, ACDT_NONE),
86        AutoConstantDefinition(ACT_LIGHT_DIFFUSE_COLOUR,          "light_diffuse_colour",         4, ET_REAL, ACDT_INT),
87        AutoConstantDefinition(ACT_LIGHT_SPECULAR_COLOUR,         "light_specular_colour",        4, ET_REAL, ACDT_INT),
88        AutoConstantDefinition(ACT_LIGHT_ATTENUATION,             "light_attenuation",            4, ET_REAL, ACDT_INT),
89        AutoConstantDefinition(ACT_LIGHT_POSITION,                "light_position",               4, ET_REAL, ACDT_INT),
90        AutoConstantDefinition(ACT_LIGHT_POSITION_OBJECT_SPACE,   "light_position_object_space",  4, ET_REAL, ACDT_INT),
91                AutoConstantDefinition(ACT_LIGHT_POSITION_VIEW_SPACE,          "light_position_view_space",    4, ET_REAL, ACDT_INT),
92        AutoConstantDefinition(ACT_LIGHT_DIRECTION,               "light_direction",              4, ET_REAL, ACDT_INT),
93        AutoConstantDefinition(ACT_LIGHT_DIRECTION_OBJECT_SPACE,  "light_direction_object_space", 4, ET_REAL, ACDT_INT),
94                AutoConstantDefinition(ACT_LIGHT_DIRECTION_VIEW_SPACE,         "light_direction_view_space",   4, ET_REAL, ACDT_INT),
95                AutoConstantDefinition(ACT_LIGHT_DISTANCE_OBJECT_SPACE,   "light_distance_object_space",  1, ET_REAL, ACDT_INT),
96        AutoConstantDefinition(ACT_LIGHT_POWER_SCALE,                     "light_power",  1, ET_REAL, ACDT_INT),
97        AutoConstantDefinition(ACT_SHADOW_EXTRUSION_DISTANCE,     "shadow_extrusion_distance",    1, ET_REAL, ACDT_INT),
98        AutoConstantDefinition(ACT_CAMERA_POSITION,               "camera_position",              3, ET_REAL, ACDT_NONE),
99        AutoConstantDefinition(ACT_CAMERA_POSITION_OBJECT_SPACE,  "camera_position_object_space", 3, ET_REAL, ACDT_NONE),
100        AutoConstantDefinition(ACT_TEXTURE_VIEWPROJ_MATRIX,       "texture_viewproj_matrix",     16, ET_REAL, ACDT_NONE),
101        AutoConstantDefinition(ACT_CUSTOM,                        "custom",                       4, ET_REAL, ACDT_INT),  // *** needs to be tested
102        AutoConstantDefinition(ACT_TIME,                               "time",                               1, ET_REAL, ACDT_REAL),
103        AutoConstantDefinition(ACT_TIME_0_X,                      "time_0_x",                     4, ET_REAL, ACDT_REAL),
104        AutoConstantDefinition(ACT_COSTIME_0_X,                   "costime_0_x",                  4, ET_REAL, ACDT_REAL),
105        AutoConstantDefinition(ACT_SINTIME_0_X,                   "sintime_0_x",                  4, ET_REAL, ACDT_REAL),
106        AutoConstantDefinition(ACT_TANTIME_0_X,                   "tantime_0_x",                  4, ET_REAL, ACDT_REAL),
107        AutoConstantDefinition(ACT_TIME_0_X_PACKED,               "time_0_x_packed",              4, ET_REAL, ACDT_REAL),
108        AutoConstantDefinition(ACT_TIME_0_1,                      "time_0_1",                     4, ET_REAL, ACDT_REAL),
109        AutoConstantDefinition(ACT_COSTIME_0_1,                   "costime_0_1",                  4, ET_REAL, ACDT_REAL),
110        AutoConstantDefinition(ACT_SINTIME_0_1,                   "sintime_0_1",                  4, ET_REAL, ACDT_REAL),
111        AutoConstantDefinition(ACT_TANTIME_0_1,                   "tantime_0_1",                  4, ET_REAL, ACDT_REAL),
112        AutoConstantDefinition(ACT_TIME_0_1_PACKED,               "time_0_1_packed",              4, ET_REAL, ACDT_REAL),
113        AutoConstantDefinition(ACT_TIME_0_2PI,                    "time_0_2pi",                   4, ET_REAL, ACDT_REAL),
114        AutoConstantDefinition(ACT_COSTIME_0_2PI,                 "costime_0_2pi",                4, ET_REAL, ACDT_REAL),
115        AutoConstantDefinition(ACT_SINTIME_0_2PI,                 "sintime_0_2pi",                4, ET_REAL, ACDT_REAL),
116        AutoConstantDefinition(ACT_TANTIME_0_2PI,                 "tantime_0_2pi",                4, ET_REAL, ACDT_REAL),
117        AutoConstantDefinition(ACT_TIME_0_2PI_PACKED,             "time_0_2pi_packed",            4, ET_REAL, ACDT_REAL),
118        AutoConstantDefinition(ACT_FRAME_TIME,                    "frame_time",                   1, ET_REAL, ACDT_REAL),
119        AutoConstantDefinition(ACT_FPS,                           "fps",                          1, ET_REAL, ACDT_NONE),
120        AutoConstantDefinition(ACT_VIEWPORT_WIDTH,                "viewport_width",               1, ET_REAL, ACDT_NONE),
121        AutoConstantDefinition(ACT_VIEWPORT_HEIGHT,               "viewport_height",              1, ET_REAL, ACDT_NONE),
122        AutoConstantDefinition(ACT_INVERSE_VIEWPORT_WIDTH,        "inverse_viewport_width",       1, ET_REAL, ACDT_NONE),
123        AutoConstantDefinition(ACT_INVERSE_VIEWPORT_HEIGHT,       "inverse_viewport_height",      1, ET_REAL, ACDT_NONE),
124        AutoConstantDefinition(ACT_VIEWPORT_SIZE,                 "viewport_size",                4, ET_REAL, ACDT_NONE),
125        AutoConstantDefinition(ACT_VIEW_DIRECTION,                "view_direction",               3, ET_REAL, ACDT_NONE),
126        AutoConstantDefinition(ACT_VIEW_SIDE_VECTOR,              "view_side_vector",             3, ET_REAL, ACDT_NONE),
127        AutoConstantDefinition(ACT_VIEW_UP_VECTOR,                "view_up_vector",               3, ET_REAL, ACDT_NONE),
128        AutoConstantDefinition(ACT_FOV,                           "fov",                          1, ET_REAL, ACDT_NONE),
129        AutoConstantDefinition(ACT_NEAR_CLIP_DISTANCE,            "near_clip_distance",           1, ET_REAL, ACDT_NONE),
130        AutoConstantDefinition(ACT_FAR_CLIP_DISTANCE,             "far_clip_distance",            1, ET_REAL, ACDT_NONE),
131        AutoConstantDefinition(ACT_PASS_NUMBER,                        "pass_number",                        1, ET_REAL, ACDT_NONE),
132        AutoConstantDefinition(ACT_PASS_ITERATION_NUMBER,              "pass_iteration_number",              1, ET_REAL, ACDT_NONE),
133                AutoConstantDefinition(ACT_ANIMATION_PARAMETRIC,               "animation_parametric",               4, ET_REAL, ACDT_INT),
134    };
135
136   
137
138    //-----------------------------------------------------------------------------
139    GpuProgram::GpuProgram(ResourceManager* creator, const String& name, ResourceHandle handle,
140        const String& group, bool isManual, ManualResourceLoader* loader)
141        :Resource(creator, name, handle, group, isManual, loader),
142        mType(GPT_VERTEX_PROGRAM), mLoadFromFile(true), mSkeletalAnimation(false),
143        mPassSurfaceAndLightStates(false)
144    {
145    }
146    //-----------------------------------------------------------------------------
147    void GpuProgram::setType(GpuProgramType t)
148    {
149        mType = t;
150    }
151    //-----------------------------------------------------------------------------
152    void GpuProgram::setSyntaxCode(const String& syntax)
153    {
154        mSyntaxCode = syntax;
155    }
156    //-----------------------------------------------------------------------------
157    void GpuProgram::setSourceFile(const String& filename)
158    {
159        mFilename = filename;
160        mSource = "";
161        mLoadFromFile = true;
162    }
163    //-----------------------------------------------------------------------------
164    void GpuProgram::setSource(const String& source)
165    {
166        mSource = source;
167        mFilename = "";
168        mLoadFromFile = false;
169    }
170
171    //-----------------------------------------------------------------------------
172    void GpuProgram::loadImpl(void)
173    {
174        if (mLoadFromFile)
175        {
176            // find & load source code
177            DataStreamPtr stream =
178                ResourceGroupManager::getSingleton().openResource(
179                                        mFilename, mGroup, true, this);
180            mSource = stream->getAsString();
181        }
182
183        // Call polymorphic load
184        loadFromSource();
185
186    }
187    //-----------------------------------------------------------------------------
188    bool GpuProgram::isSupported(void) const
189    {
190        // If skeletal animation is being done, we need support for UBYTE4
191        if (isSkeletalAnimationIncluded() &&
192            !Root::getSingleton().getRenderSystem()->getCapabilities()
193                ->hasCapability(RSC_VERTEX_FORMAT_UBYTE4))
194        {
195            return false;
196        }
197        return GpuProgramManager::getSingleton().isSyntaxSupported(mSyntaxCode);
198    }
199    //-----------------------------------------------------------------------------
200    GpuProgramParametersSharedPtr GpuProgram::createParameters(void)
201    {
202        // Default implementation simply returns standard parameters.
203        GpuProgramParametersSharedPtr ret =
204            GpuProgramManager::getSingleton().createParameters();
205        // Copy in default parameters if present
206        if (!mDefaultParams.isNull())
207            ret->copyConstantsFrom(*(mDefaultParams.get()));
208       
209        return ret;
210    }
211    //-----------------------------------------------------------------------------
212    GpuProgramParametersSharedPtr GpuProgram::getDefaultParameters(void)
213    {
214        if (mDefaultParams.isNull())
215        {
216            mDefaultParams = createParameters();
217        }
218        return mDefaultParams;
219    }
220    //-----------------------------------------------------------------------------
221    void GpuProgram::setupBaseParamDictionary(void)
222    {
223        ParamDictionary* dict = getParamDictionary();
224
225        dict->addParameter(
226            ParameterDef("type", "'vertex_program' or 'fragment_program'",
227                PT_STRING), &msTypeCmd);
228        dict->addParameter(
229            ParameterDef("syntax", "Syntax code, e.g. vs_1_1", PT_STRING), &msSyntaxCmd);
230        dict->addParameter(
231            ParameterDef("includes_skeletal_animation",
232            "Whether this vertex program includes skeletal animation", PT_BOOL),
233            &msSkeletalCmd);
234    }
235
236    //-----------------------------------------------------------------------
237    const String& GpuProgram::getLanguage(void) const
238    {
239        static const String language = "asm";
240
241        return language;
242    }
243
244
245
246    //-----------------------------------------------------------------------------
247    //      GpuProgramParameters Methods
248    //-----------------------------------------------------------------------------
249    GpuProgramParameters::GpuProgramParameters()
250        : mTransposeMatrices(false), mAutoAddParamName(false), mActivePassIterationEntry(0)
251    {
252    }
253    //-----------------------------------------------------------------------------
254
255    GpuProgramParameters::GpuProgramParameters(const GpuProgramParameters& oth)
256    {
257        *this = oth;
258    }
259
260    //-----------------------------------------------------------------------------
261    GpuProgramParameters& GpuProgramParameters::operator=(const GpuProgramParameters& oth)
262    {
263        // nfz: perform copy of all containers
264        // let compiler perform shallow copies of structures
265        // AutoConstantEntry, RealConstantEntry, IntConstantEntry
266        mRealConstants = oth.mRealConstants;
267        mIntConstants  = oth.mIntConstants;
268        mAutoConstants = oth.mAutoConstants;
269        mParamNameMap  = oth.mParamNameMap;
270
271        mTransposeMatrices = oth.mTransposeMatrices;
272        mAutoAddParamName  = oth.mAutoAddParamName;
273        mConstantDefinitions = oth.mConstantDefinitions;
274
275                return *this;
276    }
277
278    void GpuProgramParameters::setConstant(size_t index, const Vector4& vec)
279    {
280        setConstant(index, vec.val, 1);
281    }
282        //-----------------------------------------------------------------------------
283        void GpuProgramParameters::setConstant(size_t index, Real val)
284        {
285                setConstant(index, Vector4(val, 0.0f, 0.0f, 0.0f));
286        }
287    //-----------------------------------------------------------------------------
288    void GpuProgramParameters::setConstant(size_t index, const Vector3& vec)
289    {
290        setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f));
291    }
292    //-----------------------------------------------------------------------------
293    void GpuProgramParameters::setConstant(size_t index, const Matrix4& m)
294    {
295        // set as 4x 4-element floats
296        if (mTransposeMatrices)
297        {
298            Matrix4 t = m.transpose();
299            GpuProgramParameters::setConstant(index++, t[0], 1);
300            GpuProgramParameters::setConstant(index++, t[1], 1);
301            GpuProgramParameters::setConstant(index++, t[2], 1);
302            GpuProgramParameters::setConstant(index, t[3], 1);
303        }
304        else
305        {
306            GpuProgramParameters::setConstant(index++, m[0], 1);
307            GpuProgramParameters::setConstant(index++, m[1], 1);
308            GpuProgramParameters::setConstant(index++, m[2], 1);
309            GpuProgramParameters::setConstant(index, m[3], 1);
310        }
311
312    }
313    //-----------------------------------------------------------------------------
314    void GpuProgramParameters::setConstant(size_t index, const Matrix4* pMatrix,
315        size_t numEntries)
316    {
317        for (size_t i = 0; i < numEntries; ++i)
318        {
319            const Matrix4& m = pMatrix[i];
320
321            if (mTransposeMatrices)
322            {
323                Matrix4 t = m.transpose();
324                GpuProgramParameters::setConstant(index++, t[0], 1);
325                GpuProgramParameters::setConstant(index++, t[1], 1);
326                GpuProgramParameters::setConstant(index++, t[2], 1);
327                GpuProgramParameters::setConstant(index++, t[3], 1);
328            }
329            else
330            {
331                GpuProgramParameters::setConstant(index++, m[0], 1);
332                GpuProgramParameters::setConstant(index++, m[1], 1);
333                GpuProgramParameters::setConstant(index++, m[2], 1);
334                GpuProgramParameters::setConstant(index++, m[3], 1);
335            }
336        }
337
338    }
339    //-----------------------------------------------------------------------------
340    void GpuProgramParameters::setConstant(size_t index, const ColourValue& colour)
341    {
342        setConstant(index, colour.val, 1);
343    }
344    //-----------------------------------------------------------------------------
345    void GpuProgramParameters::setConstant(size_t index, const float *val, size_t count)
346    {
347        // Expand if required
348        if (mRealConstants.size() < index + count)
349            mRealConstants.resize(index + count);
350
351        // Copy in chunks of 4
352        while (count--)
353        {
354            RealConstantEntry* e = &(mRealConstants[index++]);
355            e->isSet = true;
356            memcpy(e->val, val, sizeof(float) * 4);
357            val += 4;
358        }
359
360    }
361    //-----------------------------------------------------------------------------
362    void GpuProgramParameters::setConstant(size_t index, const double *val, size_t count)
363    {
364        // Expand if required
365        if (mRealConstants.size() < index + count)
366            mRealConstants.resize(index + count);
367
368        // Copy, casting to float
369        while (count--)
370        {
371            RealConstantEntry* e = &(mRealConstants[index++]);
372            e->isSet = true;
373            e->val[0] = static_cast<float>(val[0]);
374            e->val[1] = static_cast<float>(val[1]);
375            e->val[2] = static_cast<float>(val[2]);
376            e->val[3] = static_cast<float>(val[3]);
377            val += 4;
378        }
379
380    }
381    //-----------------------------------------------------------------------------
382    void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count)
383    {
384        // Expand if required
385        if (mIntConstants.size() < index + count)
386            mIntConstants.resize(index + count);
387
388        // Copy in chunks of 4
389        while (count--)
390        {
391            IntConstantEntry* e = &(mIntConstants[index++]);
392            e->isSet = true;
393            memcpy(e->val, val, sizeof(int) * 4);
394            val += 4;
395        }
396
397    }
398    //-----------------------------------------------------------------------------
399    void GpuProgramParameters::setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo)
400    {
401        mAutoConstants.push_back(AutoConstantEntry(acType, index, extraInfo));
402    }
403    //-----------------------------------------------------------------------------
404    void GpuProgramParameters::clearAutoConstants(void)
405    {
406        mAutoConstants.clear();
407    }
408    //-----------------------------------------------------------------------------
409    GpuProgramParameters::AutoConstantIterator GpuProgramParameters::getAutoConstantIterator(void) const
410    {
411        return AutoConstantIterator(mAutoConstants.begin(), mAutoConstants.end());
412    }
413    //-----------------------------------------------------------------------------
414    void GpuProgramParameters::setAutoConstantReal(size_t index, AutoConstantType acType, Real rData)
415    {
416        mAutoConstants.push_back(AutoConstantEntry(acType, index, rData));
417    }
418    //-----------------------------------------------------------------------------
419
420    //-----------------------------------------------------------------------------
421    void GpuProgramParameters::_updateAutoParamsNoLights(const AutoParamDataSource& source)
422    {
423        if (!hasAutoConstants()) return; // abort early if no autos
424        Vector3 vec3;
425        Vector4 vec4;
426        size_t index;
427        size_t numMatrices;
428        const Matrix4* pMatrix;
429        size_t m;
430
431                mActivePassIterationEntry = 0;
432
433        AutoConstantList::const_iterator i, iend;
434        iend = mAutoConstants.end();
435        for (i = mAutoConstants.begin(); i != iend; ++i)
436        {
437            switch(i->paramType)
438            {
439            case ACT_WORLD_MATRIX:
440                setConstant(i->index, source.getWorldMatrix());
441                break;
442            case ACT_INVERSE_WORLD_MATRIX:
443                setConstant(i->index, source.getInverseWorldMatrix());
444                break;
445            case ACT_TRANSPOSE_WORLD_MATRIX:
446               setConstant(i->index, source.getTransposeWorldMatrix());
447               break;
448            case ACT_INVERSE_TRANSPOSE_WORLD_MATRIX:
449               setConstant(i->index, source.getInverseTransposeWorldMatrix());
450               break;
451
452            case ACT_WORLD_MATRIX_ARRAY_3x4:
453                // Loop over matrices
454                pMatrix = source.getWorldMatrixArray();
455                numMatrices = source.getWorldMatrixCount();
456                index = i->index;
457                for (m = 0; m < numMatrices; ++m)
458                {
459                    GpuProgramParameters::setConstant(index++, (*pMatrix)[0], 1);
460                    GpuProgramParameters::setConstant(index++, (*pMatrix)[1], 1);
461                    GpuProgramParameters::setConstant(index++, (*pMatrix)[2], 1);
462                    ++pMatrix;
463                }
464               
465                break;
466            case ACT_WORLD_MATRIX_ARRAY:
467                setConstant(i->index, source.getWorldMatrixArray(),
468                    source.getWorldMatrixCount());
469                break;
470            case ACT_VIEW_MATRIX:
471                setConstant(i->index, source.getViewMatrix());
472                break;
473            case ACT_INVERSE_VIEW_MATRIX:
474               setConstant(i->index, source.getInverseViewMatrix());
475               break;
476            case ACT_TRANSPOSE_VIEW_MATRIX:
477               setConstant(i->index, source.getTransposeViewMatrix());
478               break;
479            case ACT_INVERSE_TRANSPOSE_VIEW_MATRIX:
480               setConstant(i->index, source.getTransposeViewMatrix());
481               break;
482
483            case ACT_PROJECTION_MATRIX:
484                setConstant(i->index, source.getProjectionMatrix());
485                break;
486            case ACT_INVERSE_PROJECTION_MATRIX:
487               setConstant(i->index, source.getInverseProjectionMatrix());
488               break;
489            case ACT_TRANSPOSE_PROJECTION_MATRIX:
490               setConstant(i->index, source.getTransposeProjectionMatrix());
491               break;
492            case ACT_INVERSE_TRANSPOSE_PROJECTION_MATRIX:
493               setConstant(i->index, source.getInverseTransposeProjectionMatrix());
494               break;
495
496            case ACT_VIEWPROJ_MATRIX:
497                setConstant(i->index, source.getViewProjectionMatrix());
498                break;
499            case ACT_INVERSE_VIEWPROJ_MATRIX:
500               setConstant(i->index, source.getInverseViewProjMatrix());
501               break;
502            case ACT_TRANSPOSE_VIEWPROJ_MATRIX:
503               setConstant(i->index, source.getTransposeViewProjMatrix());
504               break;
505            case ACT_INVERSE_TRANSPOSE_VIEWPROJ_MATRIX:
506               setConstant(i->index, source.getInverseTransposeViewProjMatrix());
507               break;
508
509            case ACT_WORLDVIEW_MATRIX:
510                setConstant(i->index, source.getWorldViewMatrix());
511                break;
512            case ACT_INVERSE_WORLDVIEW_MATRIX:
513                setConstant(i->index, source.getInverseWorldViewMatrix());
514                break;
515            case ACT_TRANSPOSE_WORLDVIEW_MATRIX:
516               setConstant(i->index, source.getTransposeWorldViewMatrix());
517               break;
518            case ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX:
519               setConstant(i->index, source.getInverseTransposeWorldViewMatrix());
520               break;
521
522            case ACT_WORLDVIEWPROJ_MATRIX:
523                setConstant(i->index, source.getWorldViewProjMatrix());
524                break;
525            case ACT_INVERSE_WORLDVIEWPROJ_MATRIX:
526               setConstant(i->index, source.getInverseWorldViewProjMatrix());
527               break;
528            case ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX:
529               setConstant(i->index, source.getTransposeWorldViewProjMatrix());
530               break;
531            case ACT_INVERSE_TRANSPOSE_WORLDVIEWPROJ_MATRIX:
532               setConstant(i->index, source.getInverseTransposeWorldViewProjMatrix());
533               break;
534
535            case ACT_RENDER_TARGET_FLIPPING:
536               setConstant(i->index, source.getCurrentRenderTarget()->requiresTextureFlipping() ? -1.f : +1.f);
537               break;
538
539            // NB ambient light still here because it's not related to a specific light
540            case ACT_AMBIENT_LIGHT_COLOUR:
541                setConstant(i->index, source.getAmbientLightColour());
542                break;
543
544            case ACT_FOG_COLOUR:
545                setConstant(i->index, source.getFogColour());
546                break;
547            case ACT_FOG_PARAMS:
548                setConstant(i->index, source.getFogParams());
549                break;
550
551            case ACT_CAMERA_POSITION:
552                setConstant(i->index, source.getCameraPosition());
553                break;
554            case ACT_CAMERA_POSITION_OBJECT_SPACE:
555                setConstant(i->index, source.getCameraPositionObjectSpace());
556                break;
557
558            case ACT_TIME:
559               setConstant(i->index, Vector4(source.getTime() * i->fData, 0.f, 0.f, 0.f));
560               break;
561           case ACT_TIME_0_X:
562               setConstant(i->index, Vector4(source.getTime_0_X(i->fData), 0.f, 0.f, 0.f));
563               break;
564            case ACT_COSTIME_0_X:
565               setConstant(i->index, Vector4(source.getCosTime_0_X(i->fData), 0.f, 0.f, 0.f));
566               break;
567            case ACT_SINTIME_0_X:
568               setConstant(i->index, Vector4(source.getSinTime_0_X(i->fData), 0.f, 0.f, 0.f));
569               break;
570            case ACT_TANTIME_0_X:
571               setConstant(i->index, Vector4(source.getTanTime_0_X(i->fData), 0.f, 0.f, 0.f));
572               break;
573            case ACT_TIME_0_X_PACKED:
574               setConstant(i->index, source.getTime_0_X_packed(i->fData));
575               break;
576            case ACT_TIME_0_1:
577               setConstant(i->index, Vector4(source.getTime_0_1(i->fData), 0.f, 0.f, 0.f));
578               break;
579            case ACT_COSTIME_0_1:
580               setConstant(i->index, Vector4(source.getCosTime_0_1(i->fData), 0.f, 0.f, 0.f));
581               break;
582            case ACT_SINTIME_0_1:
583               setConstant(i->index, Vector4(source.getSinTime_0_1(i->fData), 0.f, 0.f, 0.f));
584               break;
585            case ACT_TANTIME_0_1:
586               setConstant(i->index, Vector4(source.getTanTime_0_1(i->fData), 0.f, 0.f, 0.f));
587               break;
588            case ACT_TIME_0_1_PACKED:
589               setConstant(i->index, source.getTime_0_1_packed(i->fData));
590               break;
591            case ACT_TIME_0_2PI:
592               setConstant(i->index, Vector4(source.getTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
593               break;
594            case ACT_COSTIME_0_2PI:
595               setConstant(i->index, Vector4(source.getCosTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
596               break;
597            case ACT_SINTIME_0_2PI:
598               setConstant(i->index, Vector4(source.getSinTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
599               break;
600            case ACT_TANTIME_0_2PI:
601               setConstant(i->index, Vector4(source.getTanTime_0_2Pi(i->fData), 0.f, 0.f, 0.f));
602               break;
603            case ACT_TIME_0_2PI_PACKED:
604               setConstant(i->index, source.getTime_0_2Pi_packed(i->fData));
605               break;
606            case ACT_FRAME_TIME:
607               setConstant(i->index, source.getFrameTime() * i->fData);
608               break;
609            case ACT_FPS:
610               setConstant(i->index, source.getFPS());
611               break;
612            case ACT_VIEWPORT_WIDTH:
613               setConstant(i->index, source.getViewportWidth());
614               break;
615            case ACT_VIEWPORT_HEIGHT:
616               setConstant(i->index, source.getViewportHeight());
617               break;
618            case ACT_INVERSE_VIEWPORT_WIDTH:
619               setConstant(i->index, source.getInverseViewportWidth());
620               break;
621            case ACT_INVERSE_VIEWPORT_HEIGHT:
622               setConstant(i->index, source.getInverseViewportHeight());
623               break;
624            case ACT_VIEWPORT_SIZE:
625               setConstant(i->index, Vector4(
626                   source.getViewportWidth(),
627                   source.getViewportHeight(),
628                   source.getInverseViewportWidth(),
629                   source.getInverseViewportHeight()));
630               break;
631            case ACT_VIEW_DIRECTION:
632               setConstant(i->index, source.getViewDirection());
633               break;
634            case ACT_VIEW_SIDE_VECTOR:
635               setConstant(i->index, source.getViewSideVector());
636               break;
637            case ACT_VIEW_UP_VECTOR:
638               setConstant(i->index, source.getViewUpVector());
639               break;
640            case ACT_FOV:
641               setConstant(i->index, source.getFOV());
642               break;
643            case ACT_NEAR_CLIP_DISTANCE:
644               setConstant(i->index, source.getNearClipDistance());
645               break;
646            case ACT_FAR_CLIP_DISTANCE:
647               setConstant(i->index, source.getFarClipDistance());
648               break;
649            case ACT_TEXTURE_VIEWPROJ_MATRIX:
650                setConstant(i->index, source.getTextureViewProjMatrix());
651                break;
652            case ACT_PASS_NUMBER:
653                setConstant(i->index, (float)source.getPassNumber());
654                break;
655            case ACT_PASS_ITERATION_NUMBER:
656                setConstant(i->index, 0.0f);
657                mActivePassIterationEntry = getRealConstantEntry(i->index);
658                mActivePassIterationEntryIndex = i->index;
659                break;
660            case ACT_CUSTOM:
661                        case ACT_ANIMATION_PARAMETRIC:
662                source.getCurrentRenderable()->_updateCustomGpuParameter(*i, this);
663                break;
664            default:
665                break;
666            }
667        }
668    }
669    //-----------------------------------------------------------------------------
670    void GpuProgramParameters::_updateAutoParamsLightsOnly(const AutoParamDataSource& source)
671    {
672        if (!hasAutoConstants()) return; // abort early if no autos
673        Vector3 vec3;
674        Vector4 vec4;
675
676        AutoConstantList::const_iterator i, iend;
677        iend = mAutoConstants.end();
678        for (i = mAutoConstants.begin(); i != iend; ++i)
679        {
680            switch(i->paramType)
681            {
682            case ACT_LIGHT_DIFFUSE_COLOUR:
683                setConstant(i->index, source.getLight(i->data).getDiffuseColour());
684                break;
685            case ACT_LIGHT_SPECULAR_COLOUR:
686                setConstant(i->index, source.getLight(i->data).getSpecularColour());
687                break;
688            case ACT_LIGHT_POSITION:
689                // Get as 4D vector, works for directional lights too
690                setConstant(i->index,
691                    source.getLight(i->data).getAs4DVector());
692                break;
693            case ACT_LIGHT_DIRECTION:
694                vec3 = source.getLight(i->data).getDerivedDirection();
695                // Set as 4D vector for compatibility
696                setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
697                break;
698            case ACT_LIGHT_POSITION_OBJECT_SPACE:
699                setConstant(i->index,
700                    source.getInverseWorldMatrix() * source.getLight(i->data).getAs4DVector());
701                break;
702            case ACT_LIGHT_DIRECTION_OBJECT_SPACE:
703                vec3 = source.getInverseWorldMatrix() *
704                    source.getLight(i->data).getDerivedDirection();
705                vec3.normalise();
706                // Set as 4D vector for compatibility
707                setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
708                break;
709                        case ACT_LIGHT_POSITION_VIEW_SPACE:
710                setConstant(i->index,
711                    source.getWorldViewMatrix() * source.getLight(i->data).getAs4DVector());
712                break;
713            case ACT_LIGHT_DIRECTION_VIEW_SPACE:
714                vec3 = source.getWorldViewMatrix() *
715                    source.getLight(i->data).getDerivedDirection();
716                vec3.normalise();
717                // Set as 4D vector for compatibility
718                setConstant(i->index, Vector4(vec3.x, vec3.y, vec3.z, 1.0f));
719                break;
720            case ACT_LIGHT_DISTANCE_OBJECT_SPACE:
721                vec3 = source.getInverseWorldMatrix() * source.getLight(i->data).getDerivedPosition();
722                setConstant(i->index, vec3.length());
723                break;
724            case ACT_SHADOW_EXTRUSION_DISTANCE:
725                setConstant(i->index, source.getShadowExtrusionDistance());
726                break;
727            case ACT_LIGHT_POWER_SCALE:
728                                setConstant(i->index, source.getLight(i->data).getPowerScale());
729                                break;
730            case ACT_LIGHT_ATTENUATION:
731                // range, const, linear, quad
732                const Light& l = source.getLight(i->data);
733                vec4.x = l.getAttenuationRange();
734                vec4.y = l.getAttenuationConstant();
735                vec4.z = l.getAttenuationLinear();
736                vec4.w = l.getAttenuationQuadric();
737                setConstant(i->index, vec4);
738                break;
739            }
740        }
741    }
742    //---------------------------------------------------------------------------
743    void GpuProgramParameters::_mapParameterNameToIndex(const String& name, const size_t index)
744    {
745        mParamNameMap[name] = index;
746    }
747    //---------------------------------------------------------------------------
748    size_t GpuProgramParameters::getParamIndex(const String& name)
749    {
750        ParamNameMap::const_iterator i = mParamNameMap.find(name);
751        if (i == mParamNameMap.end())
752        {
753            // name not found in map, should it be added to the map?
754            if(mAutoAddParamName)
755            {
756                // determine index
757                // don't know which Constants list the name is for
758                // so pick the largest index - a waste
759                size_t index = (mRealConstants.size() > mIntConstants.size()) ?
760                    mRealConstants.size() : mIntConstants.size();
761
762                _mapParameterNameToIndex(name, index);
763                return index;
764            }
765            else
766            {
767                OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Cannot find a parameter named " + name,
768                    "GpuProgramParameters::getParamIndex");
769            }
770        }
771        return i->second;
772    }
773    //---------------------------------------------------------------------------
774    void GpuProgramParameters::setNamedConstant(const String& name, Real val)
775    {
776        setConstant(getParamIndex(name), val);
777    }
778    //---------------------------------------------------------------------------
779    void GpuProgramParameters::setNamedConstant(const String& name, int val)
780    {
781        setConstant(getParamIndex(name), val);
782    }
783    //---------------------------------------------------------------------------
784    void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
785    {
786        setConstant(getParamIndex(name), vec);
787    }
788    //---------------------------------------------------------------------------
789    void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
790    {
791        setConstant(getParamIndex(name), vec);
792    }
793    //---------------------------------------------------------------------------
794    void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
795    {
796        setConstant(getParamIndex(name), m);
797    }
798    //---------------------------------------------------------------------------
799    void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m,
800        size_t numEntries)
801    {
802        setConstant(getParamIndex(name), m, numEntries);
803    }
804    //---------------------------------------------------------------------------
805    void GpuProgramParameters::setNamedConstant(const String& name, const float *val, size_t count)
806    {
807        setConstant(getParamIndex(name), val, count);
808    }
809    //---------------------------------------------------------------------------
810    void GpuProgramParameters::setNamedConstant(const String& name, const double *val, size_t count)
811    {
812        setConstant(getParamIndex(name), val, count);
813    }
814    //---------------------------------------------------------------------------
815    void GpuProgramParameters::setNamedConstant(const String& name, const ColourValue& colour)
816    {
817        setConstant(getParamIndex(name), colour);
818    }
819    //---------------------------------------------------------------------------
820    void GpuProgramParameters::setNamedConstant(const String& name, const int *val, size_t count)
821    {
822        setConstant(getParamIndex(name), val, count);
823    }
824    //---------------------------------------------------------------------------
825    void GpuProgramParameters::setNamedAutoConstant(const String& name, AutoConstantType acType, size_t extraInfo)
826    {
827        setAutoConstant(getParamIndex(name), acType, extraInfo);
828    }
829    //---------------------------------------------------------------------------
830    void GpuProgramParameters::setConstantFromTime(size_t index, Real factor)
831    {
832        setAutoConstantReal(index, ACT_TIME, factor);
833    }
834    //---------------------------------------------------------------------------
835    void GpuProgramParameters::setNamedConstantFromTime(const String& name, Real factor)
836    {
837        setConstantFromTime(getParamIndex(name), factor);
838    }
839    //---------------------------------------------------------------------------
840    GpuProgramParameters::RealConstantIterator GpuProgramParameters::getRealConstantIterator(void) const
841    {
842        return RealConstantIterator(mRealConstants.begin(), mRealConstants.end());
843    }
844    //---------------------------------------------------------------------------
845    GpuProgramParameters::IntConstantIterator GpuProgramParameters::getIntConstantIterator(void) const
846    {
847        return IntConstantIterator(mIntConstants.begin(), mIntConstants.end());
848    }
849
850    //---------------------------------------------------------------------------
851    GpuProgramParameters::RealConstantEntry* GpuProgramParameters::getRealConstantEntry(const size_t index)
852    {
853        if (index < mRealConstants.size())
854        {
855            return &(mRealConstants[index]);
856        }
857        else
858        {
859            return NULL;
860        }
861    }
862
863    //---------------------------------------------------------------------------
864    GpuProgramParameters::IntConstantEntry* GpuProgramParameters::getIntConstantEntry(const size_t index)
865    {
866        if (index < mIntConstants.size())
867        {
868            return &(mIntConstants[index]);
869        }
870        else
871        {
872            return NULL;
873        }
874    }
875
876    //---------------------------------------------------------------------------
877    GpuProgramParameters::AutoConstantEntry* GpuProgramParameters::getAutoConstantEntry(const size_t index)
878    {
879        if (index < mAutoConstants.size())
880        {
881            return &(mAutoConstants[index]);
882        }
883        else
884        {
885            return NULL;
886        }
887    }
888
889    //---------------------------------------------------------------------------
890    GpuProgramParameters::RealConstantEntry* GpuProgramParameters::getNamedRealConstantEntry(const String& name)
891    {
892        // check if name is found
893        ParamNameMap::const_iterator i = mParamNameMap.find(name);
894
895        if (i == mParamNameMap.end())
896        {
897            // no valid name found
898            return NULL;
899        }
900        else
901        {
902            // name found: return the entry
903            return getRealConstantEntry(i->second);
904        }
905
906    }
907
908    //---------------------------------------------------------------------------
909    GpuProgramParameters::IntConstantEntry* GpuProgramParameters::getNamedIntConstantEntry(const String& name)
910    {
911        // check if name is found
912        ParamNameMap::const_iterator i = mParamNameMap.find(name);
913
914        if (i == mParamNameMap.end())
915        {
916            // no valid name found
917            return NULL;
918        }
919        else
920        {
921            // name found: return the entry
922            return getIntConstantEntry(i->second);
923        }
924
925    }
926
927    //---------------------------------------------------------------------------
928        void GpuProgramParameters::copyConstantsFrom(const GpuProgramParameters& source)
929    {
930        // Iterate over fixed parameters
931        RealConstantIterator ri = source.getRealConstantIterator();
932        ushort i = 0;
933        while(ri.hasMoreElements())
934        {
935            RealConstantEntry re = ri.getNext();
936            if (re.isSet)
937            {
938                setConstant(i, re.val, 4);
939            }
940            ++i;
941
942        }
943        IntConstantIterator ii = source.getIntConstantIterator();
944        i = 0;
945        while (ii.hasMoreElements())
946        {
947            IntConstantEntry ie = ii.getNext();
948            if (ie.isSet)
949            {
950                setConstant(i, ie.val, 4);
951            }
952            ++i;
953        }
954
955        // Iterate over auto parameters
956        // Clear existing auto constants
957        clearAutoConstants();
958        AutoConstantIterator ai = source.getAutoConstantIterator();
959        while (ai.hasMoreElements())
960        {
961            AutoConstantEntry ae = ai.getNext();
962            setAutoConstant(ae.index, ae.paramType, ae.data);
963        }
964
965        // need to copy Parameter names from the source
966        mParamNameMap = source.mParamNameMap;
967        // copy constant definitions
968        mConstantDefinitions = source.mConstantDefinitions;
969
970       
971    }
972
973    //-----------------------------------------------------------------------
974    size_t GpuProgramParameters::addConstantDefinition(const String& name,
975                const size_t index, const size_t elementCount,
976                const ElementType elementType)
977    {
978        size_t idx;
979
980        // check if definition already exists
981        const ConstantDefinition* foundDef =
982                        findMatchingConstantDefinition(name, index, elementType);
983
984        // add the definition if it doesn't exist
985        if(!foundDef)
986        {
987            ConstantDefinition def;
988            def.elementType = elementType;
989            def.entryIndex = index;
990            def.elementCount = elementCount;
991            def.name = name;
992
993            mConstantDefinitions.push_back( def );
994            idx = mConstantDefinitions.size() - 1;
995        }
996        else
997        {
998            // calc the index of the found constant definition
999            idx = foundDef - &(*mConstantDefinitions.begin());
1000            // update element count if it was 0 before
1001            if (foundDef->elementCount == 0)
1002                mConstantDefinitions[idx].elementCount = elementCount;
1003        }
1004
1005        return idx;
1006    }
1007
1008    //-----------------------------------------------------------------------
1009    const GpuProgramParameters::ConstantDefinition*
1010        GpuProgramParameters::getConstantDefinition(const String& name) const
1011    {
1012        // find a constant definition that matches name by iterating through
1013                // the constant definition array
1014        ConstantDefinitionContainer::const_iterator currentConstDef
1015                        = mConstantDefinitions.begin();
1016        ConstantDefinitionContainer::const_iterator endConstDef
1017                        = mConstantDefinitions.end();
1018        while (currentConstDef != endConstDef)
1019        {
1020            if (name == (*currentConstDef).name)
1021                break;
1022            ++currentConstDef;
1023        }
1024
1025        if (currentConstDef != endConstDef)
1026            return &(*currentConstDef);
1027        else
1028            return 0;
1029    }
1030
1031    //-----------------------------------------------------------------------
1032    const GpuProgramParameters::ConstantDefinition*
1033                GpuProgramParameters::getConstantDefinition(const size_t idx) const
1034    {
1035        if (idx < mConstantDefinitions.size())
1036            return &mConstantDefinitions[idx];
1037        else
1038            return 0;
1039    }
1040
1041    //-----------------------------------------------------------------------
1042    const GpuProgramParameters::ConstantDefinition*
1043        GpuProgramParameters::findMatchingConstantDefinition(const String& name,
1044            const size_t entryIndex, const ElementType elementType) const
1045    {
1046        ConstantDefinitionContainer::const_iterator currentConstDef
1047                        = mConstantDefinitions.begin();
1048        ConstantDefinitionContainer::const_iterator endConstDef
1049                        = mConstantDefinitions.end();
1050
1051        while (currentConstDef != endConstDef)
1052        {
1053            if ((name == (*currentConstDef).name)
1054                && (entryIndex == (*currentConstDef).entryIndex)
1055                && (elementType == (*currentConstDef).elementType))
1056                break;
1057
1058            ++currentConstDef;
1059        }
1060
1061        if (currentConstDef != endConstDef)
1062            return &(*currentConstDef);
1063        else
1064            return 0;
1065
1066    }
1067
1068    //-----------------------------------------------------------------------
1069    void GpuProgramParameters::setConstantDefinitionAutoState(
1070                const size_t index, const bool isAuto, const size_t autoIndex )
1071    {
1072        if (index < mConstantDefinitions.size())
1073        {
1074            ConstantDefinition* cDef = &mConstantDefinitions[index];
1075            cDef->isAuto = isAuto;
1076            // if auto then check to make sure enough storage is allocated and
1077                        // update element count
1078            if (isAuto)
1079            {
1080                // get the auto constant entry associated with this constant definition
1081                const GpuProgramParameters::AutoConstantEntry* autoEntry =
1082                                        getAutoConstantEntry(autoIndex);
1083
1084                if (autoEntry)
1085                {
1086                    // get auto constant defintion
1087                    const AutoConstantDefinition* autoCDef =
1088                                                getAutoConstantDefinition(autoEntry->paramType);
1089
1090                    // if auto constant exists then its definition will override
1091                                        // constant definition attributes
1092                    if (autoCDef)
1093                    {
1094                        cDef->autoIndex = autoIndex;
1095                        cDef->elementType = autoCDef->elementType;
1096
1097                        if (cDef->elementCount < autoCDef->elementCount)
1098                        {
1099                            cDef->elementCount = autoCDef->elementCount;
1100                            // check constant entry list for proper size
1101                            const size_t endPos = cDef->entryIndex +
1102                                                                (cDef->elementCount - 1) / 4 + 1;
1103                            switch(cDef->elementType)
1104                            {
1105                            case ET_INT:
1106                                if (mIntConstants.size() < endPos)
1107                                    mIntConstants.resize(endPos);
1108                                break;
1109
1110                            case ET_REAL:
1111                                if (mRealConstants.size() < endPos)
1112                                    mRealConstants.resize(endPos);
1113                                break;
1114                            } // end switch
1115                        }
1116                    }
1117                }
1118            }
1119        }
1120    }
1121
1122    //-----------------------------------------------------------------------
1123    const GpuProgramParameters::AutoConstantDefinition*
1124        GpuProgramParameters::getAutoConstantDefinition(const String& name)
1125    {
1126        // find a constant definition that matches name by iterating through the
1127                // constant definition array
1128        bool nameFound = false;
1129        size_t i = 0;
1130        const size_t numDefs = getNumAutoConstantDefinitions();
1131        while (!nameFound && (i < numDefs))
1132        {
1133            if (name == AutoConstantDictionary[i].name)
1134                nameFound = true;
1135            else
1136                ++i;
1137        }
1138
1139        if (nameFound)
1140            return &AutoConstantDictionary[i];
1141        else
1142            return 0;
1143    }
1144
1145    //-----------------------------------------------------------------------
1146    const GpuProgramParameters::AutoConstantDefinition*
1147        GpuProgramParameters::getAutoConstantDefinition(const  size_t idx)
1148    {
1149
1150        if (idx < getNumAutoConstantDefinitions())
1151        {
1152            assert(idx == AutoConstantDictionary[idx].acType);
1153            return &AutoConstantDictionary[idx];
1154        }
1155        else
1156            return 0;
1157    }
1158    //-----------------------------------------------------------------------
1159    size_t GpuProgramParameters::getNumAutoConstantDefinitions(void)
1160    {
1161        return sizeof(AutoConstantDictionary)/sizeof(AutoConstantDefinition);
1162    }
1163
1164    //-----------------------------------------------------------------------
1165    void GpuProgramParameters::incPassIterationNumber(void)
1166    {
1167        if (mActivePassIterationEntry)
1168        {
1169            // only increment first element
1170            ++mActivePassIterationEntry->val[0];
1171            mActivePassIterationEntry->isSet = true;
1172        }
1173    }
1174
1175    //-----------------------------------------------------------------------
1176    GpuProgramParameters::RealConstantEntry* GpuProgramParameters::getPassIterationEntry(void)
1177    {
1178        return mActivePassIterationEntry;
1179    }
1180    //-----------------------------------------------------------------------
1181    //-----------------------------------------------------------------------
1182    String GpuProgram::CmdType::doGet(const void* target) const
1183    {
1184        const GpuProgram* t = static_cast<const GpuProgram*>(target);
1185        if (t->getType() == GPT_VERTEX_PROGRAM)
1186        {
1187            return "vertex_program";
1188        }
1189        else
1190        {
1191            return "fragment_program";
1192        }
1193    }
1194    void GpuProgram::CmdType::doSet(void* target, const String& val)
1195    {
1196        GpuProgram* t = static_cast<GpuProgram*>(target);
1197        if (val == "vertex_program")
1198        {
1199            t->setType(GPT_VERTEX_PROGRAM);
1200        }
1201        else
1202        {
1203            t->setType(GPT_FRAGMENT_PROGRAM);
1204        }
1205    }
1206    //-----------------------------------------------------------------------
1207    String GpuProgram::CmdSyntax::doGet(const void* target) const
1208    {
1209        const GpuProgram* t = static_cast<const GpuProgram*>(target);
1210        return t->getSyntaxCode();
1211    }
1212    void GpuProgram::CmdSyntax::doSet(void* target, const String& val)
1213    {
1214        GpuProgram* t = static_cast<GpuProgram*>(target);
1215        t->setSyntaxCode(val);
1216    }
1217    //-----------------------------------------------------------------------
1218    String GpuProgram::CmdSkeletal::doGet(const void* target) const
1219    {
1220        const GpuProgram* t = static_cast<const GpuProgram*>(target);
1221        return StringConverter::toString(t->isSkeletalAnimationIncluded());
1222    }
1223    void GpuProgram::CmdSkeletal::doSet(void* target, const String& val)
1224    {
1225        GpuProgram* t = static_cast<GpuProgram*>(target);
1226        t->setSkeletalAnimationIncluded(StringConverter::parseBool(val));
1227    }
1228    //-----------------------------------------------------------------------
1229    GpuProgramPtr& GpuProgramPtr::operator=(const HighLevelGpuProgramPtr& r)
1230    {
1231        // Can assign direct
1232        if (pRep == r.getPointer())
1233            return *this;
1234        release();
1235                // lock & copy other mutex pointer
1236                OGRE_LOCK_MUTEX(*r.OGRE_AUTO_MUTEX_NAME)
1237                OGRE_COPY_AUTO_SHARED_MUTEX(r.OGRE_AUTO_MUTEX_NAME)
1238        pRep = r.getPointer();
1239        pUseCount = r.useCountPointer();
1240        if (pUseCount)
1241        {
1242            ++(*pUseCount);
1243        }
1244        return *this;
1245    }
1246
1247}
Note: See TracBrowser for help on using the repository browser.