Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2016 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 | // ProgramVk.h: |
| 7 | // Defines the class interface for ProgramVk, implementing ProgramImpl. |
| 8 | // |
| 9 | |
| 10 | #ifndef LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_ |
| 11 | #define LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_ |
| 12 | |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 13 | #include <array> |
| 14 | |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 15 | #include "libANGLE/renderer/ProgramImpl.h" |
Luc Ferron | 6ea1b41 | 2018-03-21 16:13:01 -0400 | [diff] [blame] | 16 | #include "libANGLE/renderer/vulkan/RendererVk.h" |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 17 | #include "libANGLE/renderer/vulkan/vk_helpers.h" |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 18 | |
| 19 | namespace rx |
| 20 | { |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 21 | class ProgramVk : public ProgramImpl |
| 22 | { |
| 23 | public: |
| 24 | ProgramVk(const gl::ProgramState &state); |
| 25 | ~ProgramVk() override; |
Jamie Madill | f4a789f | 2018-10-18 16:56:20 -0400 | [diff] [blame^] | 26 | void destroy(const gl::Context *context) override; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 27 | |
Jamie Madill | 785e8a0 | 2018-10-04 17:42:00 -0400 | [diff] [blame] | 28 | angle::Result load(const gl::Context *context, |
| 29 | gl::InfoLog &infoLog, |
| 30 | gl::BinaryInputStream *stream) override; |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 31 | void save(const gl::Context *context, gl::BinaryOutputStream *stream) override; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 32 | void setBinaryRetrievableHint(bool retrievable) override; |
Yunchao He | 61afff1 | 2017-03-14 15:34:03 +0800 | [diff] [blame] | 33 | void setSeparable(bool separable) override; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 34 | |
jchen10 | 7ae70d8 | 2018-07-06 13:47:01 +0800 | [diff] [blame] | 35 | std::unique_ptr<LinkEvent> link(const gl::Context *context, |
| 36 | const gl::ProgramLinkedResources &resources, |
| 37 | gl::InfoLog &infoLog) override; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 38 | GLboolean validate(const gl::Caps &caps, gl::InfoLog *infoLog) override; |
| 39 | |
| 40 | void setUniform1fv(GLint location, GLsizei count, const GLfloat *v) override; |
| 41 | void setUniform2fv(GLint location, GLsizei count, const GLfloat *v) override; |
| 42 | void setUniform3fv(GLint location, GLsizei count, const GLfloat *v) override; |
| 43 | void setUniform4fv(GLint location, GLsizei count, const GLfloat *v) override; |
| 44 | void setUniform1iv(GLint location, GLsizei count, const GLint *v) override; |
| 45 | void setUniform2iv(GLint location, GLsizei count, const GLint *v) override; |
| 46 | void setUniform3iv(GLint location, GLsizei count, const GLint *v) override; |
| 47 | void setUniform4iv(GLint location, GLsizei count, const GLint *v) override; |
| 48 | void setUniform1uiv(GLint location, GLsizei count, const GLuint *v) override; |
| 49 | void setUniform2uiv(GLint location, GLsizei count, const GLuint *v) override; |
| 50 | void setUniform3uiv(GLint location, GLsizei count, const GLuint *v) override; |
| 51 | void setUniform4uiv(GLint location, GLsizei count, const GLuint *v) override; |
| 52 | void setUniformMatrix2fv(GLint location, |
| 53 | GLsizei count, |
| 54 | GLboolean transpose, |
| 55 | const GLfloat *value) override; |
| 56 | void setUniformMatrix3fv(GLint location, |
| 57 | GLsizei count, |
| 58 | GLboolean transpose, |
| 59 | const GLfloat *value) override; |
| 60 | void setUniformMatrix4fv(GLint location, |
| 61 | GLsizei count, |
| 62 | GLboolean transpose, |
| 63 | const GLfloat *value) override; |
| 64 | void setUniformMatrix2x3fv(GLint location, |
| 65 | GLsizei count, |
| 66 | GLboolean transpose, |
| 67 | const GLfloat *value) override; |
| 68 | void setUniformMatrix3x2fv(GLint location, |
| 69 | GLsizei count, |
| 70 | GLboolean transpose, |
| 71 | const GLfloat *value) override; |
| 72 | void setUniformMatrix2x4fv(GLint location, |
| 73 | GLsizei count, |
| 74 | GLboolean transpose, |
| 75 | const GLfloat *value) override; |
| 76 | void setUniformMatrix4x2fv(GLint location, |
| 77 | GLsizei count, |
| 78 | GLboolean transpose, |
| 79 | const GLfloat *value) override; |
| 80 | void setUniformMatrix3x4fv(GLint location, |
| 81 | GLsizei count, |
| 82 | GLboolean transpose, |
| 83 | const GLfloat *value) override; |
| 84 | void setUniformMatrix4x3fv(GLint location, |
| 85 | GLsizei count, |
| 86 | GLboolean transpose, |
| 87 | const GLfloat *value) override; |
| 88 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 89 | void getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const override; |
| 90 | void getUniformiv(const gl::Context *context, GLint location, GLint *params) const override; |
| 91 | void getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const override; |
| 92 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 93 | void setPathFragmentInputGen(const std::string &inputName, |
| 94 | GLenum genMode, |
| 95 | GLint components, |
| 96 | const GLfloat *coeffs) override; |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 97 | |
Jamie Madill | 06ca634 | 2018-07-12 15:56:53 -0400 | [diff] [blame] | 98 | // Also initializes the pipeline layout, descriptor set layouts, and used descriptor ranges. |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 99 | angle::Result initShaders(ContextVk *contextVk, |
| 100 | const gl::DrawCallParams &drawCallParams, |
| 101 | const vk::ShaderAndSerial **vertexShaderAndSerialOut, |
| 102 | const vk::ShaderAndSerial **fragmentShaderAndSerialOut, |
| 103 | const vk::PipelineLayout **pipelineLayoutOut); |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 104 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 105 | angle::Result updateUniforms(ContextVk *contextVk); |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 106 | angle::Result updateTexturesDescriptorSet(ContextVk *contextVk); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 107 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 108 | angle::Result updateDescriptorSets(ContextVk *contextVk, |
| 109 | const gl::DrawCallParams &drawCallParams, |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 110 | vk::CommandBuffer *commandBuffer); |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 111 | |
Luc Ferron | 7a06ac1 | 2018-03-15 10:17:04 -0400 | [diff] [blame] | 112 | // For testing only. |
| 113 | void setDefaultUniformBlocksMinSizeForTesting(size_t minSize); |
| 114 | |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 115 | const vk::PipelineLayout &getPipelineLayout() const { return mPipelineLayout.get(); } |
| 116 | |
| 117 | bool hasTextures() const { return !mState.getSamplerBindings().empty(); } |
| 118 | |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 119 | bool dirtyUniforms() const { return mDefaultUniformBlocksDirty.any(); } |
Jamie Madill | 88fc6da | 2018-08-30 16:18:36 -0400 | [diff] [blame] | 120 | |
Jamie Madill | 8ecf7f9 | 2017-01-13 17:29:52 -0500 | [diff] [blame] | 121 | private: |
Luc Ferron | 48cdc2e | 2018-05-31 09:58:34 -0400 | [diff] [blame] | 122 | template <int cols, int rows> |
| 123 | void setUniformMatrixfv(GLint location, |
| 124 | GLsizei count, |
| 125 | GLboolean transpose, |
| 126 | const GLfloat *value); |
| 127 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 128 | angle::Result reset(ContextVk *contextVk); |
| 129 | angle::Result allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex); |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 130 | angle::Result initDefaultUniformBlocks(const gl::Context *glContext); |
| 131 | |
Jamie Madill | 2106102 | 2018-07-12 23:56:30 -0400 | [diff] [blame] | 132 | angle::Result updateDefaultUniformsDescriptorSet(ContextVk *contextVk); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 133 | |
Luc Ferron | 7cec335 | 2018-03-13 13:29:34 -0400 | [diff] [blame] | 134 | template <class T> |
| 135 | void getUniformImpl(GLint location, T *v, GLenum entryPointType) const; |
| 136 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 137 | template <typename T> |
| 138 | void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType); |
Jamie Madill | 785e8a0 | 2018-10-04 17:42:00 -0400 | [diff] [blame] | 139 | angle::Result linkImpl(const gl::Context *glContext, |
| 140 | const gl::ProgramLinkedResources &resources, |
| 141 | gl::InfoLog &infoLog); |
Jamie Madill | c514348 | 2017-10-15 20:20:06 -0400 | [diff] [blame] | 142 | |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 143 | // State for the default uniform blocks. |
| 144 | struct DefaultUniformBlock final : private angle::NonCopyable |
| 145 | { |
| 146 | DefaultUniformBlock(); |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 147 | ~DefaultUniformBlock(); |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 148 | |
Jamie Madill | 6c7ab7f | 2018-03-31 14:19:15 -0400 | [diff] [blame] | 149 | vk::DynamicBuffer storage; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 150 | |
| 151 | // Shadow copies of the shader uniform data. |
| 152 | angle::MemoryBuffer uniformData; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 153 | |
| 154 | // Since the default blocks are laid out in std140, this tells us where to write on a call |
| 155 | // to a setUniform method. They are arranged in uniform location order. |
| 156 | std::vector<sh::BlockMemberInfo> uniformLayout; |
| 157 | }; |
| 158 | |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 159 | vk::ShaderMap<DefaultUniformBlock> mDefaultUniformBlocks; |
Jamie Madill | 2b858c2 | 2018-09-03 13:58:14 -0400 | [diff] [blame] | 160 | vk::ShaderBitSet mDefaultUniformBlocksDirty; |
Jamie Madill | 33318de | 2018-05-01 11:22:54 -0400 | [diff] [blame] | 161 | vk::ShaderMap<uint32_t> mUniformBlocksOffsets; |
Jamie Madill | 76e471e | 2017-10-21 09:56:01 -0400 | [diff] [blame] | 162 | |
| 163 | // This is a special "empty" placeholder buffer for when a shader has no uniforms. |
| 164 | // It is necessary because we want to keep a compatible pipeline layout in all cases, |
| 165 | // and Vulkan does not tolerate having null handles in a descriptor set. |
| 166 | vk::BufferAndMemory mEmptyUniformBlockStorage; |
| 167 | |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 168 | // Descriptor sets for uniform blocks and textures for this program. |
| 169 | std::vector<VkDescriptorSet> mDescriptorSets; |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 170 | gl::RangeUI mUsedDescriptorSetRange; |
Jamie Madill | 5547b38 | 2017-10-23 18:16:01 -0400 | [diff] [blame] | 171 | |
Jamie Madill | 9b168d0 | 2018-06-13 13:25:32 -0400 | [diff] [blame] | 172 | // We keep a reference to the pipeline and descriptor set layouts. This ensures they don't get |
| 173 | // deleted while this program is in use. |
| 174 | vk::BindingPointer<vk::PipelineLayout> mPipelineLayout; |
| 175 | vk::DescriptorSetLayoutPointerArray mDescriptorSetLayouts; |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 176 | |
Jamie Madill | 78bcd2b | 2018-10-16 15:05:20 -0400 | [diff] [blame] | 177 | // Keep bindings to the descriptor pools. This ensures the pools stay valid while the Program |
| 178 | // is in use. |
| 179 | vk::DescriptorSetLayoutArray<vk::SharedDescriptorPoolBinding> mDescriptorPoolBindings; |
| 180 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 181 | class ShaderInfo final : angle::NonCopyable |
| 182 | { |
| 183 | public: |
| 184 | ShaderInfo(); |
| 185 | ~ShaderInfo(); |
| 186 | |
| 187 | angle::Result getShaders(ContextVk *contextVk, |
| 188 | const std::string &vertexSource, |
| 189 | const std::string &fragmentSource, |
Jamie Madill | b36a481 | 2018-09-25 10:15:11 -0400 | [diff] [blame] | 190 | bool enableLineRasterEmulation, |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 191 | const vk::ShaderAndSerial **vertexShaderAndSerialOut, |
| 192 | const vk::ShaderAndSerial **fragmentShaderAndSerialOut); |
| 193 | void destroy(VkDevice device); |
| 194 | bool valid() const; |
| 195 | |
| 196 | private: |
| 197 | vk::ShaderAndSerial mVertexShaderAndSerial; |
| 198 | vk::ShaderAndSerial mFragmentShaderAndSerial; |
| 199 | }; |
| 200 | |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 201 | ShaderInfo mDefaultShaderInfo; |
Jamie Madill | b36a481 | 2018-09-25 10:15:11 -0400 | [diff] [blame] | 202 | ShaderInfo mLineRasterShaderInfo; |
Jamie Madill | 242c4fe | 2018-07-12 15:56:56 -0400 | [diff] [blame] | 203 | |
| 204 | // We keep the translated linked shader sources to use with shader draw call patching. |
| 205 | std::string mVertexSource; |
| 206 | std::string mFragmentSource; |
Jamie Madill | 9e54b5a | 2016-05-25 12:57:39 -0400 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } // namespace rx |
| 210 | |
| 211 | #endif // LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_ |