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