Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 1 | // SwiftShader Software Renderer
|
| 2 | //
|
| 3 | // Copyright(c) 2005-2013 TransGaming Inc.
|
| 4 | //
|
| 5 | // All rights reserved. No part of this software may be copied, distributed, transmitted,
|
| 6 | // transcribed, stored in a retrieval system, translated into any human or computer
|
| 7 | // language by any means, or disclosed to third parties without the explicit written
|
| 8 | // agreement of TransGaming Inc. Without such an agreement, no rights or licenses, express
|
| 9 | // or implied, including but not limited to any patent rights, are granted to you.
|
| 10 | //
|
| 11 |
|
| 12 | // Program.h: Defines the Program class. Implements GL program objects
|
Nicolas Capens | a9b4937 | 2015-01-30 00:33:26 -0500 | [diff] [blame] | 13 | // and related functionality.
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 14 |
|
Nicolas Capens | 79eef88 | 2015-01-09 17:38:27 -0500 | [diff] [blame] | 15 | #ifndef LIBGL_PROGRAM_H_
|
| 16 | #define LIBGL_PROGRAM_H_
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 17 |
|
| 18 | #include "Shader.h"
|
| 19 | #include "Context.h"
|
| 20 | #include "Shader/PixelShader.hpp"
|
| 21 | #include "Shader/VertexShader.hpp"
|
| 22 |
|
| 23 | #include <string>
|
| 24 | #include <vector>
|
| 25 | #include <set>
|
| 26 |
|
Nicolas Capens | f4486fd | 2015-01-22 11:10:37 -0500 | [diff] [blame] | 27 | namespace gl
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 28 | {
|
| 29 | class Device;
|
| 30 | class ResourceManager;
|
| 31 | class FragmentShader;
|
| 32 | class VertexShader;
|
| 33 |
|
| 34 | // Helper struct representing a single shader uniform
|
| 35 | struct Uniform
|
| 36 | {
|
| 37 | Uniform(GLenum type, GLenum precision, const std::string &name, unsigned int arraySize);
|
| 38 |
|
| 39 | ~Uniform();
|
| 40 |
|
| 41 | bool isArray() const;
|
| 42 | int size() const;
|
| 43 | int registerCount() const;
|
| 44 |
|
| 45 | const GLenum type;
|
| 46 | const GLenum precision;
|
| 47 | const std::string name;
|
| 48 | const unsigned int arraySize;
|
| 49 |
|
| 50 | unsigned char *data;
|
| 51 | bool dirty;
|
| 52 |
|
| 53 | short psRegisterIndex;
|
| 54 | short vsRegisterIndex;
|
| 55 | };
|
| 56 |
|
| 57 | // Struct used for correlating uniforms/elements of uniform arrays to handles
|
| 58 | struct UniformLocation
|
| 59 | {
|
| 60 | UniformLocation(const std::string &name, unsigned int element, unsigned int index);
|
| 61 |
|
| 62 | std::string name;
|
| 63 | unsigned int element;
|
| 64 | unsigned int index;
|
| 65 | };
|
| 66 |
|
| 67 | class Program
|
| 68 | {
|
| 69 | public:
|
| 70 | Program(ResourceManager *manager, GLuint handle);
|
| 71 |
|
| 72 | ~Program();
|
| 73 |
|
| 74 | bool attachShader(Shader *shader);
|
| 75 | bool detachShader(Shader *shader);
|
| 76 | int getAttachedShadersCount() const;
|
| 77 |
|
| 78 | sw::PixelShader *getPixelShader();
|
| 79 | sw::VertexShader *getVertexShader();
|
| 80 |
|
| 81 | void bindAttributeLocation(GLuint index, const char *name);
|
| 82 | GLuint getAttributeLocation(const char *name);
|
| 83 | int getAttributeStream(int attributeIndex);
|
| 84 |
|
| 85 | GLint getSamplerMapping(sw::SamplerType type, unsigned int samplerIndex);
|
| 86 | TextureType getSamplerTextureType(sw::SamplerType type, unsigned int samplerIndex);
|
| 87 |
|
| 88 | GLint getUniformLocation(std::string name);
|
| 89 | bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
|
| 90 | bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
|
| 91 | bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
|
| 92 | bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
|
| 93 | bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
|
| 94 | bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
|
| 95 | bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
|
| 96 | bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
|
| 97 | bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
|
| 98 | bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
|
| 99 | bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
|
| 100 |
|
| 101 | bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params);
|
| 102 | bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
|
| 103 |
|
| 104 | void dirtyAllUniforms();
|
| 105 | void applyUniforms();
|
| 106 |
|
| 107 | void link();
|
| 108 | bool isLinked();
|
| 109 | int getInfoLogLength() const;
|
| 110 | void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
|
| 111 | void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
|
| 112 |
|
| 113 | void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
|
Alexis Hetu | 0085c44 | 2015-06-12 15:19:20 -0400 | [diff] [blame] | 114 | size_t getActiveAttributeCount() const;
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 115 | GLint getActiveAttributeMaxLength() const;
|
| 116 |
|
| 117 | void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const;
|
Alexis Hetu | 0085c44 | 2015-06-12 15:19:20 -0400 | [diff] [blame] | 118 | size_t getActiveUniformCount() const;
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 119 | GLint getActiveUniformMaxLength() const;
|
| 120 |
|
| 121 | void addRef();
|
| 122 | void release();
|
| 123 | unsigned int getRefCount() const;
|
| 124 | void flagForDeletion();
|
| 125 | bool isFlaggedForDeletion() const;
|
| 126 |
|
| 127 | void validate();
|
| 128 | bool validateSamplers(bool logErrors);
|
| 129 | bool isValidated() const;
|
| 130 |
|
| 131 | unsigned int getSerial() const;
|
| 132 |
|
| 133 | private:
|
| 134 | void unlink();
|
| 135 |
|
Nicolas Capens | 5597eef | 2015-01-22 09:33:04 -0500 | [diff] [blame] | 136 | int packVaryings(const glsl::Varying *packing[][4]);
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 137 | bool linkVaryings();
|
| 138 |
|
| 139 | bool linkAttributes();
|
| 140 | int getAttributeBinding(const std::string &name);
|
| 141 |
|
| 142 | bool linkUniforms(Shader *shader);
|
| 143 | bool defineUniform(GLenum shader, GLenum type, GLenum precision, const std::string &_name, unsigned int arraySize, int registerIndex);
|
| 144 | bool applyUniform1bv(GLint location, GLsizei count, const GLboolean *v);
|
| 145 | bool applyUniform2bv(GLint location, GLsizei count, const GLboolean *v);
|
| 146 | bool applyUniform3bv(GLint location, GLsizei count, const GLboolean *v);
|
| 147 | bool applyUniform4bv(GLint location, GLsizei count, const GLboolean *v);
|
| 148 | bool applyUniform1fv(GLint location, GLsizei count, const GLfloat *v);
|
| 149 | bool applyUniform2fv(GLint location, GLsizei count, const GLfloat *v);
|
| 150 | bool applyUniform3fv(GLint location, GLsizei count, const GLfloat *v);
|
| 151 | bool applyUniform4fv(GLint location, GLsizei count, const GLfloat *v);
|
| 152 | bool applyUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
|
| 153 | bool applyUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
|
| 154 | bool applyUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
|
| 155 | bool applyUniform1iv(GLint location, GLsizei count, const GLint *v);
|
| 156 | bool applyUniform2iv(GLint location, GLsizei count, const GLint *v);
|
| 157 | bool applyUniform3iv(GLint location, GLsizei count, const GLint *v);
|
| 158 | bool applyUniform4iv(GLint location, GLsizei count, const GLint *v);
|
| 159 |
|
| 160 | void appendToInfoLog(const char *info, ...);
|
| 161 | void resetInfoLog();
|
| 162 |
|
| 163 | static unsigned int issueSerial();
|
| 164 |
|
| 165 | private:
|
Nicolas Capens | f4486fd | 2015-01-22 11:10:37 -0500 | [diff] [blame] | 166 | gl::Device *device;
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 167 | FragmentShader *fragmentShader;
|
| 168 | VertexShader *vertexShader;
|
| 169 |
|
| 170 | sw::PixelShader *pixelBinary;
|
| 171 | sw::VertexShader *vertexBinary;
|
| 172 |
|
| 173 | std::set<std::string> attributeBinding[MAX_VERTEX_ATTRIBS];
|
Nicolas Capens | 5597eef | 2015-01-22 09:33:04 -0500 | [diff] [blame] | 174 | glsl::Attribute linkedAttribute[MAX_VERTEX_ATTRIBS];
|
Nicolas Capens | 264f152 | 2015-01-09 17:21:17 -0500 | [diff] [blame] | 175 | int attributeStream[MAX_VERTEX_ATTRIBS];
|
| 176 |
|
| 177 | struct Sampler
|
| 178 | {
|
| 179 | bool active;
|
| 180 | GLint logicalTextureUnit;
|
| 181 | TextureType textureType;
|
| 182 | };
|
| 183 |
|
| 184 | Sampler samplersPS[MAX_TEXTURE_IMAGE_UNITS];
|
| 185 | Sampler samplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS];
|
| 186 |
|
| 187 | typedef std::vector<Uniform*> UniformArray;
|
| 188 | UniformArray uniforms;
|
| 189 | typedef std::vector<UniformLocation> UniformIndex;
|
| 190 | UniformIndex uniformIndex;
|
| 191 |
|
| 192 | bool linked;
|
| 193 | bool orphaned; // Flag to indicate that the program can be deleted when no longer in use
|
| 194 | char *infoLog;
|
| 195 | bool validated;
|
| 196 |
|
| 197 | unsigned int referenceCount;
|
| 198 | const unsigned int serial;
|
| 199 |
|
| 200 | static unsigned int currentSerial;
|
| 201 |
|
| 202 | ResourceManager *resourceManager;
|
| 203 | const GLuint handle;
|
| 204 | };
|
| 205 | }
|
| 206 |
|
Nicolas Capens | 79eef88 | 2015-01-09 17:38:27 -0500 | [diff] [blame] | 207 | #endif // LIBGL_PROGRAM_H_
|