Home | History | Annotate | Download | only in libGLESv2
      1 //
      2 // Copyright (c) 2002-2010 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 // Program.h: Defines the gl::Program class. Implements GL program objects
      8 // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
      9 
     10 #ifndef LIBGLESV2_PROGRAM_H_
     11 #define LIBGLESV2_PROGRAM_H_
     12 
     13 #include <d3dx9.h>
     14 #include <string>
     15 #include <vector>
     16 #include <set>
     17 
     18 #include "libGLESv2/Shader.h"
     19 #include "libGLESv2/Context.h"
     20 
     21 namespace gl
     22 {
     23 class ResourceManager;
     24 class FragmentShader;
     25 class VertexShader;
     26 
     27 // Helper struct representing a single shader uniform
     28 struct Uniform
     29 {
     30     Uniform(GLenum type, const std::string &name, unsigned int arraySize);
     31 
     32     ~Uniform();
     33 
     34     const GLenum type;
     35     const std::string name;
     36     const unsigned int arraySize;
     37 
     38     unsigned char *data;
     39     bool dirty;
     40 
     41     D3DXHANDLE vsHandle;
     42     D3DXHANDLE psHandle;
     43     bool handlesSet;
     44 };
     45 
     46 // Struct used for correlating uniforms/elements of uniform arrays to handles
     47 struct UniformLocation
     48 {
     49     UniformLocation(const std::string &name, unsigned int element, unsigned int index);
     50 
     51     std::string name;
     52     unsigned int element;
     53     unsigned int index;
     54 };
     55 
     56 class Program
     57 {
     58   public:
     59     Program(ResourceManager *manager, GLuint handle);
     60 
     61     ~Program();
     62 
     63     bool attachShader(Shader *shader);
     64     bool detachShader(Shader *shader);
     65     int getAttachedShadersCount() const;
     66 
     67     IDirect3DPixelShader9 *getPixelShader();
     68     IDirect3DVertexShader9 *getVertexShader();
     69 
     70     void bindAttributeLocation(GLuint index, const char *name);
     71     GLuint getAttributeLocation(const char *name);
     72     int getSemanticIndex(int attributeIndex);
     73 
     74     void dirtyAllSamplers();
     75 
     76     GLint getSamplerMapping(unsigned int samplerIndex);
     77     SamplerType getSamplerType(unsigned int samplerIndex);
     78     bool isSamplerDirty(unsigned int samplerIndex) const;
     79     void setSamplerDirty(unsigned int samplerIndex, bool dirty);
     80 
     81     GLint getUniformLocation(const char *name, bool decorated);
     82     bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
     83     bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
     84     bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
     85     bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
     86     bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
     87     bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
     88     bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
     89     bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
     90     bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
     91     bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
     92     bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
     93 
     94     bool getUniformfv(GLint location, GLfloat *params);
     95     bool getUniformiv(GLint location, GLint *params);
     96 
     97     GLint getDxDepthRangeLocation() const;
     98     GLint getDxDepthLocation() const;
     99     GLint getDxViewportLocation() const;
    100     GLint getDxHalfPixelSizeLocation() const;
    101     GLint getDxFrontCCWLocation() const;
    102     GLint getDxPointsOrLinesLocation() const;
    103 
    104     void dirtyAllUniforms();
    105     void applyUniforms();
    106 
    107     void link();
    108     bool isLinked();
    109     int getInfoLogLength() const;
    110     void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
    111     void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
    112 
    113     void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
    114     GLint getActiveAttributeCount();
    115     GLint getActiveAttributeMaxLength();
    116 
    117     void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
    118     GLint getActiveUniformCount();
    119     GLint getActiveUniformMaxLength();
    120 
    121     void addRef();
    122     void release();
    123     unsigned int getRefCount() const;
    124     void flagForDeletion();
    125     bool isFlaggedForDeletion() const;
    126 
    127     void validate();
    128     bool validateSamplers() const;
    129     bool isValidated() const;
    130 
    131     unsigned int getSerial() const;
    132 
    133   private:
    134     DISALLOW_COPY_AND_ASSIGN(Program);
    135 
    136     ID3DXBuffer *compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable);
    137     void unlink(bool destroy = false);
    138 
    139     int packVaryings(const Varying *packing[][4]);
    140     bool linkVaryings();
    141 
    142     bool linkAttributes();
    143     int getAttributeBinding(const std::string &name);
    144 
    145     bool linkUniforms(ID3DXConstantTable *constantTable);
    146     bool defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name = "");
    147     bool defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name);
    148     Uniform *createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name);
    149     bool applyUniform1bv(GLint location, GLsizei count, const GLboolean *v);
    150     bool applyUniform2bv(GLint location, GLsizei count, const GLboolean *v);
    151     bool applyUniform3bv(GLint location, GLsizei count, const GLboolean *v);
    152     bool applyUniform4bv(GLint location, GLsizei count, const GLboolean *v);
    153     bool applyUniform1fv(GLint location, GLsizei count, const GLfloat *v);
    154     bool applyUniform2fv(GLint location, GLsizei count, const GLfloat *v);
    155     bool applyUniform3fv(GLint location, GLsizei count, const GLfloat *v);
    156     bool applyUniform4fv(GLint location, GLsizei count, const GLfloat *v);
    157     bool applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
    158     bool applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
    159     bool applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
    160     bool applyUniform1iv(GLint location, GLsizei count, const GLint *v);
    161     bool applyUniform2iv(GLint location, GLsizei count, const GLint *v);
    162     bool applyUniform3iv(GLint location, GLsizei count, const GLint *v);
    163     bool applyUniform4iv(GLint location, GLsizei count, const GLint *v);
    164 
    165     void getConstantHandles(Uniform *targetUniform, D3DXHANDLE *constantPS, D3DXHANDLE *constantVS);
    166 
    167     void appendToInfoLog(const char *info, ...);
    168     void resetInfoLog();
    169 
    170     static std::string decorate(const std::string &string);     // Prepend an underscore
    171     static std::string undecorate(const std::string &string);   // Remove leading underscore
    172 
    173     static unsigned int issueSerial();
    174 
    175     FragmentShader *mFragmentShader;
    176     VertexShader *mVertexShader;
    177 
    178     std::string mPixelHLSL;
    179     std::string mVertexHLSL;
    180 
    181     IDirect3DPixelShader9 *mPixelExecutable;
    182     IDirect3DVertexShader9 *mVertexExecutable;
    183     ID3DXConstantTable *mConstantTablePS;
    184     ID3DXConstantTable *mConstantTableVS;
    185 
    186     std::set<std::string> mAttributeBinding[MAX_VERTEX_ATTRIBS];
    187     Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
    188     int mSemanticIndex[MAX_VERTEX_ATTRIBS];
    189 
    190     struct Sampler
    191     {
    192         bool active;
    193         GLint logicalTextureUnit;
    194         SamplerType type;
    195         bool dirty;
    196     };
    197 
    198     Sampler mSamplers[MAX_TEXTURE_IMAGE_UNITS];
    199 
    200     typedef std::vector<Uniform*> UniformArray;
    201     UniformArray mUniforms;
    202     typedef std::vector<UniformLocation> UniformIndex;
    203     UniformIndex mUniformIndex;
    204 
    205     GLint mDxDepthRangeLocation;
    206     GLint mDxDepthLocation;
    207     GLint mDxViewportLocation;
    208     GLint mDxHalfPixelSizeLocation;
    209     GLint mDxFrontCCWLocation;
    210     GLint mDxPointsOrLinesLocation;
    211 
    212     bool mLinked;
    213     bool mDeleteStatus;   // Flag to indicate that the program can be deleted when no longer in use
    214     char *mInfoLog;
    215     bool mValidated;
    216 
    217     unsigned int mRefCount;
    218 
    219     const unsigned int mSerial;
    220 
    221     static unsigned int mCurrentSerial;
    222 
    223     ResourceManager *mResourceManager;
    224     const GLuint mHandle;
    225 };
    226 }
    227 
    228 #endif   // LIBGLESV2_PROGRAM_H_
    229