OgreShaderParameter.h
Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org
    00006
    00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
    00008 Permission is hereby granted, free of charge, to any person obtaining a copy
    00009 of this software and associated documentation files (the "Software"), to deal
    00010 in the Software without restriction, including without limitation the rights
    00011 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00012 copies of the Software, and to permit persons to whom the Software is
    00013 furnished to do so, subject to the following conditions:
    00014
    00015 The above copyright notice and this permission notice shall be included in
    00016 all copies or substantial portions of the Software.
    00017
    00018 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00019 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00020 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00021 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00022 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00023 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00024 THE SOFTWARE.
    00025 -----------------------------------------------------------------------------
    00026 */
    00027 #ifndef _ShaderParameter_
    00028 #define _ShaderParameter_
    00029
    00030 #include "OgreShaderPrerequisites.h"
    00031 #include "OgreGpuProgram.h"
    00032 #include "OgreVector2.h"
    00033 #include "OgreVector3.h"
    00034 #include "OgreVector4.h"
    00035 #include "OgreMatrix4.h"
    00036
    00037 namespace Ogre {
    00038 namespace RTShader {
    00039
    00049 class _OgreRTSSExport Parameter : public RTShaderSystemAlloc
    00050 {
    00051 public:
    00052     // Shader parameter semantic.
    00053     enum Semantic
    00054     {
    00056         SPS_UNKNOWN = 0,
    00058         SPS_POSITION = 1,
    00060         SPS_BLEND_WEIGHTS = 2,
    00062         SPS_BLEND_INDICES = 3,
    00064         SPS_NORMAL = 4,
    00066         SPS_COLOR = 5,
    00068         SPS_TEXTURE_COORDINATES = 7,
    00070         SPS_BINORMAL = 8,
    00072         SPS_TANGENT = 9
    00073     };
    00074
    00075     // Shader parameter content.
    00076     enum Content
    00077     {
    00079         SPC_UNKNOWN,
    00080
    00082         SPC_POSITION_OBJECT_SPACE,
    00083
    00085         SPC_POSITION_WORLD_SPACE,
    00086
    00088         SPC_POSITION_VIEW_SPACE,
    00089
    00091         SPC_POSITION_PROJECTIVE_SPACE,
    00092
    00094         SPC_POSITION_LIGHT_SPACE0,
    00095         SPC_POSITION_LIGHT_SPACE1,
    00096         SPC_POSITION_LIGHT_SPACE2,
    00097         SPC_POSITION_LIGHT_SPACE3,
    00098         SPC_POSITION_LIGHT_SPACE4,
    00099         SPC_POSITION_LIGHT_SPACE5,
    00100         SPC_POSITION_LIGHT_SPACE6,
    00101         SPC_POSITION_LIGHT_SPACE7,
    00102
    00104         SPC_NORMAL_OBJECT_SPACE,
    00105
    00107         SPC_NORMAL_WORLD_SPACE,
    00108
    00110         SPC_NORMAL_VIEW_SPACE,
    00111
    00113         SPC_NORMAL_TANGENT_SPACE,
    00114
    00116         SPC_POSTOCAMERA_OBJECT_SPACE,
    00117
    00119         SPC_POSTOCAMERA_WORLD_SPACE,
    00120
    00122         SPC_POSTOCAMERA_VIEW_SPACE,
    00123
    00125         SPC_POSTOCAMERA_TANGENT_SPACE,
    00126
    00128         SPC_POSTOLIGHT_OBJECT_SPACE0,
    00129         SPC_POSTOLIGHT_OBJECT_SPACE1,
    00130         SPC_POSTOLIGHT_OBJECT_SPACE2,
    00131         SPC_POSTOLIGHT_OBJECT_SPACE3,
    00132         SPC_POSTOLIGHT_OBJECT_SPACE4,
    00133         SPC_POSTOLIGHT_OBJECT_SPACE5,
    00134         SPC_POSTOLIGHT_OBJECT_SPACE6,
    00135         SPC_POSTOLIGHT_OBJECT_SPACE7,
    00136
    00138         SPC_POSTOLIGHT_WORLD_SPACE0,
    00139         SPC_POSTOLIGHT_WORLD_SPACE1,
    00140         SPC_POSTOLIGHT_WORLD_SPACE2,
    00141         SPC_POSTOLIGHT_WORLD_SPACE3,
    00142         SPC_POSTOLIGHT_WORLD_SPACE4,
    00143         SPC_POSTOLIGHT_WORLD_SPACE5,
    00144         SPC_POSTOLIGHT_WORLD_SPACE6,
    00145         SPC_POSTOLIGHT_WORLD_SPACE7,
    00146
    00148         SPC_POSTOLIGHT_VIEW_SPACE0,
    00149         SPC_POSTOLIGHT_VIEW_SPACE1,
    00150         SPC_POSTOLIGHT_VIEW_SPACE2,
    00151         SPC_POSTOLIGHT_VIEW_SPACE3,
    00152         SPC_POSTOLIGHT_VIEW_SPACE4,
    00153         SPC_POSTOLIGHT_VIEW_SPACE5,
    00154         SPC_POSTOLIGHT_VIEW_SPACE6,
    00155         SPC_POSTOLIGHT_VIEW_SPACE7,
    00156
    00158         SPC_POSTOLIGHT_TANGENT_SPACE0,
    00159         SPC_POSTOLIGHT_TANGENT_SPACE1,
    00160         SPC_POSTOLIGHT_TANGENT_SPACE2,
    00161         SPC_POSTOLIGHT_TANGENT_SPACE3,
    00162         SPC_POSTOLIGHT_TANGENT_SPACE4,
    00163         SPC_POSTOLIGHT_TANGENT_SPACE5,
    00164         SPC_POSTOLIGHT_TANGENT_SPACE6,
    00165         SPC_POSTOLIGHT_TANGENT_SPACE7,
    00166
    00168         SPC_LIGHTDIRECTION_OBJECT_SPACE0,
    00169         SPC_LIGHTDIRECTION_OBJECT_SPACE1,
    00170         SPC_LIGHTDIRECTION_OBJECT_SPACE2,
    00171         SPC_LIGHTDIRECTION_OBJECT_SPACE3,
    00172         SPC_LIGHTDIRECTION_OBJECT_SPACE4,
    00173         SPC_LIGHTDIRECTION_OBJECT_SPACE5,
    00174         SPC_LIGHTDIRECTION_OBJECT_SPACE6,
    00175         SPC_LIGHTDIRECTION_OBJECT_SPACE7,
    00176
    00178         SPC_LIGHTDIRECTION_WORLD_SPACE0,
    00179         SPC_LIGHTDIRECTION_WORLD_SPACE1,
    00180         SPC_LIGHTDIRECTION_WORLD_SPACE2,
    00181         SPC_LIGHTDIRECTION_WORLD_SPACE3,
    00182         SPC_LIGHTDIRECTION_WORLD_SPACE4,
    00183         SPC_LIGHTDIRECTION_WORLD_SPACE5,
    00184         SPC_LIGHTDIRECTION_WORLD_SPACE6,
    00185         SPC_LIGHTDIRECTION_WORLD_SPACE7,
    00186
    00188         SPC_LIGHTDIRECTION_VIEW_SPACE0,
    00189         SPC_LIGHTDIRECTION_VIEW_SPACE1,
    00190         SPC_LIGHTDIRECTION_VIEW_SPACE2,
    00191         SPC_LIGHTDIRECTION_VIEW_SPACE3,
    00192         SPC_LIGHTDIRECTION_VIEW_SPACE4,
    00193         SPC_LIGHTDIRECTION_VIEW_SPACE5,
    00194         SPC_LIGHTDIRECTION_VIEW_SPACE6,
    00195         SPC_LIGHTDIRECTION_VIEW_SPACE7,
    00196
    00198         SPC_LIGHTDIRECTION_TANGENT_SPACE0,
    00199         SPC_LIGHTDIRECTION_TANGENT_SPACE1,
    00200         SPC_LIGHTDIRECTION_TANGENT_SPACE2,
    00201         SPC_LIGHTDIRECTION_TANGENT_SPACE3,
    00202         SPC_LIGHTDIRECTION_TANGENT_SPACE4,
    00203         SPC_LIGHTDIRECTION_TANGENT_SPACE5,
    00204         SPC_LIGHTDIRECTION_TANGENT_SPACE6,
    00205         SPC_LIGHTDIRECTION_TANGENT_SPACE7,
    00206
    00208         SPC_LIGHTPOSITION_OBJECT_SPACE0,
    00209         SPC_LIGHTPOSITION_OBJECT_SPACE1,
    00210         SPC_LIGHTPOSITION_OBJECT_SPACE2,
    00211         SPC_LIGHTPOSITION_OBJECT_SPACE3,
    00212         SPC_LIGHTPOSITION_OBJECT_SPACE4,
    00213         SPC_LIGHTPOSITION_OBJECT_SPACE5,
    00214         SPC_LIGHTPOSITION_OBJECT_SPACE6,
    00215         SPC_LIGHTPOSITION_OBJECT_SPACE7,
    00216
    00218         SPC_LIGHTPOSITION_WORLD_SPACE0,
    00219         SPC_LIGHTPOSITION_WORLD_SPACE1,
    00220         SPC_LIGHTPOSITION_WORLD_SPACE2,
    00221         SPC_LIGHTPOSITION_WORLD_SPACE3,
    00222         SPC_LIGHTPOSITION_WORLD_SPACE4,
    00223         SPC_LIGHTPOSITION_WORLD_SPACE5,
    00224         SPC_LIGHTPOSITION_WORLD_SPACE6,
    00225         SPC_LIGHTPOSITION_WORLD_SPACE7,
    00226
    00228         SPC_LIGHTPOSITIONVIEW_SPACE0,
    00229         SPC_LIGHTPOSITIONVIEW_SPACE1,
    00230         SPC_LIGHTPOSITIONVIEW_SPACE2,
    00231         SPC_LIGHTPOSITIONVIEW_SPACE3,
    00232         SPC_LIGHTPOSITIONVIEW_SPACE4,
    00233         SPC_LIGHTPOSITIONVIEW_SPACE5,
    00234         SPC_LIGHTPOSITIONVIEW_SPACE6,
    00235         SPC_LIGHTPOSITIONVIEW_SPACE7,
    00236
    00238         SPC_LIGHTPOSITION_TANGENT_SPACE,
    00239
    00241         SPC_BLEND_WEIGHTS,
    00242
    00244         SPC_BLEND_INDICES,
    00245
    00247         SPC_TANGENT_OBJECT_SPACE,
    00248
    00250         SPC_TANGENT_WORLD_SPACE,
    00251
    00253         SPC_TANGENT_VIEW_SPACE,
    00254
    00256         SPC_TANGENT_TANGENT_SPACE,
    00257
    00259         SPC_BINORMAL_OBJECT_SPACE,
    00260
    00262         SPC_BINORMAL_WORLD_SPACE,
    00263
    00265         SPC_BINORMAL_VIEW_SPACE,
    00266
    00268         SPC_BINORMAL_TANGENT_SPACE,
    00269
    00271         SPC_COLOR_DIFFUSE,
    00272
    00274         SPC_COLOR_SPECULAR,
    00275
    00277         SPC_DEPTH_OBJECT_SPACE,
    00278
    00280         SPC_DEPTH_WORLD_SPACE,
    00281
    00283         SPC_DEPTH_VIEW_SPACE,
    00284
    00286         SPC_DEPTH_PROJECTIVE_SPACE,
    00287
    00289         SPC_TEXTURE_COORDINATE0,
    00290         SPC_TEXTURE_COORDINATE1,
    00291         SPC_TEXTURE_COORDINATE2,
    00292         SPC_TEXTURE_COORDINATE3,
    00293         SPC_TEXTURE_COORDINATE4,
    00294         SPC_TEXTURE_COORDINATE5,
    00295         SPC_TEXTURE_COORDINATE6,
    00296         SPC_TEXTURE_COORDINATE7,
    00297
    00299         SPC_CUSTOM_CONTENT_BEGIN    = 1000,
    00300         SPC_CUSTOM_CONTENT_END      = 2000
    00301     };
    00302
    00303 // Interface.
    00304 public:
    00306     Parameter() {}
    00307
    00316     Parameter(GpuConstantType type, const String& name,
    00317         const Semantic& semantic, int index,
    00318         const Content& content, size_t size = 0);
    00319
    00321     virtual ~Parameter() {};
    00322
    00324     const String& getName() const { return mName; }
    00325
    00327     GpuConstantType getType() const { return mType; }
    00328
    00330     const Semantic& getSemantic() const { return mSemantic; }
    00331
    00333     int getIndex() const { return mIndex; }
    00334
    00336     Content getContent() const { return mContent; }
    00337
    00339     virtual bool isConstParameter() const { return false; }
    00340
    00342     virtual String toString() const { return mName; }
    00343
    00345     bool isArray() const { return mSize > 0; }
    00346
    00348     size_t getSize() const { return mSize; }
    00349
    00351     void setSize(size_t size) { mSize = size; }
    00352
    00353 // Attributes.
    00354 protected:
    00355     // Name of this parameter.
    00356     String mName;
    00357     // Type of this parameter.
    00358     GpuConstantType mType;
    00359     // Semantic of this parameter.
    00360     Semantic mSemantic;
    00361     // Index of this parameter.
    00362     int mIndex;
    00363     // The content of this parameter.
    00364     Content mContent;
    00365     // Number of elements in the parameter (for arrays)
    00366     size_t mSize;
    00367
    00368 };
    00369
    00370 typedef SharedPtr<Parameter>                    ParameterPtr;
    00371 typedef vector<ParameterPtr>::type              ShaderParameterList;
    00372 typedef ShaderParameterList::iterator           ShaderParameterIterator;
    00373 typedef ShaderParameterList::const_iterator     ShaderParameterConstIterator;
    00374
    00377 class UniformParameter : public Parameter
    00378 {
    00379 public:
    00380
    00390     UniformParameter(GpuConstantType type, const String& name,
    00391         const Semantic& semantic, int index,
    00392         const Content& content,
    00393         uint16 variability, size_t size);
    00394
    00400     UniformParameter(GpuProgramParameters::AutoConstantType autoType, Real fAutoConstantData, size_t size);
    00401
    00408     UniformParameter(GpuProgramParameters::AutoConstantType autoType, Real fAutoConstantData, size_t size, GpuConstantType type);
    00409
    00415     UniformParameter(GpuProgramParameters::AutoConstantType autoType, size_t nAutoConstantData, size_t size);
    00416
    00423     UniformParameter(GpuProgramParameters::AutoConstantType autoType, size_t nAutoConstantData, size_t size, GpuConstantType type);
    00424
    00425
    00427     size_t getAutoConstantIntData() const { return mAutoConstantIntData; }
    00428
    00430     Real getAutoConstantRealData() const { return mAutoConstantRealData; }
    00431
    00433     bool isFloat() const;
    00434
    00436     bool isSampler() const;
    00437
    00439     bool isAutoConstantParameter() const { return mIsAutoConstantReal || mIsAutoConstantInt; }
    00440
    00442     bool isAutoConstantIntParameter() const { return mIsAutoConstantInt; }
    00443
    00445     bool isAutoConstantRealParameter() const { return mIsAutoConstantReal; }
    00446
    00448     GpuProgramParameters::AutoConstantType getAutoConstantType  () const { return mAutoConstantType; }
    00449
    00451     uint16 getVariability() const { return mVariability; }
    00452
    00454     void bind(GpuProgramParametersSharedPtr paramsPtr);
    00455
    00456 public:
    00457
    00459     void setGpuParameter(int val)
    00460     {
    00461         if (mParamsPtr != NULL)
    00462         {
    00463             mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
    00464         }
    00465     }
    00466
    00468     void setGpuParameter(Real val)
    00469     {
    00470         if (mParamsPtr != NULL)
    00471         {
    00472             mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
    00473         }
    00474     }
    00475
    00477     void setGpuParameter(const ColourValue& val)
    00478     {
    00479         if (mParamsPtr != NULL)
    00480         {
    00481             mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
    00482         }
    00483     }
    00484
    00486     void setGpuParameter(const Vector2& val)
    00487     {
    00488         if (mParamsPtr != NULL)
    00489         {
    00490             mParamsPtr->_writeRawConstants(mPhysicalIndex, val.ptr(), 2);
    00491         }
    00492     }
    00493
    00495     void setGpuParameter(const Vector3& val)
    00496     {
    00497         if (mParamsPtr != NULL)
    00498         {
    00499             mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
    00500         }
    00501     }
    00502
    00504     void setGpuParameter(const Vector4& val)
    00505     {
    00506         if (mParamsPtr != NULL)
    00507         {
    00508             mParamsPtr->_writeRawConstant(mPhysicalIndex, val);
    00509         }
    00510     }
    00511
    00513     void setGpuParameter(const Matrix4& val)
    00514     {
    00515         if (mParamsPtr != NULL)
    00516         {
    00517             mParamsPtr->_writeRawConstant(mPhysicalIndex, val, 16);
    00518         }
    00519     }
    00520
    00522     void setGpuParameter(const float *val, size_t count, size_t multiple = 4)
    00523     {
    00524         if (mParamsPtr != NULL)
    00525         {
    00526             mParamsPtr->_writeRawConstants(mPhysicalIndex, val, count * multiple);
    00527         }
    00528     }
    00529
    00531     void setGpuParameter(const double *val, size_t count, size_t multiple = 4)
    00532     {
    00533         if (mParamsPtr != NULL)
    00534         {
    00535             mParamsPtr->_writeRawConstants(mPhysicalIndex, val, count * multiple);
    00536         }
    00537     }
    00538
    00540     void setGpuParameter(const int *val, size_t count, size_t multiple = 4)
    00541     {
    00542         if (mParamsPtr != NULL)
    00543         {
    00544             mParamsPtr->_writeRawConstants(mPhysicalIndex, val, count * multiple);
    00545         }
    00546     }
    00547
    00548 protected:
    00549     // Is it auto constant real based parameter.
    00550     bool mIsAutoConstantReal;
    00551     // Is it auto constant int based parameter.
    00552     bool mIsAutoConstantInt;
    00553     GpuProgramParameters::AutoConstantType  mAutoConstantType;      // The auto constant type of this parameter.
    00554     union
    00555     {
    00556         // Auto constant int data.
    00557         size_t mAutoConstantIntData;
    00558         // Auto constant real data.
    00559         Real mAutoConstantRealData;
    00560     };
    00561     // How this parameter varies (bitwise combination of GpuProgramVariability).
    00562     uint16 mVariability;
    00563     // The actual GPU parameters pointer.
    00564     GpuProgramParameters* mParamsPtr;
    00565     // The physical index of this parameter in the GPU program.
    00566     size_t mPhysicalIndex;
    00567 };
    00568
    00569 typedef SharedPtr<UniformParameter>             UniformParameterPtr;
    00570 typedef vector<UniformParameterPtr>::type       UniformParameterList;
    00571 typedef UniformParameterList::iterator          UniformParameterIterator;
    00572 typedef UniformParameterList::const_iterator    UniformParameterConstIterator;
    00573
    00576 template <class valueType>
    00577 class ConstParameter : public Parameter
    00578 {
    00579 public:
    00580
    00581     ConstParameter( valueType val,
    00582         GpuConstantType type,
    00583         const Semantic& semantic,
    00584         const Content& content)
    00585         : Parameter(type, "Constant", semantic, 0, content)
    00586     {
    00587         mValue = val;
    00588     }
    00589
    00590     virtual             ~ConstParameter     () {}
    00591
    00593     const valueType& getValue() const { return mValue; }
    00594
    00598     virtual bool isConstParameter() const { return true; }
    00599
    00603     virtual String toString() const = 0;
    00604
    00605 protected:
    00606     valueType mValue;
    00607 };
    00608
    00611 class _OgreRTSSExport ParameterFactory
    00612 {
    00613
    00614     // Interface.
    00615 public:
    00616
    00617     static ParameterPtr createInPosition(int index);
    00618     static ParameterPtr createOutPosition(int index);
    00619
    00620     static ParameterPtr createInNormal(int index);
    00621     static ParameterPtr createInWeights(int index);
    00622     static ParameterPtr createInIndices(int index);
    00623     static ParameterPtr createOutNormal(int index);
    00624     static ParameterPtr createInBiNormal(int index);
    00625     static ParameterPtr createOutBiNormal(int index);
    00626     static ParameterPtr createInTangent(int index);
    00627     static ParameterPtr createOutTangent(int index);
    00628     static ParameterPtr createInColor(int index);
    00629     static ParameterPtr createOutColor(int index);
    00630
    00631     static ParameterPtr createInTexcoord(GpuConstantType type, int index, Parameter::Content content);
    00632     static ParameterPtr createOutTexcoord(GpuConstantType type, int index, Parameter::Content content);
    00633     static ParameterPtr createInTexcoord1(int index, Parameter::Content content);
    00634     static ParameterPtr createOutTexcoord1(int index, Parameter::Content content);
    00635     static ParameterPtr createInTexcoord2(int index, Parameter::Content content);
    00636     static ParameterPtr createOutTexcoord2(int index, Parameter::Content content);
    00637     static ParameterPtr createInTexcoord3(int index, Parameter::Content content);
    00638     static ParameterPtr createOutTexcoord3(int index, Parameter::Content content);
    00639     static ParameterPtr createInTexcoord4(int index, Parameter::Content content);
    00640     static ParameterPtr createOutTexcoord4(int index, Parameter::Content content);
    00641
    00642     static ParameterPtr createConstParamVector2(Vector2 val);
    00643     static ParameterPtr createConstParamVector3(Vector3 val);
    00644     static ParameterPtr createConstParamVector4(Vector4 val);
    00645     static ParameterPtr createConstParamFloat(float val);
    00646
    00647     static UniformParameterPtr createSampler(GpuConstantType type, int index);
    00648     static UniformParameterPtr createSampler1D(int index);
    00649     static UniformParameterPtr createSampler2D(int index);
    00650     static UniformParameterPtr createSampler2DArray(int index);
    00651     static UniformParameterPtr createSampler3D(int index);
    00652     static UniformParameterPtr createSamplerCUBE(int index);
    00653
    00654     static UniformParameterPtr createUniform(GpuConstantType type, int index, uint16 variability, const String& suggestedName, size_t size);
    00655 };
    00656
    00657
    00658
    00662 }
    00663 }
    00664
    00665 #endif

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun Sep 2 2012 07:27:23