a9981c4560c6065a51092726e77e8c51602440ab
[occt.git] / src / Graphic3d / Graphic3d_ShaderVariable.hxx
1 // Created on: 2013-09-25
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _Graphic3d_ShaderVariable_HeaderFile
17 #define _Graphic3d_ShaderVariable_HeaderFile
18
19 #include <Graphic3d_Vec.hxx>
20 #include <Standard_Transient.hxx>
21 #include <TCollection_AsciiString.hxx>
22
23 #include <Graphic3d_ShaderVariable_Handle.hxx>
24
25 //! Interface for generic variable value.
26 struct Graphic3d_ValueInterface
27 {
28   //! Releases memory resources of variable value.
29   Standard_EXPORT virtual ~Graphic3d_ValueInterface();
30
31   //! Returns unique identifier of value type.
32   virtual Standard_Size TypeID() const = 0;
33
34   //! Returns variable value casted to specified type.
35   template <class T> T& As();
36
37   //! Returns variable value casted to specified type.
38   template <class T> const T& As() const;
39 };
40
41 //! Generates unique type identifier for variable value.
42 template<class T>
43 struct Graphic3d_UniformValueTypeID {
44   /* Not implemented */
45 };
46
47 template<>
48 struct Graphic3d_UniformValueTypeID<Standard_Integer> {
49   static const Standard_Size ID = __LINE__;
50 };
51
52 template<>
53 struct Graphic3d_UniformValueTypeID<Standard_ShortReal> {
54   static const Standard_Size ID = __LINE__;
55 };
56
57 template<>
58 struct Graphic3d_UniformValueTypeID<Graphic3d_Vec2> {
59   static const Standard_Size ID = __LINE__;
60 };
61
62 template<>
63 struct Graphic3d_UniformValueTypeID<Graphic3d_Vec3> {
64   static const Standard_Size ID = __LINE__;
65 };
66
67 template<>
68 struct Graphic3d_UniformValueTypeID<Graphic3d_Vec4> {
69   static const Standard_Size ID = __LINE__;
70 };
71
72 template<>
73 struct Graphic3d_UniformValueTypeID<Graphic3d_Vec2i> {
74   static const Standard_Size ID = __LINE__;
75 };
76
77 template<>
78 struct Graphic3d_UniformValueTypeID<Graphic3d_Vec3i> {
79   static const Standard_Size ID = __LINE__;
80 };
81
82 template<>
83 struct Graphic3d_UniformValueTypeID<Graphic3d_Vec4i> {
84   static const Standard_Size ID = __LINE__;
85 };
86
87 //! Describes specific value of custom uniform variable.
88 template <class T>
89 struct Graphic3d_UniformValue : public Graphic3d_ValueInterface
90 {
91   //! Creates new variable value.
92   Graphic3d_UniformValue (const T& theValue) : Value (theValue) { }
93
94   //! Returns unique identifier of value type.
95   virtual Standard_Size TypeID() const;
96
97   //! Value of custom uniform variable.
98   T Value;
99 };
100
101 //! Integer uniform value.
102 typedef Graphic3d_UniformValue<Standard_Integer> Graphic3d_UniformInt;
103
104 //! Integer uniform 2D vector.
105 typedef Graphic3d_UniformValue<Graphic3d_Vec2i> Graphic3d_UniformVec2i;
106
107 //! Integer uniform 3D vector.
108 typedef Graphic3d_UniformValue<Graphic3d_Vec3i> Graphic3d_UniformVec3i;
109
110 //! Integer uniform 4D vector.
111 typedef Graphic3d_UniformValue<Graphic3d_Vec4i> Graphic3d_UniformVec4i;
112
113 //! Floating-point uniform value.
114 typedef Graphic3d_UniformValue<Standard_ShortReal> Graphic3d_UniformFloat;
115
116 //! Floating-point uniform 2D vector.
117 typedef Graphic3d_UniformValue<Graphic3d_Vec2> Graphic3d_UniformVec2;
118
119 //! Floating-point uniform 3D vector.
120 typedef Graphic3d_UniformValue<Graphic3d_Vec3> Graphic3d_UniformVec3;
121
122 //! Floating-point uniform 4D vector.
123 typedef Graphic3d_UniformValue<Graphic3d_Vec4> Graphic3d_UniformVec4;
124
125 //! Describes custom uniform shader variable.
126 class Graphic3d_ShaderVariable : public Standard_Transient
127 {
128 public:
129
130   //! Releases resources of shader variable.
131   Standard_EXPORT virtual ~Graphic3d_ShaderVariable();
132   
133   //! Returns name of shader variable.
134   Standard_EXPORT const TCollection_AsciiString& Name() const;
135
136   //! Checks if the shader variable is valid or not.
137   Standard_EXPORT Standard_Boolean IsDone() const;
138
139   //! Returns interface of shader variable value.
140   Standard_EXPORT Graphic3d_ValueInterface* Value();
141
142   //! Creates new initialized shader variable.
143   template<class T>
144   static Graphic3d_ShaderVariable* Create (const TCollection_AsciiString& theName,
145                                            const T&                       theValue);
146
147 public:
148
149   DEFINE_STANDARD_RTTI (Graphic3d_ShaderVariable)
150
151 protected:
152
153   //! Creates new uninitialized shader variable.
154   Standard_EXPORT Graphic3d_ShaderVariable (const TCollection_AsciiString& theName);
155
156 protected:
157
158   //! The name of uniform shader variable.
159   TCollection_AsciiString myName;
160
161   //! The generic value of shader variable.
162   Graphic3d_ValueInterface* myValue;
163 };
164
165 #include <Graphic3d_ShaderVariable.lxx>
166
167 #endif // _Graphic3d_ShaderVariable_HeaderFile