blob: 81ce3a7fd861ebcea4282f5f6a1567068fd1237a [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
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 Ferron7a06ac12018-03-15 10:17:04 -040013#include <array>
14
Jamie Madill5547b382017-10-23 18:16:01 -040015#include "libANGLE/Constants.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040016#include "libANGLE/renderer/ProgramImpl.h"
Luc Ferron7a06ac12018-03-15 10:17:04 -040017#include "libANGLE/renderer/vulkan/StreamingBuffer.h"
Jamie Madill3c424b42018-01-19 12:35:09 -050018#include "libANGLE/renderer/vulkan/vk_utils.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040019
20namespace rx
21{
Luc Ferron7a06ac12018-03-15 10:17:04 -040022namespace
23{
24constexpr uint32_t kShaderTypeCount = 2;
25} // anonymous namespace.
Jamie Madill9e54b5a2016-05-25 12:57:39 -040026
27class ProgramVk : public ProgramImpl
28{
29 public:
30 ProgramVk(const gl::ProgramState &state);
31 ~ProgramVk() override;
Jamie Madillb7d924a2018-03-10 11:16:54 -050032 gl::Error destroy(const gl::Context *context) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040033
Jamie Madill9cf9e872017-06-05 12:59:25 -040034 gl::LinkResult load(const gl::Context *context,
35 gl::InfoLog &infoLog,
36 gl::BinaryInputStream *stream) override;
Jamie Madill27a60632017-06-30 15:12:01 -040037 void save(const gl::Context *context, gl::BinaryOutputStream *stream) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040038 void setBinaryRetrievableHint(bool retrievable) override;
Yunchao He61afff12017-03-14 15:34:03 +080039 void setSeparable(bool separable) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040040
Jamie Madill9cf9e872017-06-05 12:59:25 -040041 gl::LinkResult link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -050042 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -040043 gl::InfoLog &infoLog) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040044 GLboolean validate(const gl::Caps &caps, gl::InfoLog *infoLog) override;
45
46 void setUniform1fv(GLint location, GLsizei count, const GLfloat *v) override;
47 void setUniform2fv(GLint location, GLsizei count, const GLfloat *v) override;
48 void setUniform3fv(GLint location, GLsizei count, const GLfloat *v) override;
49 void setUniform4fv(GLint location, GLsizei count, const GLfloat *v) override;
50 void setUniform1iv(GLint location, GLsizei count, const GLint *v) override;
51 void setUniform2iv(GLint location, GLsizei count, const GLint *v) override;
52 void setUniform3iv(GLint location, GLsizei count, const GLint *v) override;
53 void setUniform4iv(GLint location, GLsizei count, const GLint *v) override;
54 void setUniform1uiv(GLint location, GLsizei count, const GLuint *v) override;
55 void setUniform2uiv(GLint location, GLsizei count, const GLuint *v) override;
56 void setUniform3uiv(GLint location, GLsizei count, const GLuint *v) override;
57 void setUniform4uiv(GLint location, GLsizei count, const GLuint *v) override;
58 void setUniformMatrix2fv(GLint location,
59 GLsizei count,
60 GLboolean transpose,
61 const GLfloat *value) override;
62 void setUniformMatrix3fv(GLint location,
63 GLsizei count,
64 GLboolean transpose,
65 const GLfloat *value) override;
66 void setUniformMatrix4fv(GLint location,
67 GLsizei count,
68 GLboolean transpose,
69 const GLfloat *value) override;
70 void setUniformMatrix2x3fv(GLint location,
71 GLsizei count,
72 GLboolean transpose,
73 const GLfloat *value) override;
74 void setUniformMatrix3x2fv(GLint location,
75 GLsizei count,
76 GLboolean transpose,
77 const GLfloat *value) override;
78 void setUniformMatrix2x4fv(GLint location,
79 GLsizei count,
80 GLboolean transpose,
81 const GLfloat *value) override;
82 void setUniformMatrix4x2fv(GLint location,
83 GLsizei count,
84 GLboolean transpose,
85 const GLfloat *value) override;
86 void setUniformMatrix3x4fv(GLint location,
87 GLsizei count,
88 GLboolean transpose,
89 const GLfloat *value) override;
90 void setUniformMatrix4x3fv(GLint location,
91 GLsizei count,
92 GLboolean transpose,
93 const GLfloat *value) override;
94
Jamie Madill54164b02017-08-28 15:17:37 -040095 void getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const override;
96 void getUniformiv(const gl::Context *context, GLint location, GLint *params) const override;
97 void getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const override;
98
Jamie Madill9e54b5a2016-05-25 12:57:39 -040099 // TODO: synchronize in syncState when dirty bits exist.
100 void setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) override;
101
Sami Väisänen46eaa942016-06-29 10:26:37 +0300102 void setPathFragmentInputGen(const std::string &inputName,
103 GLenum genMode,
104 GLint components,
105 const GLfloat *coeffs) override;
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500106
107 const vk::ShaderModule &getLinkedVertexModule() const;
Jamie Madillf2f6d372018-01-10 21:37:23 -0500108 Serial getVertexModuleSerial() const;
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500109 const vk::ShaderModule &getLinkedFragmentModule() const;
Jamie Madillf2f6d372018-01-10 21:37:23 -0500110 Serial getFragmentModuleSerial() const;
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500111
Jamie Madill76e471e2017-10-21 09:56:01 -0400112 vk::Error updateUniforms(ContextVk *contextVk);
113
Jamie Madill5547b382017-10-23 18:16:01 -0400114 const std::vector<VkDescriptorSet> &getDescriptorSets() const;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400115 const uint32_t *getDynamicOffsets();
116 uint32_t getDynamicOffsetsCount();
Jamie Madill5547b382017-10-23 18:16:01 -0400117
118 // In Vulkan, it is invalid to pass in a NULL descriptor set to vkCmdBindDescriptorSets.
119 // However, it's valid to leave them in an undefined, unbound state, if they are never used.
120 // This means when we want to ignore a descriptor set index, we need to pass in an offset
121 // parameter to BindDescriptorSets, which is an offset into the getDescriptorSets array.
Jamie Madill8c3988c2017-12-21 14:44:56 -0500122 // This allows us to skip binding blank descriptor sets when the Program doesn't use Uniforms
123 // or Textures.
124 const gl::RangeUI &getUsedDescriptorSetRange() const;
Jamie Madill5547b382017-10-23 18:16:01 -0400125
126 void updateTexturesDescriptorSet(ContextVk *contextVk);
127 void invalidateTextures();
Jamie Madill76e471e2017-10-21 09:56:01 -0400128
Luc Ferron7a06ac12018-03-15 10:17:04 -0400129 // For testing only.
130 void setDefaultUniformBlocksMinSizeForTesting(size_t minSize);
131
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500132 private:
Jamie Madillb7d924a2018-03-10 11:16:54 -0500133 vk::Error reset(ContextVk *contextVk);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400134 vk::Error allocateDescriptorSets(ContextVk *contextVk);
Jamie Madill76e471e2017-10-21 09:56:01 -0400135 gl::Error initDefaultUniformBlocks(const gl::Context *glContext);
136 vk::Error updateDefaultUniformsDescriptorSet(ContextVk *contextVk);
137
Luc Ferron7cec3352018-03-13 13:29:34 -0400138 template <class T>
139 void getUniformImpl(GLint location, T *v, GLenum entryPointType) const;
140
Jamie Madill76e471e2017-10-21 09:56:01 -0400141 template <typename T>
142 void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType);
Jamie Madillc5143482017-10-15 20:20:06 -0400143
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500144 vk::ShaderModule mLinkedVertexModule;
Jamie Madillf2f6d372018-01-10 21:37:23 -0500145 Serial mVertexModuleSerial;
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500146 vk::ShaderModule mLinkedFragmentModule;
Jamie Madillf2f6d372018-01-10 21:37:23 -0500147 Serial mFragmentModuleSerial;
Jamie Madill76e471e2017-10-21 09:56:01 -0400148
149 // State for the default uniform blocks.
150 struct DefaultUniformBlock final : private angle::NonCopyable
151 {
152 DefaultUniformBlock();
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500153 ~DefaultUniformBlock();
Jamie Madill76e471e2017-10-21 09:56:01 -0400154
Luc Ferron7a06ac12018-03-15 10:17:04 -0400155 StreamingBuffer storage;
Jamie Madill76e471e2017-10-21 09:56:01 -0400156
157 // Shadow copies of the shader uniform data.
158 angle::MemoryBuffer uniformData;
159 bool uniformsDirty;
160
161 // Since the default blocks are laid out in std140, this tells us where to write on a call
162 // to a setUniform method. They are arranged in uniform location order.
163 std::vector<sh::BlockMemberInfo> uniformLayout;
164 };
165
Luc Ferron7a06ac12018-03-15 10:17:04 -0400166 std::array<DefaultUniformBlock, kShaderTypeCount> mDefaultUniformBlocks;
167 std::array<uint32_t, kShaderTypeCount> mUniformBlocksOffsets;
Jamie Madill76e471e2017-10-21 09:56:01 -0400168
169 // This is a special "empty" placeholder buffer for when a shader has no uniforms.
170 // It is necessary because we want to keep a compatible pipeline layout in all cases,
171 // and Vulkan does not tolerate having null handles in a descriptor set.
172 vk::BufferAndMemory mEmptyUniformBlockStorage;
173
Jamie Madill5547b382017-10-23 18:16:01 -0400174 // Descriptor sets for uniform blocks and textures for this program.
175 std::vector<VkDescriptorSet> mDescriptorSets;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500176 gl::RangeUI mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400177 bool mDirtyTextures;
178
179 template <typename T>
180 using ShaderTextureArray = std::array<T, gl::IMPLEMENTATION_MAX_SHADER_TEXTURES>;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400181};
182
183} // namespace rx
184
185#endif // LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_