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