30f0ad28 |
1 | // Created on: 2013-09-19 |
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 | |
16 | #include <Graphic3d_ShaderObject.hxx> |
17 | #include <OpenGl_Context.hxx> |
18 | #include <OpenGl_ShaderObject.hxx> |
19 | #include <OSD_Path.hxx> |
20 | #include <Standard_Assert.hxx> |
21 | #include <TCollection_AsciiString.hxx> |
22 | |
30f0ad28 |
23 | |
24 | // ======================================================================= |
25 | // function : OpenGl_ShaderObject |
26 | // purpose : Creates uninitialized shader object |
27 | // ======================================================================= |
28 | OpenGl_ShaderObject::OpenGl_ShaderObject (GLenum theType) |
29 | : myType (theType), |
30 | myShaderID (NO_SHADER) |
31 | { |
32 | // |
33 | } |
34 | |
35 | // ======================================================================= |
36 | // function : ~OpenGl_ShaderObject |
37 | // purpose : Releases resources of shader object |
38 | // ======================================================================= |
39 | OpenGl_ShaderObject::~OpenGl_ShaderObject() |
40 | { |
41 | Release (NULL); |
42 | } |
43 | |
44 | // ======================================================================= |
45 | // function : LoadSource |
46 | // purpose : Loads shader source code |
47 | // ======================================================================= |
48 | Standard_Boolean OpenGl_ShaderObject::LoadSource (const Handle(OpenGl_Context)& theCtx, |
49 | const TCollection_AsciiString& theSource) |
50 | { |
51 | if (myShaderID == NO_SHADER) |
52 | { |
53 | return Standard_False; |
54 | } |
55 | |
56 | const GLchar* aLines = theSource.ToCString(); |
4e1523ef |
57 | theCtx->core20fwd->glShaderSource (myShaderID, 1, &aLines, NULL); |
30f0ad28 |
58 | return Standard_True; |
59 | } |
60 | |
61 | // ======================================================================= |
62 | // function : Compile |
63 | // purpose : Compiles the shader object |
64 | // ======================================================================= |
65 | Standard_Boolean OpenGl_ShaderObject::Compile (const Handle(OpenGl_Context)& theCtx) |
66 | { |
67 | if (myShaderID == NO_SHADER) |
68 | { |
69 | return Standard_False; |
70 | } |
71 | |
72 | // Try to compile shader |
4e1523ef |
73 | theCtx->core20fwd->glCompileShader (myShaderID); |
30f0ad28 |
74 | |
75 | // Check compile status |
76 | GLint aStatus = GL_FALSE; |
4e1523ef |
77 | theCtx->core20fwd->glGetShaderiv (myShaderID, GL_COMPILE_STATUS, &aStatus); |
30f0ad28 |
78 | return aStatus != GL_FALSE; |
79 | } |
80 | |
81 | // ======================================================================= |
82 | // function : FetchInfoLog |
83 | // purpose : Fetches information log of the last compile operation |
84 | // ======================================================================= |
85 | Standard_Boolean OpenGl_ShaderObject::FetchInfoLog (const Handle(OpenGl_Context)& theCtx, |
86 | TCollection_AsciiString& theLog) |
87 | { |
88 | if (myShaderID == NO_SHADER) |
89 | { |
90 | return Standard_False; |
91 | } |
92 | |
93 | // Load information log of the compiler |
94 | GLint aLength = 0; |
4e1523ef |
95 | theCtx->core20fwd->glGetShaderiv (myShaderID, GL_INFO_LOG_LENGTH, &aLength); |
30f0ad28 |
96 | if (aLength > 0) |
97 | { |
98 | GLchar* aLog = (GLchar*) alloca (aLength); |
99 | memset (aLog, 0, aLength); |
4e1523ef |
100 | theCtx->core20fwd->glGetShaderInfoLog (myShaderID, aLength, NULL, aLog); |
30f0ad28 |
101 | theLog = aLog; |
102 | } |
103 | |
104 | return Standard_True; |
105 | } |
106 | |
107 | // ======================================================================= |
108 | // function : Create |
109 | // purpose : Creates new empty shader object of specified type |
110 | // ======================================================================= |
111 | Standard_Boolean OpenGl_ShaderObject::Create (const Handle(OpenGl_Context)& theCtx) |
112 | { |
113 | if (myShaderID == NO_SHADER |
4e1523ef |
114 | && theCtx->core20fwd != NULL) |
30f0ad28 |
115 | { |
4e1523ef |
116 | myShaderID = theCtx->core20fwd->glCreateShader (myType); |
30f0ad28 |
117 | } |
118 | |
119 | return myShaderID != NO_SHADER; |
120 | } |
121 | |
122 | // ======================================================================= |
123 | // function : Release |
124 | // purpose : Destroys shader object |
125 | // ======================================================================= |
10b9c7df |
126 | void OpenGl_ShaderObject::Release (OpenGl_Context* theCtx) |
30f0ad28 |
127 | { |
128 | if (myShaderID == NO_SHADER) |
129 | { |
130 | return; |
131 | } |
132 | |
133 | Standard_ASSERT_RETURN (theCtx != NULL, |
134 | "OpenGl_ShaderObject destroyed without GL context! Possible GPU memory leakage...",); |
135 | |
4e1523ef |
136 | if (theCtx->core20fwd != NULL |
ec2eeb2d |
137 | && theCtx->IsValid()) |
30f0ad28 |
138 | { |
4e1523ef |
139 | theCtx->core20fwd->glDeleteShader (myShaderID); |
30f0ad28 |
140 | } |
141 | myShaderID = NO_SHADER; |
142 | } |