blob: 129ab02e7110957be10189b3a95310179e093e70 [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.cpp:
7// Implements the class methods for ProgramVk.
8//
9
10#include "libANGLE/renderer/vulkan/ProgramVk.h"
11
12#include "common/debug.h"
Jamie Madill76e471e2017-10-21 09:56:01 -040013#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040014#include "libANGLE/Context.h"
Luc Ferron48cdc2e2018-05-31 09:58:34 -040015#include "libANGLE/renderer/renderer_utils.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050016#include "libANGLE/renderer/vulkan/ContextVk.h"
17#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
18#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill5547b382017-10-23 18:16:01 -040019#include "libANGLE/renderer/vulkan/TextureVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040020
21namespace rx
22{
23
Jamie Madill76e471e2017-10-21 09:56:01 -040024namespace
25{
26
Jamie Madillf3614372018-03-31 14:19:14 -040027constexpr size_t kUniformBlockDynamicBufferMinSize = 256 * 128;
Luc Ferron7a06ac12018-03-15 10:17:04 -040028
Jamie Madill242c4fe2018-07-12 15:56:56 -040029void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms,
Jamie Madill21061022018-07-12 23:56:30 -040030 gl::Shader *shader,
31 sh::BlockLayoutMap *blockLayoutMapOut,
32 size_t *blockSizeOut)
Jamie Madill76e471e2017-10-21 09:56:01 -040033{
Jamie Madill76e471e2017-10-21 09:56:01 -040034 if (uniforms.empty())
35 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040036 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040037 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040038 }
39
40 sh::Std140BlockEncoder blockEncoder;
Olli Etuaho3de27032017-11-30 12:16:47 +020041 sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
Jamie Madill76e471e2017-10-21 09:56:01 -040042
43 size_t blockSize = blockEncoder.getBlockSize();
44
45 // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized.
46 if (blockSize == 0)
47 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040048 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040049 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040050 }
51
Luc Ferron7a06ac12018-03-15 10:17:04 -040052 *blockSizeOut = blockSize;
Jamie Madill21061022018-07-12 23:56:30 -040053 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040054}
55
56template <typename T>
57void UpdateDefaultUniformBlock(GLsizei count,
Luc Ferron2371aca2018-03-27 16:03:03 -040058 uint32_t arrayIndex,
Jamie Madill76e471e2017-10-21 09:56:01 -040059 int componentCount,
60 const T *v,
61 const sh::BlockMemberInfo &layoutInfo,
62 angle::MemoryBuffer *uniformData)
63{
Luc Ferron2371aca2018-03-27 16:03:03 -040064 const int elementSize = sizeof(T) * componentCount;
65
66 uint8_t *dst = uniformData->data() + layoutInfo.offset;
Jamie Madill76e471e2017-10-21 09:56:01 -040067 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
68 {
Luc Ferron2371aca2018-03-27 16:03:03 -040069 uint32_t arrayOffset = arrayIndex * layoutInfo.arrayStride;
70 uint8_t *writePtr = dst + arrayOffset;
Jamie Madill76e471e2017-10-21 09:56:01 -040071 memcpy(writePtr, v, elementSize * count);
72 }
73 else
74 {
Luc Ferron2371aca2018-03-27 16:03:03 -040075 // Have to respect the arrayStride between each element of the array.
76 int maxIndex = arrayIndex + count;
77 for (int writeIndex = arrayIndex, readIndex = 0; writeIndex < maxIndex;
78 writeIndex++, readIndex++)
79 {
80 const int arrayOffset = writeIndex * layoutInfo.arrayStride;
81 uint8_t *writePtr = dst + arrayOffset;
Luc Ferrone9465a62018-06-04 10:41:52 -040082 const T *readPtr = v + (readIndex * componentCount);
Luc Ferron2371aca2018-03-27 16:03:03 -040083 memcpy(writePtr, readPtr, elementSize);
84 }
Jamie Madill76e471e2017-10-21 09:56:01 -040085 }
86}
87
Luc Ferron7cec3352018-03-13 13:29:34 -040088template <typename T>
89void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040090 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040091 T *dst,
92 const sh::BlockMemberInfo &layoutInfo,
93 const angle::MemoryBuffer *uniformData)
94{
95 ASSERT(layoutInfo.offset != -1);
96
Luc Ferron2371aca2018-03-27 16:03:03 -040097 const int elementSize = sizeof(T) * componentCount;
98 const uint8_t *source = uniformData->data() + layoutInfo.offset;
99
Luc Ferron7cec3352018-03-13 13:29:34 -0400100 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
101 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400102 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400103 memcpy(dst, readPtr, elementSize);
104 }
105 else
106 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400107 // Have to respect the arrayStride between each element of the array.
108 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
109 const uint8_t *readPtr = source + arrayOffset;
110 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400111 }
112}
113
Jamie Madill21061022018-07-12 23:56:30 -0400114angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
115 vk::DynamicBuffer *dynamicBuffer,
116 const angle::MemoryBuffer &bufferData,
117 uint32_t *outOffset,
118 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400119{
Luc Ferron7a06ac12018-03-15 10:17:04 -0400120 ASSERT(!bufferData.empty());
121 uint8_t *data = nullptr;
122 VkBuffer *outBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -0400123 VkDeviceSize offset = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400124 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400125 outBufferModified));
Jamie Madill4c310832018-08-29 13:43:17 -0400126 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400127 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400128 ANGLE_TRY(dynamicBuffer->flush(contextVk));
129 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400130}
Jamie Madill76e471e2017-10-21 09:56:01 -0400131} // anonymous namespace
132
Jamie Madill242c4fe2018-07-12 15:56:56 -0400133// ProgramVk::ShaderInfo implementation.
134ProgramVk::ShaderInfo::ShaderInfo()
135{
136}
137
138ProgramVk::ShaderInfo::~ShaderInfo() = default;
139
140angle::Result ProgramVk::ShaderInfo::getShaders(
141 ContextVk *contextVk,
142 const std::string &vertexSource,
143 const std::string &fragmentSource,
144 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
145 const vk::ShaderAndSerial **fragmentShaderAndSerialOut)
146{
147 if (!valid())
148 {
149 std::vector<uint32_t> vertexCode;
150 std::vector<uint32_t> fragmentCode;
151 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(), vertexSource,
152 fragmentSource, &vertexCode, &fragmentCode));
153
154 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mVertexShaderAndSerial, vertexCode.data(),
155 vertexCode.size() * sizeof(uint32_t)));
156 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mFragmentShaderAndSerial, fragmentCode.data(),
157 fragmentCode.size() * sizeof(uint32_t)));
158 }
159
160 *fragmentShaderAndSerialOut = &mFragmentShaderAndSerial;
161 *vertexShaderAndSerialOut = &mVertexShaderAndSerial;
162 return angle::Result::Continue();
163}
164
165void ProgramVk::ShaderInfo::destroy(VkDevice device)
166{
167 mVertexShaderAndSerial.destroy(device);
168 mFragmentShaderAndSerial.destroy(device);
169}
170
171bool ProgramVk::ShaderInfo::valid() const
172{
173 return mVertexShaderAndSerial.valid();
174}
175
176// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400177ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400178 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Jamie Madill2b858c22018-09-03 13:58:14 -0400179 kUniformBlockDynamicBufferMinSize)
Jamie Madill76e471e2017-10-21 09:56:01 -0400180{
181}
182
Jamie Madill242c4fe2018-07-12 15:56:56 -0400183ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500184
Jamie Madill88fc6da2018-08-30 16:18:36 -0400185ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400186{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500187 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400188}
189
Jamie Madill242c4fe2018-07-12 15:56:56 -0400190ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400191
Jamie Madill242c4fe2018-07-12 15:56:56 -0400192gl::Error ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500193{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400194 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500195 return reset(contextVk);
Jamie Madillc5143482017-10-15 20:20:06 -0400196}
Jamie Madill5deea722017-02-16 10:44:46 -0500197
Jamie Madill21061022018-07-12 23:56:30 -0400198angle::Result ProgramVk::reset(ContextVk *contextVk)
Jamie Madillc5143482017-10-15 20:20:06 -0400199{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500200 VkDevice device = contextVk->getDevice();
201
Jamie Madill9b168d02018-06-13 13:25:32 -0400202 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
203 {
204 descriptorSetLayout.reset();
205 }
206 mPipelineLayout.reset();
207
Jamie Madillcaaff162018-06-22 08:55:37 -0400208 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400209 for (auto &uniformBlock : mDefaultUniformBlocks)
210 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400211 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400212 }
213
Jamie Madill242c4fe2018-07-12 15:56:56 -0400214 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
215 mDefaultShaderInfo.destroy(device);
216
Jamie Madillcaaff162018-06-22 08:55:37 -0400217 Serial currentSerial = renderer->getCurrentQueueSerial();
218 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
219 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400220
Jamie Madill5547b382017-10-23 18:16:01 -0400221 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500222 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500223
Jamie Madill21061022018-07-12 23:56:30 -0400224 return angle::Result::Continue();
Jamie Madill5deea722017-02-16 10:44:46 -0500225}
226
Jamie Madill242c4fe2018-07-12 15:56:56 -0400227gl::LinkResult ProgramVk::load(const gl::Context *context,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400228 gl::InfoLog &infoLog,
229 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400230{
231 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500232 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400233}
234
Jamie Madill27a60632017-06-30 15:12:01 -0400235void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400236{
237 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400238}
239
240void ProgramVk::setBinaryRetrievableHint(bool retrievable)
241{
242 UNIMPLEMENTED();
243}
244
Yunchao He61afff12017-03-14 15:34:03 +0800245void ProgramVk::setSeparable(bool separable)
246{
247 UNIMPLEMENTED();
248}
249
jchen107ae70d82018-07-06 13:47:01 +0800250std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *glContext,
251 const gl::ProgramLinkedResources &resources,
252 gl::InfoLog &infoLog)
253{
254 // TODO(jie.a.chen@intel.com): Parallelize linking.
255 // http://crbug.com/849576
256 return std::make_unique<LinkEventDone>(linkImpl(glContext, resources, infoLog));
257}
258
259gl::LinkResult ProgramVk::linkImpl(const gl::Context *glContext,
260 const gl::ProgramLinkedResources &resources,
261 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400262{
Jamie Madill06ca6342018-07-12 15:56:53 -0400263 ContextVk *contextVk = vk::GetImpl(glContext);
264 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400265
Jamie Madillb7d924a2018-03-10 11:16:54 -0500266 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500267
jchen103fd614d2018-08-13 12:21:58 +0800268 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500269
Jamie Madill76e471e2017-10-21 09:56:01 -0400270 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500271
Jamie Madill8c3988c2017-12-21 14:44:56 -0500272 if (!mState.getSamplerUniformRange().empty())
273 {
274 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400275 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500276 }
277
Jamie Madill9b168d02018-06-13 13:25:32 -0400278 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
279 // don't already exist in the cache.
280 vk::DescriptorSetLayoutDesc uniformsSetDesc;
281 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
282 1);
283 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
284 1);
285
286 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400287 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400288
Jamie Madill9b168d02018-06-13 13:25:32 -0400289 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400290
291 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
292 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400293 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400294 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
295
296 // The front-end always binds array sampler units sequentially.
297 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
298 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400299 }
300
Jamie Madill21061022018-07-12 23:56:30 -0400301 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400302 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
303
Jamie Madillb01b4802018-07-10 12:43:57 -0400304 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
305 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
306 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400307 contextVk, driverUniformsSetDesc,
308 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400309
Jamie Madill9b168d02018-06-13 13:25:32 -0400310 vk::PipelineLayoutDesc pipelineLayoutDesc;
311 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
312 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400313 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
314 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400315
Jamie Madill21061022018-07-12 23:56:30 -0400316 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
317 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400318
Jamie Madill242c4fe2018-07-12 15:56:56 -0400319 if (!mState.getUniforms().empty())
320 {
321 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
322
323 if (mState.getUniforms().size() > samplerRange.length())
324 {
325 // Ensure the descriptor set range includes the uniform buffers at position 0.
326 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
327 }
328
329 if (!samplerRange.empty())
330 {
331 // Ensure the descriptor set range includes the textures at position 1.
332 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400333 }
334 }
335
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500336 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400337}
338
Jamie Madill242c4fe2018-07-12 15:56:56 -0400339angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400340{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400341 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500342 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400343
344 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400345 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
346 vk::ShaderMap<size_t> requiredBufferSize;
347 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400348
Jamie Madill33318de2018-05-01 11:22:54 -0400349 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400350 {
Jamie Madill33318de2018-05-01 11:22:54 -0400351 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400352 gl::Shader *shader = mState.getAttachedShader(glShaderType);
jchen103fd614d2018-08-13 12:21:58 +0800353 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400354 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
355 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400356 }
357
358 // Init the default block layout info.
Jamie Madill76e471e2017-10-21 09:56:01 -0400359 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400360 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400361 {
Jamie Madill33318de2018-05-01 11:22:54 -0400362 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400363
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 if (location.used() && !location.ignored)
365 {
Jamie Madillde03e002017-10-21 14:04:20 -0400366 const auto &uniform = uniforms[location.index];
367
368 if (uniform.isSampler())
369 continue;
370
Jamie Madill76e471e2017-10-21 09:56:01 -0400371 std::string uniformName = uniform.name;
372 if (uniform.isArray())
373 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400374 // Gets the uniform name without the [0] at the end.
375 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400376 }
377
378 bool found = false;
379
Jamie Madill33318de2018-05-01 11:22:54 -0400380 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400381 {
Jamie Madill33318de2018-05-01 11:22:54 -0400382 auto it = layoutMap[shaderType].find(uniformName);
383 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400384 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400385 found = true;
386 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400387 }
388 }
389
390 ASSERT(found);
391 }
392
Jamie Madill33318de2018-05-01 11:22:54 -0400393 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400394 {
Jamie Madill33318de2018-05-01 11:22:54 -0400395 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400396 }
397 }
398
Jamie Madill33318de2018-05-01 11:22:54 -0400399 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400400 {
Jamie Madill33318de2018-05-01 11:22:54 -0400401 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400402 {
Jamie Madill33318de2018-05-01 11:22:54 -0400403 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
404 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400405 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400406 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400407 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400408 size_t minAlignment = static_cast<size_t>(
409 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
410
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400411 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400412
413 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400414 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400415 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400416 }
417 }
418
Jamie Madill2b858c22018-09-03 13:58:14 -0400419 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400420 {
421 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400422 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400423 {
424 VkBufferCreateInfo uniformBufferInfo;
425 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
426 uniformBufferInfo.pNext = nullptr;
427 uniformBufferInfo.flags = 0;
428 uniformBufferInfo.size = 1;
429 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
430 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
431 uniformBufferInfo.queueFamilyIndexCount = 0;
432 uniformBufferInfo.pQueueFamilyIndices = nullptr;
433
Jamie Madill21061022018-07-12 23:56:30 -0400434 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400435
Luc Ferron7a06ac12018-03-15 10:17:04 -0400436 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500437 VkMemoryPropertyFlags flags =
438 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tobin Ehlisf5aad062018-09-19 14:39:00 -0600439 VkMemoryPropertyFlags flagsOut = 0;
440 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &flagsOut,
441 &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400442 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400443 }
Jamie Madill5547b382017-10-23 18:16:01 -0400444 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400445
Jamie Madill242c4fe2018-07-12 15:56:56 -0400446 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400447}
448
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400449GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
450{
Luc Ferronfba1f612018-06-04 14:37:17 -0400451 // No-op. The spec is very vague about the behavior of validation.
452 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400453}
454
Jamie Madill76e471e2017-10-21 09:56:01 -0400455template <typename T>
456void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
457{
458 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
459 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
460
Luc Ferron7cec3352018-03-13 13:29:34 -0400461 if (linkedUniform.isSampler())
462 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400463 // We could potentially cache some indexing here. For now this is a no-op since the mapping
464 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400465 return;
466 }
467
Luc Ferron24a31372018-04-04 11:49:14 -0400468 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400469 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400470 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400471 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400472 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400473 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400474
Luc Ferron24a31372018-04-04 11:49:14 -0400475 // Assume an offset of -1 means the block is unused.
476 if (layoutInfo.offset == -1)
477 {
478 continue;
479 }
480
481 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400482 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
483 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400484 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400485 }
Luc Ferron24a31372018-04-04 11:49:14 -0400486 }
487 else
488 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400489 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400490 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400491 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400492 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
493
494 // Assume an offset of -1 means the block is unused.
495 if (layoutInfo.offset == -1)
496 {
497 continue;
498 }
499
500 const GLint componentCount = linkedUniform.typeInfo->componentCount;
501
Luc Ferron62059a52018-03-29 07:01:35 -0400502 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
503
Luc Ferrond91c3792018-04-06 09:36:36 -0400504 GLint initialArrayOffset =
505 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400506 for (GLint i = 0; i < count; i++)
507 {
508 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
509 GLint *dest =
510 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
511 const T *source = v + i * componentCount;
512
513 for (int c = 0; c < componentCount; c++)
514 {
515 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
516 }
517 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400518
519 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400520 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400521 }
522}
523
Luc Ferron7cec3352018-03-13 13:29:34 -0400524template <typename T>
525void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
526{
527 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
528 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
529
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400530 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400531
Olli Etuaho107c7242018-03-20 15:45:35 +0200532 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800533 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400534
Jiawei Shao385b3e02018-03-21 09:43:28 +0800535 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400536 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400537 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400538
539 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
540 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400541
542 if (gl::IsMatrixType(linkedUniform.type))
543 {
544 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400545 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400546 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
547 }
548 else
549 {
550 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
551 v, layoutInfo, &uniformBlock.uniformData);
552 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400553}
554
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400555void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
556{
Jamie Madill76e471e2017-10-21 09:56:01 -0400557 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400558}
559
560void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
561{
Jamie Madill76e471e2017-10-21 09:56:01 -0400562 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400563}
564
565void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
566{
Jamie Madill76e471e2017-10-21 09:56:01 -0400567 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400568}
569
570void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
571{
Jamie Madill76e471e2017-10-21 09:56:01 -0400572 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400573}
574
575void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
576{
Luc Ferron7cec3352018-03-13 13:29:34 -0400577 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400578}
579
580void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
581{
Luc Ferron489243f2018-03-28 16:55:28 -0400582 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400583}
584
585void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
586{
Luc Ferron489243f2018-03-28 16:55:28 -0400587 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400588}
589
590void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
591{
Luc Ferron489243f2018-03-28 16:55:28 -0400592 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400593}
594
595void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
596{
597 UNIMPLEMENTED();
598}
599
600void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
601{
602 UNIMPLEMENTED();
603}
604
605void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
606{
607 UNIMPLEMENTED();
608}
609
610void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
611{
612 UNIMPLEMENTED();
613}
614
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400615template <int cols, int rows>
616void ProgramVk::setUniformMatrixfv(GLint location,
617 GLsizei count,
618 GLboolean transpose,
619 const GLfloat *value)
620{
621 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
622 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
623
Jamie Madill2b858c22018-09-03 13:58:14 -0400624 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400625 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400626 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400627 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
628
629 // Assume an offset of -1 means the block is unused.
630 if (layoutInfo.offset == -1)
631 {
632 continue;
633 }
634
Luc Ferronc8fbff32018-06-04 10:30:48 -0400635 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400636 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
637 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400638
639 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
640 // setter did not update any data. We still want the uniform to be included when we'll
641 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400642 if (updated)
643 {
644 mDefaultUniformBlocksDirty.set(shaderType);
645 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400646 }
647}
648
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400649void ProgramVk::setUniformMatrix2fv(GLint location,
650 GLsizei count,
651 GLboolean transpose,
652 const GLfloat *value)
653{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400654 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400655}
656
657void ProgramVk::setUniformMatrix3fv(GLint location,
658 GLsizei count,
659 GLboolean transpose,
660 const GLfloat *value)
661{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400662 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400663}
664
665void ProgramVk::setUniformMatrix4fv(GLint location,
666 GLsizei count,
667 GLboolean transpose,
668 const GLfloat *value)
669{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400670 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400671}
672
673void ProgramVk::setUniformMatrix2x3fv(GLint location,
674 GLsizei count,
675 GLboolean transpose,
676 const GLfloat *value)
677{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400678 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400679}
680
681void ProgramVk::setUniformMatrix3x2fv(GLint location,
682 GLsizei count,
683 GLboolean transpose,
684 const GLfloat *value)
685{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400686 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400687}
688
689void ProgramVk::setUniformMatrix2x4fv(GLint location,
690 GLsizei count,
691 GLboolean transpose,
692 const GLfloat *value)
693{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400694 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400695}
696
697void ProgramVk::setUniformMatrix4x2fv(GLint location,
698 GLsizei count,
699 GLboolean transpose,
700 const GLfloat *value)
701{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400702 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400703}
704
705void ProgramVk::setUniformMatrix3x4fv(GLint location,
706 GLsizei count,
707 GLboolean transpose,
708 const GLfloat *value)
709{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400710 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400711}
712
713void ProgramVk::setUniformMatrix4x3fv(GLint location,
714 GLsizei count,
715 GLboolean transpose,
716 const GLfloat *value)
717{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400718 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400719}
720
Sami Väisänen46eaa942016-06-29 10:26:37 +0300721void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
722 GLenum genMode,
723 GLint components,
724 const GLfloat *coeffs)
725{
726 UNIMPLEMENTED();
727}
728
Jamie Madill242c4fe2018-07-12 15:56:56 -0400729angle::Result ProgramVk::initShaders(ContextVk *contextVk,
730 const gl::DrawCallParams &drawCallParams,
731 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
732 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
733 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500734{
Jamie Madill06ca6342018-07-12 15:56:53 -0400735 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
Jamie Madill242c4fe2018-07-12 15:56:56 -0400736 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource,
737 vertexShaderAndSerialOut, fragmentShaderAndSerialOut));
738 ASSERT(mDefaultShaderInfo.valid());
739
740 *pipelineLayoutOut = &mPipelineLayout.get();
741
742 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500743}
744
Jamie Madill21061022018-07-12 23:56:30 -0400745angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400746{
Jamie Madill76e471e2017-10-21 09:56:01 -0400747 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400748 vk::DynamicDescriptorPool *dynamicDescriptorPool =
749 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400750
Luc Ferron6ea1b412018-03-21 16:13:01 -0400751 uint32_t potentialNewCount = descriptorSetIndex + 1;
752 if (potentialNewCount > mDescriptorSets.size())
753 {
754 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
755 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400756
Jamie Madillc7918ce2018-06-13 13:25:31 -0400757 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400758 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400759 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400760 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400761 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400762}
763
Jamie Madill54164b02017-08-28 15:17:37 -0400764void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
765{
Luc Ferron7cec3352018-03-13 13:29:34 -0400766 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400767}
768
769void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
770{
Luc Ferron7cec3352018-03-13 13:29:34 -0400771 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400772}
773
774void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
775{
776 UNIMPLEMENTED();
777}
778
Jamie Madill21061022018-07-12 23:56:30 -0400779angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400780{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400781 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400782
Jamie Madill76e471e2017-10-21 09:56:01 -0400783 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400784 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400785 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400786 {
Jamie Madill33318de2018-05-01 11:22:54 -0400787 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400788
Jamie Madill2b858c22018-09-03 13:58:14 -0400789 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400790 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400791 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400792 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400793 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400794 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400795 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400796
797 if (bufferModified)
798 {
799 anyNewBufferAllocated = true;
800 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400801 }
802 }
803
Luc Ferron7a06ac12018-03-15 10:17:04 -0400804 if (anyNewBufferAllocated)
805 {
806 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
807 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400808 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400809 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
810 }
811
Jamie Madill21061022018-07-12 23:56:30 -0400812 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400813}
814
Jamie Madill21061022018-07-12 23:56:30 -0400815angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400816{
Jamie Madill33318de2018-05-01 11:22:54 -0400817 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
818 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400819
Jamie Madill33318de2018-05-01 11:22:54 -0400820 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400821 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400822 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
823 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
824 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400825
826 if (!uniformBlock.uniformData.empty())
827 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400828 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400829 }
830 else
831 {
832 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
833 }
834
835 bufferInfo.offset = 0;
836 bufferInfo.range = VK_WHOLE_SIZE;
837
Jamie Madill76e471e2017-10-21 09:56:01 -0400838 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
839 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400840 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400841 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400842 writeInfo.dstArrayElement = 0;
843 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400844 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400845 writeInfo.pImageInfo = nullptr;
846 writeInfo.pBufferInfo = &bufferInfo;
847 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400848 }
849
850 VkDevice device = contextVk->getDevice();
851
Jamie Madill33318de2018-05-01 11:22:54 -0400852 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400853
Jamie Madill21061022018-07-12 23:56:30 -0400854 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400855}
856
Jamie Madill84c662b2018-07-12 15:56:55 -0400857angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400858{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400859 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400860 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400861
Jamie Madill8c3988c2017-12-21 14:44:56 -0500862 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400863 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400864
Jamie Madill84c662b2018-07-12 15:56:55 -0400865 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
866 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400867 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400868
Jamie Madill84c662b2018-07-12 15:56:55 -0400869 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400870
Jamie Madill4cc753e2018-06-13 13:25:33 -0400871 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
872 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400873 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400874 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
875
Jamie Madill5547b382017-10-23 18:16:01 -0400876 ASSERT(!samplerBinding.unreferenced);
877
Jamie Madill4cc753e2018-06-13 13:25:33 -0400878 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
879 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400880 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400881 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
882 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400883 const vk::ImageHelper &image = textureVk->getImage();
884
885 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
886
887 imageInfo.sampler = textureVk->getSampler().getHandle();
888 imageInfo.imageView = textureVk->getImageView().getHandle();
889 imageInfo.imageLayout = image.getCurrentLayout();
890
891 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
892
893 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
894 writeInfo.pNext = nullptr;
895 writeInfo.dstSet = descriptorSet;
896 writeInfo.dstBinding = textureIndex;
897 writeInfo.dstArrayElement = arrayElement;
898 writeInfo.descriptorCount = 1;
899 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
900 writeInfo.pImageInfo = &imageInfo;
901 writeInfo.pBufferInfo = nullptr;
902 writeInfo.pTexelBufferView = nullptr;
903
904 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400905 }
Jamie Madill5547b382017-10-23 18:16:01 -0400906 }
907
908 VkDevice device = contextVk->getDevice();
909
Jamie Madill4cc753e2018-06-13 13:25:33 -0400910 ASSERT(writeCount > 0);
911 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400912
Jamie Madill84c662b2018-07-12 15:56:55 -0400913 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400914}
915
Luc Ferron7a06ac12018-03-15 10:17:04 -0400916void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
917{
918 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
919 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400920 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400921 }
922}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400923
924angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
925 const gl::DrawCallParams &drawCallParams,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400926 vk::CommandBuffer *commandBuffer)
927{
928 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
929 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400930
931 if (mUsedDescriptorSetRange.empty())
932 return angle::Result::Continue();
933
934 ASSERT(!mDescriptorSets.empty());
935
936 unsigned int low = mUsedDescriptorSetRange.low();
937
938 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
939 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
940 {
941 commandBuffer->bindDescriptorSets(
942 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
943 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
944 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
945 }
946 else
947 {
948 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
949 low, mUsedDescriptorSetRange.length(),
950 &mDescriptorSets[low], 0, nullptr);
951 }
952
953 return angle::Result::Continue();
954}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400955} // namespace rx