Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 1 | // |
| 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 Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 9 | #ifndef LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_ |
| 10 | #define LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_ |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 11 | |
Daniel Bratell | 73941de | 2015-02-25 14:34:49 +0100 | [diff] [blame] | 12 | #include "compiler/translator/blocklayoutHLSL.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 13 | #include "libANGLE/Constants.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 14 | #include "libANGLE/renderer/ProgramImpl.h" |
| 15 | #include "libANGLE/renderer/Workarounds.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 16 | #include "libANGLE/renderer/d3d/DynamicHLSL.h" |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 17 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 21 | namespace gl |
| 22 | { |
| 23 | struct LinkedUniform; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 24 | struct VariableLocation; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 25 | struct VertexFormat; |
| 26 | } |
| 27 | |
| 28 | namespace rx |
| 29 | { |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 30 | class RendererD3D; |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 31 | class UniformStorageD3D; |
| 32 | class ShaderExecutableD3D; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 33 | |
Jamie Madill | 2db1fbb | 2014-12-03 10:58:55 -0500 | [diff] [blame] | 34 | #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 Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 40 | class ProgramD3D : public ProgramImpl |
| 41 | { |
| 42 | public: |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 43 | ProgramD3D(RendererD3D *renderer); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 44 | virtual ~ProgramD3D(); |
| 45 | |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 46 | const std::vector<PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 47 | int getShaderVersion() const { return mShaderVersion; } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 48 | GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 49 | |
| 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 Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 55 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 56 | bool usesPointSize() const { return mUsesPointSize; } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 57 | bool usesPointSpriteEmulation() const; |
| 58 | bool usesGeometryShader() const; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 59 | bool usesInstancedPointSpriteEmulation() const; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 60 | |
| 61 | GLenum getBinaryFormat() { return GL_PROGRAM_BINARY_ANGLE; } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 62 | LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 63 | gl::Error save(gl::BinaryOutputStream *stream); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 64 | |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 65 | 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 Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 69 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 70 | LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader, |
| 71 | int registers); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 72 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 73 | 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 Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 79 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 80 | void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const; |
| 81 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 82 | void initializeUniformStorage(); |
| 83 | gl::Error applyUniforms(); |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 84 | gl::Error applyUniformBuffers(const gl::Data &data, GLuint uniformBlockBindings[]) override; |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 85 | bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader, |
| 86 | unsigned int registerIndex, const gl::Caps &caps); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 87 | 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 Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 114 | |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 115 | const UniformStorageD3D &getVertexUniformStorage() const { return *mVertexUniformStorage; } |
| 116 | const UniformStorageD3D &getFragmentUniformStorage() const { return *mFragmentUniformStorage; } |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 117 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 118 | 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 Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 122 | void reset(); |
| 123 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 124 | unsigned int getSerial() const; |
| 125 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 126 | void initAttributesByLayout(); |
| 127 | void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], |
| 128 | int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const; |
| 129 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 130 | private: |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 131 | class VertexExecutable |
| 132 | { |
| 133 | public: |
| 134 | VertexExecutable(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], |
| 135 | const GLenum signature[gl::MAX_VERTEX_ATTRIBS], |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 136 | ShaderExecutableD3D *shaderExecutable); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 137 | ~VertexExecutable(); |
| 138 | |
| 139 | bool matchesSignature(const GLenum convertedLayout[gl::MAX_VERTEX_ATTRIBS]) const; |
| 140 | |
| 141 | const gl::VertexFormat *inputs() const { return mInputs; } |
| 142 | const GLenum *signature() const { return mSignature; } |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 143 | ShaderExecutableD3D *shaderExecutable() const { return mShaderExecutable; } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 144 | |
| 145 | private: |
| 146 | gl::VertexFormat mInputs[gl::MAX_VERTEX_ATTRIBS]; |
| 147 | GLenum mSignature[gl::MAX_VERTEX_ATTRIBS]; |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 148 | ShaderExecutableD3D *mShaderExecutable; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | class PixelExecutable |
| 152 | { |
| 153 | public: |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 154 | PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutableD3D *shaderExecutable); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 155 | ~PixelExecutable(); |
| 156 | |
| 157 | bool matchesSignature(const std::vector<GLenum> &signature) const { return mOutputSignature == signature; } |
| 158 | |
| 159 | const std::vector<GLenum> &outputSignature() const { return mOutputSignature; } |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 160 | ShaderExecutableD3D *shaderExecutable() const { return mShaderExecutable; } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 161 | |
| 162 | private: |
| 163 | std::vector<GLenum> mOutputSignature; |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 164 | ShaderExecutableD3D *mShaderExecutable; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 167 | struct Sampler |
| 168 | { |
| 169 | Sampler(); |
| 170 | |
| 171 | bool active; |
| 172 | GLint logicalTextureUnit; |
| 173 | GLenum textureType; |
| 174 | }; |
| 175 | |
Geoff Lang | 492a7e4 | 2014-11-05 13:27:06 -0500 | [diff] [blame] | 176 | void defineUniformBase(const ShaderD3D *shader, const sh::Uniform &uniform, unsigned int uniformRegister); |
| 177 | void defineUniform(const ShaderD3D *shader, const sh::ShaderVariable &uniform, const std::string &fullName, |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 178 | sh::HLSLBlockEncoder *encoder); |
| 179 | bool indexSamplerUniform(const gl::LinkedUniform &uniform, gl::InfoLog &infoLog, const gl::Caps &caps); |
| 180 | bool indexUniforms(gl::InfoLog &infoLog, const gl::Caps &caps); |
| 181 | static bool assignSamplers(unsigned int startSamplerIndex, GLenum samplerType, unsigned int samplerCount, |
| 182 | std::vector<Sampler> &outSamplers, GLuint *outUsedRange); |
| 183 | |
| 184 | template <typename T> |
| 185 | void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType); |
| 186 | |
| 187 | template <int cols, int rows> |
| 188 | void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType); |
| 189 | |
| 190 | template <typename T> |
| 191 | void getUniformv(GLint location, T *params, GLenum uniformType); |
| 192 | |
| 193 | template <typename VarT> |
| 194 | void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex, |
| 195 | sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes, |
| 196 | bool inRowMajorLayout); |
| 197 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 198 | RendererD3D *mRenderer; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 199 | DynamicHLSL *mDynamicHLSL; |
| 200 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 201 | std::vector<VertexExecutable *> mVertexExecutables; |
| 202 | std::vector<PixelExecutable *> mPixelExecutables; |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 203 | ShaderExecutableD3D *mGeometryExecutable; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 204 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 205 | std::string mVertexHLSL; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 206 | D3DCompilerWorkarounds mVertexWorkarounds; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 207 | |
| 208 | std::string mPixelHLSL; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 209 | D3DCompilerWorkarounds mPixelWorkarounds; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 210 | bool mUsesFragDepth; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 211 | std::vector<PixelShaderOutputVariable> mPixelShaderKey; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 212 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 213 | bool mUsesPointSize; |
| 214 | |
Geoff Lang | 359ef26 | 2015-01-05 14:42:29 -0500 | [diff] [blame] | 215 | UniformStorageD3D *mVertexUniformStorage; |
| 216 | UniformStorageD3D *mFragmentUniformStorage; |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 217 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 218 | GLenum mTransformFeedbackBufferMode; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 219 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 220 | std::vector<Sampler> mSamplersPS; |
| 221 | std::vector<Sampler> mSamplersVS; |
| 222 | GLuint mUsedVertexSamplerRange; |
| 223 | GLuint mUsedPixelSamplerRange; |
| 224 | bool mDirtySamplerMapping; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 225 | |
Geoff Lang | 7a26a1a | 2015-03-25 12:29:06 -0400 | [diff] [blame] | 226 | // Cache for validateSamplers |
| 227 | std::vector<GLenum> mTextureUnitTypesCache; |
| 228 | |
| 229 | // Cache for getPixelExecutableForFramebuffer |
| 230 | std::vector<GLenum> mPixelShaderOutputFormatCache; |
| 231 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 232 | int mShaderVersion; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 233 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 234 | int mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS]; |
| 235 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 236 | unsigned int mSerial; |
| 237 | |
| 238 | static unsigned int issueSerial(); |
| 239 | static unsigned int mCurrentSerial; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 240 | }; |
| 241 | |
| 242 | } |
| 243 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 244 | #endif // LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_ |