blob: 6efc65445869237338a2ec99d4bacd209758e1c3 [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
Jamie Madilld3dfda22015-07-06 08:28:49 -040012#include <string>
13#include <vector>
14
Jamie Madill13776892015-04-28 12:39:06 -040015#include "common/Optional.h"
Daniel Bratell73941de2015-02-25 14:34:49 +010016#include "compiler/translator/blocklayoutHLSL.h"
Jamie Madill437d2662014-12-05 14:23:35 -050017#include "libANGLE/Constants.h"
Jamie Madilld3dfda22015-07-06 08:28:49 -040018#include "libANGLE/formatutils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050019#include "libANGLE/renderer/ProgramImpl.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050020#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Geoff Lang6941a552015-07-27 11:06:45 -040021#include "libANGLE/renderer/d3d/WorkaroundsD3D.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070022
23namespace gl
24{
25struct LinkedUniform;
Brandon Jones22502d52014-08-29 16:58:36 -070026struct VariableLocation;
Brandon Jonesc9610c52014-08-25 17:02:59 -070027struct VertexFormat;
28}
29
30namespace rx
31{
Jamie Madill93e13fb2014-11-06 15:27:25 -050032class RendererD3D;
Geoff Lang359ef262015-01-05 14:42:29 -050033class UniformStorageD3D;
34class ShaderExecutableD3D;
Brandon Jonesc9610c52014-08-25 17:02:59 -070035
Jamie Madill2db1fbb2014-12-03 10:58:55 -050036#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
37// WARNING: D3DCOMPILE_OPTIMIZATION_LEVEL3 may lead to a DX9 shader compiler hang.
38// It should only be used selectively to work around specific bugs.
39#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL1
40#endif
41
Brandon Jonesc9610c52014-08-25 17:02:59 -070042class ProgramD3D : public ProgramImpl
43{
44 public:
Jamie Madill63805b42015-08-25 13:17:39 -040045 typedef int SemanticIndexArray[gl::MAX_VERTEX_ATTRIBS];
46
Jamie Madill5c6b7bf2015-08-17 12:53:35 -040047 ProgramD3D(const gl::Program::Data &data, RendererD3D *renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -070048 virtual ~ProgramD3D();
49
Jamie Madill30d6c252014-11-13 10:03:33 -050050 const std::vector<PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; }
Brandon Jones44151a92014-09-10 11:32:25 -070051 int getShaderVersion() const { return mShaderVersion; }
Brandon Jones1a8a7e32014-10-01 12:49:30 -070052
53 GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const;
54 GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const;
55 GLint getUsedSamplerRange(gl::SamplerType type) const;
56 void updateSamplerMapping();
57 bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps);
Brandon Jones44151a92014-09-10 11:32:25 -070058
Brandon Joneseb994362014-09-24 10:27:28 -070059 bool usesPointSize() const { return mUsesPointSize; }
Brandon Jones44151a92014-09-10 11:32:25 -070060 bool usesPointSpriteEmulation() const;
61 bool usesGeometryShader() const;
Cooper Partine6664f02015-01-09 16:22:24 -080062 bool usesInstancedPointSpriteEmulation() const;
Brandon Jones22502d52014-08-29 16:58:36 -070063
64 GLenum getBinaryFormat() { return GL_PROGRAM_BINARY_ANGLE; }
Geoff Lang7dd2e102014-11-10 15:19:26 -050065 LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream);
Geoff Langb543aff2014-09-30 14:52:54 -040066 gl::Error save(gl::BinaryOutputStream *stream);
Brandon Jones22502d52014-08-29 16:58:36 -070067
Geoff Lang359ef262015-01-05 14:42:29 -050068 gl::Error getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExectuable);
69 gl::Error getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputLayout, ShaderExecutableD3D **outExectuable, gl::InfoLog *infoLog);
Jamie Madilld3dfda22015-07-06 08:28:49 -040070 gl::Error getVertexExecutableForInputLayout(const gl::InputLayout &inputLayout, ShaderExecutableD3D **outExectuable, gl::InfoLog *infoLog);
Geoff Lang359ef262015-01-05 14:42:29 -050071 ShaderExecutableD3D *getGeometryExecutable() const { return mGeometryExecutable; }
Brandon Joneseb994362014-09-24 10:27:28 -070072
Jamie Madillf5f4ad22015-09-02 18:32:38 +000073 LinkResult link(const gl::Data &data, gl::InfoLog &infoLog) override;
Jamie Madill36cfd6a2015-08-18 10:46:20 -040074 GLboolean validate(const gl::Caps &caps, gl::InfoLog *infoLog) override;
Brandon Jonesc9610c52014-08-25 17:02:59 -070075
Brandon Jones1a8a7e32014-10-01 12:49:30 -070076 void initializeUniformStorage();
77 gl::Error applyUniforms();
Jamie Madilld1fe1642015-08-21 16:26:04 -040078 gl::Error applyUniformBuffers(const gl::Data &data);
Jamie Madille473dee2015-08-18 14:49:01 -040079 void assignUniformBlockRegister(gl::UniformBlock *uniformBlock,
80 GLenum shader,
81 unsigned int registerIndex,
82 const gl::Caps &caps);
Brandon Jones1a8a7e32014-10-01 12:49:30 -070083 void dirtyAllUniforms();
84
85 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
86 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
87 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
88 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
89 void setUniform1iv(GLint location, GLsizei count, const GLint *v);
90 void setUniform2iv(GLint location, GLsizei count, const GLint *v);
91 void setUniform3iv(GLint location, GLsizei count, const GLint *v);
92 void setUniform4iv(GLint location, GLsizei count, const GLint *v);
93 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
94 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
95 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
96 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
97 void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
98 void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
99 void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
100 void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
101 void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
102 void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
103 void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
104 void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
105 void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
106
107 void getUniformfv(GLint location, GLfloat *params);
108 void getUniformiv(GLint location, GLint *params);
109 void getUniformuiv(GLint location, GLuint *params);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700110
Geoff Lang359ef262015-01-05 14:42:29 -0500111 const UniformStorageD3D &getVertexUniformStorage() const { return *mVertexUniformStorage; }
112 const UniformStorageD3D &getFragmentUniformStorage() const { return *mFragmentUniformStorage; }
Brandon Jonesc9610c52014-08-25 17:02:59 -0700113
114 void reset();
115
Geoff Lang7dd2e102014-11-10 15:19:26 -0500116 unsigned int getSerial() const;
117
Jamie Madill476682e2015-06-30 10:04:29 -0400118 void sortAttributesByLayout(const std::vector<TranslatedAttribute> &unsortedAttributes,
Jamie Madillf9327d32015-06-22 13:57:16 -0400119 int sortedSemanticIndicesOut[gl::MAX_VERTEX_ATTRIBS],
120 const rx::TranslatedAttribute *sortedAttributesOut[gl::MAX_VERTEX_ATTRIBS]) const;
Jamie Madill63805b42015-08-25 13:17:39 -0400121 const SemanticIndexArray &getSemanticIndexes() const { return mSemanticIndexes; }
Jamie Madillc349ec02015-08-21 16:53:12 -0400122 const SemanticIndexArray &getAttributesByLayout() const { return mAttributesByLayout; }
Jamie Madill437d2662014-12-05 14:23:35 -0500123
Jamie Madill63805b42015-08-25 13:17:39 -0400124 void updateCachedInputLayout(const gl::State &state);
Jamie Madilld3dfda22015-07-06 08:28:49 -0400125 const gl::InputLayout &getCachedInputLayout() const { return mCachedInputLayout; }
126
Brandon Jonesc9610c52014-08-25 17:02:59 -0700127 private:
Brandon Joneseb994362014-09-24 10:27:28 -0700128 class VertexExecutable
129 {
130 public:
Jamie Madillf8dd7b12015-08-05 13:50:08 -0400131 typedef std::vector<bool> Signature;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400132
133 VertexExecutable(const gl::InputLayout &inputLayout,
134 const Signature &signature,
Geoff Lang359ef262015-01-05 14:42:29 -0500135 ShaderExecutableD3D *shaderExecutable);
Brandon Joneseb994362014-09-24 10:27:28 -0700136 ~VertexExecutable();
137
Jamie Madilld3dfda22015-07-06 08:28:49 -0400138 bool matchesSignature(const Signature &signature) const;
139 static void getSignature(RendererD3D *renderer,
140 const gl::InputLayout &inputLayout,
141 Signature *signatureOut);
Brandon Joneseb994362014-09-24 10:27:28 -0700142
Jamie Madilld3dfda22015-07-06 08:28:49 -0400143 const gl::InputLayout &inputs() const { return mInputs; }
144 const Signature &signature() const { return mSignature; }
Geoff Lang359ef262015-01-05 14:42:29 -0500145 ShaderExecutableD3D *shaderExecutable() const { return mShaderExecutable; }
Brandon Joneseb994362014-09-24 10:27:28 -0700146
147 private:
Jamie Madilld3dfda22015-07-06 08:28:49 -0400148 gl::InputLayout mInputs;
149 Signature mSignature;
Geoff Lang359ef262015-01-05 14:42:29 -0500150 ShaderExecutableD3D *mShaderExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700151 };
152
153 class PixelExecutable
154 {
155 public:
Geoff Lang359ef262015-01-05 14:42:29 -0500156 PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable);
Brandon Joneseb994362014-09-24 10:27:28 -0700157 ~PixelExecutable();
158
159 bool matchesSignature(const std::vector<GLenum> &signature) const { return mOutputSignature == signature; }
160
161 const std::vector<GLenum> &outputSignature() const { return mOutputSignature; }
Geoff Lang359ef262015-01-05 14:42:29 -0500162 ShaderExecutableD3D *shaderExecutable() const { return mShaderExecutable; }
Brandon Joneseb994362014-09-24 10:27:28 -0700163
164 private:
165 std::vector<GLenum> mOutputSignature;
Geoff Lang359ef262015-01-05 14:42:29 -0500166 ShaderExecutableD3D *mShaderExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700167 };
168
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700169 struct Sampler
170 {
171 Sampler();
172
173 bool active;
174 GLint logicalTextureUnit;
175 GLenum textureType;
176 };
177
Jamie Madillea918db2015-08-18 14:48:59 -0400178 bool defineUniforms(gl::InfoLog &infoLog, const gl::Caps &caps);
Geoff Lang492a7e42014-11-05 13:27:06 -0500179 void defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister);
180 void defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform, const std::string &fullName,
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700181 sh::HLSLBlockEncoder *encoder);
182 bool indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps);
183 bool indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps);
184 static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount,
185 std::vector<Sampler> &outSamplers, GLuint *outUsedRange);
186
Jamie Madille473dee2015-08-18 14:49:01 -0400187 void defineUniformBlocks(const gl::Caps &caps);
188 void defineUniformBlock(const gl::Shader &shader,
189 const sh::InterfaceBlock &interfaceBlock,
190 const gl::Caps &caps);
191
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700192 template <typename T>
193 void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
194
195 template <int cols, int rows>
196 void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
197
198 template <typename T>
199 void getUniformv(GLint location, T *params, GLenum uniformType);
200
201 template <typename VarT>
202 void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
203 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
204 bool inRowMajorLayout);
205
Jamie Madillca03b352015-09-02 12:38:13 -0400206 LinkResult compileProgramExecutables(gl::InfoLog &infoLog,
207 int registers,
208 const std::vector<PackedVarying> &packedVaryings);
Jamie Madill31c8c562015-08-19 14:08:03 -0400209
Jamie Madillccdf74b2015-08-18 10:46:12 -0400210 void gatherTransformFeedbackVaryings(const std::vector<gl::LinkedVarying> &varyings);
211
Jamie Madill63805b42015-08-25 13:17:39 -0400212 void initSemanticIndex();
213 void initAttributesByLayout();
214
Jamie Madill93e13fb2014-11-06 15:27:25 -0500215 RendererD3D *mRenderer;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700216 DynamicHLSL *mDynamicHLSL;
217
Brandon Joneseb994362014-09-24 10:27:28 -0700218 std::vector<VertexExecutable *> mVertexExecutables;
219 std::vector<PixelExecutable *> mPixelExecutables;
Geoff Lang359ef262015-01-05 14:42:29 -0500220 ShaderExecutableD3D *mGeometryExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700221
Brandon Jones22502d52014-08-29 16:58:36 -0700222 std::string mVertexHLSL;
Arun Patole44efa0b2015-03-04 17:11:05 +0530223 D3DCompilerWorkarounds mVertexWorkarounds;
Brandon Jones22502d52014-08-29 16:58:36 -0700224
225 std::string mPixelHLSL;
Arun Patole44efa0b2015-03-04 17:11:05 +0530226 D3DCompilerWorkarounds mPixelWorkarounds;
Brandon Jones22502d52014-08-29 16:58:36 -0700227 bool mUsesFragDepth;
Jamie Madill30d6c252014-11-13 10:03:33 -0500228 std::vector<PixelShaderOutputVariable> mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700229
Brandon Jones44151a92014-09-10 11:32:25 -0700230 bool mUsesPointSize;
231
Geoff Lang359ef262015-01-05 14:42:29 -0500232 UniformStorageD3D *mVertexUniformStorage;
233 UniformStorageD3D *mFragmentUniformStorage;
Brandon Jones44151a92014-09-10 11:32:25 -0700234
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700235 std::vector<Sampler> mSamplersPS;
236 std::vector<Sampler> mSamplersVS;
237 GLuint mUsedVertexSamplerRange;
238 GLuint mUsedPixelSamplerRange;
239 bool mDirtySamplerMapping;
Brandon Joneseb994362014-09-24 10:27:28 -0700240
Geoff Lang7a26a1a2015-03-25 12:29:06 -0400241 // Cache for validateSamplers
242 std::vector<GLenum> mTextureUnitTypesCache;
243
244 // Cache for getPixelExecutableForFramebuffer
245 std::vector<GLenum> mPixelShaderOutputFormatCache;
246
Brandon Jones44151a92014-09-10 11:32:25 -0700247 int mShaderVersion;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500248
Jamie Madill63805b42015-08-25 13:17:39 -0400249 SemanticIndexArray mSemanticIndexes;
Jamie Madillc349ec02015-08-21 16:53:12 -0400250 SemanticIndexArray mAttributesByLayout;
Jamie Madill437d2662014-12-05 14:23:35 -0500251
Geoff Lang7dd2e102014-11-10 15:19:26 -0500252 unsigned int mSerial;
253
Jamie Madill13776892015-04-28 12:39:06 -0400254 Optional<bool> mCachedValidateSamplersResult;
255
Jamie Madill03260fa2015-06-22 13:57:22 -0400256 std::vector<GLint> mVertexUBOCache;
257 std::vector<GLint> mFragmentUBOCache;
Jamie Madilld3dfda22015-07-06 08:28:49 -0400258 VertexExecutable::Signature mCachedVertexSignature;
259 gl::InputLayout mCachedInputLayout;
Jamie Madill03260fa2015-06-22 13:57:22 -0400260
Jamie Madillccdf74b2015-08-18 10:46:12 -0400261 std::vector<gl::LinkedVarying> mTransformFeedbackLinkedVaryings;
262
Geoff Lang7dd2e102014-11-10 15:19:26 -0500263 static unsigned int issueSerial();
264 static unsigned int mCurrentSerial;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700265};
266
267}
268
Geoff Lang0a73dd82014-11-19 16:18:08 -0500269#endif // LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_