blob: 85b72e19a69f0a0d7f416e8c13893af7372e72d8 [file] [log] [blame]
Brandon Jonesc9610c52014-08-25 17:02:59 -07001//
2// Copyright (c) 2014 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// ProgramD3D.h: Defines the rx::ProgramD3D class which implements rx::ProgramImpl.
8
9#ifndef LIBGLESV2_RENDERER_PROGRAMD3D_H_
10#define LIBGLESV2_RENDERER_PROGRAMD3D_H_
11
12#include "libGLESv2/renderer/ProgramImpl.h"
Jamie Madill7acae0a2014-09-24 17:10:51 -040013#include "libGLESv2/renderer/Workarounds.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070014
Brandon Jones22502d52014-08-29 16:58:36 -070015#include <string>
16#include <vector>
17
Brandon Jonesc9610c52014-08-25 17:02:59 -070018namespace gl
19{
20struct LinkedUniform;
Brandon Jones22502d52014-08-29 16:58:36 -070021struct VariableLocation;
Brandon Jonesc9610c52014-08-25 17:02:59 -070022struct VertexFormat;
23}
24
25namespace rx
26{
Jamie Madill93e13fb2014-11-06 15:27:25 -050027class RendererD3D;
Brandon Jonesc9610c52014-08-25 17:02:59 -070028class UniformStorage;
29
30class ProgramD3D : public ProgramImpl
31{
32 public:
Jamie Madill93e13fb2014-11-06 15:27:25 -050033 ProgramD3D(rx::RendererD3D *renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -070034 virtual ~ProgramD3D();
35
36 static ProgramD3D *makeProgramD3D(ProgramImpl *impl);
37 static const ProgramD3D *makeProgramD3D(const ProgramImpl *impl);
38
Brandon Jones22502d52014-08-29 16:58:36 -070039 const std::vector<rx::PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; }
Brandon Jones44151a92014-09-10 11:32:25 -070040 int getShaderVersion() const { return mShaderVersion; }
Brandon Joneseb994362014-09-24 10:27:28 -070041 GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; }
Brandon Jones1a8a7e32014-10-01 12:49:30 -070042
43 GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const;
44 GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const;
45 GLint getUsedSamplerRange(gl::SamplerType type) const;
46 void updateSamplerMapping();
47 bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps);
Brandon Jones44151a92014-09-10 11:32:25 -070048
Brandon Joneseb994362014-09-24 10:27:28 -070049 bool usesPointSize() const { return mUsesPointSize; }
Brandon Jones44151a92014-09-10 11:32:25 -070050 bool usesPointSpriteEmulation() const;
51 bool usesGeometryShader() const;
Brandon Jones22502d52014-08-29 16:58:36 -070052
53 GLenum getBinaryFormat() { return GL_PROGRAM_BINARY_ANGLE; }
Geoff Langb543aff2014-09-30 14:52:54 -040054 gl::LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream);
55 gl::Error save(gl::BinaryOutputStream *stream);
Brandon Jones22502d52014-08-29 16:58:36 -070056
Geoff Langb543aff2014-09-30 14:52:54 -040057 gl::Error getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExectuable);
58 gl::Error getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputLayout, ShaderExecutable **outExectuable);
59 gl::Error getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], ShaderExecutable **outExectuable);
Brandon Joneseb994362014-09-24 10:27:28 -070060 ShaderExecutable *getGeometryExecutable() const { return mGeometryExecutable; }
61
Geoff Langb543aff2014-09-30 14:52:54 -040062 gl::LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
63 int registers);
Brandon Jones22502d52014-08-29 16:58:36 -070064
Jamie Madillde8892b2014-11-11 13:00:22 -050065 gl::LinkResult link(const gl::Data &data, gl::InfoLog &infoLog,
66 gl::Shader *fragmentShader, gl::Shader *vertexShader,
67 const std::vector<std::string> &transformFeedbackVaryings,
68 GLenum transformFeedbackBufferMode,
Geoff Langb543aff2014-09-30 14:52:54 -040069 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
Jamie Madillde8892b2014-11-11 13:00:22 -050070 std::map<int, gl::VariableLocation> *outputVariables);
Brandon Jonesc9610c52014-08-25 17:02:59 -070071
Brandon Jones44151a92014-09-10 11:32:25 -070072 void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const;
73
Brandon Jones1a8a7e32014-10-01 12:49:30 -070074 void initializeUniformStorage();
75 gl::Error applyUniforms();
76 gl::Error applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps);
Brandon Jones18bd4102014-09-22 14:21:44 -070077 bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
78 unsigned int registerIndex, const gl::Caps &caps);
Brandon Jones1a8a7e32014-10-01 12:49:30 -070079 void dirtyAllUniforms();
80
81 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
82 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
83 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
84 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
85 void setUniform1iv(GLint location, GLsizei count, const GLint *v);
86 void setUniform2iv(GLint location, GLsizei count, const GLint *v);
87 void setUniform3iv(GLint location, GLsizei count, const GLint *v);
88 void setUniform4iv(GLint location, GLsizei count, const GLint *v);
89 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
90 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
91 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
92 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
93 void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
94 void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
95 void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
96 void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
97 void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
98 void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
99 void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
100 void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
101 void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
102
103 void getUniformfv(GLint location, GLfloat *params);
104 void getUniformiv(GLint location, GLint *params);
105 void getUniformuiv(GLint location, GLuint *params);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700106
107 const UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; }
108 const UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; }
109
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700110 bool linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
111 const gl::Caps &caps);
112 bool defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock, const gl::Caps &caps);
113
Brandon Jonesc9610c52014-08-25 17:02:59 -0700114 void reset();
115
116 private:
117 DISALLOW_COPY_AND_ASSIGN(ProgramD3D);
118
Brandon Joneseb994362014-09-24 10:27:28 -0700119 class VertexExecutable
120 {
121 public:
122 VertexExecutable(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
123 const GLenum signature[gl::MAX_VERTEX_ATTRIBS],
124 rx::ShaderExecutable *shaderExecutable);
125 ~VertexExecutable();
126
127 bool matchesSignature(const GLenum convertedLayout[gl::MAX_VERTEX_ATTRIBS]) const;
128
129 const gl::VertexFormat *inputs() const { return mInputs; }
130 const GLenum *signature() const { return mSignature; }
131 rx::ShaderExecutable *shaderExecutable() const { return mShaderExecutable; }
132
133 private:
134 gl::VertexFormat mInputs[gl::MAX_VERTEX_ATTRIBS];
135 GLenum mSignature[gl::MAX_VERTEX_ATTRIBS];
136 rx::ShaderExecutable *mShaderExecutable;
137 };
138
139 class PixelExecutable
140 {
141 public:
142 PixelExecutable(const std::vector<GLenum> &outputSignature, rx::ShaderExecutable *shaderExecutable);
143 ~PixelExecutable();
144
145 bool matchesSignature(const std::vector<GLenum> &signature) const { return mOutputSignature == signature; }
146
147 const std::vector<GLenum> &outputSignature() const { return mOutputSignature; }
148 rx::ShaderExecutable *shaderExecutable() const { return mShaderExecutable; }
149
150 private:
151 std::vector<GLenum> mOutputSignature;
152 rx::ShaderExecutable *mShaderExecutable;
153 };
154
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700155 struct Sampler
156 {
157 Sampler();
158
159 bool active;
160 GLint logicalTextureUnit;
161 GLenum textureType;
162 };
163
164 void defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister);
165 void defineUniform(GLenum shader, const sh::ShaderVariable &uniform, const std::string &fullName,
166 sh::HLSLBlockEncoder *encoder);
167 bool indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps);
168 bool indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps);
169 static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount,
170 std::vector<Sampler> &outSamplers, GLuint *outUsedRange);
171
172 template <typename T>
173 void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
174
175 template <int cols, int rows>
176 void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
177
178 template <typename T>
179 void getUniformv(GLint location, T *params, GLenum uniformType);
180
181 template <typename VarT>
182 void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
183 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
184 bool inRowMajorLayout);
185
Jamie Madill93e13fb2014-11-06 15:27:25 -0500186 RendererD3D *mRenderer;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700187 DynamicHLSL *mDynamicHLSL;
188
Brandon Joneseb994362014-09-24 10:27:28 -0700189 std::vector<VertexExecutable *> mVertexExecutables;
190 std::vector<PixelExecutable *> mPixelExecutables;
191 rx::ShaderExecutable *mGeometryExecutable;
192
Brandon Jones22502d52014-08-29 16:58:36 -0700193 std::string mVertexHLSL;
194 rx::D3DWorkaroundType mVertexWorkarounds;
195
196 std::string mPixelHLSL;
197 rx::D3DWorkaroundType mPixelWorkarounds;
198 bool mUsesFragDepth;
199 std::vector<rx::PixelShaderOutputVariable> mPixelShaderKey;
200
Brandon Jones44151a92014-09-10 11:32:25 -0700201 bool mUsesPointSize;
202
Brandon Jonesc9610c52014-08-25 17:02:59 -0700203 UniformStorage *mVertexUniformStorage;
204 UniformStorage *mFragmentUniformStorage;
Brandon Jones44151a92014-09-10 11:32:25 -0700205
Brandon Joneseb994362014-09-24 10:27:28 -0700206 GLenum mTransformFeedbackBufferMode;
Brandon Joneseb994362014-09-24 10:27:28 -0700207
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700208 std::vector<Sampler> mSamplersPS;
209 std::vector<Sampler> mSamplersVS;
210 GLuint mUsedVertexSamplerRange;
211 GLuint mUsedPixelSamplerRange;
212 bool mDirtySamplerMapping;
Brandon Joneseb994362014-09-24 10:27:28 -0700213
Brandon Jones44151a92014-09-10 11:32:25 -0700214 int mShaderVersion;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700215};
216
217}
218
219#endif // LIBGLESV2_RENDERER_PROGRAMD3D_H_