0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / Graphic3d / Graphic3d_ShaderProgram.cxx
1 // Created on: 2013-09-20
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 #include <Graphic3d_ShaderProgram.hxx>
17
18 #include <Graphic3d_GraphicDriver.hxx>
19 #include <Graphic3d_ShaderObject.hxx>
20 #include <Graphic3d_TextureSetBits.hxx>
21 #include <OSD_Directory.hxx>
22 #include <OSD_Environment.hxx>
23 #include <OSD_File.hxx>
24 #include <OSD_Path.hxx>
25 #include <Standard_Atomic.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_ShaderProgram,Standard_Transient)
28
29 namespace
30 {
31   static volatile Standard_Integer THE_PROGRAM_OBJECT_COUNTER = 0;
32 }
33
34 // =======================================================================
35 // function : ShadersFolder
36 // purpose  :
37 // =======================================================================
38 const TCollection_AsciiString& Graphic3d_ShaderProgram::ShadersFolder()
39 {
40   static Standard_Boolean        THE_IS_DEFINED = Standard_False;
41   static TCollection_AsciiString THE_SHADERS_FOLDER;
42   if (!THE_IS_DEFINED)
43   {
44     THE_IS_DEFINED = Standard_True;
45     OSD_Environment aDirEnv ("CSF_ShadersDirectory");
46     THE_SHADERS_FOLDER = aDirEnv.Value();
47     if (THE_SHADERS_FOLDER.IsEmpty())
48     {
49       OSD_Environment aCasRootEnv ("CASROOT");
50       THE_SHADERS_FOLDER = aCasRootEnv.Value();
51       if (!THE_SHADERS_FOLDER.IsEmpty())
52       {
53         THE_SHADERS_FOLDER += "/src/Shaders";
54       }
55     }
56
57     if (THE_SHADERS_FOLDER.IsEmpty())
58     {
59       return THE_SHADERS_FOLDER;
60     }
61
62     const OSD_Path aDirPath (THE_SHADERS_FOLDER);
63     OSD_Directory aDir (aDirPath);
64     const TCollection_AsciiString aProgram = THE_SHADERS_FOLDER + "/Declarations.glsl";
65     OSD_File aProgramFile (aProgram);
66     if (!aDir.Exists()
67      || !aProgramFile.Exists())
68     {
69       std::cerr << "Standard GLSL programs are not found in: " << THE_SHADERS_FOLDER.ToCString() << std::endl;
70       throw Standard_Failure("CSF_ShadersDirectory or CASROOT is set incorrectly");
71     }
72   }
73   return THE_SHADERS_FOLDER;
74 }
75
76 // =======================================================================
77 // function : Graphic3d_ShaderProgram
78 // purpose  : Creates new empty program object
79 // =======================================================================
80 Graphic3d_ShaderProgram::Graphic3d_ShaderProgram()
81 : myNbLightsMax (THE_MAX_LIGHTS_DEFAULT),
82   myNbShadowMaps (0),
83   myNbClipPlanesMax (THE_MAX_CLIP_PLANES_DEFAULT),
84   myNbFragOutputs (THE_NB_FRAG_OUTPUTS),
85   myTextureSetBits (Graphic3d_TextureSetBits_NONE),
86   myOitOutput (Graphic3d_RTM_BLEND_UNORDERED),
87   myHasDefSampler (true),
88   myHasAlphaTest (false),
89   myIsPBR (false)
90 {
91   myID = TCollection_AsciiString ("Graphic3d_ShaderProgram_")
92        + TCollection_AsciiString (Standard_Atomic_Increment (&THE_PROGRAM_OBJECT_COUNTER));
93 }
94
95 // =======================================================================
96 // function : ~Graphic3d_ShaderProgram
97 // purpose  : Releases resources of program object
98 // =======================================================================
99 Graphic3d_ShaderProgram::~Graphic3d_ShaderProgram()
100 {
101   //
102 }
103
104 // =======================================================================
105 // function : IsDone
106 // purpose  : Checks if the program object is valid or not
107 // =======================================================================
108 Standard_Boolean Graphic3d_ShaderProgram::IsDone() const
109 {
110   if (myShaderObjects.IsEmpty())
111   {
112     return Standard_False;
113   }
114
115   for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
116   {
117     if (!anIt.Value()->IsDone())
118       return Standard_False;
119   }
120
121   return Standard_True;
122 }
123
124 // =======================================================================
125 // function : AttachShader
126 // purpose  : Attaches shader object to the program object
127 // =======================================================================
128 Standard_Boolean Graphic3d_ShaderProgram::AttachShader (const Handle(Graphic3d_ShaderObject)& theShader)
129 {
130   if (theShader.IsNull())
131   {
132     return Standard_False;
133   }
134
135   for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
136   {
137     if (anIt.Value() == theShader)
138       return Standard_False;
139   }
140
141   myShaderObjects.Append (theShader);
142   return Standard_True;
143 }
144
145 // =======================================================================
146 // function : DetachShader
147 // purpose  : Detaches shader object from the program object
148 // =======================================================================
149 Standard_Boolean Graphic3d_ShaderProgram::DetachShader (const Handle(Graphic3d_ShaderObject)& theShader)
150 {
151   if (theShader.IsNull())
152   {
153     return Standard_False;
154   }
155
156   for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
157   {
158     if (anIt.Value() == theShader)
159     {
160       myShaderObjects.Remove (anIt);
161       return Standard_True;
162     }
163   }
164   
165   return Standard_False;
166 }
167
168 // =======================================================================
169 // function : ClearVariables
170 // purpose  : Removes all custom uniform variables from the program
171 // =======================================================================
172 void Graphic3d_ShaderProgram::ClearVariables()
173 {
174   myVariables.Clear();
175 }
176
177 // =======================================================================
178 // function : SetAttributes
179 // purpose  :
180 // =======================================================================
181 void Graphic3d_ShaderProgram::SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes)
182 {
183   myAttributes = theAttributes;
184 }