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