0024192: Adding support for shaders to OCCT visualization toolkit
[occt.git] / src / Graphic3d / Graphic3d_ShaderProgram.hxx
CommitLineData
30f0ad28 1// Created on: 2013-09-20
2// Created by: Denis BOGOLEPOV
3// Copyright (c) 2013 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
20#ifndef _Graphic3d_ShaderProgram_HeaderFile
21#define _Graphic3d_ShaderProgram_HeaderFile
22
23#include <Graphic3d_ShaderObject_Handle.hxx>
24#include <Graphic3d_ShaderProgram_Handle.hxx>
25#include <Graphic3d_ShaderVariable.hxx>
26#include <NCollection_Sequence.hxx>
27
28//! List of shader objects.
29typedef NCollection_Sequence<Handle(Graphic3d_ShaderObject)> Graphic3d_ShaderObjectList;
30
31//! List of custom uniform shader variables.
32typedef NCollection_Sequence<Handle(Graphic3d_ShaderVariable)> Graphic3d_ShaderVariableList;
33
34//! This class is responsible for managing shader programs.
35class Graphic3d_ShaderProgram : public Standard_Transient
36{
37public:
38
39 //! Creates new empty program object.
40 Standard_EXPORT Graphic3d_ShaderProgram();
41
42 //! Releases resources of program object.
43 Standard_EXPORT virtual ~Graphic3d_ShaderProgram();
44
45 //! Releases resources of program object.
46 Standard_EXPORT void Destroy() const;
47
48 //! Checks if the program object is valid or not.
49 Standard_EXPORT virtual Standard_Boolean IsDone() const;
50
51 //! Returns unique ID used to manage resource in graphic driver.
52 const TCollection_AsciiString& GetId() const { return myID; }
53
54 //! Attaches shader object to the program object.
55 Standard_EXPORT Standard_Boolean AttachShader (const Handle(Graphic3d_ShaderObject)& theShader);
56
57 //! Detaches shader object from the program object.
58 Standard_EXPORT Standard_Boolean DetachShader (const Handle(Graphic3d_ShaderObject)& theShader);
59
60 //! Returns list of attached shader objects.
61 const Graphic3d_ShaderObjectList& ShaderObjects() const { return myShaderObjects; }
62
63 //! Returns list of custom uniform variables.
64 const Graphic3d_ShaderVariableList& Variables() const { return myVariables; }
65
66 //! Pushes custom uniform variable to the program.
67 template<class T>
68 Standard_Boolean PushVariable (const TCollection_AsciiString& theName,
69 const T& theValue);
70
71 //! Removes all custom uniform variables from the program.
72 Standard_EXPORT void ClearVariables();
73
74public:
75
76 DEFINE_STANDARD_RTTI (Graphic3d_ShaderProgram)
77
78private:
79
80 TCollection_AsciiString myID; //!< The unique identifier of program object.
81 Graphic3d_ShaderObjectList myShaderObjects; //!< the list of attached shader objects.
82 Graphic3d_ShaderVariableList myVariables; //!< the list of custom uniform variables.
83
84};
85
86// =======================================================================
87// function : PushVariable
88// purpose : Pushes custom uniform variable to the program
89// =======================================================================
90template<class T> inline
91Standard_Boolean Graphic3d_ShaderProgram::PushVariable (const TCollection_AsciiString& theName,
92 const T& theValue)
93{
94 Handle(Graphic3d_ShaderVariable) aVariable = Graphic3d_ShaderVariable::Create (theName, theValue);
95 if (aVariable.IsNull() || !aVariable->IsDone())
96 {
97 return Standard_False;
98 }
99
100 myVariables.Append (aVariable);
101 return Standard_True;
102}
103
104#endif