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