30f0ad28 |
1 | // Created on: 2013-09-20 |
2 | // Created by: Denis BOGOLEPOV |
d5f74e42 |
3 | // Copyright (c) 2013-2014 OPEN CASCADE SAS |
30f0ad28 |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
30f0ad28 |
6 | // |
d5f74e42 |
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 |
973c2be1 |
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. |
30f0ad28 |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
30f0ad28 |
15 | |
ee5befae |
16 | #include <Graphic3d_ShaderProgram.hxx> |
30f0ad28 |
17 | |
18 | #include <Graphic3d_GraphicDriver.hxx> |
19 | #include <Graphic3d_ShaderObject.hxx> |
392ac980 |
20 | #include <OSD_Directory.hxx> |
21 | #include <OSD_Environment.hxx> |
22 | #include <OSD_File.hxx> |
23 | #include <OSD_Path.hxx> |
ee5befae |
24 | #include <Standard_Atomic.hxx> |
30f0ad28 |
25 | |
92efcf78 |
26 | IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_ShaderProgram,Standard_Transient) |
27 | |
30f0ad28 |
28 | namespace |
29 | { |
30 | static volatile Standard_Integer THE_PROGRAM_OBJECT_COUNTER = 0; |
a3f6f591 |
31 | } |
30f0ad28 |
32 | |
392ac980 |
33 | // ======================================================================= |
34 | // function : ShadersFolder |
35 | // purpose : |
36 | // ======================================================================= |
37 | const TCollection_AsciiString& Graphic3d_ShaderProgram::ShadersFolder() |
38 | { |
39 | static Standard_Boolean THE_IS_DEFINED = Standard_False; |
40 | static TCollection_AsciiString THE_SHADERS_FOLDER; |
41 | if (!THE_IS_DEFINED) |
42 | { |
43 | THE_IS_DEFINED = Standard_True; |
44 | OSD_Environment aDirEnv ("CSF_ShadersDirectory"); |
45 | THE_SHADERS_FOLDER = aDirEnv.Value(); |
46 | if (THE_SHADERS_FOLDER.IsEmpty()) |
47 | { |
48 | OSD_Environment aCasRootEnv ("CASROOT"); |
49 | THE_SHADERS_FOLDER = aCasRootEnv.Value(); |
50 | if (!THE_SHADERS_FOLDER.IsEmpty()) |
51 | { |
52 | THE_SHADERS_FOLDER += "/src/Shaders"; |
53 | } |
54 | } |
55 | |
56 | if (THE_SHADERS_FOLDER.IsEmpty()) |
57 | { |
392ac980 |
58 | return THE_SHADERS_FOLDER; |
59 | } |
60 | |
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); |
65 | if (!aDir.Exists() |
66 | || !aProgramFile.Exists()) |
67 | { |
68 | std::cerr << "Standard GLSL programs are not found in: " << THE_SHADERS_FOLDER.ToCString() << std::endl; |
9775fa61 |
69 | throw Standard_Failure("CSF_ShadersDirectory or CASROOT is set incorrectly"); |
392ac980 |
70 | } |
71 | } |
72 | return THE_SHADERS_FOLDER; |
73 | } |
74 | |
30f0ad28 |
75 | // ======================================================================= |
76 | // function : Graphic3d_ShaderProgram |
77 | // purpose : Creates new empty program object |
78 | // ======================================================================= |
79 | Graphic3d_ShaderProgram::Graphic3d_ShaderProgram() |
daf73ab7 |
80 | : myNbLightsMax (THE_MAX_LIGHTS_DEFAULT), |
b17e5bae |
81 | myNbClipPlanesMax (THE_MAX_CLIP_PLANES_DEFAULT), |
82 | myNbFragOutputs (THE_NB_FRAG_OUTPUTS), |
c40eb6b9 |
83 | myHasAlphaTest (false), |
b17e5bae |
84 | myHasWeightOitOutput (false) |
30f0ad28 |
85 | { |
86 | myID = TCollection_AsciiString ("Graphic3d_ShaderProgram_") |
87 | + TCollection_AsciiString (Standard_Atomic_Increment (&THE_PROGRAM_OBJECT_COUNTER)); |
88 | } |
89 | |
90 | // ======================================================================= |
91 | // function : ~Graphic3d_ShaderProgram |
92 | // purpose : Releases resources of program object |
93 | // ======================================================================= |
94 | Graphic3d_ShaderProgram::~Graphic3d_ShaderProgram() |
95 | { |
ee5befae |
96 | // |
30f0ad28 |
97 | } |
98 | |
30f0ad28 |
99 | // ======================================================================= |
100 | // function : IsDone |
101 | // purpose : Checks if the program object is valid or not |
102 | // ======================================================================= |
103 | Standard_Boolean Graphic3d_ShaderProgram::IsDone() const |
104 | { |
105 | if (myShaderObjects.IsEmpty()) |
106 | { |
107 | return Standard_False; |
108 | } |
109 | |
110 | for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next()) |
111 | { |
112 | if (!anIt.Value()->IsDone()) |
113 | return Standard_False; |
114 | } |
115 | |
116 | return Standard_True; |
117 | } |
118 | |
119 | // ======================================================================= |
120 | // function : AttachShader |
121 | // purpose : Attaches shader object to the program object |
122 | // ======================================================================= |
123 | Standard_Boolean Graphic3d_ShaderProgram::AttachShader (const Handle(Graphic3d_ShaderObject)& theShader) |
124 | { |
125 | if (theShader.IsNull()) |
126 | { |
127 | return Standard_False; |
128 | } |
129 | |
130 | for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next()) |
131 | { |
132 | if (anIt.Value() == theShader) |
133 | return Standard_False; |
134 | } |
135 | |
136 | myShaderObjects.Append (theShader); |
137 | return Standard_True; |
138 | } |
139 | |
140 | // ======================================================================= |
141 | // function : DetachShader |
142 | // purpose : Detaches shader object from the program object |
143 | // ======================================================================= |
144 | Standard_Boolean Graphic3d_ShaderProgram::DetachShader (const Handle(Graphic3d_ShaderObject)& theShader) |
145 | { |
146 | if (theShader.IsNull()) |
147 | { |
148 | return Standard_False; |
149 | } |
150 | |
151 | for (Graphic3d_ShaderObjectList::Iterator anIt (myShaderObjects); anIt.More(); anIt.Next()) |
152 | { |
153 | if (anIt.Value() == theShader) |
154 | { |
155 | myShaderObjects.Remove (anIt); |
156 | return Standard_True; |
157 | } |
158 | } |
159 | |
160 | return Standard_False; |
161 | } |
162 | |
163 | // ======================================================================= |
164 | // function : ClearVariables |
165 | // purpose : Removes all custom uniform variables from the program |
166 | // ======================================================================= |
167 | void Graphic3d_ShaderProgram::ClearVariables() |
168 | { |
169 | myVariables.Clear(); |
170 | } |
4a535d3f |
171 | |
172 | // ======================================================================= |
173 | // function : SetAttributes |
174 | // purpose : |
175 | // ======================================================================= |
176 | void Graphic3d_ShaderProgram::SetVertexAttributes (const Graphic3d_ShaderAttributeList& theAttributes) |
177 | { |
178 | myAttributes = theAttributes; |
179 | } |