blob: 241630cab3d46d9aa556542b62211d8160e33607 [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
Daniel Bratell73941de2015-02-25 14:34:49 +010012#include "compiler/translator/blocklayoutHLSL.h"
Jamie Madill437d2662014-12-05 14:23:35 -050013#include "libANGLE/Constants.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050014#include "libANGLE/renderer/ProgramImpl.h"
15#include "libANGLE/renderer/Workarounds.h"
Geoff Lang7dd2e102014-11-10 15:19:26 -050016#include "libANGLE/renderer/d3d/DynamicHLSL.h"
Brandon Jonesc9610c52014-08-25 17:02:59 -070017
Brandon Jones22502d52014-08-29 16:58:36 -070018#include <string>
19#include <vector>
20
Brandon Jonesc9610c52014-08-25 17:02:59 -070021namespace gl
22{
23struct LinkedUniform;
Brandon Jones22502d52014-08-29 16:58:36 -070024struct VariableLocation;
Brandon Jonesc9610c52014-08-25 17:02:59 -070025struct VertexFormat;
26}
27
28namespace rx
29{
Jamie Madill93e13fb2014-11-06 15:27:25 -050030class RendererD3D;
Geoff Lang359ef262015-01-05 14:42:29 -050031class UniformStorageD3D;
32class ShaderExecutableD3D;
Brandon Jonesc9610c52014-08-25 17:02:59 -070033
Jamie Madill2db1fbb2014-12-03 10:58:55 -050034#if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL)
35// WARNING: D3DCOMPILE_OPTIMIZATION_LEVEL3 may lead to a DX9 shader compiler hang.
36// It should only be used selectively to work around specific bugs.
37#define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL1
38#endif
39
Brandon Jonesc9610c52014-08-25 17:02:59 -070040class ProgramD3D : public ProgramImpl
41{
42 public:
Jamie Madill30d6c252014-11-13 10:03:33 -050043 ProgramD3D(RendererD3D *renderer);
Brandon Jonesc9610c52014-08-25 17:02:59 -070044 virtual ~ProgramD3D();
45
Jamie Madill30d6c252014-11-13 10:03:33 -050046 const std::vector<PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; }
Brandon Jones44151a92014-09-10 11:32:25 -070047 int getShaderVersion() const { return mShaderVersion; }
Brandon Joneseb994362014-09-24 10:27:28 -070048 GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; }
Brandon Jones1a8a7e32014-10-01 12:49:30 -070049
50 GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const;
51 GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const;
52 GLint getUsedSamplerRange(gl::SamplerType type) const;
53 void updateSamplerMapping();
54 bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps);
Brandon Jones44151a92014-09-10 11:32:25 -070055
Brandon Joneseb994362014-09-24 10:27:28 -070056 bool usesPointSize() const { return mUsesPointSize; }
Brandon Jones44151a92014-09-10 11:32:25 -070057 bool usesPointSpriteEmulation() const;
58 bool usesGeometryShader() const;
Cooper Partine6664f02015-01-09 16:22:24 -080059 bool usesInstancedPointSpriteEmulation() const;
Brandon Jones22502d52014-08-29 16:58:36 -070060
61 GLenum getBinaryFormat() { return GL_PROGRAM_BINARY_ANGLE; }
Geoff Lang7dd2e102014-11-10 15:19:26 -050062 LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream);
Geoff Langb543aff2014-09-30 14:52:54 -040063 gl::Error save(gl::BinaryOutputStream *stream);
Brandon Jones22502d52014-08-29 16:58:36 -070064
Geoff Lang359ef262015-01-05 14:42:29 -050065 gl::Error getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutableD3D **outExectuable);
66 gl::Error getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputLayout, ShaderExecutableD3D **outExectuable, gl::InfoLog *infoLog);
67 gl::Error getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], ShaderExecutableD3D **outExectuable, gl::InfoLog *infoLog);
68 ShaderExecutableD3D *getGeometryExecutable() const { return mGeometryExecutable; }
Brandon Joneseb994362014-09-24 10:27:28 -070069
Geoff Lang7dd2e102014-11-10 15:19:26 -050070 LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader,
71 int registers);
Brandon Jones22502d52014-08-29 16:58:36 -070072
Geoff Lang7dd2e102014-11-10 15:19:26 -050073 LinkResult link(const gl::Data &data, gl::InfoLog &infoLog,
74 gl::Shader *fragmentShader, gl::Shader *vertexShader,
75 const std::vector<std::string> &transformFeedbackVaryings,
76 GLenum transformFeedbackBufferMode,
77 int *registers, std::vector<gl::LinkedVarying> *linkedVaryings,
78 std::map<int, gl::VariableLocation> *outputVariables);
Brandon Jonesc9610c52014-08-25 17:02:59 -070079
Brandon Jones44151a92014-09-10 11:32:25 -070080 void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const;
81
Brandon Jones1a8a7e32014-10-01 12:49:30 -070082 void initializeUniformStorage();
83 gl::Error applyUniforms();
84 gl::Error applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps);
Brandon Jones18bd4102014-09-22 14:21:44 -070085 bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader,
86 unsigned int registerIndex, const gl::Caps &caps);
Brandon Jones1a8a7e32014-10-01 12:49:30 -070087 void dirtyAllUniforms();
88
89 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v);
90 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v);
91 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v);
92 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v);
93 void setUniform1iv(GLint location, GLsizei count, const GLint *v);
94 void setUniform2iv(GLint location, GLsizei count, const GLint *v);
95 void setUniform3iv(GLint location, GLsizei count, const GLint *v);
96 void setUniform4iv(GLint location, GLsizei count, const GLint *v);
97 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v);
98 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v);
99 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v);
100 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v);
101 void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
102 void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
103 void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
104 void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
105 void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
106 void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
107 void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
108 void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
109 void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value);
110
111 void getUniformfv(GLint location, GLfloat *params);
112 void getUniformiv(GLint location, GLint *params);
113 void getUniformuiv(GLint location, GLuint *params);
Brandon Jonesc9610c52014-08-25 17:02:59 -0700114
Geoff Lang359ef262015-01-05 14:42:29 -0500115 const UniformStorageD3D &getVertexUniformStorage() const { return *mVertexUniformStorage; }
116 const UniformStorageD3D &getFragmentUniformStorage() const { return *mFragmentUniformStorage; }
Brandon Jonesc9610c52014-08-25 17:02:59 -0700117
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700118 bool linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
119 const gl::Caps &caps);
120 bool defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock, const gl::Caps &caps);
121
Brandon Jonesc9610c52014-08-25 17:02:59 -0700122 void reset();
123
Geoff Lang7dd2e102014-11-10 15:19:26 -0500124 unsigned int getSerial() const;
125
Jamie Madill437d2662014-12-05 14:23:35 -0500126 void initAttributesByLayout();
127 void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS],
128 int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const;
129
Brandon Jonesc9610c52014-08-25 17:02:59 -0700130 private:
131 DISALLOW_COPY_AND_ASSIGN(ProgramD3D);
132
Brandon Joneseb994362014-09-24 10:27:28 -0700133 class VertexExecutable
134 {
135 public:
136 VertexExecutable(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS],
137 const GLenum signature[gl::MAX_VERTEX_ATTRIBS],
Geoff Lang359ef262015-01-05 14:42:29 -0500138 ShaderExecutableD3D *shaderExecutable);
Brandon Joneseb994362014-09-24 10:27:28 -0700139 ~VertexExecutable();
140
141 bool matchesSignature(const GLenum convertedLayout[gl::MAX_VERTEX_ATTRIBS]) const;
142
143 const gl::VertexFormat *inputs() const { return mInputs; }
144 const GLenum *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:
148 gl::VertexFormat mInputs[gl::MAX_VERTEX_ATTRIBS];
149 GLenum mSignature[gl::MAX_VERTEX_ATTRIBS];
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
Geoff Lang492a7e42014-11-05 13:27:06 -0500178 void defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister);
179 void defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform, const std::string &fullName,
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700180 sh::HLSLBlockEncoder *encoder);
181 bool indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps);
182 bool indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps);
183 static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount,
184 std::vector<Sampler> &outSamplers, GLuint *outUsedRange);
185
186 template <typename T>
187 void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType);
188
189 template <int cols, int rows>
190 void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType);
191
192 template <typename T>
193 void getUniformv(GLint location, T *params, GLenum uniformType);
194
195 template <typename VarT>
196 void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex,
197 sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes,
198 bool inRowMajorLayout);
199
Jamie Madill93e13fb2014-11-06 15:27:25 -0500200 RendererD3D *mRenderer;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700201 DynamicHLSL *mDynamicHLSL;
202
Brandon Joneseb994362014-09-24 10:27:28 -0700203 std::vector<VertexExecutable *> mVertexExecutables;
204 std::vector<PixelExecutable *> mPixelExecutables;
Geoff Lang359ef262015-01-05 14:42:29 -0500205 ShaderExecutableD3D *mGeometryExecutable;
Brandon Joneseb994362014-09-24 10:27:28 -0700206
Brandon Jones22502d52014-08-29 16:58:36 -0700207 std::string mVertexHLSL;
Arun Patole44efa0b2015-03-04 17:11:05 +0530208 D3DCompilerWorkarounds mVertexWorkarounds;
Brandon Jones22502d52014-08-29 16:58:36 -0700209
210 std::string mPixelHLSL;
Arun Patole44efa0b2015-03-04 17:11:05 +0530211 D3DCompilerWorkarounds mPixelWorkarounds;
Brandon Jones22502d52014-08-29 16:58:36 -0700212 bool mUsesFragDepth;
Jamie Madill30d6c252014-11-13 10:03:33 -0500213 std::vector<PixelShaderOutputVariable> mPixelShaderKey;
Brandon Jones22502d52014-08-29 16:58:36 -0700214
Brandon Jones44151a92014-09-10 11:32:25 -0700215 bool mUsesPointSize;
216
Geoff Lang359ef262015-01-05 14:42:29 -0500217 UniformStorageD3D *mVertexUniformStorage;
218 UniformStorageD3D *mFragmentUniformStorage;
Brandon Jones44151a92014-09-10 11:32:25 -0700219
Brandon Joneseb994362014-09-24 10:27:28 -0700220 GLenum mTransformFeedbackBufferMode;
Brandon Joneseb994362014-09-24 10:27:28 -0700221
Brandon Jones1a8a7e32014-10-01 12:49:30 -0700222 std::vector<Sampler> mSamplersPS;
223 std::vector<Sampler> mSamplersVS;
224 GLuint mUsedVertexSamplerRange;
225 GLuint mUsedPixelSamplerRange;
226 bool mDirtySamplerMapping;
Brandon Joneseb994362014-09-24 10:27:28 -0700227
Brandon Jones44151a92014-09-10 11:32:25 -0700228 int mShaderVersion;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500229
Jamie Madill437d2662014-12-05 14:23:35 -0500230 int mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS];
231
Geoff Lang7dd2e102014-11-10 15:19:26 -0500232 unsigned int mSerial;
233
234 static unsigned int issueSerial();
235 static unsigned int mCurrentSerial;
Brandon Jonesc9610c52014-08-25 17:02:59 -0700236};
237
238}
239
Geoff Lang0a73dd82014-11-19 16:18:08 -0500240#endif // LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_