blob: 6d71c0c9143632fc739e70d8786763a1205c6fcf [file] [log] [blame]
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +00001//
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.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"
apatrick@chromium.org60dafe82012-09-05 22:26:10 +000024#include "libGLESv2/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;
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000034}
daniel@transgaming.coma9c71422012-11-28 20:58:45 +000035
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000036namespace gl
37{
38class FragmentShader;
39class VertexShader;
shannon.woods@transgaming.comd2811d62013-02-28 23:11:19 +000040class InfoLog;
41class AttributeBindings;
42struct Varying;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000043
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000044// Struct used for correlating uniforms/elements of uniform arrays to handles
45struct UniformLocation
46{
apatrick@chromium.org90080e32012-07-09 22:15:33 +000047 UniformLocation()
48 {
49 }
50
daniel@transgaming.comdb019952012-12-20 21:13:32 +000051 UniformLocation(const std::string &name, unsigned int element, unsigned int index);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000052
53 std::string name;
54 unsigned int element;
55 unsigned int index;
56};
57
58// 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 +000059class ProgramBinary : public RefCountObject
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000060{
61 public:
daniel@transgaming.com70062c92012-11-28 19:32:30 +000062 explicit ProgramBinary(rx::Renderer *renderer);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000063 ~ProgramBinary();
64
daniel@transgaming.com95892412012-11-28 20:59:09 +000065 rx::ShaderExecutable *getPixelExecutable();
66 rx::ShaderExecutable *getVertexExecutable();
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000067 rx::ShaderExecutable *getGeometryExecutable();
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000068
69 GLuint getAttributeLocation(const char *name);
70 int getSemanticIndex(int attributeIndex);
71
72 GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex);
73 TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex);
74 GLint getUsedSamplerRange(SamplerType type);
daniel@transgaming.com087e5782012-09-17 21:28:47 +000075 bool usesPointSize() const;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +000076 bool usesPointSpriteEmulation() const;
77 bool usesGeometryShader() const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000078
79 GLint getUniformLocation(std::string name);
80 bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
81 bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
82 bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
83 bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000084 bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
85 bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
86 bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
87 bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
shannon.woods%transgaming.com@gtempaccount.com50ea4ab2013-04-13 03:40:36 +000088 bool setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
89 bool setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
90 bool setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
91 bool setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
92 bool setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
93 bool setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
94 bool setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +000095 bool setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
96 bool setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
97 bool setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
98 bool setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
99 bool setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
100 bool setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000101
102 bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params);
103 bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
104
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000105 void dirtyAllUniforms();
106 void applyUniforms();
107
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000108 bool load(InfoLog &infoLog, const void *binary, GLsizei length);
109 bool save(void* binary, GLsizei bufSize, GLsizei *length);
110 GLint getLength();
111
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000112 bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000113 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
114
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +0000115 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
116 GLint getActiveAttributeCount() const;
117 GLint getActiveAttributeMaxLength() const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000118
shannon.woods@transgaming.com4f4215f2013-02-28 23:14:58 +0000119 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
120 GLint getActiveUniformCount() const;
121 GLint getActiveUniformMaxLength() const;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000122
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000123 void validate(InfoLog &infoLog);
124 bool validateSamplers(InfoLog *infoLog);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000125 bool isValidated() const;
126
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000127 unsigned int getSerial() const;
128
shannon.woods@transgaming.com0b600142013-02-28 23:15:04 +0000129 void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const;
130
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000131 static std::string decorateAttribute(const std::string &name); // Prepend an underscore
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000132
133 private:
134 DISALLOW_COPY_AND_ASSIGN(ProgramBinary);
135
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000136 int packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader);
shannon.woods@transgaming.com5bcf7df2013-01-25 21:55:33 +0000137 bool linkVaryings(InfoLog &infoLog, int registers, const Varying *packing[][4],
138 std::string& pixelHLSL, std::string& vertexHLSL,
139 FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000140
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000141 bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000142
daniel@transgaming.com68aaf932012-12-20 21:13:16 +0000143 bool linkUniforms(InfoLog &infoLog, const sh::ActiveUniforms &vertexUniforms, const sh::ActiveUniforms &fragmentUniforms);
daniel@transgaming.comda8d3802012-12-20 21:12:55 +0000144 bool defineUniform(GLenum shader, const sh::Uniform &constant, InfoLog &infoLog);
daniel@transgaming.comb6e55102012-12-20 21:08:14 +0000145
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000146 std::string generateGeometryShaderHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const;
147 std::string generatePointSpriteHLSL(int registers, const Varying *packing[][4], FragmentShader *fragmentShader, VertexShader *vertexShader) const;
148
shannon.woods%transgaming.com@gtempaccount.com8a19eed2013-04-13 03:40:22 +0000149 template <typename T>
150 bool setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
151
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000152 template <int cols, int rows>
shannon.woods%transgaming.com@gtempaccount.coma741b642013-04-13 03:40:10 +0000153 bool setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
shannon.woods%transgaming.com@gtempaccount.com36c76a92013-04-13 03:39:58 +0000154
daniel@transgaming.com77fbf972012-11-28 21:02:55 +0000155 rx::Renderer *const mRenderer;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000156
daniel@transgaming.com4f0f65e2012-11-28 21:00:00 +0000157 rx::ShaderExecutable *mPixelExecutable;
158 rx::ShaderExecutable *mVertexExecutable;
shannon.woods@transgaming.com3e773bb2013-01-25 21:55:47 +0000159 rx::ShaderExecutable *mGeometryExecutable;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000160
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000161 Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
162 int mSemanticIndex[MAX_VERTEX_ATTRIBS];
163
164 struct Sampler
165 {
apatrick@chromium.org90080e32012-07-09 22:15:33 +0000166 Sampler();
167
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000168 bool active;
169 GLint logicalTextureUnit;
170 TextureType textureType;
171 };
172
173 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS];
shannon.woods@transgaming.com233fe952013-01-25 21:51:57 +0000174 Sampler mSamplersVS[IMPLEMENTATION_MAX_VERTEX_TEXTURE_IMAGE_UNITS];
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000175 GLuint mUsedVertexSamplerRange;
176 GLuint mUsedPixelSamplerRange;
daniel@transgaming.com087e5782012-09-17 21:28:47 +0000177 bool mUsesPointSize;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000178
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000179 UniformArray mUniforms;
180 typedef std::vector<UniformLocation> UniformIndex;
181 UniformIndex mUniformIndex;
182
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000183 bool mValidated;
daniel@transgaming.come87ca002012-07-24 18:30:43 +0000184
185 const unsigned int mSerial;
186
187 static unsigned int issueSerial();
188 static unsigned int mCurrentSerial;
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000189};
190}
191
192#endif // LIBGLESV2_PROGRAM_BINARY_H_