apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2012 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_BINARY_H_ |
| 11 | #define LIBGLESV2_PROGRAM_BINARY_H_ |
| 12 | |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 13 | #define GL_APICALL |
daniel@transgaming.com | 29ab952 | 2012-08-27 16:25:37 +0000 | [diff] [blame] | 14 | #include <GLES2/gl2.h> |
| 15 | #include <GLES2/gl2ext.h> |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 16 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
daniel@transgaming.com | 4a186ed | 2012-11-28 20:56:15 +0000 | [diff] [blame] | 20 | #include "libGLESv2/Program.h" |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 21 | #include "libGLESv2/Context.h" |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 22 | #include "libGLESv2/mathutil.h" |
| 23 | #include "libGLESv2/Shader.h" |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 24 | |
daniel@transgaming.com | 4f0f65e | 2012-11-28 21:00:00 +0000 | [diff] [blame] | 25 | #include "libGLESv2/renderer/ShaderExecutable.h" |
daniel@transgaming.com | a9c7142 | 2012-11-28 20:58:45 +0000 | [diff] [blame] | 26 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 27 | namespace gl |
| 28 | { |
| 29 | class FragmentShader; |
| 30 | class VertexShader; |
| 31 | |
| 32 | // Helper struct representing a single shader uniform |
| 33 | struct Uniform |
| 34 | { |
| 35 | Uniform(GLenum type, const std::string &_name, unsigned int arraySize); |
| 36 | |
| 37 | ~Uniform(); |
| 38 | |
| 39 | bool isArray(); |
| 40 | |
| 41 | const GLenum type; |
| 42 | const std::string _name; // Decorated name |
| 43 | const std::string name; // Undecorated name |
| 44 | const unsigned int arraySize; |
| 45 | |
| 46 | unsigned char *data; |
| 47 | bool dirty; |
| 48 | |
| 49 | struct RegisterInfo |
| 50 | { |
| 51 | RegisterInfo() |
| 52 | { |
| 53 | float4Index = -1; |
| 54 | samplerIndex = -1; |
| 55 | boolIndex = -1; |
| 56 | registerCount = 0; |
| 57 | } |
| 58 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 59 | void set(const rx::D3DConstant *constant) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 60 | { |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 61 | switch(constant->registerSet) |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 62 | { |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 63 | case rx::D3DConstant::RS_BOOL: boolIndex = constant->registerIndex; break; |
| 64 | case rx::D3DConstant::RS_FLOAT4: float4Index = constant->registerIndex; break; |
| 65 | case rx::D3DConstant::RS_SAMPLER: samplerIndex = constant->registerIndex; break; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 66 | default: UNREACHABLE(); |
| 67 | } |
| 68 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 69 | ASSERT(registerCount == 0 || registerCount == (int)constant->registerCount); |
| 70 | registerCount = constant->registerCount; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | int float4Index; |
| 74 | int samplerIndex; |
| 75 | int boolIndex; |
| 76 | |
| 77 | int registerCount; |
| 78 | }; |
| 79 | |
| 80 | RegisterInfo ps; |
| 81 | RegisterInfo vs; |
| 82 | }; |
| 83 | |
| 84 | // Struct used for correlating uniforms/elements of uniform arrays to handles |
| 85 | struct UniformLocation |
| 86 | { |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 87 | UniformLocation() |
| 88 | { |
| 89 | } |
| 90 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 91 | UniformLocation(const std::string &_name, unsigned int element, unsigned int index); |
| 92 | |
| 93 | std::string name; |
| 94 | unsigned int element; |
| 95 | unsigned int index; |
| 96 | }; |
| 97 | |
| 98 | // This is the result of linking a program. It is the state that would be passed to ProgramBinary. |
daniel@transgaming.com | 989c1c8 | 2012-07-24 18:40:38 +0000 | [diff] [blame] | 99 | class ProgramBinary : public RefCountObject |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 100 | { |
| 101 | public: |
daniel@transgaming.com | 70062c9 | 2012-11-28 19:32:30 +0000 | [diff] [blame] | 102 | explicit ProgramBinary(rx::Renderer *renderer); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 103 | ~ProgramBinary(); |
| 104 | |
daniel@transgaming.com | 9589241 | 2012-11-28 20:59:09 +0000 | [diff] [blame] | 105 | rx::ShaderExecutable *getPixelExecutable(); |
| 106 | rx::ShaderExecutable *getVertexExecutable(); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 107 | |
| 108 | GLuint getAttributeLocation(const char *name); |
| 109 | int getSemanticIndex(int attributeIndex); |
| 110 | |
| 111 | GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex); |
| 112 | TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex); |
| 113 | GLint getUsedSamplerRange(SamplerType type); |
daniel@transgaming.com | 087e578 | 2012-09-17 21:28:47 +0000 | [diff] [blame] | 114 | bool usesPointSize() const; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 115 | |
| 116 | GLint getUniformLocation(std::string name); |
| 117 | bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v); |
| 118 | bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v); |
| 119 | bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v); |
| 120 | bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v); |
| 121 | bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value); |
| 122 | bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value); |
| 123 | bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value); |
| 124 | bool setUniform1iv(GLint location, GLsizei count, const GLint *v); |
| 125 | bool setUniform2iv(GLint location, GLsizei count, const GLint *v); |
| 126 | bool setUniform3iv(GLint location, GLsizei count, const GLint *v); |
| 127 | bool setUniform4iv(GLint location, GLsizei count, const GLint *v); |
| 128 | |
| 129 | bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params); |
| 130 | bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params); |
| 131 | |
daniel@transgaming.com | 88853c5 | 2012-12-20 20:56:40 +0000 | [diff] [blame^] | 132 | void applyDxDepthRange(float near, float far, float diff); |
| 133 | void applyDxDepthFront(float range, float start, float frontCCW); |
| 134 | void applyDxCoord(float halfWidth, float halfHeight, float x0, float y0); |
| 135 | void applyDxHalfPixelSize(float width, float height); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 136 | |
| 137 | void dirtyAllUniforms(); |
| 138 | void applyUniforms(); |
| 139 | |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 140 | bool load(InfoLog &infoLog, const void *binary, GLsizei length); |
| 141 | bool save(void* binary, GLsizei bufSize, GLsizei *length); |
| 142 | GLint getLength(); |
| 143 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 144 | bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 145 | void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders); |
| 146 | |
| 147 | void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name); |
| 148 | GLint getActiveAttributeCount(); |
| 149 | GLint getActiveAttributeMaxLength(); |
| 150 | |
| 151 | void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name); |
| 152 | GLint getActiveUniformCount(); |
| 153 | GLint getActiveUniformMaxLength(); |
| 154 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 155 | void validate(InfoLog &infoLog); |
| 156 | bool validateSamplers(InfoLog *infoLog); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 157 | bool isValidated() const; |
| 158 | |
daniel@transgaming.com | e87ca00 | 2012-07-24 18:30:43 +0000 | [diff] [blame] | 159 | unsigned int getSerial() const; |
| 160 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 161 | static std::string decorateAttribute(const std::string &name); // Prepend an underscore |
| 162 | static std::string undecorateUniform(const std::string &_name); // Remove leading underscore |
| 163 | |
| 164 | private: |
| 165 | DISALLOW_COPY_AND_ASSIGN(ProgramBinary); |
| 166 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 167 | int packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader); |
| 168 | bool linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 169 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 170 | bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 171 | |
daniel@transgaming.com | 3124048 | 2012-11-28 21:06:41 +0000 | [diff] [blame] | 172 | bool linkUniforms(InfoLog &infoLog, rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable); |
| 173 | bool defineUniform(InfoLog &infoLog, GLenum shader, const rx::D3DConstant *constant, const std::string &name, |
| 174 | rx::D3DConstantTable *vsConstantTable, rx::D3DConstantTable *psConstantTable); |
| 175 | bool defineUniform(GLenum shader, const rx::D3DConstant *constant, const std::string &name); |
| 176 | Uniform *createUniform(const rx::D3DConstant *constant, const std::string &name); |
daniel@transgaming.com | 77fbf97 | 2012-11-28 21:02:55 +0000 | [diff] [blame] | 177 | bool applyUniformnfv(IDirect3DDevice9 *device, Uniform *targetUniform, const GLfloat *v); // D3D9_REPLACE |
| 178 | bool applyUniform1iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v); |
| 179 | bool applyUniform2iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v); |
| 180 | bool applyUniform3iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v); |
| 181 | bool applyUniform4iv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const GLint *v); |
| 182 | void applyUniformniv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, const Vector4 *vector); |
| 183 | void applyUniformnbv(IDirect3DDevice9 *device, Uniform *targetUniform, GLsizei count, int width, const GLboolean *v); |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 184 | |
daniel@transgaming.com | 77fbf97 | 2012-11-28 21:02:55 +0000 | [diff] [blame] | 185 | rx::Renderer *const mRenderer; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 186 | |
daniel@transgaming.com | 4f0f65e | 2012-11-28 21:00:00 +0000 | [diff] [blame] | 187 | rx::ShaderExecutable *mPixelExecutable; |
| 188 | rx::ShaderExecutable *mVertexExecutable; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 189 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 190 | Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS]; |
| 191 | int mSemanticIndex[MAX_VERTEX_ATTRIBS]; |
| 192 | |
| 193 | struct Sampler |
| 194 | { |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 195 | Sampler(); |
| 196 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 197 | bool active; |
| 198 | GLint logicalTextureUnit; |
| 199 | TextureType textureType; |
| 200 | }; |
| 201 | |
| 202 | Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS]; |
| 203 | Sampler mSamplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF]; |
| 204 | GLuint mUsedVertexSamplerRange; |
| 205 | GLuint mUsedPixelSamplerRange; |
daniel@transgaming.com | 087e578 | 2012-09-17 21:28:47 +0000 | [diff] [blame] | 206 | bool mUsesPointSize; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 207 | |
| 208 | typedef std::vector<Uniform*> UniformArray; |
| 209 | UniformArray mUniforms; |
| 210 | typedef std::vector<UniformLocation> UniformIndex; |
| 211 | UniformIndex mUniformIndex; |
| 212 | |
| 213 | GLint mDxDepthRangeLocation; |
daniel@transgaming.com | 1298518 | 2012-12-20 20:56:31 +0000 | [diff] [blame] | 214 | GLint mDxDepthFrontLocation; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 215 | GLint mDxCoordLocation; |
| 216 | GLint mDxHalfPixelSizeLocation; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 217 | |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 218 | bool mValidated; |
daniel@transgaming.com | e87ca00 | 2012-07-24 18:30:43 +0000 | [diff] [blame] | 219 | |
| 220 | const unsigned int mSerial; |
| 221 | |
| 222 | static unsigned int issueSerial(); |
| 223 | static unsigned int mCurrentSerial; |
apatrick@chromium.org | ea09f9b | 2012-06-08 00:45:32 +0000 | [diff] [blame] | 224 | }; |
| 225 | } |
| 226 | |
| 227 | #endif // LIBGLESV2_PROGRAM_BINARY_H_ |