blob: 45b407c652699923746e0b03fb9ee00e23193bc6 [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 Madill9e54b5a2016-05-25 12:57:39 -040015#include "libANGLE/renderer/ProgramImpl.h"
Luc Ferron6ea1b412018-03-21 16:13:01 -040016#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill6c7ab7f2018-03-31 14:19:15 -040017#include "libANGLE/renderer/vulkan/vk_helpers.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040018
19namespace rx
20{
Jamie Madill9e54b5a2016-05-25 12:57:39 -040021class ProgramVk : public ProgramImpl
22{
23 public:
24 ProgramVk(const gl::ProgramState &state);
25 ~ProgramVk() override;
Jamie Madillf4a789f2018-10-18 16:56:20 -040026 void destroy(const gl::Context *context) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040027
Jamie Madill785e8a02018-10-04 17:42:00 -040028 angle::Result load(const gl::Context *context,
29 gl::InfoLog &infoLog,
30 gl::BinaryInputStream *stream) override;
Jamie Madill27a60632017-06-30 15:12:01 -040031 void save(const gl::Context *context, gl::BinaryOutputStream *stream) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040032 void setBinaryRetrievableHint(bool retrievable) override;
Yunchao He61afff12017-03-14 15:34:03 +080033 void setSeparable(bool separable) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040034
jchen107ae70d82018-07-06 13:47:01 +080035 std::unique_ptr<LinkEvent> link(const gl::Context *context,
36 const gl::ProgramLinkedResources &resources,
37 gl::InfoLog &infoLog) override;
Jamie Madill9e54b5a2016-05-25 12:57:39 -040038 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 Madill54164b02017-08-28 15:17:37 -040089 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änen46eaa942016-06-29 10:26:37 +030093 void setPathFragmentInputGen(const std::string &inputName,
94 GLenum genMode,
95 GLint components,
96 const GLfloat *coeffs) override;
Jamie Madill8ecf7f92017-01-13 17:29:52 -050097
Jamie Madill06ca6342018-07-12 15:56:53 -040098 // Also initializes the pipeline layout, descriptor set layouts, and used descriptor ranges.
Jamie Madill242c4fe2018-07-12 15:56:56 -040099 angle::Result initShaders(ContextVk *contextVk,
Jamie Madillbfe31c42018-10-25 17:03:47 -0400100 gl::PrimitiveMode mode,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400101 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
102 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
103 const vk::PipelineLayout **pipelineLayoutOut);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500104
Jamie Madill21061022018-07-12 23:56:30 -0400105 angle::Result updateUniforms(ContextVk *contextVk);
Jamie Madill88fc6da2018-08-30 16:18:36 -0400106 angle::Result updateTexturesDescriptorSet(ContextVk *contextVk);
Jamie Madill76e471e2017-10-21 09:56:01 -0400107
Jamie Madill77abad82018-10-25 17:03:48 -0400108 angle::Result updateDescriptorSets(ContextVk *contextVk, vk::CommandBuffer *commandBuffer);
Jamie Madill9b168d02018-06-13 13:25:32 -0400109
Luc Ferron7a06ac12018-03-15 10:17:04 -0400110 // For testing only.
111 void setDefaultUniformBlocksMinSizeForTesting(size_t minSize);
112
Jamie Madill88fc6da2018-08-30 16:18:36 -0400113 const vk::PipelineLayout &getPipelineLayout() const { return mPipelineLayout.get(); }
114
115 bool hasTextures() const { return !mState.getSamplerBindings().empty(); }
116
Jamie Madill2b858c22018-09-03 13:58:14 -0400117 bool dirtyUniforms() const { return mDefaultUniformBlocksDirty.any(); }
Jamie Madill88fc6da2018-08-30 16:18:36 -0400118
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500119 private:
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400120 template <int cols, int rows>
121 void setUniformMatrixfv(GLint location,
122 GLsizei count,
123 GLboolean transpose,
124 const GLfloat *value);
125
Jamie Madill21061022018-07-12 23:56:30 -0400126 angle::Result reset(ContextVk *contextVk);
127 angle::Result allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400128 angle::Result initDefaultUniformBlocks(const gl::Context *glContext);
129
Jamie Madill21061022018-07-12 23:56:30 -0400130 angle::Result updateDefaultUniformsDescriptorSet(ContextVk *contextVk);
Jamie Madill76e471e2017-10-21 09:56:01 -0400131
Luc Ferron7cec3352018-03-13 13:29:34 -0400132 template <class T>
133 void getUniformImpl(GLint location, T *v, GLenum entryPointType) const;
134
Jamie Madill76e471e2017-10-21 09:56:01 -0400135 template <typename T>
136 void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType);
Jamie Madill785e8a02018-10-04 17:42:00 -0400137 angle::Result linkImpl(const gl::Context *glContext,
138 const gl::ProgramLinkedResources &resources,
139 gl::InfoLog &infoLog);
Jamie Madillc5143482017-10-15 20:20:06 -0400140
Jamie Madill76e471e2017-10-21 09:56:01 -0400141 // State for the default uniform blocks.
142 struct DefaultUniformBlock final : private angle::NonCopyable
143 {
144 DefaultUniformBlock();
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500145 ~DefaultUniformBlock();
Jamie Madill76e471e2017-10-21 09:56:01 -0400146
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400147 vk::DynamicBuffer storage;
Jamie Madill76e471e2017-10-21 09:56:01 -0400148
149 // Shadow copies of the shader uniform data.
150 angle::MemoryBuffer uniformData;
Jamie Madill76e471e2017-10-21 09:56:01 -0400151
152 // Since the default blocks are laid out in std140, this tells us where to write on a call
153 // to a setUniform method. They are arranged in uniform location order.
154 std::vector<sh::BlockMemberInfo> uniformLayout;
155 };
156
Jamie Madill33318de2018-05-01 11:22:54 -0400157 vk::ShaderMap<DefaultUniformBlock> mDefaultUniformBlocks;
Jamie Madill2b858c22018-09-03 13:58:14 -0400158 vk::ShaderBitSet mDefaultUniformBlocksDirty;
Jamie Madill33318de2018-05-01 11:22:54 -0400159 vk::ShaderMap<uint32_t> mUniformBlocksOffsets;
Jamie Madill76e471e2017-10-21 09:56:01 -0400160
161 // This is a special "empty" placeholder buffer for when a shader has no uniforms.
162 // It is necessary because we want to keep a compatible pipeline layout in all cases,
163 // and Vulkan does not tolerate having null handles in a descriptor set.
164 vk::BufferAndMemory mEmptyUniformBlockStorage;
165
Jamie Madill5547b382017-10-23 18:16:01 -0400166 // Descriptor sets for uniform blocks and textures for this program.
167 std::vector<VkDescriptorSet> mDescriptorSets;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500168 gl::RangeUI mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400169
Jamie Madill9b168d02018-06-13 13:25:32 -0400170 // We keep a reference to the pipeline and descriptor set layouts. This ensures they don't get
171 // deleted while this program is in use.
172 vk::BindingPointer<vk::PipelineLayout> mPipelineLayout;
173 vk::DescriptorSetLayoutPointerArray mDescriptorSetLayouts;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400174
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400175 // Keep bindings to the descriptor pools. This ensures the pools stay valid while the Program
176 // is in use.
177 vk::DescriptorSetLayoutArray<vk::SharedDescriptorPoolBinding> mDescriptorPoolBindings;
178
Jamie Madill242c4fe2018-07-12 15:56:56 -0400179 class ShaderInfo final : angle::NonCopyable
180 {
181 public:
182 ShaderInfo();
183 ~ShaderInfo();
184
185 angle::Result getShaders(ContextVk *contextVk,
186 const std::string &vertexSource,
187 const std::string &fragmentSource,
Jamie Madillb36a4812018-09-25 10:15:11 -0400188 bool enableLineRasterEmulation,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400189 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
190 const vk::ShaderAndSerial **fragmentShaderAndSerialOut);
191 void destroy(VkDevice device);
192 bool valid() const;
193
194 private:
195 vk::ShaderAndSerial mVertexShaderAndSerial;
196 vk::ShaderAndSerial mFragmentShaderAndSerial;
197 };
198
Jamie Madill242c4fe2018-07-12 15:56:56 -0400199 ShaderInfo mDefaultShaderInfo;
Jamie Madillb36a4812018-09-25 10:15:11 -0400200 ShaderInfo mLineRasterShaderInfo;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400201
202 // We keep the translated linked shader sources to use with shader draw call patching.
203 std::string mVertexSource;
204 std::string mFragmentSource;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400205};
206
207} // namespace rx
208
209#endif // LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_