blob: c03a121b8fd3092cfb6fc691b8157a8ceb4333f1 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
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.com4f39fd92010-03-08 20:26:45 +000013#include <d3dx9.h>
apatrick@chromium.org8ea5afe2011-03-23 20:44:36 +000014#include <d3dcompiler.h>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000015#include <string>
16#include <vector>
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +000017#include <set>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018
daniel@transgaming.com85423182010-04-22 13:35:27 +000019#include "libGLESv2/Shader.h"
daniel@transgaming.come6842292010-04-20 18:52:50 +000020#include "libGLESv2/Context.h"
21
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000022namespace gl
23{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000024class ResourceManager;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000025class FragmentShader;
26class VertexShader;
27
daniel@transgaming.com86487c22010-03-11 19:41:43 +000028// Helper struct representing a single shader uniform
29struct Uniform
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030{
daniel@transgaming.com1b3a8152010-04-22 13:35:37 +000031 Uniform(GLenum type, const std::string &name, unsigned int arraySize);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
33 ~Uniform();
34
daniel@transgaming.com0361b922010-03-28 19:36:15 +000035 const GLenum type;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036 const std::string name;
daniel@transgaming.com1b3a8152010-04-22 13:35:37 +000037 const unsigned int arraySize;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +000038
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039 unsigned char *data;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +000040 bool dirty;
daniel@transgaming.com2d84df02010-05-14 17:31:13 +000041
42 D3DXHANDLE vsHandle;
43 D3DXHANDLE psHandle;
44 bool handlesSet;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045};
46
daniel@transgaming.com916ffaa2010-04-23 18:34:52 +000047// Struct used for correlating uniforms/elements of uniform arrays to handles
48struct UniformLocation
49{
50 UniformLocation(const std::string &name, unsigned int element, unsigned int index);
51
52 std::string name;
53 unsigned int element;
54 unsigned int index;
55};
56
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000057class Program
58{
59 public:
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000060 Program(ResourceManager *manager, GLuint handle);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061
62 ~Program();
63
64 bool attachShader(Shader *shader);
65 bool detachShader(Shader *shader);
daniel@transgaming.comcba50572010-03-28 19:36:09 +000066 int getAttachedShadersCount() const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000067
68 IDirect3DPixelShader9 *getPixelShader();
69 IDirect3DVertexShader9 *getVertexShader();
70
71 void bindAttributeLocation(GLuint index, const char *name);
72 GLuint getAttributeLocation(const char *name);
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +000073 int getSemanticIndex(int attributeIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074
daniel@transgaming.comd4a35172011-05-11 15:36:45 +000075 GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex);
76 TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077
daniel@transgaming.coma3bbfd42010-06-07 02:06:09 +000078 GLint getUniformLocation(const char *name, bool decorated);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079 bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
80 bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
81 bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
82 bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
83 bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
84 bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
85 bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
86 bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +000087 bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
88 bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
89 bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090
daniel@transgaming.combb3d9d02010-04-13 03:26:06 +000091 bool getUniformfv(GLint location, GLfloat *params);
92 bool getUniformiv(GLint location, GLint *params);
93
daniel@transgaming.com31754962010-11-28 02:02:52 +000094 GLint getDxDepthRangeLocation() const;
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +000095 GLint getDxDepthLocation() const;
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +000096 GLint getDxViewportLocation() const;
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +000097 GLint getDxHalfPixelSizeLocation() const;
98 GLint getDxFrontCCWLocation() const;
99 GLint getDxPointsOrLinesLocation() const;
100
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000101 void dirtyAllUniforms();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 void applyUniforms();
daniel@transgaming.comfab5a1a2010-03-11 19:22:30 +0000103
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104 void link();
105 bool isLinked();
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000106 int getInfoLogLength() const;
107 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
daniel@transgaming.com6c785212010-03-30 03:36:17 +0000108 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.com85423182010-04-22 13:35:27 +0000110 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
111 GLint getActiveAttributeCount();
112 GLint getActiveAttributeMaxLength();
113
daniel@transgaming.com09fbfef2010-04-22 13:35:31 +0000114 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
115 GLint getActiveUniformCount();
116 GLint getActiveUniformMaxLength();
117
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000118 void addRef();
119 void release();
120 unsigned int getRefCount() const;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121 void flagForDeletion();
122 bool isFlaggedForDeletion() const;
123
daniel@transgaming.com86a7a132010-04-29 03:32:32 +0000124 void validate();
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +0000125 bool validateSamplers(bool logErrors);
daniel@transgaming.com86a7a132010-04-29 03:32:32 +0000126 bool isValidated() const;
127
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000128 unsigned int getSerial() const;
129
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130 private:
131 DISALLOW_COPY_AND_ASSIGN(Program);
132
apatrick@chromium.org8ea5afe2011-03-23 20:44:36 +0000133 ID3D10Blob *compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134 void unlink(bool destroy = false);
135
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000136 int packVaryings(const Varying *packing[][4]);
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000137 bool linkVaryings();
138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139 bool linkAttributes();
daniel@transgaming.com85423182010-04-22 13:35:27 +0000140 int getAttributeBinding(const std::string &name);
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000141
daniel@transgaming.com86487c22010-03-11 19:41:43 +0000142 bool linkUniforms(ID3DXConstantTable *constantTable);
143 bool defineUniform(const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name = "");
144 bool defineUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name);
145 Uniform *createUniform(const D3DXCONSTANT_DESC &constantDescription, std::string &name);
daniel@transgaming.comf4a0c8e2010-04-13 03:26:01 +0000146 bool applyUniform1bv(GLint location, GLsizei count, const GLboolean *v);
147 bool applyUniform2bv(GLint location, GLsizei count, const GLboolean *v);
148 bool applyUniform3bv(GLint location, GLsizei count, const GLboolean *v);
149 bool applyUniform4bv(GLint location, GLsizei count, const GLboolean *v);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150 bool applyUniform1fv(GLint location, GLsizei count, const GLfloat *v);
151 bool applyUniform2fv(GLint location, GLsizei count, const GLfloat *v);
152 bool applyUniform3fv(GLint location, GLsizei count, const GLfloat *v);
153 bool applyUniform4fv(GLint location, GLsizei count, const GLfloat *v);
154 bool applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
155 bool applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
156 bool applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
157 bool applyUniform1iv(GLint location, GLsizei count, const GLint *v);
daniel@transgaming.com9a95e2b2010-04-13 03:26:03 +0000158 bool applyUniform2iv(GLint location, GLsizei count, const GLint *v);
159 bool applyUniform3iv(GLint location, GLsizei count, const GLint *v);
160 bool applyUniform4iv(GLint location, GLsizei count, const GLint *v);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161
daniel@transgaming.com2d84df02010-05-14 17:31:13 +0000162 void getConstantHandles(Uniform *targetUniform, D3DXHANDLE *constantPS, D3DXHANDLE *constantVS);
163
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000164 void appendToInfoLog(const char *info, ...);
daniel@transgaming.com86a7a132010-04-29 03:32:32 +0000165 void resetInfoLog();
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000166
daniel@transgaming.comfeba9ba2010-04-29 03:32:45 +0000167 static std::string decorate(const std::string &string); // Prepend an underscore
168 static std::string undecorate(const std::string &string); // Remove leading underscore
169
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000170 static unsigned int issueSerial();
171
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172 FragmentShader *mFragmentShader;
173 VertexShader *mVertexShader;
174
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000175 std::string mPixelHLSL;
176 std::string mVertexHLSL;
daniel@transgaming.com0e3358a2010-04-05 20:32:42 +0000177
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000178 IDirect3DPixelShader9 *mPixelExecutable;
179 IDirect3DVertexShader9 *mVertexExecutable;
180 ID3DXConstantTable *mConstantTablePS;
181 ID3DXConstantTable *mConstantTableVS;
182
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000183 std::set<std::string> mAttributeBinding[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com85423182010-04-22 13:35:27 +0000184 Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
daniel@transgaming.comb4ff1f82010-04-22 13:35:18 +0000185 int mSemanticIndex[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000187 struct Sampler
188 {
189 bool active;
190 GLint logicalTextureUnit;
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000191 TextureType textureType;
daniel@transgaming.com416485f2010-03-16 06:23:23 +0000192 };
193
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000194 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS];
195 Sampler mSamplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196
197 typedef std::vector<Uniform*> UniformArray;
198 UniformArray mUniforms;
daniel@transgaming.com916ffaa2010-04-23 18:34:52 +0000199 typedef std::vector<UniformLocation> UniformIndex;
200 UniformIndex mUniformIndex;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201
daniel@transgaming.com31754962010-11-28 02:02:52 +0000202 GLint mDxDepthRangeLocation;
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +0000203 GLint mDxDepthLocation;
daniel@transgaming.com4f921eb2010-07-28 19:20:44 +0000204 GLint mDxViewportLocation;
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +0000205 GLint mDxHalfPixelSizeLocation;
206 GLint mDxFrontCCWLocation;
207 GLint mDxPointsOrLinesLocation;
208
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209 bool mLinked;
210 bool mDeleteStatus; // Flag to indicate that the program can be deleted when no longer in use
daniel@transgaming.comcba50572010-03-28 19:36:09 +0000211 char *mInfoLog;
daniel@transgaming.com86a7a132010-04-29 03:32:32 +0000212 bool mValidated;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000213
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000214 unsigned int mRefCount;
215
daniel@transgaming.com73a5db62010-10-15 17:58:13 +0000216 const unsigned int mSerial;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000217
218 static unsigned int mCurrentSerial;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000219
220 ResourceManager *mResourceManager;
221 const GLuint mHandle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222};
223}
224
225#endif // LIBGLESV2_PROGRAM_H_