1//
2// Copyright (c) 2002-2013 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.h: Defines the abstract gl::Shader class and its concrete derived
8// classes VertexShader and FragmentShader. Implements GL shader objects and
9// related functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section
10// 3.8 page 84.
11
12#ifndef LIBGLESV2_SHADER_H_
13#define LIBGLESV2_SHADER_H_
14
15
16#include <string>
17#include <list>
18#include <vector>
19
20#include "angle_gl.h"
21#include <GLSLANG/ShaderLang.h>
22
23#include "common/angleutils.h"
24#include "libGLESv2/angletypes.h"
25
26namespace rx
27{
28class ShaderImpl;
29}
30
31namespace gl
32{
33class ResourceManager;
34
35struct PackedVarying : public sh::Varying
36{
37    unsigned int registerIndex; // Assigned during link
38    unsigned int columnIndex; // Assigned during link, defaults to 0
39
40    PackedVarying(const sh::Varying &varying)
41      : sh::Varying(varying),
42        registerIndex(GL_INVALID_INDEX),
43        columnIndex(0)
44    {}
45
46    bool registerAssigned() const { return registerIndex != GL_INVALID_INDEX; }
47    bool isBuiltIn() const { return name.compare(0, 3, "gl_") == 0; }
48
49    void resetRegisterAssignment()
50    {
51        registerIndex = GL_INVALID_INDEX;
52    }
53};
54
55class Shader
56{
57  public:
58    Shader(ResourceManager *manager, rx::ShaderImpl *impl, GLenum type, GLuint handle);
59
60    virtual ~Shader();
61
62    GLenum getType() const { return mType; }
63    GLuint getHandle() const;
64
65    rx::ShaderImpl *getImplementation() { return mShader; }
66    const rx::ShaderImpl *getImplementation() const { return mShader; }
67
68    void deleteSource();
69    void setSource(GLsizei count, const char *const *string, const GLint *length);
70    int getInfoLogLength() const;
71    void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) const;
72    int getSourceLength() const;
73    void getSource(GLsizei bufSize, GLsizei *length, char *buffer) const;
74    int getTranslatedSourceLength() const;
75    void getTranslatedSource(GLsizei bufSize, GLsizei *length, char *buffer) const;
76
77    void compile();
78    bool isCompiled() const { return mCompiled; }
79
80    void addRef();
81    void release();
82    unsigned int getRefCount() const;
83    bool isFlaggedForDeletion() const;
84    void flagForDeletion();
85
86    const std::vector<gl::PackedVarying> &getVaryings() const;
87    const std::vector<sh::Uniform> &getUniforms() const;
88    const std::vector<sh::InterfaceBlock> &getInterfaceBlocks() const;
89    const std::vector<sh::Attribute> &getActiveAttributes() const;
90    const std::vector<sh::Attribute> &getActiveOutputVariables() const;
91
92    std::vector<gl::PackedVarying> &getVaryings();
93    std::vector<sh::Uniform> &getUniforms();
94    std::vector<sh::InterfaceBlock> &getInterfaceBlocks();
95    std::vector<sh::Attribute> &getActiveAttributes();
96    std::vector<sh::Attribute> &getActiveOutputVariables();
97
98  private:
99    DISALLOW_COPY_AND_ASSIGN(Shader);
100
101    static void getSourceImpl(const std::string &source, GLsizei bufSize, GLsizei *length, char *buffer);
102
103    rx::ShaderImpl *mShader;
104    const GLuint mHandle;
105    const GLenum mType;
106    std::string mSource;
107    unsigned int mRefCount;     // Number of program objects this shader is attached to
108    bool mDeleteStatus;         // Flag to indicate that the shader can be deleted when no longer in use
109    bool mCompiled;             // Indicates if this shader has been successfully compiled
110
111    ResourceManager *mResourceManager;
112};
113
114}
115
116#endif   // LIBGLESV2_SHADER_H_
117