blob: 1e9c5e2aec6a413d79b70a8255b75fa7f85e8e26 [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
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000131 bool link(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000132 void getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders);
133
134 void getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
135 GLint getActiveAttributeCount();
136 GLint getActiveAttributeMaxLength();
137
138 void getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name);
139 GLint getActiveUniformCount();
140 GLint getActiveUniformMaxLength();
141
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000142 void validate(InfoLog &infoLog);
143 bool validateSamplers(InfoLog *infoLog);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000144 bool isValidated() const;
145
146 static std::string decorateAttribute(const std::string &name); // Prepend an underscore
147 static std::string undecorateUniform(const std::string &_name); // Remove leading underscore
148
149 private:
150 DISALLOW_COPY_AND_ASSIGN(ProgramBinary);
151
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000152 ID3D10Blob *compileToBinary(InfoLog &infoLog, const char *hlsl, const char *profile, ID3DXConstantTable **constantTable);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000153
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000154 int packVaryings(InfoLog &infoLog, const Varying *packing[][4], FragmentShader *fragmentShader);
155 bool linkVaryings(InfoLog &infoLog, std::string& pixelHLSL, std::string& vertexHLSL, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000156
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000157 bool linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, FragmentShader *fragmentShader, VertexShader *vertexShader);
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000158
apatrick@chromium.org253b8d22012-06-22 19:27:21 +0000159 bool linkUniforms(InfoLog &infoLog, GLenum shader, ID3DXConstantTable *constantTable);
160 bool defineUniform(InfoLog &infoLog, GLenum shader, const D3DXHANDLE &constantHandle, const D3DXCONSTANT_DESC &constantDescription, std::string name = "");
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000161 bool defineUniform(GLenum shader, const D3DXCONSTANT_DESC &constantDescription, const std::string &name);
162 Uniform *createUniform(const D3DXCONSTANT_DESC &constantDescription, const std::string &name);
163 bool applyUniformnfv(Uniform *targetUniform, const GLfloat *v);
164 bool applyUniform1iv(Uniform *targetUniform, GLsizei count, const GLint *v);
165 bool applyUniform2iv(Uniform *targetUniform, GLsizei count, const GLint *v);
166 bool applyUniform3iv(Uniform *targetUniform, GLsizei count, const GLint *v);
167 bool applyUniform4iv(Uniform *targetUniform, GLsizei count, const GLint *v);
168 void applyUniformniv(Uniform *targetUniform, GLsizei count, const D3DXVECTOR4 *vector);
169 void applyUniformnbv(Uniform *targetUniform, GLsizei count, int width, const GLboolean *v);
170
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000171 IDirect3DDevice9 *mDevice;
172
173 IDirect3DPixelShader9 *mPixelExecutable;
174 IDirect3DVertexShader9 *mVertexExecutable;
175
176 // These are only used during linking.
177 ID3DXConstantTable *mConstantTablePS;
178 ID3DXConstantTable *mConstantTableVS;
179
180 Attribute mLinkedAttribute[MAX_VERTEX_ATTRIBS];
181 int mSemanticIndex[MAX_VERTEX_ATTRIBS];
182
183 struct Sampler
184 {
185 bool active;
186 GLint logicalTextureUnit;
187 TextureType textureType;
188 };
189
190 Sampler mSamplersPS[MAX_TEXTURE_IMAGE_UNITS];
191 Sampler mSamplersVS[MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF];
192 GLuint mUsedVertexSamplerRange;
193 GLuint mUsedPixelSamplerRange;
194
195 typedef std::vector<Uniform*> UniformArray;
196 UniformArray mUniforms;
197 typedef std::vector<UniformLocation> UniformIndex;
198 UniformIndex mUniformIndex;
199
200 GLint mDxDepthRangeLocation;
201 GLint mDxDepthLocation;
202 GLint mDxCoordLocation;
203 GLint mDxHalfPixelSizeLocation;
204 GLint mDxFrontCCWLocation;
205 GLint mDxPointsOrLinesLocation;
206
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +0000207 bool mValidated;
208};
209}
210
211#endif // LIBGLESV2_PROGRAM_BINARY_H_