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