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 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 12 | #include "libANGLE/Constants.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 13 | #include "libANGLE/renderer/ProgramImpl.h" |
| 14 | #include "libANGLE/renderer/Workarounds.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 15 | #include "libANGLE/renderer/d3d/DynamicHLSL.h" |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 16 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 17 | #include <string> |
| 18 | #include <vector> |
| 19 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 20 | namespace gl |
| 21 | { |
| 22 | struct LinkedUniform; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 23 | struct VariableLocation; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 24 | struct VertexFormat; |
| 25 | } |
| 26 | |
| 27 | namespace rx |
| 28 | { |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 29 | class RendererD3D; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 30 | class UniformStorage; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 31 | class ShaderExecutable; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 32 | |
Jamie Madill | 2db1fbb | 2014-12-03 10:58:55 -0500 | [diff] [blame] | 33 | #if !defined(ANGLE_COMPILE_OPTIMIZATION_LEVEL) |
| 34 | // WARNING: D3DCOMPILE_OPTIMIZATION_LEVEL3 may lead to a DX9 shader compiler hang. |
| 35 | // It should only be used selectively to work around specific bugs. |
| 36 | #define ANGLE_COMPILE_OPTIMIZATION_LEVEL D3DCOMPILE_OPTIMIZATION_LEVEL1 |
| 37 | #endif |
| 38 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 39 | class ProgramD3D : public ProgramImpl |
| 40 | { |
| 41 | public: |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 42 | ProgramD3D(RendererD3D *renderer); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 43 | virtual ~ProgramD3D(); |
| 44 | |
| 45 | static ProgramD3D *makeProgramD3D(ProgramImpl *impl); |
| 46 | static const ProgramD3D *makeProgramD3D(const ProgramImpl *impl); |
| 47 | |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 48 | const std::vector<PixelShaderOutputVariable> &getPixelShaderKey() { return mPixelShaderKey; } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 49 | int getShaderVersion() const { return mShaderVersion; } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 50 | GLenum getTransformFeedbackBufferMode() const { return mTransformFeedbackBufferMode; } |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 51 | |
| 52 | GLint getSamplerMapping(gl::SamplerType type, unsigned int samplerIndex, const gl::Caps &caps) const; |
| 53 | GLenum getSamplerTextureType(gl::SamplerType type, unsigned int samplerIndex) const; |
| 54 | GLint getUsedSamplerRange(gl::SamplerType type) const; |
| 55 | void updateSamplerMapping(); |
| 56 | bool validateSamplers(gl::InfoLog *infoLog, const gl::Caps &caps); |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 57 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 58 | bool usesPointSize() const { return mUsesPointSize; } |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 59 | bool usesPointSpriteEmulation() const; |
| 60 | bool usesGeometryShader() const; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 61 | |
| 62 | GLenum getBinaryFormat() { return GL_PROGRAM_BINARY_ANGLE; } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 63 | LinkResult load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream); |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 64 | gl::Error save(gl::BinaryOutputStream *stream); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 65 | |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 66 | gl::Error getPixelExecutableForFramebuffer(const gl::Framebuffer *fbo, ShaderExecutable **outExectuable); |
| 67 | gl::Error getPixelExecutableForOutputLayout(const std::vector<GLenum> &outputLayout, ShaderExecutable **outExectuable); |
| 68 | gl::Error getVertexExecutableForInputLayout(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], ShaderExecutable **outExectuable); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 69 | ShaderExecutable *getGeometryExecutable() const { return mGeometryExecutable; } |
| 70 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 71 | LinkResult compileProgramExecutables(gl::InfoLog &infoLog, gl::Shader *fragmentShader, gl::Shader *vertexShader, |
| 72 | int registers); |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 73 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 74 | LinkResult link(const gl::Data &data, gl::InfoLog &infoLog, |
| 75 | gl::Shader *fragmentShader, gl::Shader *vertexShader, |
| 76 | const std::vector<std::string> &transformFeedbackVaryings, |
| 77 | GLenum transformFeedbackBufferMode, |
| 78 | int *registers, std::vector<gl::LinkedVarying> *linkedVaryings, |
| 79 | std::map<int, gl::VariableLocation> *outputVariables); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 80 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 81 | void getInputLayoutSignature(const gl::VertexFormat inputLayout[], GLenum signature[]) const; |
| 82 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 83 | void initializeUniformStorage(); |
| 84 | gl::Error applyUniforms(); |
| 85 | gl::Error applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const gl::Caps &caps); |
Brandon Jones | 18bd410 | 2014-09-22 14:21:44 -0700 | [diff] [blame] | 86 | bool assignUniformBlockRegister(gl::InfoLog &infoLog, gl::UniformBlock *uniformBlock, GLenum shader, |
| 87 | unsigned int registerIndex, const gl::Caps &caps); |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 88 | void dirtyAllUniforms(); |
| 89 | |
| 90 | void setUniform1fv(GLint location, GLsizei count, const GLfloat *v); |
| 91 | void setUniform2fv(GLint location, GLsizei count, const GLfloat *v); |
| 92 | void setUniform3fv(GLint location, GLsizei count, const GLfloat *v); |
| 93 | void setUniform4fv(GLint location, GLsizei count, const GLfloat *v); |
| 94 | void setUniform1iv(GLint location, GLsizei count, const GLint *v); |
| 95 | void setUniform2iv(GLint location, GLsizei count, const GLint *v); |
| 96 | void setUniform3iv(GLint location, GLsizei count, const GLint *v); |
| 97 | void setUniform4iv(GLint location, GLsizei count, const GLint *v); |
| 98 | void setUniform1uiv(GLint location, GLsizei count, const GLuint *v); |
| 99 | void setUniform2uiv(GLint location, GLsizei count, const GLuint *v); |
| 100 | void setUniform3uiv(GLint location, GLsizei count, const GLuint *v); |
| 101 | void setUniform4uiv(GLint location, GLsizei count, const GLuint *v); |
| 102 | void setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 103 | void setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 104 | void setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 105 | void setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 106 | void setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 107 | void setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 108 | void setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 109 | void setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 110 | void setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value); |
| 111 | |
| 112 | void getUniformfv(GLint location, GLfloat *params); |
| 113 | void getUniformiv(GLint location, GLint *params); |
| 114 | void getUniformuiv(GLint location, GLuint *params); |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 115 | |
| 116 | const UniformStorage &getVertexUniformStorage() const { return *mVertexUniformStorage; } |
| 117 | const UniformStorage &getFragmentUniformStorage() const { return *mFragmentUniformStorage; } |
| 118 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 119 | bool linkUniforms(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader, |
| 120 | const gl::Caps &caps); |
| 121 | bool defineUniformBlock(gl::InfoLog &infoLog, const gl::Shader &shader, const sh::InterfaceBlock &interfaceBlock, const gl::Caps &caps); |
| 122 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 123 | void reset(); |
| 124 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 125 | unsigned int getSerial() const; |
| 126 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 127 | void initAttributesByLayout(); |
| 128 | void sortAttributesByLayout(rx::TranslatedAttribute attributes[gl::MAX_VERTEX_ATTRIBS], |
| 129 | int sortedSemanticIndices[gl::MAX_VERTEX_ATTRIBS]) const; |
| 130 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 131 | private: |
| 132 | DISALLOW_COPY_AND_ASSIGN(ProgramD3D); |
| 133 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 134 | class VertexExecutable |
| 135 | { |
| 136 | public: |
| 137 | VertexExecutable(const gl::VertexFormat inputLayout[gl::MAX_VERTEX_ATTRIBS], |
| 138 | const GLenum signature[gl::MAX_VERTEX_ATTRIBS], |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 139 | ShaderExecutable *shaderExecutable); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 140 | ~VertexExecutable(); |
| 141 | |
| 142 | bool matchesSignature(const GLenum convertedLayout[gl::MAX_VERTEX_ATTRIBS]) const; |
| 143 | |
| 144 | const gl::VertexFormat *inputs() const { return mInputs; } |
| 145 | const GLenum *signature() const { return mSignature; } |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 146 | ShaderExecutable *shaderExecutable() const { return mShaderExecutable; } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 147 | |
| 148 | private: |
| 149 | gl::VertexFormat mInputs[gl::MAX_VERTEX_ATTRIBS]; |
| 150 | GLenum mSignature[gl::MAX_VERTEX_ATTRIBS]; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 151 | ShaderExecutable *mShaderExecutable; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | class PixelExecutable |
| 155 | { |
| 156 | public: |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 157 | PixelExecutable(const std::vector<GLenum> &outputSignature, ShaderExecutable *shaderExecutable); |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 158 | ~PixelExecutable(); |
| 159 | |
| 160 | bool matchesSignature(const std::vector<GLenum> &signature) const { return mOutputSignature == signature; } |
| 161 | |
| 162 | const std::vector<GLenum> &outputSignature() const { return mOutputSignature; } |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 163 | ShaderExecutable *shaderExecutable() const { return mShaderExecutable; } |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 164 | |
| 165 | private: |
| 166 | std::vector<GLenum> mOutputSignature; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 167 | ShaderExecutable *mShaderExecutable; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 170 | struct Sampler |
| 171 | { |
| 172 | Sampler(); |
| 173 | |
| 174 | bool active; |
| 175 | GLint logicalTextureUnit; |
| 176 | GLenum textureType; |
| 177 | }; |
| 178 | |
| 179 | void defineUniformBase(GLenum shader, const sh::Uniform &uniform, unsigned int uniformRegister); |
| 180 | void defineUniform(GLenum shader, const sh::ShaderVariable &uniform, const std::string &fullName, |
| 181 | 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 | |
| 187 | template <typename T> |
| 188 | void setUniform(GLint location, GLsizei count, const T* v, GLenum targetUniformType); |
| 189 | |
| 190 | template <int cols, int rows> |
| 191 | void setUniformMatrixfv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value, GLenum targetUniformType); |
| 192 | |
| 193 | template <typename T> |
| 194 | void getUniformv(GLint location, T *params, GLenum uniformType); |
| 195 | |
| 196 | template <typename VarT> |
| 197 | void defineUniformBlockMembers(const std::vector<VarT> &fields, const std::string &prefix, int blockIndex, |
| 198 | sh::BlockLayoutEncoder *encoder, std::vector<unsigned int> *blockUniformIndexes, |
| 199 | bool inRowMajorLayout); |
| 200 | |
Jamie Madill | 93e13fb | 2014-11-06 15:27:25 -0500 | [diff] [blame] | 201 | RendererD3D *mRenderer; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 202 | DynamicHLSL *mDynamicHLSL; |
| 203 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 204 | std::vector<VertexExecutable *> mVertexExecutables; |
| 205 | std::vector<PixelExecutable *> mPixelExecutables; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 206 | ShaderExecutable *mGeometryExecutable; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 207 | |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 208 | std::string mVertexHLSL; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 209 | D3DWorkaroundType mVertexWorkarounds; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 210 | |
| 211 | std::string mPixelHLSL; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 212 | D3DWorkaroundType mPixelWorkarounds; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 213 | bool mUsesFragDepth; |
Jamie Madill | 30d6c25 | 2014-11-13 10:03:33 -0500 | [diff] [blame] | 214 | std::vector<PixelShaderOutputVariable> mPixelShaderKey; |
Brandon Jones | 22502d5 | 2014-08-29 16:58:36 -0700 | [diff] [blame] | 215 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 216 | bool mUsesPointSize; |
| 217 | |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 218 | UniformStorage *mVertexUniformStorage; |
| 219 | UniformStorage *mFragmentUniformStorage; |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 220 | |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 221 | GLenum mTransformFeedbackBufferMode; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 222 | |
Brandon Jones | 1a8a7e3 | 2014-10-01 12:49:30 -0700 | [diff] [blame] | 223 | std::vector<Sampler> mSamplersPS; |
| 224 | std::vector<Sampler> mSamplersVS; |
| 225 | GLuint mUsedVertexSamplerRange; |
| 226 | GLuint mUsedPixelSamplerRange; |
| 227 | bool mDirtySamplerMapping; |
Brandon Jones | eb99436 | 2014-09-24 10:27:28 -0700 | [diff] [blame] | 228 | |
Brandon Jones | 44151a9 | 2014-09-10 11:32:25 -0700 | [diff] [blame] | 229 | int mShaderVersion; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 230 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 231 | int mAttributesByLayout[gl::MAX_VERTEX_ATTRIBS]; |
| 232 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 233 | unsigned int mSerial; |
| 234 | |
| 235 | static unsigned int issueSerial(); |
| 236 | static unsigned int mCurrentSerial; |
Brandon Jones | c9610c5 | 2014-08-25 17:02:59 -0700 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | } |
| 240 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 241 | #endif // LIBANGLE_RENDERER_D3D_PROGRAMD3D_H_ |