1 // Created on: 2013-09-20
2 // Created by: Denis BOGOLEPOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Graphic3d_ShaderProgram.hxx>
18 #include <Graphic3d_GraphicDriver.hxx>
19 #include <Graphic3d_ShaderObject.hxx>
20 #include <OSD_Directory.hxx>
21 #include <OSD_Environment.hxx>
22 #include <OSD_File.hxx>
23 #include <OSD_Path.hxx>
24 #include <Standard_Atomic.hxx>
26 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_ShaderProgram,Standard_Transient)
30 static volatile Standard_Integer THE_PROGRAM_OBJECT_COUNTER = 0;
33 // =======================================================================
34 // function : ShadersFolder
36 // =======================================================================
37 const TCollection_AsciiString& Graphic3d_ShaderProgram::ShadersFolder()
39 static Standard_Boolean THE_IS_DEFINED = Standard_False;
40 static TCollection_AsciiString THE_SHADERS_FOLDER;
43 THE_IS_DEFINED = Standard_True;
44 OSD_Environment aDirEnv ("CSF_ShadersDirectory");
45 THE_SHADERS_FOLDER = aDirEnv.Value();
46 if (THE_SHADERS_FOLDER.IsEmpty())
48 OSD_Environment aCasRootEnv ("CASROOT");
49 THE_SHADERS_FOLDER = aCasRootEnv.Value();
50 if (!THE_SHADERS_FOLDER.IsEmpty())
52 THE_SHADERS_FOLDER += "/src/Shaders";
56 if (THE_SHADERS_FOLDER.IsEmpty())
58 return THE_SHADERS_FOLDER;
61 const OSD_Path aDirPath (THE_SHADERS_FOLDER);
62 OSD_Directory aDir (aDirPath);
63 const TCollection_AsciiString aProgram = THE_SHADERS_FOLDER + "/Declarations.glsl";
64 OSD_File aProgramFile (aProgram);
66 || !aProgramFile.Exists())
68 std::cerr << "Standard GLSL programs are not found in: " << THE_SHADERS_FOLDER.ToCString() << std::endl;
69 throw Standard_Failure("CSF_ShadersDirectory or CASROOT is set incorrectly");
72 return THE_SHADERS_FOLDER;
75 // =======================================================================
76 // function : Graphic3d_ShaderProgram
77 // purpose : Creates new empty program object
78 // =======================================================================
79 Graphic3d_ShaderProgram::Graphic3d_ShaderProgram()
80 : myNbLightsMax (THE_MAX_LIGHTS_DEFAULT),
81 myNbClipPlanesMax (THE_MAX_CLIP_PLANES_DEFAULT),
82 myNbFragOutputs (THE_NB_FRAG_OUTPUTS),
83 myTextureSetBits (Graphic3d_TextureSetBits_NONE),
84 myHasDefSampler (true),
85 myHasAlphaTest (false),
86 myHasWeightOitOutput (false),
89 myID = TCollection_AsciiString ("Graphic3d_ShaderProgram_")
90 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_PROGRAM_OBJECT_COUNTER));
93 // =======================================================================
94 // function : ~Graphic3d_ShaderProgram
95 // purpose : Releases resources of program object
96 // =======================================================================
97 Graphic3d_ShaderProgram::~Graphic3d_ShaderProgram()
102 // =======================================================================
104 // purpose : Checks if the program object is valid or not
105 // =======================================================================
106 Standard_Boolean Graphic3d_ShaderProgram::IsDone() const
108 if (myShaderObjects.IsEmpty())
110 return Standard_False;
113 for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
115 if (!anIt.Value()->IsDone())
116 return Standard_False;
119 return Standard_True;
122 // =======================================================================
123 // function : AttachShader
124 // purpose : Attaches shader object to the program object
125 // =======================================================================
126 Standard_Boolean Graphic3d_ShaderProgram::AttachShader (const Handle(Graphic3d_ShaderObject)& theShader)
128 if (theShader.IsNull())
130 return Standard_False;
133 for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
135 if (anIt.Value() == theShader)
136 return Standard_False;
139 myShaderObjects.Append (theShader);
140 return Standard_True;
143 // =======================================================================
144 // function : DetachShader
145 // purpose : Detaches shader object from the program object
146 // =======================================================================
147 Standard_Boolean Graphic3d_ShaderProgram::DetachShader (const Handle(Graphic3d_ShaderObject)& theShader)
149 if (theShader.IsNull())
151 return Standard_False;
154 for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
156 if (anIt.Value() == theShader)
158 myShaderObjects.Remove (anIt);
159 return Standard_True;
163 return Standard_False;
166 // =======================================================================
167 // function : ClearVariables
168 // purpose : Removes all custom uniform variables from the program
169 // =======================================================================
170 void Graphic3d_ShaderProgram::ClearVariables()
175 // =======================================================================
176 // function : SetAttributes
178 // =======================================================================
179 void Graphic3d_ShaderProgram::SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes)
181 myAttributes = theAttributes;