blob: 4822c191902c8195e6b57ca5e9b593087765b0e9 [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,
100 const gl::DrawCallParams &drawCallParams,
101 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 Madill242c4fe2018-07-12 15:56:56 -0400108 angle::Result updateDescriptorSets(ContextVk *contextVk,
109 const gl::DrawCallParams &drawCallParams,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400110 vk::CommandBuffer *commandBuffer);
Jamie Madill9b168d02018-06-13 13:25:32 -0400111
Luc Ferron7a06ac12018-03-15 10:17:04 -0400112 // For testing only.
113 void setDefaultUniformBlocksMinSizeForTesting(size_t minSize);
114
Jamie Madill88fc6da2018-08-30 16:18:36 -0400115 const vk::PipelineLayout &getPipelineLayout() const { return mPipelineLayout.get(); }
116
117 bool hasTextures() const { return !mState.getSamplerBindings().empty(); }
118
Jamie Madill2b858c22018-09-03 13:58:14 -0400119 bool dirtyUniforms() const { return mDefaultUniformBlocksDirty.any(); }
Jamie Madill88fc6da2018-08-30 16:18:36 -0400120
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500121 private:
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400122 template <int cols, int rows>
123 void setUniformMatrixfv(GLint location,
124 GLsizei count,
125 GLboolean transpose,
126 const GLfloat *value);
127
Jamie Madill21061022018-07-12 23:56:30 -0400128 angle::Result reset(ContextVk *contextVk);
129 angle::Result allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400130 angle::Result initDefaultUniformBlocks(const gl::Context *glContext);
131
Jamie Madill21061022018-07-12 23:56:30 -0400132 angle::Result updateDefaultUniformsDescriptorSet(ContextVk *contextVk);
Jamie Madill76e471e2017-10-21 09:56:01 -0400133
Luc Ferron7cec3352018-03-13 13:29:34 -0400134 template <class T>
135 void getUniformImpl(GLint location, T *v, GLenum entryPointType) const;
136
Jamie Madill76e471e2017-10-21 09:56:01 -0400137 template <typename T>
138 void setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType);
Jamie Madill785e8a02018-10-04 17:42:00 -0400139 angle::Result linkImpl(const gl::Context *glContext,
140 const gl::ProgramLinkedResources &resources,
141 gl::InfoLog &infoLog);
Jamie Madillc5143482017-10-15 20:20:06 -0400142
Jamie Madill76e471e2017-10-21 09:56:01 -0400143 // State for the default uniform blocks.
144 struct DefaultUniformBlock final : private angle::NonCopyable
145 {
146 DefaultUniformBlock();
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500147 ~DefaultUniformBlock();
Jamie Madill76e471e2017-10-21 09:56:01 -0400148
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400149 vk::DynamicBuffer storage;
Jamie Madill76e471e2017-10-21 09:56:01 -0400150
151 // Shadow copies of the shader uniform data.
152 angle::MemoryBuffer uniformData;
Jamie Madill76e471e2017-10-21 09:56:01 -0400153
154 // Since the default blocks are laid out in std140, this tells us where to write on a call
155 // to a setUniform method. They are arranged in uniform location order.
156 std::vector<sh::BlockMemberInfo> uniformLayout;
157 };
158
Jamie Madill33318de2018-05-01 11:22:54 -0400159 vk::ShaderMap<DefaultUniformBlock> mDefaultUniformBlocks;
Jamie Madill2b858c22018-09-03 13:58:14 -0400160 vk::ShaderBitSet mDefaultUniformBlocksDirty;
Jamie Madill33318de2018-05-01 11:22:54 -0400161 vk::ShaderMap<uint32_t> mUniformBlocksOffsets;
Jamie Madill76e471e2017-10-21 09:56:01 -0400162
163 // This is a special "empty" placeholder buffer for when a shader has no uniforms.
164 // It is necessary because we want to keep a compatible pipeline layout in all cases,
165 // and Vulkan does not tolerate having null handles in a descriptor set.
166 vk::BufferAndMemory mEmptyUniformBlockStorage;
167
Jamie Madill5547b382017-10-23 18:16:01 -0400168 // Descriptor sets for uniform blocks and textures for this program.
169 std::vector<VkDescriptorSet> mDescriptorSets;
Jamie Madill8c3988c2017-12-21 14:44:56 -0500170 gl::RangeUI mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400171
Jamie Madill9b168d02018-06-13 13:25:32 -0400172 // We keep a reference to the pipeline and descriptor set layouts. This ensures they don't get
173 // deleted while this program is in use.
174 vk::BindingPointer<vk::PipelineLayout> mPipelineLayout;
175 vk::DescriptorSetLayoutPointerArray mDescriptorSetLayouts;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400176
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400177 // Keep bindings to the descriptor pools. This ensures the pools stay valid while the Program
178 // is in use.
179 vk::DescriptorSetLayoutArray<vk::SharedDescriptorPoolBinding> mDescriptorPoolBindings;
180
Jamie Madill242c4fe2018-07-12 15:56:56 -0400181 class ShaderInfo final : angle::NonCopyable
182 {
183 public:
184 ShaderInfo();
185 ~ShaderInfo();
186
187 angle::Result getShaders(ContextVk *contextVk,
188 const std::string &vertexSource,
189 const std::string &fragmentSource,
Jamie Madillb36a4812018-09-25 10:15:11 -0400190 bool enableLineRasterEmulation,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400191 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
192 const vk::ShaderAndSerial **fragmentShaderAndSerialOut);
193 void destroy(VkDevice device);
194 bool valid() const;
195
196 private:
197 vk::ShaderAndSerial mVertexShaderAndSerial;
198 vk::ShaderAndSerial mFragmentShaderAndSerial;
199 };
200
Jamie Madill242c4fe2018-07-12 15:56:56 -0400201 ShaderInfo mDefaultShaderInfo;
Jamie Madillb36a4812018-09-25 10:15:11 -0400202 ShaderInfo mLineRasterShaderInfo;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400203
204 // We keep the translated linked shader sources to use with shader draw call patching.
205 std::string mVertexSource;
206 std::string mFragmentSource;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400207};
208
209} // namespace rx
210
211#endif // LIBANGLE_RENDERER_VULKAN_PROGRAMVK_H_