Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / src / libGLESv2 / Shader.cpp
1 //
2 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // Shader.cpp: Implements the gl::Shader class and its  derived classes
8 // VertexShader and FragmentShader. Implements GL shader objects and related
9 // functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 3.8 page 84.
10
11 #include "libGLESv2/Shader.h"
12 #include "libGLESv2/renderer/Renderer.h"
13 #include "libGLESv2/renderer/ShaderImpl.h"
14 #include "libGLESv2/Constants.h"
15 #include "libGLESv2/ResourceManager.h"
16
17 #include "common/utilities.h"
18
19 #include "GLSLANG/ShaderLang.h"
20
21 #include <sstream>
22
23 namespace gl
24 {
25
26 Shader::Shader(ResourceManager *manager, rx::ShaderImpl *impl, GLenum type, GLuint handle)
27     : mShader(impl),
28       mType(type),
29       mHandle(handle),
30       mResourceManager(manager),
31       mRefCount(0),
32       mDeleteStatus(false),
33       mCompiled(false)
34 {
35     ASSERT(impl);
36 }
37
38 Shader::~Shader()
39 {
40     SafeDelete(mShader);
41 }
42
43 GLuint Shader::getHandle() const
44 {
45     return mHandle;
46 }
47
48 void Shader::setSource(GLsizei count, const char *const *string, const GLint *length)
49 {
50     std::ostringstream stream;
51
52     for (int i = 0; i < count; i++)
53     {
54         stream << string[i];
55     }
56
57     mSource = stream.str();
58 }
59
60 int Shader::getInfoLogLength() const
61 {
62     return  mShader->getInfoLog().empty() ? 0 : (mShader->getInfoLog().length() + 1);
63 }
64
65 void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
66 {
67     int index = 0;
68
69     if (bufSize > 0)
70     {
71         index = std::min(bufSize - 1, static_cast<GLsizei>(mShader->getInfoLog().length()));
72         memcpy(infoLog, mShader->getInfoLog().c_str(), index);
73
74         infoLog[index] = '\0';
75     }
76
77     if (length)
78     {
79         *length = index;
80     }
81 }
82
83 int Shader::getSourceLength() const
84 {
85     return mSource.empty() ? 0 : (mSource.length() + 1);
86 }
87
88 int Shader::getTranslatedSourceLength() const
89 {
90     return mShader->getTranslatedSource().empty() ? 0 : (mShader->getTranslatedSource().length() + 1);
91 }
92
93 void Shader::getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer)
94 {
95     int index = 0;
96
97     if (bufSize > 0)
98     {
99         index = std::min(bufSize - 1, static_cast<GLsizei>(source.length()));
100         memcpy(buffer, source.c_str(), index);
101
102         buffer[index] = '\0';
103     }
104
105     if (length)
106     {
107         *length = index;
108     }
109 }
110
111 void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) const
112 {
113     getSourceImpl(mSource, bufSize, length, buffer);
114 }
115
116 void Shader::getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const
117 {
118     getSourceImpl(mShader->getTranslatedSource(), bufSize, length, buffer);
119 }
120
121 void Shader::getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer) const
122 {
123     std::string debugInfo(mShader->getDebugInfo());
124     getSourceImpl(debugInfo, bufSize, length, buffer);
125 }
126
127 void Shader::compile()
128 {
129     mCompiled = mShader->compile(mSource);
130 }
131
132 void Shader::addRef()
133 {
134     mRefCount++;
135 }
136
137 void Shader::release()
138 {
139     mRefCount--;
140
141     if (mRefCount == 0 && mDeleteStatus)
142     {
143         mResourceManager->deleteShader(mHandle);
144     }
145 }
146
147 unsigned int Shader::getRefCount() const
148 {
149     return mRefCount;
150 }
151
152 bool Shader::isFlaggedForDeletion() const
153 {
154     return mDeleteStatus;
155 }
156
157 void Shader::flagForDeletion()
158 {
159     mDeleteStatus = true;
160 }
161
162 const std::vector<gl::PackedVarying> &Shader::getVaryings() const
163 {
164     return mShader->getVaryings();
165 }
166
167 const std::vector<sh::Uniform> &Shader::getUniforms() const
168 {
169     return mShader->getUniforms();
170 }
171
172 const std::vector<sh::InterfaceBlock> &Shader::getInterfaceBlocks() const
173 {
174     return mShader->getInterfaceBlocks();
175 }
176
177 const std::vector<sh::Attribute> &Shader::getActiveAttributes() const
178 {
179     return mShader->getActiveAttributes();
180 }
181
182 const std::vector<sh::Attribute> &Shader::getActiveOutputVariables() const
183 {
184     return mShader->getActiveOutputVariables();
185 }
186
187 std::vector<gl::PackedVarying> &Shader::getVaryings()
188 {
189     return mShader->getVaryings();
190 }
191
192 std::vector<sh::Uniform> &Shader::getUniforms()
193 {
194     return mShader->getUniforms();
195 }
196
197 std::vector<sh::InterfaceBlock> &Shader::getInterfaceBlocks()
198 {
199     return mShader->getInterfaceBlocks();
200 }
201
202 std::vector<sh::Attribute> &Shader::getActiveAttributes()
203 {
204     return mShader->getActiveAttributes();
205 }
206
207 std::vector<sh::Attribute> &Shader::getActiveOutputVariables()
208 {
209     return mShader->getActiveOutputVariables();
210 }
211
212 }