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.
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.
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"
17 #include "common/utilities.h"
19 #include "GLSLANG/ShaderLang.h"
26 Shader::Shader(ResourceManager *manager, rx::ShaderImpl *impl, GLenum type, GLuint handle)
30 mResourceManager(manager),
43 GLuint Shader::getHandle() const
48 void Shader::setSource(GLsizei count, const char *const *string, const GLint *length)
50 std::ostringstream stream;
52 for (int i = 0; i < count; i++)
57 mSource = stream.str();
60 int Shader::getInfoLogLength() const
62 return mShader->getInfoLog().empty() ? 0 : (mShader->getInfoLog().length() + 1);
65 void Shader::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const
71 index = std::min(bufSize - 1, static_cast<GLsizei>(mShader->getInfoLog().length()));
72 memcpy(infoLog, mShader->getInfoLog().c_str(), index);
74 infoLog[index] = '\0';
83 int Shader::getSourceLength() const
85 return mSource.empty() ? 0 : (mSource.length() + 1);
88 int Shader::getTranslatedSourceLength() const
90 return mShader->getTranslatedSource().empty() ? 0 : (mShader->getTranslatedSource().length() + 1);
93 void Shader::getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer)
99 index = std::min(bufSize - 1, static_cast<GLsizei>(source.length()));
100 memcpy(buffer, source.c_str(), index);
102 buffer[index] = '\0';
111 void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) const
113 getSourceImpl(mSource, bufSize, length, buffer);
116 void Shader::getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const
118 getSourceImpl(mShader->getTranslatedSource(), bufSize, length, buffer);
121 void Shader::getTranslatedSourceWithDebugInfo(GLsizei bufSize, GLsizei *length, char *buffer) const
123 std::string debugInfo(mShader->getDebugInfo());
124 getSourceImpl(debugInfo, bufSize, length, buffer);
127 void Shader::compile()
129 mCompiled = mShader->compile(mSource);
132 void Shader::addRef()
137 void Shader::release()
141 if (mRefCount == 0 && mDeleteStatus)
143 mResourceManager->deleteShader(mHandle);
147 unsigned int Shader::getRefCount() const
152 bool Shader::isFlaggedForDeletion() const
154 return mDeleteStatus;
157 void Shader::flagForDeletion()
159 mDeleteStatus = true;
162 const std::vector<gl::PackedVarying> &Shader::getVaryings() const
164 return mShader->getVaryings();
167 const std::vector<sh::Uniform> &Shader::getUniforms() const
169 return mShader->getUniforms();
172 const std::vector<sh::InterfaceBlock> &Shader::getInterfaceBlocks() const
174 return mShader->getInterfaceBlocks();
177 const std::vector<sh::Attribute> &Shader::getActiveAttributes() const
179 return mShader->getActiveAttributes();
182 const std::vector<sh::Attribute> &Shader::getActiveOutputVariables() const
184 return mShader->getActiveOutputVariables();
187 std::vector<gl::PackedVarying> &Shader::getVaryings()
189 return mShader->getVaryings();
192 std::vector<sh::Uniform> &Shader::getUniforms()
194 return mShader->getUniforms();
197 std::vector<sh::InterfaceBlock> &Shader::getInterfaceBlocks()
199 return mShader->getInterfaceBlocks();
202 std::vector<sh::Attribute> &Shader::getActiveAttributes()
204 return mShader->getActiveAttributes();
207 std::vector<sh::Attribute> &Shader::getActiveOutputVariables()
209 return mShader->getActiveOutputVariables();