0024295: vclose behavior is inconsistent
[occt.git] / src / OpenGl / OpenGl_ShaderObject.cxx
CommitLineData
30f0ad28 1// Created on: 2013-09-19
2// Created by: Denis BOGOLEPOV
3// Copyright (c) 2013 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
20#include <Graphic3d_ShaderObject.hxx>
21#include <OpenGl_Context.hxx>
22#include <OpenGl_ShaderObject.hxx>
23#include <OSD_Path.hxx>
24#include <Standard_Assert.hxx>
25#include <TCollection_AsciiString.hxx>
26
27IMPLEMENT_STANDARD_HANDLE (OpenGl_ShaderObject, OpenGl_Resource)
28IMPLEMENT_STANDARD_RTTIEXT(OpenGl_ShaderObject, OpenGl_Resource)
29
30// =======================================================================
31// function : OpenGl_ShaderObject
32// purpose : Creates uninitialized shader object
33// =======================================================================
34OpenGl_ShaderObject::OpenGl_ShaderObject (GLenum theType)
35: myType (theType),
36 myShaderID (NO_SHADER)
37{
38 //
39}
40
41// =======================================================================
42// function : ~OpenGl_ShaderObject
43// purpose : Releases resources of shader object
44// =======================================================================
45OpenGl_ShaderObject::~OpenGl_ShaderObject()
46{
47 Release (NULL);
48}
49
50// =======================================================================
51// function : LoadSource
52// purpose : Loads shader source code
53// =======================================================================
54Standard_Boolean OpenGl_ShaderObject::LoadSource (const Handle(OpenGl_Context)& theCtx,
55 const TCollection_AsciiString& theSource)
56{
57 if (myShaderID == NO_SHADER)
58 {
59 return Standard_False;
60 }
61
62 const GLchar* aLines = theSource.ToCString();
63 theCtx->core20->glShaderSource (myShaderID, 1, &aLines, NULL);
64 return Standard_True;
65}
66
67// =======================================================================
68// function : Compile
69// purpose : Compiles the shader object
70// =======================================================================
71Standard_Boolean OpenGl_ShaderObject::Compile (const Handle(OpenGl_Context)& theCtx)
72{
73 if (myShaderID == NO_SHADER)
74 {
75 return Standard_False;
76 }
77
78 // Try to compile shader
79 theCtx->core20->glCompileShader (myShaderID);
80
81 // Check compile status
82 GLint aStatus = GL_FALSE;
83 theCtx->core20->glGetShaderiv (myShaderID, GL_COMPILE_STATUS, &aStatus);
84 return aStatus != GL_FALSE;
85}
86
87// =======================================================================
88// function : FetchInfoLog
89// purpose : Fetches information log of the last compile operation
90// =======================================================================
91Standard_Boolean OpenGl_ShaderObject::FetchInfoLog (const Handle(OpenGl_Context)& theCtx,
92 TCollection_AsciiString& theLog)
93{
94 if (myShaderID == NO_SHADER)
95 {
96 return Standard_False;
97 }
98
99 // Load information log of the compiler
100 GLint aLength = 0;
101 theCtx->core20->glGetShaderiv (myShaderID, GL_INFO_LOG_LENGTH, &aLength);
102 if (aLength > 0)
103 {
104 GLchar* aLog = (GLchar*) alloca (aLength);
105 memset (aLog, 0, aLength);
106 theCtx->core20->glGetShaderInfoLog (myShaderID, aLength, NULL, aLog);
107 theLog = aLog;
108 }
109
110 return Standard_True;
111}
112
113// =======================================================================
114// function : Create
115// purpose : Creates new empty shader object of specified type
116// =======================================================================
117Standard_Boolean OpenGl_ShaderObject::Create (const Handle(OpenGl_Context)& theCtx)
118{
119 if (myShaderID == NO_SHADER
120 && theCtx->core20 != NULL)
121 {
122 myShaderID = theCtx->core20->glCreateShader (myType);
123 }
124
125 return myShaderID != NO_SHADER;
126}
127
128// =======================================================================
129// function : Release
130// purpose : Destroys shader object
131// =======================================================================
132void OpenGl_ShaderObject::Release (const OpenGl_Context* theCtx)
133{
134 if (myShaderID == NO_SHADER)
135 {
136 return;
137 }
138
139 Standard_ASSERT_RETURN (theCtx != NULL,
140 "OpenGl_ShaderObject destroyed without GL context! Possible GPU memory leakage...",);
141
142 if (theCtx->core20 != NULL)
143 {
144 theCtx->core20->glDeleteShader (myShaderID);
145 }
146 myShaderID = NO_SHADER;
147}