blob: cdc717260175dce9c1e93f690b35d7c236efbba3 [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
13#include <d3dx9.h>
14#include <d3dcompiler.h>
15#include <string>
16#include <vector>
17
18#include "libGLESv2/Shader.h"
19#include "libGLESv2/Context.h"
20
21namespace gl
22{
23class FragmentShader;
24class VertexShader;
25
26// Helper struct representing a single shader uniform
27struct Uniform
28{
29 Uniform(GLenum type, const std::string &_name, unsigned int arraySize);
30
31 ~Uniform();
32
33 bool isArray();
34
35 const GLenum type;
36 const std::string _name; // Decorated name
37 const std::string name; // Undecorated name
38 const unsigned int arraySize;
39
40 unsigned char *data;
41 bool dirty;
42
43 struct RegisterInfo
44 {
45 RegisterInfo()
46 {
47 float4Index = -1;
48 samplerIndex = -1;
49 boolIndex = -1;
50 registerCount = 0;
51 }
52
53 void set(const D3DXCONSTANT_DESC &constantDescription)
54 {
55 switch(constantDescription.RegisterSet)
56 {
57 case D3DXRS_BOOL: boolIndex = constantDescription.RegisterIndex; break;
58 case D3DXRS_FLOAT4: float4Index = constantDescription.RegisterIndex; break;
59 case D3DXRS_SAMPLER: samplerIndex = constantDescription.RegisterIndex; break;
60 default: UNREACHABLE();
61 }
62
63 ASSERT(registerCount == 0 || registerCount == (int)constantDescription.RegisterCount);
64 registerCount = constantDescription.RegisterCount;
65 }
66
67 int float4Index;
68 int samplerIndex;
69 int boolIndex;
70
71 int registerCount;
72 };
73
74 RegisterInfo ps;
75 RegisterInfo vs;
76};
77
78// Struct used for correlating uniforms/elements of uniform arrays to handles
79struct UniformLocation
80{
81 UniformLocation(const std::string &_name, unsigned int element, unsigned int index);
82
83 std::string name;
84 unsigned int element;
85 unsigned int index;
86};
87
88// This is the result of linking a program. It is the state that would be passed to ProgramBinary.
89class ProgramBinary
90{
91 public:
92 ProgramBinary();
93 ~ProgramBinary();
94
95 IDirect3DPixelShader9 *getPixelShader();
96 IDirect3DVertexShader9 *getVertexShader();
97
98 GLuint getAttributeLocation(const char *name);
99 int getSemanticIndex(int attributeIndex);
100
101 GLint getSamplerMapping(SamplerType type, unsigned int samplerIndex);
102 TextureType getSamplerTextureType(SamplerType type, unsigned int samplerIndex);
103 GLint getUsedSamplerRange(SamplerType type);
104
105 GLint getUniformLocation(std::string name);
106 bool setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
107 bool setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
108 bool setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
109 bool setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
110 bool setUniformMatrix2fv(GLint location, GLsizei count, const GLfloat *value);
111 bool setUniformMatrix3fv(GLint location, GLsizei count, const GLfloat *value);
112 bool setUniformMatrix4fv(GLint location, GLsizei count, const GLfloat *value);
113 bool setUniform1iv(GLint location, GLsizei count, const GLint *v);
114 bool setUniform2iv(GLint location, GLsizei count, const GLint *v);
115 bool setUniform3iv(GLint location, GLsizei count, const GLint *v);
116 bool setUniform4iv(GLint location, GLsizei count, const GLint *v);
117
118 bool getUniformfv(GLint location, GLsizei *bufSize, GLfloat *params);
119 bool getUniformiv(GLint location, GLsizei *bufSize, GLint *params);
120
121 GLint getDxDepthRangeLocation() const;
122 GLint getDxDepthLocation() const;
123 GLint getDxCoordLocation() const;
124 GLint getDxHalfPixelSizeLocation() const;
125 GLint getDxFrontCCWLocation() const;
126 GLint getDxPointsOrLinesLocation() const;
127
128 void dirtyAllUniforms();
129 void applyUniforms();
130
131 bool link(const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
132 int getInfoLogLength() const;
133 void getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog);
134 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
135
136 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
137 GLint getActiveAttributeCount();
138 GLint getActiveAttributeMaxLength();
139
140 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
141 GLint getActiveUniformCount();
142 GLint getActiveUniformMaxLength();
143
144 void validate();
145 bool validateSamplers(bool logErrors);
146 bool isValidated() const;
147
148 static std::string decorateAttribute(const std::string &name); // Prepend an underscore
149 static std::string undecorateUniform(const std::string &_name); // Remove leading underscore
150
151 private:
152 DISALLOW_COPY_AND_ASSIGN(ProgramBinary);
153
154 ID3D10Blob *compileToBinary(const char *hlsl, const char *profile, ID3DXConstantTable **constantTable);
155
156 int packVaryings(const Varying *packing[][4], FragmentShader *fragmentShader);
157 bool linkVaryings(std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader);
158
159 bool linkAttributes(const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
160
161 bool linkUniforms(GLenum shader, ID3DXConstantTable *constantTable);
162 bool defineUniform(GLenum shader, const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name = "");
163 bool defineUniform(GLenum shader, const D3DXCONSTANT_DESC &constantDescription, const std::string &name);
164 Uniform *createUniform(const D3DXCONSTANT_DESC &constantDescription, const std::string &name);
165 bool applyUniformnfv(Uniform *targetUniform, const GLfloat *v);
166 bool applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v);
167 bool applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v);
168 bool applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v);
169 bool applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v);
170 void applyUniformniv(Uniform *targetUniform, GLsizei count, const D3DXVECTOR4 *vector);
171 void applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v);
172
173 void appendToInfoLogSanitized(const char *message);
174 void appendToInfoLog(const char *info, ...);
175 void resetInfoLog();
176
177 static unsigned int issueSerial();
178
179 IDirect3DDevice9 *mDevice;
180
181 IDirect3DPixelShader9 *mPixelExecutable;
182 IDirect3DVertexShader9 *mVertexExecutable;
183
184 // These are only used during linking.
185 ID3DXConstantTable *mConstantTablePS;
186 ID3DXConstantTable *mConstantTableVS;
187
188 Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
189 int mSemanticIndex[MAX_VERTEX_ATTRIBS];
190
191 struct Sampler
192 {
193 bool active;
194 GLint logicalTextureUnit;
195 TextureType textureType;
196 };
197
198 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS];
199 Sampler mSamplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF];
200 GLuint mUsedVertexSamplerRange;
201 GLuint mUsedPixelSamplerRange;
202
203 typedef std::vector<Uniform*> UniformArray;
204 UniformArray mUniforms;
205 typedef std::vector<UniformLocation> UniformIndex;
206 UniformIndex mUniformIndex;
207
208 GLint mDxDepthRangeLocation;
209 GLint mDxDepthLocation;
210 GLint mDxCoordLocation;
211 GLint mDxHalfPixelSizeLocation;
212 GLint mDxFrontCCWLocation;
213 GLint mDxPointsOrLinesLocation;
214
215 char *mInfoLog;
216 bool mValidated;
217};
218}
219
220#endif // LIBGLESV2_PROGRAM_BINARY_H_