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 <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>
27 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_ShaderProgram,Standard_Transient)
31 static volatile Standard_Integer THE_PROGRAM_OBJECT_COUNTER = 0;
34 // =======================================================================
35 // function : ShadersFolder
37 // =======================================================================
38 const TCollection_AsciiString& Graphic3d_ShaderProgram::ShadersFolder()
40 static Standard_Boolean THE_IS_DEFINED = Standard_False;
41 static TCollection_AsciiString THE_SHADERS_FOLDER;
44 THE_IS_DEFINED = Standard_True;
45 OSD_Environment aDirEnv ("CSF_ShadersDirectory");
46 THE_SHADERS_FOLDER = aDirEnv.Value();
47 if (THE_SHADERS_FOLDER.IsEmpty())
49 OSD_Environment aCasRootEnv ("CASROOT");
50 THE_SHADERS_FOLDER = aCasRootEnv.Value();
51 if (!THE_SHADERS_FOLDER.IsEmpty())
53 THE_SHADERS_FOLDER += "/src/Shaders";
57 if (THE_SHADERS_FOLDER.IsEmpty())
59 return THE_SHADERS_FOLDER;
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);
67 || !aProgramFile.Exists())
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");
73 return THE_SHADERS_FOLDER;
76 // =======================================================================
77 // function : Graphic3d_ShaderProgram
78 // purpose : Creates new empty program object
79 // =======================================================================
80 Graphic3d_ShaderProgram::Graphic3d_ShaderProgram()
81 : myNbLightsMax (THE_MAX_LIGHTS_DEFAULT),
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),
91 myID = TCollection_AsciiString ("Graphic3d_ShaderProgram_")
92 + TCollection_AsciiString (Standard_Atomic_Increment (&THE_PROGRAM_OBJECT_COUNTER));
95 // =======================================================================
96 // function : ~Graphic3d_ShaderProgram
97 // purpose : Releases resources of program object
98 // =======================================================================
99 Graphic3d_ShaderProgram::~Graphic3d_ShaderProgram()
104 // =======================================================================
106 // purpose : Checks if the program object is valid or not
107 // =======================================================================
108 Standard_Boolean Graphic3d_ShaderProgram::IsDone() const
110 if (myShaderObjects.IsEmpty())
112 return Standard_False;
115 for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
117 if (!anIt.Value()->IsDone())
118 return Standard_False;
121 return Standard_True;
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)
130 if (theShader.IsNull())
132 return Standard_False;
135 for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
137 if (anIt.Value() == theShader)
138 return Standard_False;
141 myShaderObjects.Append (theShader);
142 return Standard_True;
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)
151 if (theShader.IsNull())
153 return Standard_False;
156 for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next())
158 if (anIt.Value() == theShader)
160 myShaderObjects.Remove (anIt);
161 return Standard_True;
165 return Standard_False;
168 // =======================================================================
169 // function : ClearVariables
170 // purpose : Removes all custom uniform variables from the program
171 // =======================================================================
172 void Graphic3d_ShaderProgram::ClearVariables()
177 // =======================================================================
178 // function : SetAttributes
180 // =======================================================================
181 void Graphic3d_ShaderProgram::SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes)
183 myAttributes = theAttributes;