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