blob: feb6d15fd2fab236fc21a7de3205c097cfd7d1f1 [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 Madillc564c072017-06-01 12:45:42 -040013#include "libANGLE/Context.h"
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -040014#include "libANGLE/ProgramLinkedResources.h"
Luc Ferron48cdc2e2018-05-31 09:58:34 -040015#include "libANGLE/renderer/renderer_utils.h"
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -040016#include "libANGLE/renderer/vulkan/BufferVk.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050017#include "libANGLE/renderer/vulkan/ContextVk.h"
18#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
19#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill5547b382017-10-23 18:16:01 -040020#include "libANGLE/renderer/vulkan/TextureVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040021
22namespace rx
23{
24
Jamie Madill76e471e2017-10-21 09:56:01 -040025namespace
26{
27
Jamie Madillf3614372018-03-31 14:19:14 -040028constexpr size_t kUniformBlockDynamicBufferMinSize = 256 * 128;
Luc Ferron7a06ac12018-03-15 10:17:04 -040029
Jamie Madill242c4fe2018-07-12 15:56:56 -040030void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms,
Jamie Madill21061022018-07-12 23:56:30 -040031 gl::Shader *shader,
32 sh::BlockLayoutMap *blockLayoutMapOut,
33 size_t *blockSizeOut)
Jamie Madill76e471e2017-10-21 09:56:01 -040034{
Jamie Madill76e471e2017-10-21 09:56:01 -040035 if (uniforms.empty())
36 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040037 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040038 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040039 }
40
41 sh::Std140BlockEncoder blockEncoder;
Olli Etuaho3de27032017-11-30 12:16:47 +020042 sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
Jamie Madill76e471e2017-10-21 09:56:01 -040043
Jamie Madill4e712be2019-01-03 13:53:59 -050044 size_t blockSize = blockEncoder.getCurrentOffset();
Jamie Madill76e471e2017-10-21 09:56:01 -040045
46 // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized.
47 if (blockSize == 0)
48 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040049 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040050 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040051 }
52
Luc Ferron7a06ac12018-03-15 10:17:04 -040053 *blockSizeOut = blockSize;
Jamie Madill21061022018-07-12 23:56:30 -040054 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040055}
56
57template <typename T>
58void UpdateDefaultUniformBlock(GLsizei count,
Luc Ferron2371aca2018-03-27 16:03:03 -040059 uint32_t arrayIndex,
Jamie Madill76e471e2017-10-21 09:56:01 -040060 int componentCount,
61 const T *v,
62 const sh::BlockMemberInfo &layoutInfo,
63 angle::MemoryBuffer *uniformData)
64{
Luc Ferron2371aca2018-03-27 16:03:03 -040065 const int elementSize = sizeof(T) * componentCount;
66
67 uint8_t *dst = uniformData->data() + layoutInfo.offset;
Jamie Madill76e471e2017-10-21 09:56:01 -040068 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
69 {
Luc Ferron2371aca2018-03-27 16:03:03 -040070 uint32_t arrayOffset = arrayIndex * layoutInfo.arrayStride;
71 uint8_t *writePtr = dst + arrayOffset;
Geoff Langbeb0c942018-09-27 11:22:23 -040072 ASSERT(writePtr + (elementSize * count) <= uniformData->data() + uniformData->size());
Jamie Madill76e471e2017-10-21 09:56:01 -040073 memcpy(writePtr, v, elementSize * count);
74 }
75 else
76 {
Luc Ferron2371aca2018-03-27 16:03:03 -040077 // Have to respect the arrayStride between each element of the array.
78 int maxIndex = arrayIndex + count;
79 for (int writeIndex = arrayIndex, readIndex = 0; writeIndex < maxIndex;
80 writeIndex++, readIndex++)
81 {
82 const int arrayOffset = writeIndex * layoutInfo.arrayStride;
83 uint8_t *writePtr = dst + arrayOffset;
Luc Ferrone9465a62018-06-04 10:41:52 -040084 const T *readPtr = v + (readIndex * componentCount);
Geoff Langbeb0c942018-09-27 11:22:23 -040085 ASSERT(writePtr + elementSize <= uniformData->data() + uniformData->size());
Luc Ferron2371aca2018-03-27 16:03:03 -040086 memcpy(writePtr, readPtr, elementSize);
87 }
Jamie Madill76e471e2017-10-21 09:56:01 -040088 }
89}
90
Luc Ferron7cec3352018-03-13 13:29:34 -040091template <typename T>
92void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040093 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040094 T *dst,
95 const sh::BlockMemberInfo &layoutInfo,
96 const angle::MemoryBuffer *uniformData)
97{
98 ASSERT(layoutInfo.offset != -1);
99
Luc Ferron2371aca2018-03-27 16:03:03 -0400100 const int elementSize = sizeof(T) * componentCount;
101 const uint8_t *source = uniformData->data() + layoutInfo.offset;
102
Luc Ferron7cec3352018-03-13 13:29:34 -0400103 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
104 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400105 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400106 memcpy(dst, readPtr, elementSize);
107 }
108 else
109 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400110 // Have to respect the arrayStride between each element of the array.
111 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
112 const uint8_t *readPtr = source + arrayOffset;
113 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400114 }
115}
116
Jamie Madill21061022018-07-12 23:56:30 -0400117angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
118 vk::DynamicBuffer *dynamicBuffer,
119 const angle::MemoryBuffer &bufferData,
120 uint32_t *outOffset,
121 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400122{
Shahbaz Youssefi5829c032018-11-28 10:39:41 -0500123 dynamicBuffer->releaseRetainedBuffers(contextVk->getRenderer());
124
Luc Ferron7a06ac12018-03-15 10:17:04 -0400125 ASSERT(!bufferData.empty());
126 uint8_t *data = nullptr;
127 VkBuffer *outBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -0400128 VkDeviceSize offset = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400129 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400130 outBufferModified));
Jamie Madill4c310832018-08-29 13:43:17 -0400131 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400132 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400133 ANGLE_TRY(dynamicBuffer->flush(contextVk));
Jamie Madill7c985f52018-11-29 18:16:17 -0500134 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400135}
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400136
137uint32_t GetUniformBlockArraySize(const std::vector<gl::InterfaceBlock> &uniformBlocks,
138 uint32_t bufferIndex)
139{
140 const gl::InterfaceBlock &uniformBlock = uniformBlocks[bufferIndex];
141
142 if (!uniformBlock.isArray)
143 {
144 return 1;
145 }
146
147 ASSERT(uniformBlock.arrayElement == 0);
148
149 // Search consecutively until all array indices of this block are visited.
150 uint32_t arraySize;
151 for (arraySize = 1; bufferIndex + arraySize < uniformBlocks.size(); ++arraySize)
152 {
153 const gl::InterfaceBlock &nextBlock = uniformBlocks[bufferIndex + arraySize];
154
155 if (nextBlock.arrayElement != arraySize)
156 {
157 break;
158 }
159
160 // It's unexpected for an array to start at a non-zero array size, so we can always rely on
161 // the sequential `arrayElement`s to belong to the same block.
162 ASSERT(nextBlock.name == uniformBlock.name);
163 ASSERT(nextBlock.isArray);
164 }
165
166 return arraySize;
167}
Shahbaz Youssefi9d519ab2019-05-09 23:09:46 -0400168
169class Std140BlockLayoutEncoderFactory : public gl::CustomBlockLayoutEncoderFactory
170{
171 public:
172 sh::BlockLayoutEncoder *makeEncoder() override { return new sh::Std140BlockEncoder(); }
173};
Jamie Madill76e471e2017-10-21 09:56:01 -0400174} // anonymous namespace
175
Jamie Madill242c4fe2018-07-12 15:56:56 -0400176// ProgramVk::ShaderInfo implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500177ProgramVk::ShaderInfo::ShaderInfo() {}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400178
179ProgramVk::ShaderInfo::~ShaderInfo() = default;
180
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500181angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk,
182 const std::string &vertexSource,
183 const std::string &fragmentSource,
184 bool enableLineRasterEmulation)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400185{
Jamie Madilldbc605c2019-01-04 16:39:14 -0500186 ASSERT(!valid());
Jamie Madill242c4fe2018-07-12 15:56:56 -0400187
Jamie Madilldbc605c2019-01-04 16:39:14 -0500188 std::vector<uint32_t> vertexCode;
189 std::vector<uint32_t> fragmentCode;
190 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
191 enableLineRasterEmulation, vertexSource, fragmentSource,
192 &vertexCode, &fragmentCode));
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500193
Jamie Madilldbc605c2019-01-04 16:39:14 -0500194 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(),
195 vertexCode.data(), vertexCode.size() * sizeof(uint32_t)));
196 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(),
197 fragmentCode.data(), fragmentCode.size() * sizeof(uint32_t)));
198
199 mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]);
200 mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400201
Jamie Madill7c985f52018-11-29 18:16:17 -0500202 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400203}
204
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500205void ProgramVk::ShaderInfo::release(RendererVk *renderer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400206{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500207 mProgramHelper.release(renderer);
208
209 for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders)
210 {
211 shader.get().destroy(renderer->getDevice());
212 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400213}
214
Jamie Madill242c4fe2018-07-12 15:56:56 -0400215// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400216ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400217 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500218 kUniformBlockDynamicBufferMinSize,
219 true)
Jamie Madillb980c562018-11-27 11:34:27 -0500220{}
Jamie Madill76e471e2017-10-21 09:56:01 -0400221
Jamie Madill242c4fe2018-07-12 15:56:56 -0400222ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500223
Courtney Goeltzenleuchtereaf2d922019-04-18 16:31:25 -0600224ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{} {}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400225
Jamie Madill242c4fe2018-07-12 15:56:56 -0400226ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400227
Jamie Madillf4a789f2018-10-18 16:56:20 -0400228void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500229{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400230 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500231 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400232}
Jamie Madill5deea722017-02-16 10:44:46 -0500233
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500234void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400235{
Jamie Madill9b168d02018-06-13 13:25:32 -0400236 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
237 {
238 descriptorSetLayout.reset();
239 }
240 mPipelineLayout.reset();
241
Jamie Madill76e471e2017-10-21 09:56:01 -0400242 for (auto &uniformBlock : mDefaultUniformBlocks)
243 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400244 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400245 }
246
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500247 mDefaultShaderInfo.release(renderer);
248 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400249
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500250 mEmptyUniformBlockStorage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400251
Jamie Madill5547b382017-10-23 18:16:01 -0400252 mDescriptorSets.clear();
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400253 mEmptyDescriptorSets.fill(VK_NULL_HANDLE);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500254
Shahbaz Youssefid8381782019-03-04 11:07:47 -0500255 for (vk::RefCountedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400256 {
257 binding.reset();
258 }
Jamie Madill5deea722017-02-16 10:44:46 -0500259}
260
Geoff Lange332e622019-02-14 12:53:04 -0500261std::unique_ptr<rx::LinkEvent> ProgramVk::load(const gl::Context *context,
262 gl::BinaryInputStream *stream,
263 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400264{
265 UNIMPLEMENTED();
Geoff Lange332e622019-02-14 12:53:04 -0500266 return std::make_unique<LinkEventDone>(angle::Result::Stop);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400267}
268
Jamie Madill27a60632017-06-30 15:12:01 -0400269void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400270{
271 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400272}
273
274void ProgramVk::setBinaryRetrievableHint(bool retrievable)
275{
276 UNIMPLEMENTED();
277}
278
Yunchao He61afff12017-03-14 15:34:03 +0800279void ProgramVk::setSeparable(bool separable)
280{
281 UNIMPLEMENTED();
282}
283
Jamie Madill785e8a02018-10-04 17:42:00 -0400284std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800285 const gl::ProgramLinkedResources &resources,
286 gl::InfoLog &infoLog)
287{
288 // TODO(jie.a.chen@intel.com): Parallelize linking.
289 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400290 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800291}
292
Jamie Madill785e8a02018-10-04 17:42:00 -0400293angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
294 const gl::ProgramLinkedResources &resources,
295 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400296{
Jamie Madill06ca6342018-07-12 15:56:53 -0400297 ContextVk *contextVk = vk::GetImpl(glContext);
298 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400299
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500300 reset(renderer);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500301
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400302 // Link resources before calling GetShaderSource to make sure they are ready for the set/binding
303 // assignment done in that function.
304 linkResources(resources);
305
jchen103fd614d2018-08-13 12:21:58 +0800306 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500307
Jamie Madill76e471e2017-10-21 09:56:01 -0400308 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500309
Jamie Madill9b168d02018-06-13 13:25:32 -0400310 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
311 // don't already exist in the cache.
312 vk::DescriptorSetLayoutDesc uniformsSetDesc;
313 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
314 1);
315 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
316 1);
317
318 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400319 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400320
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400321 vk::DescriptorSetLayoutDesc uniformBlocksSetDesc;
322
323 const std::vector<gl::InterfaceBlock> &uniformBlocks = mState.getUniformBlocks();
324 for (uint32_t bufferIndex = 0; bufferIndex < uniformBlocks.size();)
325 {
326 const uint32_t arraySize = GetUniformBlockArraySize(uniformBlocks, bufferIndex);
327
328 uniformBlocksSetDesc.update(bufferIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, arraySize);
329
330 bufferIndex += arraySize;
331 }
332
333 ANGLE_TRY(renderer->getDescriptorSetLayout(
334 contextVk, uniformBlocksSetDesc, &mDescriptorSetLayouts[kUniformBlockDescriptorSetIndex]));
335
Jamie Madill9b168d02018-06-13 13:25:32 -0400336 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400337
338 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
339 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400340 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400341 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
342
343 // The front-end always binds array sampler units sequentially.
344 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
345 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400346 }
347
Jamie Madill21061022018-07-12 23:56:30 -0400348 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400349 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
350
Jamie Madillb01b4802018-07-10 12:43:57 -0400351 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
352 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
353 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400354 contextVk, driverUniformsSetDesc,
355 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400356
Jamie Madill9b168d02018-06-13 13:25:32 -0400357 vk::PipelineLayoutDesc pipelineLayoutDesc;
358 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400359 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformBlockDescriptorSetIndex,
360 uniformBlocksSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400361 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400362 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
363 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400364
Jamie Madill21061022018-07-12 23:56:30 -0400365 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
366 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400367
Jamie Madill7c985f52018-11-29 18:16:17 -0500368 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400369}
370
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400371void ProgramVk::linkResources(const gl::ProgramLinkedResources &resources)
372{
Shahbaz Youssefi9d519ab2019-05-09 23:09:46 -0400373 Std140BlockLayoutEncoderFactory std140EncoderFactory;
374 gl::ProgramLinkedResourcesLinker linker(&std140EncoderFactory);
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400375
376 linker.linkResources(mState, resources);
377}
378
Jamie Madill242c4fe2018-07-12 15:56:56 -0400379angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400380{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400381 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500382 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400383
384 // Process vertex and fragment uniforms into std140 packing.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500385 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
386 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400387 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400388
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500389 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400390 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500391 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800392 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400393 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
394 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400395 }
396
397 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400398 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400399 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400400 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500401 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400402
Jamie Madill76e471e2017-10-21 09:56:01 -0400403 if (location.used() && !location.ignored)
404 {
Jamie Madillde03e002017-10-21 14:04:20 -0400405 const auto &uniform = uniforms[location.index];
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400406 if (uniform.isInDefaultBlock() && !uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400407 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400408 std::string uniformName = uniform.name;
409 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400410 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400411 // Gets the uniform name without the [0] at the end.
412 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400413 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400414
Geoff Langbeb0c942018-09-27 11:22:23 -0400415 bool found = false;
416
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500417 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400418 {
419 auto it = layoutMap[shaderType].find(uniformName);
420 if (it != layoutMap[shaderType].end())
421 {
422 found = true;
423 layoutInfo[shaderType] = it->second;
424 }
425 }
426
427 ASSERT(found);
428 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400429 }
430
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500431 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400432 {
Jamie Madill33318de2018-05-01 11:22:54 -0400433 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400434 }
435 }
436
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500437 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400438 {
Jamie Madill33318de2018-05-01 11:22:54 -0400439 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400440 {
Jamie Madill33318de2018-05-01 11:22:54 -0400441 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
442 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400443 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400444 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400445 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400446 size_t minAlignment = static_cast<size_t>(
447 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
448
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400449 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400450
451 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400452 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400453 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400454 }
455 }
456
Jamie Madill2b858c22018-09-03 13:58:14 -0400457 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400458 {
459 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400460 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400461 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400462 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400463 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400464 uniformBufferInfo.flags = 0;
465 uniformBufferInfo.size = 1;
466 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
467 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
468 uniformBufferInfo.queueFamilyIndexCount = 0;
469 uniformBufferInfo.pQueueFamilyIndices = nullptr;
470
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400471 constexpr VkMemoryPropertyFlags kMemoryType = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
472 ANGLE_TRY(mEmptyUniformBlockStorage.init(contextVk, uniformBufferInfo, kMemoryType));
Jamie Madill76e471e2017-10-21 09:56:01 -0400473 }
Jamie Madill5547b382017-10-23 18:16:01 -0400474 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400475
Jamie Madill7c985f52018-11-29 18:16:17 -0500476 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400477}
478
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400479GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
480{
Luc Ferronfba1f612018-06-04 14:37:17 -0400481 // No-op. The spec is very vague about the behavior of validation.
482 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400483}
484
Jamie Madill76e471e2017-10-21 09:56:01 -0400485template <typename T>
486void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
487{
488 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
489 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
490
Luc Ferron7cec3352018-03-13 13:29:34 -0400491 if (linkedUniform.isSampler())
492 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400493 // We could potentially cache some indexing here. For now this is a no-op since the mapping
494 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400495 return;
496 }
497
Luc Ferron24a31372018-04-04 11:49:14 -0400498 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400499 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500500 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400501 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400502 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400503 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400504
Luc Ferron24a31372018-04-04 11:49:14 -0400505 // Assume an offset of -1 means the block is unused.
506 if (layoutInfo.offset == -1)
507 {
508 continue;
509 }
510
511 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400512 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
513 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400514 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400515 }
Luc Ferron24a31372018-04-04 11:49:14 -0400516 }
517 else
518 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500519 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400520 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400521 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400522 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
523
524 // Assume an offset of -1 means the block is unused.
525 if (layoutInfo.offset == -1)
526 {
527 continue;
528 }
529
530 const GLint componentCount = linkedUniform.typeInfo->componentCount;
531
Luc Ferron62059a52018-03-29 07:01:35 -0400532 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
533
Luc Ferrond91c3792018-04-06 09:36:36 -0400534 GLint initialArrayOffset =
535 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400536 for (GLint i = 0; i < count; i++)
537 {
538 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
539 GLint *dest =
540 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
541 const T *source = v + i * componentCount;
542
543 for (int c = 0; c < componentCount; c++)
544 {
545 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
546 }
547 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400548
549 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400550 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400551 }
552}
553
Luc Ferron7cec3352018-03-13 13:29:34 -0400554template <typename T>
555void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
556{
557 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
558 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
559
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400560 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400561
Olli Etuaho107c7242018-03-20 15:45:35 +0200562 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800563 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400564
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500565 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madillb980c562018-11-27 11:34:27 -0500566 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400567
568 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
569 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400570
571 if (gl::IsMatrixType(linkedUniform.type))
572 {
573 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400574 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400575 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
576 }
577 else
578 {
579 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
580 v, layoutInfo, &uniformBlock.uniformData);
581 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400582}
583
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400584void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
585{
Jamie Madill76e471e2017-10-21 09:56:01 -0400586 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400587}
588
589void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
590{
Jamie Madill76e471e2017-10-21 09:56:01 -0400591 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400592}
593
594void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
595{
Jamie Madill76e471e2017-10-21 09:56:01 -0400596 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400597}
598
599void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
600{
Jamie Madill76e471e2017-10-21 09:56:01 -0400601 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400602}
603
604void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
605{
Luc Ferron7cec3352018-03-13 13:29:34 -0400606 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400607}
608
609void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
610{
Luc Ferron489243f2018-03-28 16:55:28 -0400611 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400612}
613
614void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
615{
Luc Ferron489243f2018-03-28 16:55:28 -0400616 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400617}
618
619void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
620{
Luc Ferron489243f2018-03-28 16:55:28 -0400621 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400622}
623
624void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
625{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400626 setUniformImpl(location, count, v, GL_UNSIGNED_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400627}
628
629void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
630{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400631 setUniformImpl(location, count, v, GL_UNSIGNED_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400632}
633
634void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
635{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400636 setUniformImpl(location, count, v, GL_UNSIGNED_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400637}
638
639void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
640{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400641 setUniformImpl(location, count, v, GL_UNSIGNED_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400642}
643
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400644template <int cols, int rows>
645void ProgramVk::setUniformMatrixfv(GLint location,
646 GLsizei count,
647 GLboolean transpose,
648 const GLfloat *value)
649{
650 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
651 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
652
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500653 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400654 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400655 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400656 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
657
658 // Assume an offset of -1 means the block is unused.
659 if (layoutInfo.offset == -1)
660 {
661 continue;
662 }
663
Shahbaz Youssefib783fbc2019-05-10 23:16:17 -0400664 bool updated = SetFloatUniformMatrixGLSL<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400665 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
666 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400667
668 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
669 // setter did not update any data. We still want the uniform to be included when we'll
670 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400671 if (updated)
672 {
673 mDefaultUniformBlocksDirty.set(shaderType);
674 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400675 }
676}
677
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400678void ProgramVk::setUniformMatrix2fv(GLint location,
679 GLsizei count,
680 GLboolean transpose,
681 const GLfloat *value)
682{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400683 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400684}
685
686void ProgramVk::setUniformMatrix3fv(GLint location,
687 GLsizei count,
688 GLboolean transpose,
689 const GLfloat *value)
690{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400691 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400692}
693
694void ProgramVk::setUniformMatrix4fv(GLint location,
695 GLsizei count,
696 GLboolean transpose,
697 const GLfloat *value)
698{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400699 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400700}
701
702void ProgramVk::setUniformMatrix2x3fv(GLint location,
703 GLsizei count,
704 GLboolean transpose,
705 const GLfloat *value)
706{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400707 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400708}
709
710void ProgramVk::setUniformMatrix3x2fv(GLint location,
711 GLsizei count,
712 GLboolean transpose,
713 const GLfloat *value)
714{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400715 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400716}
717
718void ProgramVk::setUniformMatrix2x4fv(GLint location,
719 GLsizei count,
720 GLboolean transpose,
721 const GLfloat *value)
722{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400723 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400724}
725
726void ProgramVk::setUniformMatrix4x2fv(GLint location,
727 GLsizei count,
728 GLboolean transpose,
729 const GLfloat *value)
730{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400731 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400732}
733
734void ProgramVk::setUniformMatrix3x4fv(GLint location,
735 GLsizei count,
736 GLboolean transpose,
737 const GLfloat *value)
738{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400739 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400740}
741
742void ProgramVk::setUniformMatrix4x3fv(GLint location,
743 GLsizei count,
744 GLboolean transpose,
745 const GLfloat *value)
746{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400747 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400748}
749
Sami Väisänen46eaa942016-06-29 10:26:37 +0300750void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
751 GLenum genMode,
752 GLint components,
753 const GLfloat *coeffs)
754{
755 UNIMPLEMENTED();
756}
757
Jamie Madill21061022018-07-12 23:56:30 -0400758angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400759{
Jamie Madill76e471e2017-10-21 09:56:01 -0400760 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400761 vk::DynamicDescriptorPool *dynamicDescriptorPool =
762 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400763
Luc Ferron6ea1b412018-03-21 16:13:01 -0400764 uint32_t potentialNewCount = descriptorSetIndex + 1;
765 if (potentialNewCount > mDescriptorSets.size())
766 {
767 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
768 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400769
Jamie Madillc7918ce2018-06-13 13:25:31 -0400770 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400771 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400772 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400773 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400774 &mDescriptorSets[descriptorSetIndex]));
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400775 mEmptyDescriptorSets[descriptorSetIndex] = VK_NULL_HANDLE;
776
Jamie Madill7c985f52018-11-29 18:16:17 -0500777 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400778}
779
Jamie Madill54164b02017-08-28 15:17:37 -0400780void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
781{
Luc Ferron7cec3352018-03-13 13:29:34 -0400782 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400783}
784
785void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
786{
Luc Ferron7cec3352018-03-13 13:29:34 -0400787 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400788}
789
790void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
791{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400792 getUniformImpl(location, params, GL_UNSIGNED_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400793}
794
Jamie Madill21061022018-07-12 23:56:30 -0400795angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400796{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400797 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400798
Jamie Madill76e471e2017-10-21 09:56:01 -0400799 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400800 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500801 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400802 {
Jamie Madill33318de2018-05-01 11:22:54 -0400803 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400804
Jamie Madill2b858c22018-09-03 13:58:14 -0400805 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400806 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400807 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400808 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400809 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400810 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400811 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400812
813 if (bufferModified)
814 {
815 anyNewBufferAllocated = true;
816 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400817 }
818 }
819
Luc Ferron7a06ac12018-03-15 10:17:04 -0400820 if (anyNewBufferAllocated)
821 {
822 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
823 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400824 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400825 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
826 }
827
Jamie Madill7c985f52018-11-29 18:16:17 -0500828 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400829}
830
Jamie Madill21061022018-07-12 23:56:30 -0400831angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400832{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500833 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
834 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400835
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400836 // Write default uniforms for each shader type.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500837 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400838 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400839 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
840 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
841 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400842
843 if (!uniformBlock.uniformData.empty())
844 {
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400845 const vk::BufferHelper *bufferHelper = uniformBlock.storage.getCurrentBuffer();
846 bufferInfo.buffer = bufferHelper->getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400847 }
848 else
849 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500850 bufferInfo.buffer = mEmptyUniformBlockStorage.getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400851 }
852
853 bufferInfo.offset = 0;
854 bufferInfo.range = VK_WHOLE_SIZE;
855
Jamie Madill76e471e2017-10-21 09:56:01 -0400856 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
857 writeInfo.pNext = nullptr;
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400858 writeInfo.dstSet = mDescriptorSets[kUniformsDescriptorSetIndex];
Jamie Madill33318de2018-05-01 11:22:54 -0400859 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400860 writeInfo.dstArrayElement = 0;
861 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400862 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400863 writeInfo.pImageInfo = nullptr;
864 writeInfo.pBufferInfo = &bufferInfo;
865 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400866 }
867
868 VkDevice device = contextVk->getDevice();
869
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400870 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
871 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
872 constexpr uint32_t kGLES2ShaderCount = kShaderTypeMax - kShaderTypeMin + 1;
873 vkUpdateDescriptorSets(device, kGLES2ShaderCount, writeDescriptorInfo.data(), 0, nullptr);
874
875 return angle::Result::Continue;
876}
877
878angle::Result ProgramVk::updateUniformBuffersDescriptorSet(ContextVk *contextVk,
879 vk::FramebufferHelper *framebuffer)
880{
881 ASSERT(hasUniformBuffers());
882 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformBlockDescriptorSetIndex));
883
884 VkDescriptorSet descriptorSet = mDescriptorSets[kUniformBlockDescriptorSetIndex];
885
886 gl::UniformBuffersArray<VkDescriptorBufferInfo> descriptorBufferInfo;
887 gl::UniformBuffersArray<VkWriteDescriptorSet> writeDescriptorInfo;
888 uint32_t writeCount = 0;
889 uint32_t currentBinding = 0;
890
891 // Write uniform buffers.
892 const gl::State &glState = contextVk->getState();
893 const std::vector<gl::InterfaceBlock> &uniformBlocks = mState.getUniformBlocks();
894 for (uint32_t bufferIndex = 0; bufferIndex < uniformBlocks.size(); ++bufferIndex)
895 {
896 if (glState.getIndexedUniformBuffer(uniformBlocks[bufferIndex].binding).get() == nullptr)
897 {
898 continue;
899 }
900
901 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
902 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[writeCount];
903
904 const gl::InterfaceBlock &uniformBlock = uniformBlocks[bufferIndex];
905 const gl::OffsetBindingPointer<gl::Buffer> &bufferBinding =
906 glState.getIndexedUniformBuffer(uniformBlock.binding);
907 gl::Buffer *buffer = bufferBinding.get();
908 ASSERT(buffer != nullptr);
909
910 // Make sure there's no possible under/overflow with binding size.
911 static_assert(sizeof(VkDeviceSize) >= sizeof(bufferBinding.getSize()),
912 "VkDeviceSize too small");
913 ASSERT(bufferBinding.getSize() >= 0);
914
915 BufferVk *bufferVk = vk::GetImpl(buffer);
916 GLintptr offset = bufferBinding.getOffset();
917 VkDeviceSize size = bufferBinding.getSize();
918 VkDeviceSize blockSize = uniformBlock.dataSize;
919 vk::BufferHelper &bufferHelper = bufferVk->getBuffer();
920
921 ANGLE_TRY(bufferVk->onRead(contextVk, framebuffer, VK_ACCESS_UNIFORM_READ_BIT));
922
923 // If size is 0, we can't always use VK_WHOLE_SIZE (or bufferHelper.getSize()), as the
924 // backing buffer may be larger than maxUniformBufferRange. In that case, we use the
925 // minimum of the backing buffer size (what's left after offset) and the uniform buffer
926 // size as defined by the shader.
927 size = std::min(size > 0 ? size : (bufferHelper.getSize() - offset), blockSize);
928
929 bufferInfo.buffer = bufferHelper.getBuffer().getHandle();
930 bufferInfo.offset = offset;
931 bufferInfo.range = size;
932
933 if (!uniformBlock.isArray || uniformBlock.arrayElement == 0)
934 {
935 // Array indices of the same buffer binding are placed sequentially in `uniformBlocks`.
936 // Thus, the uniform block binding is updated only when array index 0 is encountered.
937 currentBinding = bufferIndex;
938 }
939
940 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
941 writeInfo.pNext = nullptr;
942 writeInfo.dstSet = descriptorSet;
943 writeInfo.dstBinding = currentBinding;
944 writeInfo.dstArrayElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
945 writeInfo.descriptorCount = 1;
946 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
947 writeInfo.pImageInfo = nullptr;
948 writeInfo.pBufferInfo = &bufferInfo;
949 writeInfo.pTexelBufferView = nullptr;
950 ASSERT(writeInfo.pBufferInfo[0].buffer != VK_NULL_HANDLE);
951
952 ++writeCount;
953 }
954
955 VkDevice device = contextVk->getDevice();
956
957 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400958
Jamie Madill7c985f52018-11-29 18:16:17 -0500959 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400960}
961
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100962angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk,
963 vk::FramebufferHelper *framebuffer)
Jamie Madill5547b382017-10-23 18:16:01 -0400964{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400965 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400966 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400967
Jamie Madillc7918ce2018-06-13 13:25:31 -0400968 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400969
Jamie Madill84c662b2018-07-12 15:56:55 -0400970 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
971 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400972 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400973
Jamie Madill84c662b2018-07-12 15:56:55 -0400974 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400975
Jamie Madill4cc753e2018-06-13 13:25:33 -0400976 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
977 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400978 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400979 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
980
Jamie Madill5547b382017-10-23 18:16:01 -0400981 ASSERT(!samplerBinding.unreferenced);
982
Jamie Madill4cc753e2018-06-13 13:25:33 -0400983 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
984 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400985 {
Jamie Madilldbc605c2019-01-04 16:39:14 -0500986 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
987 TextureVk *textureVk = activeTextures[textureUnit];
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100988
989 // Ensure any writes to the textures are flushed before we read from them.
990 ANGLE_TRY(textureVk->ensureImageInitialized(contextVk));
991 vk::ImageHelper &image = textureVk->getImage();
992
993 // Ensure the image is in read-only layout
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500994 if (image.isLayoutChangeNecessary(vk::ImageLayout::FragmentShaderReadOnly))
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100995 {
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400996 vk::CommandBuffer *srcLayoutChange;
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100997 ANGLE_TRY(image.recordCommands(contextVk, &srcLayoutChange));
998
Courtney Goeltzenleuchtereaf2d922019-04-18 16:31:25 -0600999 VkImageAspectFlags aspectFlags = image.getAspectFlags();
1000 ASSERT(aspectFlags != 0);
1001 image.changeLayout(aspectFlags, vk::ImageLayout::FragmentShaderReadOnly,
1002 srcLayoutChange);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001003 }
1004
1005 image.addReadDependency(framebuffer);
Jamie Madill4cc753e2018-06-13 13:25:33 -04001006
1007 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
1008
1009 imageInfo.sampler = textureVk->getSampler().getHandle();
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001010 imageInfo.imageView = textureVk->getReadImageView().getHandle();
Jamie Madill4cc753e2018-06-13 13:25:33 -04001011 imageInfo.imageLayout = image.getCurrentLayout();
1012
1013 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
1014
1015 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1016 writeInfo.pNext = nullptr;
1017 writeInfo.dstSet = descriptorSet;
1018 writeInfo.dstBinding = textureIndex;
1019 writeInfo.dstArrayElement = arrayElement;
1020 writeInfo.descriptorCount = 1;
1021 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1022 writeInfo.pImageInfo = &imageInfo;
1023 writeInfo.pBufferInfo = nullptr;
1024 writeInfo.pTexelBufferView = nullptr;
1025
1026 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -04001027 }
Jamie Madill5547b382017-10-23 18:16:01 -04001028 }
1029
1030 VkDevice device = contextVk->getDevice();
1031
Jamie Madill4cc753e2018-06-13 13:25:33 -04001032 ASSERT(writeCount > 0);
1033 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -04001034
Jamie Madill7c985f52018-11-29 18:16:17 -05001035 return angle::Result::Continue;
Jamie Madill5547b382017-10-23 18:16:01 -04001036}
1037
Luc Ferron7a06ac12018-03-15 10:17:04 -04001038void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
1039{
1040 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
1041 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -04001042 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -04001043 }
1044}
Jamie Madill242c4fe2018-07-12 15:56:56 -04001045
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001046angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
1047 vk::CommandBuffer *commandBuffer)
Jamie Madill242c4fe2018-07-12 15:56:56 -04001048{
Jamie Madill242c4fe2018-07-12 15:56:56 -04001049 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -04001050
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001051 if (mDescriptorSets.empty())
Jamie Madill7c985f52018-11-29 18:16:17 -05001052 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -04001053
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001054 // Find the maximum non-null descriptor set. This is used in conjunction with a driver
1055 // workaround to bind empty descriptor sets only for gaps in between 0 and max and avoid
1056 // binding unnecessary empty descriptor sets for the sets beyond max.
1057 size_t descriptorSetRange = 0;
1058 for (size_t descriptorSetIndex = 0; descriptorSetIndex < mDescriptorSets.size();
1059 ++descriptorSetIndex)
Jamie Madill242c4fe2018-07-12 15:56:56 -04001060 {
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001061 if (mDescriptorSets[descriptorSetIndex] != VK_NULL_HANDLE)
1062 {
1063 descriptorSetRange = descriptorSetIndex + 1;
1064 }
1065 }
1066
1067 for (size_t descriptorSetIndex = 0; descriptorSetIndex < descriptorSetRange;
1068 ++descriptorSetIndex)
1069 {
1070 VkDescriptorSet descSet = mDescriptorSets[descriptorSetIndex];
1071 if (descSet == VK_NULL_HANDLE)
1072 {
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -04001073 if (!contextVk->getRenderer()->getFeatures().bindEmptyForUnusedDescriptorSets.enabled)
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001074 {
1075 continue;
1076 }
1077
1078 // Workaround a driver bug where missing (though unused) descriptor sets indices cause
1079 // later sets to misbehave.
1080 if (mEmptyDescriptorSets[descriptorSetIndex] == VK_NULL_HANDLE)
1081 {
1082 vk::DynamicDescriptorPool *dynamicDescriptorPool =
1083 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
1084 const vk::DescriptorSetLayout &descriptorSetLayout =
1085 mDescriptorSetLayouts[descriptorSetIndex].get();
1086
1087 ANGLE_TRY(dynamicDescriptorPool->allocateSets(
1088 contextVk, descriptorSetLayout.ptr(), 1,
1089 &mDescriptorPoolBindings[descriptorSetIndex],
1090 &mEmptyDescriptorSets[descriptorSetIndex]));
1091 }
1092 descSet = mEmptyDescriptorSets[descriptorSetIndex];
1093 }
1094
Courtney Goeltzenleuchtereaf2d922019-04-18 16:31:25 -06001095 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
1096 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001097 constexpr uint32_t kShaderTypeCount = kShaderTypeMax - kShaderTypeMin + 1;
1098
1099 // Default uniforms are encompassed in a block per shader stage, and they are assigned
1100 // through dynamic uniform buffers (requiring dynamic offsets). No other descriptor
1101 // requires a dynamic offset.
1102 const uint32_t uniformBlockOffsetCount =
1103 descriptorSetIndex == kUniformsDescriptorSetIndex ? kShaderTypeCount : 0;
1104
1105 commandBuffer->bindGraphicsDescriptorSets(mPipelineLayout.get(), descriptorSetIndex, 1,
1106 &descSet, uniformBlockOffsetCount,
1107 mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -04001108 }
1109
Jamie Madill7c985f52018-11-29 18:16:17 -05001110 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -04001111}
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001112} // namespace rx