This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
forked from google/swiftshader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.h
333 lines (268 loc) · 13.3 KB
/
Program.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Program.h: Defines the Program class. Implements GL program objects
// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
#ifndef LIBGLESV2_PROGRAM_H_
#define LIBGLESV2_PROGRAM_H_
#include "Shader.h"
#include "Context.h"
#include "Shader/PixelShader.hpp"
#include "Shader/VertexShader.hpp"
#include <string>
#include <vector>
#include <set>
#include <map>
namespace es2
{
class Device;
class ResourceManager;
class FragmentShader;
class VertexShader;
// Helper struct representing a single shader uniform
struct Uniform
{
struct BlockInfo
{
BlockInfo(const glsl::Uniform& uniform, int blockIndex);
int index = -1;
int offset = -1;
int arrayStride = -1;
int matrixStride = -1;
bool isRowMajorMatrix = false;
};
Uniform(const glsl::Uniform &uniform, const BlockInfo &blockInfo);
~Uniform();
bool isArray() const;
int size() const;
int registerCount() const;
const GLenum type;
const GLenum precision;
const std::string name;
const unsigned int arraySize;
const BlockInfo blockInfo;
std::vector<glsl::ShaderVariable> fields;
unsigned char *data = nullptr;
bool dirty = true;
short psRegisterIndex = -1;
short vsRegisterIndex = -1;
};
// Helper struct representing a single shader uniform block
struct UniformBlock
{
// use GL_INVALID_INDEX for non-array elements
UniformBlock(const std::string &name, unsigned int elementIndex, unsigned int dataSize, std::vector<unsigned int> memberUniformIndexes);
void setRegisterIndex(GLenum shader, unsigned int registerIndex);
bool isArrayElement() const;
bool isReferencedByVertexShader() const;
bool isReferencedByFragmentShader() const;
const std::string name;
const unsigned int elementIndex;
const unsigned int dataSize;
std::vector<unsigned int> memberUniformIndexes;
unsigned int psRegisterIndex;
unsigned int vsRegisterIndex;
};
// Struct used for correlating uniforms/elements of uniform arrays to handles
struct UniformLocation
{
UniformLocation(const std::string &name, unsigned int element, unsigned int index);
std::string name;
unsigned int element;
unsigned int index;
};
struct LinkedVarying
{
LinkedVarying();
LinkedVarying(const std::string &name, GLenum type, GLsizei size, int reg, int col);
// Original GL name
std::string name;
GLenum type;
GLsizei size;
int reg; // First varying register, assigned during link
int col; // First register element, assigned during link
};
class Program
{
public:
Program(ResourceManager *manager, GLuint handle);
~Program();
bool attachShader(Shader *shader);
bool detachShader(Shader *shader);
int getAttachedShadersCount() const;
sw::PixelShader *getPixelShader();
sw::VertexShader *getVertexShader();
void bindAttributeLocation(GLuint index, const char *name);
GLint getAttributeLocation(const char *name);
int getAttributeStream(int attributeIndex);
GLint getSamplerMapping(sw::SamplerType type, unsigned int samplerIndex);
TextureType getSamplerTextureType(sw::SamplerType type, unsigned int samplerIndex);
GLuint getUniformIndex(const std::string &name) const;
GLuint getUniformBlockIndex(const std::string &name) const;
void bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding);
GLuint getUniformBlockBinding(GLuint uniformBlockIndex) const;
void getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const;
GLint getUniformLocation(const std::string &name) const;
bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
bool setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
bool setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
bool setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
bool setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
bool setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params);
bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
bool getUniformuiv(GLint location, GLsizei *bufSize, GLuint *params);
void dirtyAllUniforms();
void applyUniforms(Device *device);
void applyUniformBuffers(Device *device, BufferBinding* uniformBuffers);
void applyTransformFeedback(Device *device, TransformFeedback* transformFeedback);
void link();
bool isLinked() const;
size_t getInfoLogLength() const;
void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
GLint getFragDataLocation(const GLchar *name);
void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
size_t getActiveAttributeCount() const;
GLint getActiveAttributeMaxLength() const;
void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
size_t getActiveUniformCount() const;
GLint getActiveUniformMaxLength() const;
GLint getActiveUniformi(GLuint index, GLenum pname) const;
void getActiveUniformBlockName(GLuint index, GLsizei bufSize, GLsizei *length, GLchar *name) const;
size_t getActiveUniformBlockCount() const;
GLint getActiveUniformBlockMaxLength() const;
void setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode);
void getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const;
GLsizei getTransformFeedbackVaryingCount() const;
GLsizei getTransformFeedbackVaryingMaxLength() const;
GLenum getTransformFeedbackBufferMode() const;
void addRef();
void release();
unsigned int getRefCount() const;
void flagForDeletion();
bool isFlaggedForDeletion() const;
void validate(Device* device);
bool validateSamplers(bool logErrors);
bool isValidated() const;
unsigned int getSerial() const;
bool getBinaryRetrievableHint() const { return retrievableBinary; }
void setBinaryRetrievable(bool retrievable) { retrievableBinary = retrievable; }
GLint getBinaryLength() const;
private:
void unlink();
void resetUniformBlockBindings();
bool linkVaryings();
bool linkTransformFeedback();
bool linkAttributes();
bool linkAttribute(const glsl::Attribute &attribute, int location, unsigned int &usedLocations);
int getAttributeLocation(const std::string &name);
Uniform *getUniform(const std::string &name) const;
bool linkUniforms(const Shader *shader);
bool linkUniformBlocks(const Shader *vertexShader, const Shader *fragmentShader);
bool areMatchingUniformBlocks(const glsl::UniformBlock &block1, const glsl::UniformBlock &block2, const Shader *shader1, const Shader *shader2);
bool areMatchingFields(const std::vector<glsl::ShaderVariable>& fields1, const std::vector<glsl::ShaderVariable>& fields2, const std::string& name);
bool validateUniformStruct(GLenum shader, const glsl::Uniform &newUniformStruct);
bool defineUniform(GLenum shader, const glsl::Uniform &uniform, const Uniform::BlockInfo& blockInfo);
bool defineUniformBlock(const Shader *shader, const glsl::UniformBlock &block);
bool applyUniform(Device *device, GLint location, float* data);
bool applyUniform1bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
bool applyUniform2bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
bool applyUniform3bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
bool applyUniform4bv(Device *device, GLint location, GLsizei count, const GLboolean *v);
bool applyUniform1fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
bool applyUniform2fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
bool applyUniform3fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
bool applyUniform4fv(Device *device, GLint location, GLsizei count, const GLfloat *v);
bool applyUniformMatrix2fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix2x3fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix2x4fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix3fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix3x2fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix3x4fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix4fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix4x2fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniformMatrix4x3fv(Device *device, GLint location, GLsizei count, const GLfloat *value);
bool applyUniform1iv(Device *device, GLint location, GLsizei count, const GLint *v);
bool applyUniform2iv(Device *device, GLint location, GLsizei count, const GLint *v);
bool applyUniform3iv(Device *device, GLint location, GLsizei count, const GLint *v);
bool applyUniform4iv(Device *device, GLint location, GLsizei count, const GLint *v);
bool applyUniform1uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
bool applyUniform2uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
bool applyUniform3uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
bool applyUniform4uiv(Device *device, GLint location, GLsizei count, const GLuint *v);
bool setUniformfv(GLint location, GLsizei count, const GLfloat *v, int numElements);
bool setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum type);
bool setUniformiv(GLint location, GLsizei count, const GLint *v, int numElements);
bool setUniformuiv(GLint location, GLsizei count, const GLuint *v, int numElements);
void appendToInfoLog(const char *info, ...);
void resetInfoLog();
static unsigned int issueSerial();
FragmentShader *fragmentShader;
VertexShader *vertexShader;
sw::PixelShader *pixelBinary;
sw::VertexShader *vertexBinary;
std::map<std::string, GLuint> attributeBinding;
std::map<std::string, GLuint> linkedAttributeLocation;
std::vector<glsl::Attribute> linkedAttribute;
int attributeStream[MAX_VERTEX_ATTRIBS];
GLuint uniformBlockBindings[MAX_UNIFORM_BUFFER_BINDINGS];
std::vector<std::string> transformFeedbackVaryings;
GLenum transformFeedbackBufferMode;
size_t totalLinkedVaryingsComponents;
struct Sampler
{
bool active;
GLint logicalTextureUnit;
TextureType textureType;
};
Sampler samplersPS[MAX_TEXTURE_IMAGE_UNITS];
Sampler samplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS];
typedef std::vector<Uniform*> UniformArray;
UniformArray uniforms;
typedef std::vector<Uniform> UniformStructArray;
UniformStructArray uniformStructs;
typedef std::vector<UniformLocation> UniformIndex;
UniformIndex uniformIndex;
typedef std::vector<UniformBlock*> UniformBlockArray;
UniformBlockArray uniformBlocks;
typedef std::vector<LinkedVarying> LinkedVaryingArray;
LinkedVaryingArray transformFeedbackLinkedVaryings;
bool linked;
bool orphaned; // Flag to indicate that the program can be deleted when no longer in use
char *infoLog;
bool validated;
bool retrievableBinary;
unsigned int referenceCount;
const unsigned int serial;
static unsigned int currentSerial;
ResourceManager *resourceManager;
const GLuint handle;
};
}
#endif // LIBGLESV2_PROGRAM_H_