blob: 54f9920c9264d1bad9dfd0a0a3db47ca2b7952f6 [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
shannon.woods%transgaming.com@gtempaccount.com7bc65f22013-04-13 03:41:39 +00002// Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved.
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00003// 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.org90080e32012-07-09 22:15:33 +000013#define GL_APICALL
shannon.woods%transgaming.com@gtempaccount.comf26ddae2013-04-13 03:29:13 +000014#include <GLES3/gl3.h>
15#include <GLES3/gl3ext.h>
daniel@transgaming.com29ab9522012-08-27 16:25:37 +000016#include <GLES2/gl2.h>
17#include <GLES2/gl2ext.h>
apatrick@chromium.org90080e32012-07-09 22:15:33 +000018
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000019#include <string>
20#include <vector>
21
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000022#include "common/RefCountObject.h"
23#include "angletypes.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000024#include "common/mathutil.h"
daniel@transgaming.com15186aa2012-12-20 21:08:23 +000025#include "libGLESv2/Uniform.h"
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000026#include "libGLESv2/Shader.h"
shannon.woods@transgaming.com486d9e92013-02-28 23:15:41 +000027#include "libGLESv2/Constants.h"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000028
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000029namespace rx
30{
31class ShaderExecutable;
32class Renderer;
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +000033struct TranslatedAttribute;
Jamie Madill8ff21ae2014-02-04 16:04:05 -050034class UniformStorage;
Jamie Madill5f562732014-02-14 16:41:24 -050035class DynamicHLSL;
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000036}
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000037
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000038namespace gl
39{
40class FragmentShader;
41class VertexShader;
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000042class InfoLog;
43class AttributeBindings;
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +000044class Buffer;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000045
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000046// Struct used for correlating uniforms/elements of uniform arrays to handles
Jamie Madill63491ea2013-06-06 11:56:45 -040047struct VariableLocation
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000048{
Jamie Madill63491ea2013-06-06 11:56:45 -040049 VariableLocation()
apatrick@chromium.org90080e32012-07-09 22:15:33 +000050 {
51 }
52
Jamie Madill63491ea2013-06-06 11:56:45 -040053 VariableLocation(const std::string &name, unsigned int element, unsigned int index);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000054
55 std::string name;
56 unsigned int element;
57 unsigned int index;
58};
59
60// This is the result of linking a program. It is the state that would be passed to ProgramBinary.
daniel@transgaming.com989c1c82012-07-24 18:40:38 +000061class ProgramBinary : public RefCountObject
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000062{
63 public:
daniel@transgaming.com70062c92012-11-28 19:32:30 +000064 explicit ProgramBinary(rx::Renderer *renderer);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000065 ~ProgramBinary();
66
Jamie Madill8ff21ae2014-02-04 16:04:05 -050067 rx::ShaderExecutable *getPixelExecutable() const;
68 rx::ShaderExecutable *getVertexExecutable() const;
69 rx::ShaderExecutable *getGeometryExecutable() const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000070
71 GLuint getAttributeLocation(const char *name);
72 int getSemanticIndex(int attributeIndex);
73
74 GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex);
75 TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex);
76 GLint getUsedSamplerRange(SamplerType type);
daniel@transgaming.com087e5782012-09-17 21:28:47 +000077 bool usesPointSize() const;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000078 bool usesPointSpriteEmulation() const;
79 bool usesGeometryShader() const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000080
81 GLint getUniformLocation(std::string name);
shannonwoods@chromium.orgc2ed9912013-05-30 00:05:33 +000082 GLuint getUniformIndex(std::string name);
shannonwoods@chromium.org42766252013-05-30 00:07:12 +000083 GLuint getUniformBlockIndex(std::string name);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000084 bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
85 bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
86 bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
87 bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000088 bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
89 bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
90 bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
91 bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +000092 bool setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
93 bool setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
94 bool setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
95 bool setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
96 bool setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
97 bool setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
98 bool setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +000099 bool setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
100 bool setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
101 bool setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
102 bool setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
103 bool setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
104 bool setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000105
106 bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params);
107 bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
shannon.woods%transgaming.com@gtempaccount.come2290122013-04-13 03:41:07 +0000108 bool getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000109
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000110 void dirtyAllUniforms();
111 void applyUniforms();
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000112 bool applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000113
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000114 bool load(InfoLog &infoLog, const void *binary, GLsizei length);
115 bool save(void* binary, GLsizei bufSize, GLsizei *length);
116 GLint getLength();
117
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000118 bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000119 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
120
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +0000121 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
122 GLint getActiveAttributeCount() const;
123 GLint getActiveAttributeMaxLength() const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000124
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +0000125 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
126 GLint getActiveUniformCount() const;
127 GLint getActiveUniformMaxLength() const;
shannonwoods@chromium.org2a9a9d22013-05-30 00:05:40 +0000128 GLint getActiveUniformi(GLuint index, GLenum pname) const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000129
shannonwoods@chromium.orgbeb02782013-05-30 00:07:28 +0000130 void getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const;
shannonwoods@chromium.orge7317ca2013-05-30 00:07:35 +0000131 void getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const;
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000132 GLuint getActiveUniformBlockCount() const;
shannonwoods@chromium.orge684b582013-05-30 00:07:42 +0000133 GLuint getActiveUniformBlockMaxLength() const;
shannonwoods@chromium.org1bddfb92013-05-30 00:11:29 +0000134 UniformBlock *getUniformBlockByIndex(GLuint blockIndex);
shannonwoods@chromium.org70eb1ea2013-05-30 00:07:20 +0000135
Jamie Madilld1e78c92013-06-20 11:55:50 -0400136 GLint getFragDataLocation(const char *name) const;
137
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000138 void validate(InfoLog &infoLog);
139 bool validateSamplers(InfoLog *infoLog);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000140 bool isValidated() const;
141
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000142 unsigned int getSerial() const;
shannonwoods@chromium.org83ac5e82013-05-30 00:15:29 +0000143 int getShaderVersion() const;
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000144
Al Patrick3f2daa82013-08-07 12:58:57 -0700145 void initAttributesByLayout();
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +0000146 void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const;
147
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500148 const UniformArray &getUniforms() const { return mUniforms; }
149 const rx::UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; }
150 const rx::UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; }
151
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000152 private:
153 DISALLOW_COPY_AND_ASSIGN(ProgramBinary);
154
Jamie Madill5f562732014-02-14 16:41:24 -0500155 bool linkVaryings(InfoLog &infoLog, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000156 bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000157
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000158 typedef sh::BlockMemberInfoArray::const_iterator BlockInfoItr;
159
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400160 template <class ShaderVarType>
161 bool linkValidateFields(InfoLog &infoLog, const std::string &varName, const ShaderVarType &vertexVar, const ShaderVarType &fragmentVar);
Jamie Madill28167c62013-08-30 13:21:10 -0400162 bool linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable, const sh::ShaderVariable &fragmentVariable, bool validatePrecision);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400163
164 bool linkValidateVariables(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform);
Jamie Madill28167c62013-08-30 13:21:10 -0400165 bool linkValidateVariables(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400166 bool linkValidateVariables(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform);
167 bool linkUniforms(InfoLog &infoLog, const std::vector<sh::Uniform> &vertexUniforms, const std::vector<sh::Uniform> &fragmentUniforms);
daniel@transgaming.comda8d3802012-12-20 21:12:55 +0000168 bool defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog);
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +0000169 bool areMatchingInterfaceBlocks(InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock, const sh::InterfaceBlock &fragmentInterfaceBlock);
170 bool linkUniformBlocks(InfoLog &infoLog, const sh::ActiveInterfaceBlocks &vertexUniformBlocks, const sh::ActiveInterfaceBlocks &fragmentUniformBlocks);
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400171 void defineUniformBlockMembers(const std::vector<sh::InterfaceBlockField> &fields, const std::string &prefix, int blockIndex, BlockInfoItr *blockInfoItr, std::vector<unsigned int> *blockUniformIndexes);
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +0000172 bool defineUniformBlock(InfoLog &infoLog, GLenum shader, const sh::InterfaceBlock &interfaceBlock);
173 bool assignUniformBlockRegister(InfoLog &infoLog, UniformBlock *uniformBlock, GLenum shader, unsigned int registerIndex);
Jamie Madill46131a32013-06-20 11:55:50 -0400174 void defineOutputVariables(FragmentShader *fragmentShader);
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500175 void initializeUniformStorage();
Jamie Madill46131a32013-06-20 11:55:50 -0400176
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000177 template <typename T>
178 bool setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
179
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000180 template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000181 bool setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000182
shannon.woods%transgaming.com@gtempaccount.com4590d892013-04-13 03:41:01 +0000183 template <typename T>
184 bool getUniformv(GLint location, GLsizei *bufSize, T *params, GLenum uniformType);
185
Nicolas Capens43e8ba82013-07-09 10:35:15 -0400186 static TextureType getTextureType(GLenum samplerType, InfoLog &infoLog);
187
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000188 rx::Renderer *const mRenderer;
Jamie Madill5f562732014-02-14 16:41:24 -0500189 DynamicHLSL *mDynamicHLSL;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000190
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +0000191 rx::ShaderExecutable *mPixelExecutable;
192 rx::ShaderExecutable *mVertexExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000193 rx::ShaderExecutable *mGeometryExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000194
Jamie Madill9d2ffb12013-08-30 13:21:04 -0400195 sh::Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000196 int mSemanticIndex[MAX_VERTEX_ATTRIBS];
Al Patrick3f2daa82013-08-07 12:58:57 -0700197 int mAttributesByLayout[MAX_VERTEX_ATTRIBS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000198
199 struct Sampler
200 {
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000201 Sampler();
202
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000203 bool active;
204 GLint logicalTextureUnit;
205 TextureType textureType;
206 };
207
208 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS];
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000209 Sampler mSamplersVS[IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000210 GLuint mUsedVertexSamplerRange;
211 GLuint mUsedPixelSamplerRange;
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000212 bool mUsesPointSize;
shannonwoods@chromium.org83ac5e82013-05-30 00:15:29 +0000213 int mShaderVersion;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000214
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000215 UniformArray mUniforms;
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +0000216 UniformBlockArray mUniformBlocks;
Jamie Madill5f562732014-02-14 16:41:24 -0500217 std::vector<VariableLocation> mUniformIndex;
218 std::map<int, VariableLocation> mOutputVariables;
Jamie Madill8ff21ae2014-02-04 16:04:05 -0500219 rx::UniformStorage *mVertexUniformStorage;
220 rx::UniformStorage *mFragmentUniformStorage;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000221
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000222 bool mValidated;
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000223
224 const unsigned int mSerial;
225
226 static unsigned int issueSerial();
227 static unsigned int mCurrentSerial;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000228};
Jamie Madill5f562732014-02-14 16:41:24 -0500229
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000230}
231
232#endif // LIBGLESV2_PROGRAM_BINARY_H_