blob: a70d06a2c00cc9e283ffe750abe1fba7ff46dcf8 [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 Ferron6ea1b412018-03-21 16:13:01 -040017#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040018#include "libANGLE/renderer/vulkan/vk_helpers.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040019
20namespace rx
21{
Jamie Madill9e54b5a2016-05-25 12:57:39 -040022class ProgramVk : public ProgramImpl
23{
24 public:
25 ProgramVk(const gl::ProgramState &state);
26 ~ProgramVk() override;
Jamie Madillb7d924a2018-03-10 11:16:54 -050027 gl::Error destroy(const gl::Context *context) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040028
Jamie Madill9cf9e872017-06-05 12:59:25 -040029 gl::LinkResult load(const gl::Context *context,
30 gl::InfoLog &infoLog,
31 gl::BinaryInputStream *stream) override;
Jamie Madill27a60632017-06-30 15:12:01 -040032 void save(const gl::Context *context, gl::BinaryOutputStream *stream) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040033 void setBinaryRetrievableHint(bool retrievable) override;
Yunchao He61afff12017-03-14 15:34:03 +080034 void setSeparable(bool separable) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040035
Jamie Madill9cf9e872017-06-05 12:59:25 -040036 gl::LinkResult link(const gl::Context *context,
Jamie Madillc9727f32017-11-07 12:37:07 -050037 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -040038 gl::InfoLog &infoLog) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040039 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 Madill54164b02017-08-28 15:17:37 -040090 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
Jamie Madill9e54b5a2016-05-25 12:57:39 -040094 // TODO: synchronize in syncState when dirty bits exist.
95 void setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) override;
96
Sami Väisänen46eaa942016-06-29 10:26:37 +030097 void setPathFragmentInputGen(const std::string &inputName,
98 GLenum genMode,
99 GLint components,
100 const GLfloat *coeffs) override;
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500101
Jamie Madill06ca6342018-07-12 15:56:53 -0400102 // Also initializes the pipeline layout, descriptor set layouts, and used descriptor ranges.
103 gl::Error initShaders(const ContextVk *contextVk,
104 const gl::DrawCallParams &drawCallParams,
105 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
106 const vk::ShaderAndSerial **fragmentShaderAndSerialOut);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500107
Jamie Madill21061022018-07-12 23:56:30 -0400108 angle::Result updateUniforms(ContextVk *contextVk);
Jamie Madill76e471e2017-10-21 09:56:01 -0400109
Jamie Madill5547b382017-10-23 18:16:01 -0400110 const std::vector<VkDescriptorSet> &getDescriptorSets() const;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400111 const uint32_t *getDynamicOffsets();
112 uint32_t getDynamicOffsetsCount();
Jamie Madill5547b382017-10-23 18:16:01 -0400113
114 // In Vulkan, it is invalid to pass in a NULL descriptor set to vkCmdBindDescriptorSets.
115 // However, it's valid to leave them in an undefined, unbound state, if they are never used.
116 // This means when we want to ignore a descriptor set index, we need to pass in an offset
117 // parameter to BindDescriptorSets, which is an offset into the getDescriptorSets array.
Jamie Madill8c3988c2017-12-21 14:44:56 -0500118 // This allows us to skip binding blank descriptor sets when the Program doesn't use Uniforms
119 // or Textures.
120 const gl::RangeUI &getUsedDescriptorSetRange() const;
Jamie Madill5547b382017-10-23 18:16:01 -0400121
Luc Ferron90968362018-05-04 08:47:22 -0400122 gl::Error updateTexturesDescriptorSet(const gl::Context *context);
Jamie Madill5547b382017-10-23 18:16:01 -0400123 void invalidateTextures();
Jamie Madill76e471e2017-10-21 09:56:01 -0400124
Jamie Madill9b168d02018-06-13 13:25:32 -0400125 const vk::PipelineLayout &getPipelineLayout() const;
126
Luc Ferron7a06ac12018-03-15 10:17:04 -0400127 // For testing only.
128 void setDefaultUniformBlocksMinSizeForTesting(size_t minSize);
129
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500130 private:
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400131 template <int cols, int rows>
132 void setUniformMatrixfv(GLint location,
133 GLsizei count,
134 GLboolean transpose,
135 const GLfloat *value);
136
Jamie Madill21061022018-07-12 23:56:30 -0400137 angle::Result reset(ContextVk *contextVk);
138 angle::Result allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400139 gl::Error initDefaultUniformBlocks(const gl::Context *glContext);
Jamie Madill21061022018-07-12 23:56:30 -0400140 angle::Result updateDefaultUniformsDescriptorSet(ContextVk *contextVk);
Jamie Madill76e471e2017-10-21 09:56:01 -0400141
Luc Ferron7cec3352018-03-13 13:29:34 -0400142 template <class T>
143 void getUniformImpl(GLint location, T *v, GLenum entryPointType) const;
144
Jamie Madill76e471e2017-10-21 09:56:01 -0400145 template <typename T>
146 void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType);
Jamie Madillc5143482017-10-15 20:20:06 -0400147
Jamie Madill06ca6342018-07-12 15:56:53 -0400148 vk::ShaderAndSerial mDefaultVertexShaderAndSerial;
149 vk::ShaderAndSerial mDefaultFragmentShaderAndSerial;
Jamie Madill76e471e2017-10-21 09:56:01 -0400150
151 // State for the default uniform blocks.
152 struct DefaultUniformBlock final : private angle::NonCopyable
153 {
154 DefaultUniformBlock();
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500155 ~DefaultUniformBlock();
Jamie Madill76e471e2017-10-21 09:56:01 -0400156
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400157 vk::DynamicBuffer storage;
Jamie Madill76e471e2017-10-21 09:56:01 -0400158
159 // Shadow copies of the shader uniform data.
160 angle::MemoryBuffer uniformData;
161 bool uniformsDirty;
162
163 // Since the default blocks are laid out in std140, this tells us where to write on a call
164 // to a setUniform method. They are arranged in uniform location order.
165 std::vector<sh::BlockMemberInfo> uniformLayout;
166 };
167
Jamie Madill33318de2018-05-01 11:22:54 -0400168 vk::ShaderMap<DefaultUniformBlock> mDefaultUniformBlocks;
169 vk::ShaderMap<uint32_t> mUniformBlocksOffsets;
Jamie Madill76e471e2017-10-21 09:56:01 -0400170
171 // This is a special "empty" placeholder buffer for when a shader has no uniforms.
172 // It is necessary because we want to keep a compatible pipeline layout in all cases,
173 // and Vulkan does not tolerate having null handles in a descriptor set.
174 vk::BufferAndMemory mEmptyUniformBlockStorage;
175
Jamie Madill5547b382017-10-23 18:16:01 -0400176 // Descriptor sets for uniform blocks and textures for this program.
177 std::vector<VkDescriptorSet> mDescriptorSets;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500178 gl::RangeUI mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400179 bool mDirtyTextures;
180
181 template <typename T>
182 using ShaderTextureArray = std::array<T, gl::IMPLEMENTATION_MAX_SHADER_TEXTURES>;
Jamie Madill9b168d02018-06-13 13:25:32 -0400183
184 // We keep a reference to the pipeline and descriptor set layouts. This ensures they don't get
185 // deleted while this program is in use.
186 vk::BindingPointer<vk::PipelineLayout> mPipelineLayout;
187 vk::DescriptorSetLayoutPointerArray mDescriptorSetLayouts;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400188};
189
190} // namespace rx
191
192#endif // LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_