blob: 7b6219ad10b2766c52083248f0cc67691707ac5d [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
Geoff Lang0a73dd82014-11-19 16:18:08 -05009#ifndef LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_
10#define LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_
Brandon Jonesc9610c52014-08-25 17:02:59 -070011
Geoff Lang2b5420c2014-11-19 14:20:15 -050012#include "libANGLE/renderer/ProgramImpl.h"
13#include "libANGLE/renderer/Workarounds.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050014#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070015
Brandon Jones22502d52014-08-29 16:58:36 -070016#include <string>
17#include <vector>
18
Brandon Jonesc9610c52014-08-25 17:02:59 -070019namespace gl
20{
21struct LinkedUniform;
Brandon Jones22502d52014-08-29 16:58:36 -070022struct VariableLocation;
Brandon Jonesc9610c52014-08-25 17:02:59 -070023struct VertexFormat;
24}
25
26namespace rx
27{
Jamie Madill93e13fb2014-11-06 15:27:25 -050028class RendererD3D;
Brandon Jonesc9610c52014-08-25 17:02:59 -070029class UniformStorage;
Geoff Lang7dd2e102014-11-10 15:19:26 -050030class ShaderExecutable;
Brandon Jonesc9610c52014-08-25 17:02:59 -070031
32class ProgramD3D : public ProgramImpl
33{
34 public:
Jamie Madill30d6c252014-11-13 10:03:33 -050035 ProgramD3D(RendererD3D *renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -070036 virtual ~ProgramD3D();
37
38 static ProgramD3D *makeProgramD3D(ProgramImpl *impl);
39 static const ProgramD3D *makeProgramD3D(const ProgramImpl *impl);
40
Jamie Madill30d6c252014-11-13 10:03:33 -050041 const std::vector<PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; }
Brandon Jones44151a92014-09-10 11:32:25 -070042 int getShaderVersion() const { return mShaderVersion; }
Brandon Joneseb994362014-09-24 10:27:28 -070043 GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; }
Brandon Jones1a8a7e32014-10-01 12:49:30 -070044
45 GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const;
46 GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const;
47 GLint getUsedSamplerRange(gl::SamplerType type) const;
48 void updateSamplerMapping();
49 bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps);
Brandon Jones44151a92014-09-10 11:32:25 -070050
Brandon Joneseb994362014-09-24 10:27:28 -070051 bool usesPointSize() const { return mUsesPointSize; }
Brandon Jones44151a92014-09-10 11:32:25 -070052 bool usesPointSpriteEmulation() const;
53 bool usesGeometryShader() const;
Brandon Jones22502d52014-08-29 16:58:36 -070054
55 GLenum getBinaryFormat() { return GL_PROGRAM_BINARY_ANGLE; }
Geoff Lang7dd2e102014-11-10 15:19:26 -050056 LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream);
Geoff Langb543aff2014-09-30 14:52:54 -040057 gl::Error save(gl::BinaryOutputStream *stream);
Brandon Jones22502d52014-08-29 16:58:36 -070058
Geoff Langb543aff2014-09-30 14:52:54 -040059 gl::Error getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExectuable);
60 gl::Error getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputLayout, ShaderExecutable **outExectuable);
61 gl::Error getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], ShaderExecutable **outExectuable);
Brandon Joneseb994362014-09-24 10:27:28 -070062 ShaderExecutable *getGeometryExecutable() const { return mGeometryExecutable; }
63
Geoff Lang7dd2e102014-11-10 15:19:26 -050064 LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
65 int registers);
Brandon Jones22502d52014-08-29 16:58:36 -070066
Geoff Lang7dd2e102014-11-10 15:19:26 -050067 LinkResult link(const gl::Data &data, gl::InfoLog &infoLog,
68 gl::Shader *fragmentShader, gl::Shader *vertexShader,
69 const std::vector<std::string> &transformFeedbackVaryings,
70 GLenum transformFeedbackBufferMode,
71 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
72 std::map<int, gl::VariableLocation> *outputVariables);
Brandon Jonesc9610c52014-08-25 17:02:59 -070073
Brandon Jones44151a92014-09-10 11:32:25 -070074 void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const;
75
Brandon Jones1a8a7e32014-10-01 12:49:30 -070076 void initializeUniformStorage();
77 gl::Error applyUniforms();
78 gl::Error applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps);
Brandon Jones18bd4102014-09-22 14:21:44 -070079 bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
80 unsigned int registerIndex, const gl::Caps &caps);
Brandon Jones1a8a7e32014-10-01 12:49:30 -070081 void dirtyAllUniforms();
82
83 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
84 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
85 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
86 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
87 void setUniform1iv(GLint location, GLsizei count, const GLint *v);
88 void setUniform2iv(GLint location, GLsizei count, const GLint *v);
89 void setUniform3iv(GLint location, GLsizei count, const GLint *v);
90 void setUniform4iv(GLint location, GLsizei count, const GLint *v);
91 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
92 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
93 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
94 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
95 void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
96 void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
97 void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
98 void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
99 void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
100 void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
101 void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
102 void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
103 void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
104
105 void getUniformfv(GLint location, GLfloat *params);
106 void getUniformiv(GLint location, GLint *params);
107 void getUniformuiv(GLint location, GLuint *params);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700108
109 const UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; }
110 const UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; }
111
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700112 bool linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
113 const gl::Caps &caps);
114 bool defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock, const gl::Caps &caps);
115
Brandon Jonesc9610c52014-08-25 17:02:59 -0700116 void reset();
117
Geoff Lang7dd2e102014-11-10 15:19:26 -0500118 unsigned int getSerial() const;
119
Brandon Jonesc9610c52014-08-25 17:02:59 -0700120 private:
121 DISALLOW_COPY_AND_ASSIGN(ProgramD3D);
122
Brandon Joneseb994362014-09-24 10:27:28 -0700123 class VertexExecutable
124 {
125 public:
126 VertexExecutable(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
127 const GLenum signature[gl::MAX_VERTEX_ATTRIBS],
Jamie Madill30d6c252014-11-13 10:03:33 -0500128 ShaderExecutable *shaderExecutable);
Brandon Joneseb994362014-09-24 10:27:28 -0700129 ~VertexExecutable();
130
131 bool matchesSignature(const GLenum convertedLayout[gl::MAX_VERTEX_ATTRIBS]) const;
132
133 const gl::VertexFormat *inputs() const { return mInputs; }
134 const GLenum *signature() const { return mSignature; }
Jamie Madill30d6c252014-11-13 10:03:33 -0500135 ShaderExecutable *shaderExecutable() const { return mShaderExecutable; }
Brandon Joneseb994362014-09-24 10:27:28 -0700136
137 private:
138 gl::VertexFormat mInputs[gl::MAX_VERTEX_ATTRIBS];
139 GLenum mSignature[gl::MAX_VERTEX_ATTRIBS];
Jamie Madill30d6c252014-11-13 10:03:33 -0500140 ShaderExecutable *mShaderExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700141 };
142
143 class PixelExecutable
144 {
145 public:
Jamie Madill30d6c252014-11-13 10:03:33 -0500146 PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutable *shaderExecutable);
Brandon Joneseb994362014-09-24 10:27:28 -0700147 ~PixelExecutable();
148
149 bool matchesSignature(const std::vector<GLenum> &signature) const { return mOutputSignature == signature; }
150
151 const std::vector<GLenum> &outputSignature() const { return mOutputSignature; }
Jamie Madill30d6c252014-11-13 10:03:33 -0500152 ShaderExecutable *shaderExecutable() const { return mShaderExecutable; }
Brandon Joneseb994362014-09-24 10:27:28 -0700153
154 private:
155 std::vector<GLenum> mOutputSignature;
Jamie Madill30d6c252014-11-13 10:03:33 -0500156 ShaderExecutable *mShaderExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700157 };
158
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700159 struct Sampler
160 {
161 Sampler();
162
163 bool active;
164 GLint logicalTextureUnit;
165 GLenum textureType;
166 };
167
168 void defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister);
169 void defineUniform(GLenum shader, const sh::ShaderVariable &uniform, const std::string &fullName,
170 sh::HLSLBlockEncoder *encoder);
171 bool indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps);
172 bool indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps);
173 static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount,
174 std::vector<Sampler> &outSamplers, GLuint *outUsedRange);
175
176 template <typename T>
177 void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
178
179 template <int cols, int rows>
180 void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
181
182 template <typename T>
183 void getUniformv(GLint location, T *params, GLenum uniformType);
184
185 template <typename VarT>
186 void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
187 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
188 bool inRowMajorLayout);
189
Jamie Madill93e13fb2014-11-06 15:27:25 -0500190 RendererD3D *mRenderer;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700191 DynamicHLSL *mDynamicHLSL;
192
Brandon Joneseb994362014-09-24 10:27:28 -0700193 std::vector<VertexExecutable *> mVertexExecutables;
194 std::vector<PixelExecutable *> mPixelExecutables;
Jamie Madill30d6c252014-11-13 10:03:33 -0500195 ShaderExecutable *mGeometryExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700196
Brandon Jones22502d52014-08-29 16:58:36 -0700197 std::string mVertexHLSL;
Jamie Madill30d6c252014-11-13 10:03:33 -0500198 D3DWorkaroundType mVertexWorkarounds;
Brandon Jones22502d52014-08-29 16:58:36 -0700199
200 std::string mPixelHLSL;
Jamie Madill30d6c252014-11-13 10:03:33 -0500201 D3DWorkaroundType mPixelWorkarounds;
Brandon Jones22502d52014-08-29 16:58:36 -0700202 bool mUsesFragDepth;
Jamie Madill30d6c252014-11-13 10:03:33 -0500203 std::vector<PixelShaderOutputVariable> mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700204
Brandon Jones44151a92014-09-10 11:32:25 -0700205 bool mUsesPointSize;
206
Brandon Jonesc9610c52014-08-25 17:02:59 -0700207 UniformStorage *mVertexUniformStorage;
208 UniformStorage *mFragmentUniformStorage;
Brandon Jones44151a92014-09-10 11:32:25 -0700209
Brandon Joneseb994362014-09-24 10:27:28 -0700210 GLenum mTransformFeedbackBufferMode;
Brandon Joneseb994362014-09-24 10:27:28 -0700211
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700212 std::vector<Sampler> mSamplersPS;
213 std::vector<Sampler> mSamplersVS;
214 GLuint mUsedVertexSamplerRange;
215 GLuint mUsedPixelSamplerRange;
216 bool mDirtySamplerMapping;
Brandon Joneseb994362014-09-24 10:27:28 -0700217
Brandon Jones44151a92014-09-10 11:32:25 -0700218 int mShaderVersion;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500219
220 unsigned int mSerial;
221
222 static unsigned int issueSerial();
223 static unsigned int mCurrentSerial;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700224};
225
226}
227
Geoff Lang0a73dd82014-11-19 16:18:08 -0500228#endif // LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_