daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 13 | #include <d3dx9.h> |
| 14 | #include <string> |
| 15 | #include <vector> |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame^] | 16 | #include <set> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 17 | |
daniel@transgaming.com | e684229 | 2010-04-20 18:52:50 +0000 | [diff] [blame] | 18 | #include "libGLESv2/Context.h" |
| 19 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | namespace gl |
| 21 | { |
| 22 | class FragmentShader; |
| 23 | class VertexShader; |
| 24 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 25 | // Helper struct representing a single shader uniform |
| 26 | struct Uniform |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | { |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 28 | Uniform(GLenum type, const std::string &name, unsigned int bytes); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 29 | |
| 30 | ~Uniform(); |
| 31 | |
daniel@transgaming.com | 0361b92 | 2010-03-28 19:36:15 +0000 | [diff] [blame] | 32 | const GLenum type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 33 | const std::string name; |
| 34 | const unsigned int bytes; |
| 35 | unsigned char *data; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | class Program |
| 39 | { |
| 40 | public: |
| 41 | Program(); |
| 42 | |
| 43 | ~Program(); |
| 44 | |
| 45 | bool attachShader(Shader *shader); |
| 46 | bool detachShader(Shader *shader); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 47 | int getAttachedShadersCount() const; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 48 | |
| 49 | IDirect3DPixelShader9 *getPixelShader(); |
| 50 | IDirect3DVertexShader9 *getVertexShader(); |
| 51 | |
| 52 | void bindAttributeLocation(GLuint index, const char *name); |
| 53 | GLuint getAttributeLocation(const char *name); |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame^] | 54 | int getSemanticIndex(int attributeIndex); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 55 | |
| 56 | GLint getSamplerMapping(unsigned int samplerIndex); |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 57 | SamplerType getSamplerType(unsigned int samplerIndex); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 58 | |
| 59 | GLint getUniformLocation(const char *name); |
| 60 | bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v); |
| 61 | bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v); |
| 62 | bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v); |
| 63 | bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v); |
| 64 | bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value); |
| 65 | bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value); |
| 66 | bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value); |
| 67 | bool setUniform1iv(GLint location, GLsizei count, const GLint *v); |
daniel@transgaming.com | 9a95e2b | 2010-04-13 03:26:03 +0000 | [diff] [blame] | 68 | bool setUniform2iv(GLint location, GLsizei count, const GLint *v); |
| 69 | bool setUniform3iv(GLint location, GLsizei count, const GLint *v); |
| 70 | bool setUniform4iv(GLint location, GLsizei count, const GLint *v); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 71 | |
daniel@transgaming.com | bb3d9d0 | 2010-04-13 03:26:06 +0000 | [diff] [blame] | 72 | bool getUniformfv(GLint location, GLfloat *params); |
| 73 | bool getUniformiv(GLint location, GLint *params); |
| 74 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 75 | void applyUniforms(); |
daniel@transgaming.com | fab5a1a | 2010-03-11 19:22:30 +0000 | [diff] [blame] | 76 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 77 | void link(); |
| 78 | bool isLinked(); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 79 | int getInfoLogLength() const; |
| 80 | void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog); |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 81 | void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 82 | |
| 83 | void flagForDeletion(); |
| 84 | bool isFlaggedForDeletion() const; |
| 85 | |
| 86 | private: |
| 87 | DISALLOW_COPY_AND_ASSIGN(Program); |
| 88 | |
| 89 | ID3DXBuffer *compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable); |
| 90 | void unlink(bool destroy = false); |
| 91 | |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame] | 92 | struct Varying |
| 93 | { |
| 94 | Varying(const std::string &name, char *declaration) : name(name), declaration(declaration) |
| 95 | { |
| 96 | link = -1; |
| 97 | } |
| 98 | |
| 99 | int link; |
| 100 | std::string name; |
| 101 | char *declaration; |
| 102 | }; |
| 103 | |
| 104 | typedef std::vector<Varying> VaryingArray; |
| 105 | |
| 106 | void parseVaryings(const char *structure, char *hlsl, VaryingArray &varyings); |
| 107 | bool linkVaryings(); |
| 108 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 109 | bool linkAttributes(); |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame^] | 110 | int getAttributeBinding(const char *name); |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame] | 111 | |
daniel@transgaming.com | 86487c2 | 2010-03-11 19:41:43 +0000 | [diff] [blame] | 112 | bool linkUniforms(ID3DXConstantTable *constantTable); |
| 113 | bool defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name = ""); |
| 114 | bool defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name); |
| 115 | Uniform *createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name); |
daniel@transgaming.com | 72d0b52 | 2010-04-13 19:53:44 +0000 | [diff] [blame] | 116 | static std::string decorate(const std::string &string); // Prepend an underscore |
daniel@transgaming.com | f4a0c8e | 2010-04-13 03:26:01 +0000 | [diff] [blame] | 117 | bool applyUniform1bv(GLint location, GLsizei count, const GLboolean *v); |
| 118 | bool applyUniform2bv(GLint location, GLsizei count, const GLboolean *v); |
| 119 | bool applyUniform3bv(GLint location, GLsizei count, const GLboolean *v); |
| 120 | bool applyUniform4bv(GLint location, GLsizei count, const GLboolean *v); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 121 | bool applyUniform1fv(GLint location, GLsizei count, const GLfloat *v); |
| 122 | bool applyUniform2fv(GLint location, GLsizei count, const GLfloat *v); |
| 123 | bool applyUniform3fv(GLint location, GLsizei count, const GLfloat *v); |
| 124 | bool applyUniform4fv(GLint location, GLsizei count, const GLfloat *v); |
| 125 | bool applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value); |
| 126 | bool applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value); |
| 127 | bool applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value); |
| 128 | bool applyUniform1iv(GLint location, GLsizei count, const GLint *v); |
daniel@transgaming.com | 9a95e2b | 2010-04-13 03:26:03 +0000 | [diff] [blame] | 129 | bool applyUniform2iv(GLint location, GLsizei count, const GLint *v); |
| 130 | bool applyUniform3iv(GLint location, GLsizei count, const GLint *v); |
| 131 | bool applyUniform4iv(GLint location, GLsizei count, const GLint *v); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 132 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame^] | 133 | void appendToInfoLog(const char *info, ...); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 134 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | FragmentShader *mFragmentShader; |
| 136 | VertexShader *mVertexShader; |
| 137 | |
daniel@transgaming.com | 0e3358a | 2010-04-05 20:32:42 +0000 | [diff] [blame] | 138 | char *mPixelHLSL; |
| 139 | char *mVertexHLSL; |
| 140 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 141 | IDirect3DPixelShader9 *mPixelExecutable; |
| 142 | IDirect3DVertexShader9 *mVertexExecutable; |
| 143 | ID3DXConstantTable *mConstantTablePS; |
| 144 | ID3DXConstantTable *mConstantTableVS; |
| 145 | |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame^] | 146 | std::set<std::string> mAttributeBinding[MAX_VERTEX_ATTRIBS]; |
| 147 | std::string mLinkedAttribute[MAX_VERTEX_ATTRIBS]; |
| 148 | int mSemanticIndex[MAX_VERTEX_ATTRIBS]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 149 | |
daniel@transgaming.com | 416485f | 2010-03-16 06:23:23 +0000 | [diff] [blame] | 150 | struct Sampler |
| 151 | { |
| 152 | bool active; |
| 153 | GLint logicalTextureUnit; |
| 154 | SamplerType type; |
| 155 | }; |
| 156 | |
| 157 | Sampler mSamplers[MAX_TEXTURE_IMAGE_UNITS]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 158 | |
| 159 | typedef std::vector<Uniform*> UniformArray; |
| 160 | UniformArray mUniforms; |
| 161 | |
| 162 | bool mLinked; |
| 163 | bool mDeleteStatus; // Flag to indicate that the program can be deleted when no longer in use |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 164 | char *mInfoLog; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 165 | }; |
| 166 | } |
| 167 | |
| 168 | #endif // LIBGLESV2_PROGRAM_H_ |