blob: 2bc50509035dc74843e1181776d4a661c3ef8b09 [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}
Jamie Madill76e471e2017-10-21 09:56:01 -0400168} // anonymous namespace
169
Jamie Madill242c4fe2018-07-12 15:56:56 -0400170// ProgramVk::ShaderInfo implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500171ProgramVk::ShaderInfo::ShaderInfo() {}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400172
173ProgramVk::ShaderInfo::~ShaderInfo() = default;
174
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500175angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk,
176 const std::string &vertexSource,
177 const std::string &fragmentSource,
178 bool enableLineRasterEmulation)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400179{
Jamie Madilldbc605c2019-01-04 16:39:14 -0500180 ASSERT(!valid());
Jamie Madill242c4fe2018-07-12 15:56:56 -0400181
Jamie Madilldbc605c2019-01-04 16:39:14 -0500182 std::vector<uint32_t> vertexCode;
183 std::vector<uint32_t> fragmentCode;
184 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
185 enableLineRasterEmulation, vertexSource, fragmentSource,
186 &vertexCode, &fragmentCode));
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500187
Jamie Madilldbc605c2019-01-04 16:39:14 -0500188 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(),
189 vertexCode.data(), vertexCode.size() * sizeof(uint32_t)));
190 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(),
191 fragmentCode.data(), fragmentCode.size() * sizeof(uint32_t)));
192
193 mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]);
194 mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400195
Jamie Madill7c985f52018-11-29 18:16:17 -0500196 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400197}
198
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500199void ProgramVk::ShaderInfo::release(RendererVk *renderer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400200{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500201 mProgramHelper.release(renderer);
202
203 for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders)
204 {
205 shader.get().destroy(renderer->getDevice());
206 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400207}
208
Jamie Madill242c4fe2018-07-12 15:56:56 -0400209// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400210ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400211 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500212 kUniformBlockDynamicBufferMinSize,
213 true)
Jamie Madillb980c562018-11-27 11:34:27 -0500214{}
Jamie Madill76e471e2017-10-21 09:56:01 -0400215
Jamie Madill242c4fe2018-07-12 15:56:56 -0400216ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500217
Jamie Madill88fc6da2018-08-30 16:18:36 -0400218ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400219{
220}
221
Jamie Madill242c4fe2018-07-12 15:56:56 -0400222ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400223
Jamie Madillf4a789f2018-10-18 16:56:20 -0400224void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500225{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400226 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500227 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400228}
Jamie Madill5deea722017-02-16 10:44:46 -0500229
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500230void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400231{
Jamie Madill9b168d02018-06-13 13:25:32 -0400232 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
233 {
234 descriptorSetLayout.reset();
235 }
236 mPipelineLayout.reset();
237
Jamie Madill76e471e2017-10-21 09:56:01 -0400238 for (auto &uniformBlock : mDefaultUniformBlocks)
239 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400240 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400241 }
242
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500243 mDefaultShaderInfo.release(renderer);
244 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400245
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500246 mEmptyUniformBlockStorage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400247
Jamie Madill5547b382017-10-23 18:16:01 -0400248 mDescriptorSets.clear();
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400249 mEmptyDescriptorSets.fill(VK_NULL_HANDLE);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500250
Shahbaz Youssefid8381782019-03-04 11:07:47 -0500251 for (vk::RefCountedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400252 {
253 binding.reset();
254 }
Jamie Madill5deea722017-02-16 10:44:46 -0500255}
256
Geoff Lange332e622019-02-14 12:53:04 -0500257std::unique_ptr<rx::LinkEvent> ProgramVk::load(const gl::Context *context,
258 gl::BinaryInputStream *stream,
259 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400260{
261 UNIMPLEMENTED();
Geoff Lange332e622019-02-14 12:53:04 -0500262 return std::make_unique<LinkEventDone>(angle::Result::Stop);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400263}
264
Jamie Madill27a60632017-06-30 15:12:01 -0400265void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400266{
267 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400268}
269
270void ProgramVk::setBinaryRetrievableHint(bool retrievable)
271{
272 UNIMPLEMENTED();
273}
274
Yunchao He61afff12017-03-14 15:34:03 +0800275void ProgramVk::setSeparable(bool separable)
276{
277 UNIMPLEMENTED();
278}
279
Jamie Madill785e8a02018-10-04 17:42:00 -0400280std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800281 const gl::ProgramLinkedResources &resources,
282 gl::InfoLog &infoLog)
283{
284 // TODO(jie.a.chen@intel.com): Parallelize linking.
285 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400286 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800287}
288
Jamie Madill785e8a02018-10-04 17:42:00 -0400289angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
290 const gl::ProgramLinkedResources &resources,
291 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400292{
Jamie Madill06ca6342018-07-12 15:56:53 -0400293 ContextVk *contextVk = vk::GetImpl(glContext);
294 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400295
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500296 reset(renderer);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500297
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400298 // Link resources before calling GetShaderSource to make sure they are ready for the set/binding
299 // assignment done in that function.
300 linkResources(resources);
301
jchen103fd614d2018-08-13 12:21:58 +0800302 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500303
Jamie Madill76e471e2017-10-21 09:56:01 -0400304 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500305
Jamie Madill9b168d02018-06-13 13:25:32 -0400306 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
307 // don't already exist in the cache.
308 vk::DescriptorSetLayoutDesc uniformsSetDesc;
309 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
310 1);
311 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
312 1);
313
314 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400315 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400316
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400317 vk::DescriptorSetLayoutDesc uniformBlocksSetDesc;
318
319 const std::vector<gl::InterfaceBlock> &uniformBlocks = mState.getUniformBlocks();
320 for (uint32_t bufferIndex = 0; bufferIndex < uniformBlocks.size();)
321 {
322 const uint32_t arraySize = GetUniformBlockArraySize(uniformBlocks, bufferIndex);
323
324 uniformBlocksSetDesc.update(bufferIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, arraySize);
325
326 bufferIndex += arraySize;
327 }
328
329 ANGLE_TRY(renderer->getDescriptorSetLayout(
330 contextVk, uniformBlocksSetDesc, &mDescriptorSetLayouts[kUniformBlockDescriptorSetIndex]));
331
Jamie Madill9b168d02018-06-13 13:25:32 -0400332 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400333
334 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
335 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400336 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400337 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
338
339 // The front-end always binds array sampler units sequentially.
340 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
341 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400342 }
343
Jamie Madill21061022018-07-12 23:56:30 -0400344 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400345 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
346
Jamie Madillb01b4802018-07-10 12:43:57 -0400347 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
348 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
349 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400350 contextVk, driverUniformsSetDesc,
351 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400352
Jamie Madill9b168d02018-06-13 13:25:32 -0400353 vk::PipelineLayoutDesc pipelineLayoutDesc;
354 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400355 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformBlockDescriptorSetIndex,
356 uniformBlocksSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400357 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400358 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
359 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400360
Jamie Madill21061022018-07-12 23:56:30 -0400361 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
362 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400363
Jamie Madill7c985f52018-11-29 18:16:17 -0500364 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400365}
366
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400367void ProgramVk::linkResources(const gl::ProgramLinkedResources &resources)
368{
369 gl::ProgramLinkedResourcesLinker linker(nullptr);
370
371 linker.linkResources(mState, resources);
372}
373
Jamie Madill242c4fe2018-07-12 15:56:56 -0400374angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400375{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400376 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500377 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400378
379 // Process vertex and fragment uniforms into std140 packing.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500380 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
381 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400382 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400383
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500384 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400385 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500386 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800387 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400388 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
389 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400390 }
391
392 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400393 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400394 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400395 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500396 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400397
Jamie Madill76e471e2017-10-21 09:56:01 -0400398 if (location.used() && !location.ignored)
399 {
Jamie Madillde03e002017-10-21 14:04:20 -0400400 const auto &uniform = uniforms[location.index];
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400401 if (uniform.isInDefaultBlock() && !uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400402 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400403 std::string uniformName = uniform.name;
404 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400405 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400406 // Gets the uniform name without the [0] at the end.
407 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400408 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400409
Geoff Langbeb0c942018-09-27 11:22:23 -0400410 bool found = false;
411
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500412 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400413 {
414 auto it = layoutMap[shaderType].find(uniformName);
415 if (it != layoutMap[shaderType].end())
416 {
417 found = true;
418 layoutInfo[shaderType] = it->second;
419 }
420 }
421
422 ASSERT(found);
423 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400424 }
425
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500426 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400427 {
Jamie Madill33318de2018-05-01 11:22:54 -0400428 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400429 }
430 }
431
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500432 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400433 {
Jamie Madill33318de2018-05-01 11:22:54 -0400434 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400435 {
Jamie Madill33318de2018-05-01 11:22:54 -0400436 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
437 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400438 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400439 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400440 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400441 size_t minAlignment = static_cast<size_t>(
442 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
443
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400444 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400445
446 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400447 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400448 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400449 }
450 }
451
Jamie Madill2b858c22018-09-03 13:58:14 -0400452 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400453 {
454 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400455 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400456 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400457 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400458 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400459 uniformBufferInfo.flags = 0;
460 uniformBufferInfo.size = 1;
461 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
462 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
463 uniformBufferInfo.queueFamilyIndexCount = 0;
464 uniformBufferInfo.pQueueFamilyIndices = nullptr;
465
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400466 constexpr VkMemoryPropertyFlags kMemoryType = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
467 ANGLE_TRY(mEmptyUniformBlockStorage.init(contextVk, uniformBufferInfo, kMemoryType));
Jamie Madill76e471e2017-10-21 09:56:01 -0400468 }
Jamie Madill5547b382017-10-23 18:16:01 -0400469 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400470
Jamie Madill7c985f52018-11-29 18:16:17 -0500471 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400472}
473
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400474GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
475{
Luc Ferronfba1f612018-06-04 14:37:17 -0400476 // No-op. The spec is very vague about the behavior of validation.
477 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400478}
479
Jamie Madill76e471e2017-10-21 09:56:01 -0400480template <typename T>
481void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
482{
483 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
484 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
485
Luc Ferron7cec3352018-03-13 13:29:34 -0400486 if (linkedUniform.isSampler())
487 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400488 // We could potentially cache some indexing here. For now this is a no-op since the mapping
489 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400490 return;
491 }
492
Luc Ferron24a31372018-04-04 11:49:14 -0400493 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400494 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500495 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400496 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400497 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400498 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400499
Luc Ferron24a31372018-04-04 11:49:14 -0400500 // Assume an offset of -1 means the block is unused.
501 if (layoutInfo.offset == -1)
502 {
503 continue;
504 }
505
506 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400507 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
508 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400509 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400510 }
Luc Ferron24a31372018-04-04 11:49:14 -0400511 }
512 else
513 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500514 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400515 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400516 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400517 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
518
519 // Assume an offset of -1 means the block is unused.
520 if (layoutInfo.offset == -1)
521 {
522 continue;
523 }
524
525 const GLint componentCount = linkedUniform.typeInfo->componentCount;
526
Luc Ferron62059a52018-03-29 07:01:35 -0400527 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
528
Luc Ferrond91c3792018-04-06 09:36:36 -0400529 GLint initialArrayOffset =
530 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400531 for (GLint i = 0; i < count; i++)
532 {
533 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
534 GLint *dest =
535 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
536 const T *source = v + i * componentCount;
537
538 for (int c = 0; c < componentCount; c++)
539 {
540 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
541 }
542 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400543
544 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400545 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400546 }
547}
548
Luc Ferron7cec3352018-03-13 13:29:34 -0400549template <typename T>
550void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
551{
552 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
553 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
554
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400555 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400556
Olli Etuaho107c7242018-03-20 15:45:35 +0200557 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800558 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400559
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500560 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madillb980c562018-11-27 11:34:27 -0500561 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400562
563 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
564 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400565
566 if (gl::IsMatrixType(linkedUniform.type))
567 {
568 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400569 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400570 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
571 }
572 else
573 {
574 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
575 v, layoutInfo, &uniformBlock.uniformData);
576 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400577}
578
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400579void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
580{
Jamie Madill76e471e2017-10-21 09:56:01 -0400581 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400582}
583
584void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
585{
Jamie Madill76e471e2017-10-21 09:56:01 -0400586 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400587}
588
589void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
590{
Jamie Madill76e471e2017-10-21 09:56:01 -0400591 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400592}
593
594void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
595{
Jamie Madill76e471e2017-10-21 09:56:01 -0400596 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400597}
598
599void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
600{
Luc Ferron7cec3352018-03-13 13:29:34 -0400601 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400602}
603
604void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
605{
Luc Ferron489243f2018-03-28 16:55:28 -0400606 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400607}
608
609void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
610{
Luc Ferron489243f2018-03-28 16:55:28 -0400611 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400612}
613
614void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
615{
Luc Ferron489243f2018-03-28 16:55:28 -0400616 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400617}
618
619void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
620{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400621 setUniformImpl(location, count, v, GL_UNSIGNED_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400622}
623
624void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
625{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400626 setUniformImpl(location, count, v, GL_UNSIGNED_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400627}
628
629void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
630{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400631 setUniformImpl(location, count, v, GL_UNSIGNED_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400632}
633
634void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
635{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400636 setUniformImpl(location, count, v, GL_UNSIGNED_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400637}
638
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400639template <int cols, int rows>
640void ProgramVk::setUniformMatrixfv(GLint location,
641 GLsizei count,
642 GLboolean transpose,
643 const GLfloat *value)
644{
645 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
646 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
647
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500648 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400649 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400650 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400651 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
652
653 // Assume an offset of -1 means the block is unused.
654 if (layoutInfo.offset == -1)
655 {
656 continue;
657 }
658
Luc Ferronc8fbff32018-06-04 10:30:48 -0400659 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400660 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
661 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400662
663 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
664 // setter did not update any data. We still want the uniform to be included when we'll
665 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400666 if (updated)
667 {
668 mDefaultUniformBlocksDirty.set(shaderType);
669 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400670 }
671}
672
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400673void ProgramVk::setUniformMatrix2fv(GLint location,
674 GLsizei count,
675 GLboolean transpose,
676 const GLfloat *value)
677{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400678 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400679}
680
681void ProgramVk::setUniformMatrix3fv(GLint location,
682 GLsizei count,
683 GLboolean transpose,
684 const GLfloat *value)
685{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400686 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400687}
688
689void ProgramVk::setUniformMatrix4fv(GLint location,
690 GLsizei count,
691 GLboolean transpose,
692 const GLfloat *value)
693{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400694 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400695}
696
697void ProgramVk::setUniformMatrix2x3fv(GLint location,
698 GLsizei count,
699 GLboolean transpose,
700 const GLfloat *value)
701{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400702 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400703}
704
705void ProgramVk::setUniformMatrix3x2fv(GLint location,
706 GLsizei count,
707 GLboolean transpose,
708 const GLfloat *value)
709{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400710 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400711}
712
713void ProgramVk::setUniformMatrix2x4fv(GLint location,
714 GLsizei count,
715 GLboolean transpose,
716 const GLfloat *value)
717{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400718 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400719}
720
721void ProgramVk::setUniformMatrix4x2fv(GLint location,
722 GLsizei count,
723 GLboolean transpose,
724 const GLfloat *value)
725{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400726 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400727}
728
729void ProgramVk::setUniformMatrix3x4fv(GLint location,
730 GLsizei count,
731 GLboolean transpose,
732 const GLfloat *value)
733{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400734 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400735}
736
737void ProgramVk::setUniformMatrix4x3fv(GLint location,
738 GLsizei count,
739 GLboolean transpose,
740 const GLfloat *value)
741{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400742 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400743}
744
Sami Väisänen46eaa942016-06-29 10:26:37 +0300745void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
746 GLenum genMode,
747 GLint components,
748 const GLfloat *coeffs)
749{
750 UNIMPLEMENTED();
751}
752
Jamie Madill21061022018-07-12 23:56:30 -0400753angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400754{
Jamie Madill76e471e2017-10-21 09:56:01 -0400755 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400756 vk::DynamicDescriptorPool *dynamicDescriptorPool =
757 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400758
Luc Ferron6ea1b412018-03-21 16:13:01 -0400759 uint32_t potentialNewCount = descriptorSetIndex + 1;
760 if (potentialNewCount > mDescriptorSets.size())
761 {
762 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
763 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400764
Jamie Madillc7918ce2018-06-13 13:25:31 -0400765 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400766 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400767 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400768 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400769 &mDescriptorSets[descriptorSetIndex]));
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400770 mEmptyDescriptorSets[descriptorSetIndex] = VK_NULL_HANDLE;
771
Jamie Madill7c985f52018-11-29 18:16:17 -0500772 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400773}
774
Jamie Madill54164b02017-08-28 15:17:37 -0400775void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
776{
Luc Ferron7cec3352018-03-13 13:29:34 -0400777 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400778}
779
780void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
781{
Luc Ferron7cec3352018-03-13 13:29:34 -0400782 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400783}
784
785void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
786{
Shahbaz Youssefifdbece22019-05-09 18:06:34 -0400787 getUniformImpl(location, params, GL_UNSIGNED_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400788}
789
Jamie Madill21061022018-07-12 23:56:30 -0400790angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400791{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400792 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400793
Jamie Madill76e471e2017-10-21 09:56:01 -0400794 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400795 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500796 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400797 {
Jamie Madill33318de2018-05-01 11:22:54 -0400798 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400799
Jamie Madill2b858c22018-09-03 13:58:14 -0400800 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400801 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400802 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400803 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400804 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400805 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400806 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400807
808 if (bufferModified)
809 {
810 anyNewBufferAllocated = true;
811 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400812 }
813 }
814
Luc Ferron7a06ac12018-03-15 10:17:04 -0400815 if (anyNewBufferAllocated)
816 {
817 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
818 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400819 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400820 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
821 }
822
Jamie Madill7c985f52018-11-29 18:16:17 -0500823 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400824}
825
Jamie Madill21061022018-07-12 23:56:30 -0400826angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400827{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500828 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
829 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400830
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400831 // Write default uniforms for each shader type.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500832 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400833 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400834 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
835 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
836 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400837
838 if (!uniformBlock.uniformData.empty())
839 {
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400840 const vk::BufferHelper *bufferHelper = uniformBlock.storage.getCurrentBuffer();
841 bufferInfo.buffer = bufferHelper->getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400842 }
843 else
844 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500845 bufferInfo.buffer = mEmptyUniformBlockStorage.getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400846 }
847
848 bufferInfo.offset = 0;
849 bufferInfo.range = VK_WHOLE_SIZE;
850
Jamie Madill76e471e2017-10-21 09:56:01 -0400851 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
852 writeInfo.pNext = nullptr;
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400853 writeInfo.dstSet = mDescriptorSets[kUniformsDescriptorSetIndex];
Jamie Madill33318de2018-05-01 11:22:54 -0400854 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400855 writeInfo.dstArrayElement = 0;
856 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400857 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400858 writeInfo.pImageInfo = nullptr;
859 writeInfo.pBufferInfo = &bufferInfo;
860 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400861 }
862
863 VkDevice device = contextVk->getDevice();
864
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -0400865 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
866 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
867 constexpr uint32_t kGLES2ShaderCount = kShaderTypeMax - kShaderTypeMin + 1;
868 vkUpdateDescriptorSets(device, kGLES2ShaderCount, writeDescriptorInfo.data(), 0, nullptr);
869
870 return angle::Result::Continue;
871}
872
873angle::Result ProgramVk::updateUniformBuffersDescriptorSet(ContextVk *contextVk,
874 vk::FramebufferHelper *framebuffer)
875{
876 ASSERT(hasUniformBuffers());
877 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformBlockDescriptorSetIndex));
878
879 VkDescriptorSet descriptorSet = mDescriptorSets[kUniformBlockDescriptorSetIndex];
880
881 gl::UniformBuffersArray<VkDescriptorBufferInfo> descriptorBufferInfo;
882 gl::UniformBuffersArray<VkWriteDescriptorSet> writeDescriptorInfo;
883 uint32_t writeCount = 0;
884 uint32_t currentBinding = 0;
885
886 // Write uniform buffers.
887 const gl::State &glState = contextVk->getState();
888 const std::vector<gl::InterfaceBlock> &uniformBlocks = mState.getUniformBlocks();
889 for (uint32_t bufferIndex = 0; bufferIndex < uniformBlocks.size(); ++bufferIndex)
890 {
891 if (glState.getIndexedUniformBuffer(uniformBlocks[bufferIndex].binding).get() == nullptr)
892 {
893 continue;
894 }
895
896 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
897 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[writeCount];
898
899 const gl::InterfaceBlock &uniformBlock = uniformBlocks[bufferIndex];
900 const gl::OffsetBindingPointer<gl::Buffer> &bufferBinding =
901 glState.getIndexedUniformBuffer(uniformBlock.binding);
902 gl::Buffer *buffer = bufferBinding.get();
903 ASSERT(buffer != nullptr);
904
905 // Make sure there's no possible under/overflow with binding size.
906 static_assert(sizeof(VkDeviceSize) >= sizeof(bufferBinding.getSize()),
907 "VkDeviceSize too small");
908 ASSERT(bufferBinding.getSize() >= 0);
909
910 BufferVk *bufferVk = vk::GetImpl(buffer);
911 GLintptr offset = bufferBinding.getOffset();
912 VkDeviceSize size = bufferBinding.getSize();
913 VkDeviceSize blockSize = uniformBlock.dataSize;
914 vk::BufferHelper &bufferHelper = bufferVk->getBuffer();
915
916 ANGLE_TRY(bufferVk->onRead(contextVk, framebuffer, VK_ACCESS_UNIFORM_READ_BIT));
917
918 // If size is 0, we can't always use VK_WHOLE_SIZE (or bufferHelper.getSize()), as the
919 // backing buffer may be larger than maxUniformBufferRange. In that case, we use the
920 // minimum of the backing buffer size (what's left after offset) and the uniform buffer
921 // size as defined by the shader.
922 size = std::min(size > 0 ? size : (bufferHelper.getSize() - offset), blockSize);
923
924 bufferInfo.buffer = bufferHelper.getBuffer().getHandle();
925 bufferInfo.offset = offset;
926 bufferInfo.range = size;
927
928 if (!uniformBlock.isArray || uniformBlock.arrayElement == 0)
929 {
930 // Array indices of the same buffer binding are placed sequentially in `uniformBlocks`.
931 // Thus, the uniform block binding is updated only when array index 0 is encountered.
932 currentBinding = bufferIndex;
933 }
934
935 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
936 writeInfo.pNext = nullptr;
937 writeInfo.dstSet = descriptorSet;
938 writeInfo.dstBinding = currentBinding;
939 writeInfo.dstArrayElement = uniformBlock.isArray ? uniformBlock.arrayElement : 0;
940 writeInfo.descriptorCount = 1;
941 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
942 writeInfo.pImageInfo = nullptr;
943 writeInfo.pBufferInfo = &bufferInfo;
944 writeInfo.pTexelBufferView = nullptr;
945 ASSERT(writeInfo.pBufferInfo[0].buffer != VK_NULL_HANDLE);
946
947 ++writeCount;
948 }
949
950 VkDevice device = contextVk->getDevice();
951
952 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400953
Jamie Madill7c985f52018-11-29 18:16:17 -0500954 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400955}
956
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100957angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk,
958 vk::FramebufferHelper *framebuffer)
Jamie Madill5547b382017-10-23 18:16:01 -0400959{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400960 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400961 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400962
Jamie Madillc7918ce2018-06-13 13:25:31 -0400963 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400964
Jamie Madill84c662b2018-07-12 15:56:55 -0400965 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
966 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400967 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400968
Jamie Madill84c662b2018-07-12 15:56:55 -0400969 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400970
Jamie Madill4cc753e2018-06-13 13:25:33 -0400971 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
972 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400973 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400974 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
975
Jamie Madill5547b382017-10-23 18:16:01 -0400976 ASSERT(!samplerBinding.unreferenced);
977
Jamie Madill4cc753e2018-06-13 13:25:33 -0400978 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
979 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400980 {
Jamie Madilldbc605c2019-01-04 16:39:14 -0500981 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
982 TextureVk *textureVk = activeTextures[textureUnit];
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100983
984 // Ensure any writes to the textures are flushed before we read from them.
985 ANGLE_TRY(textureVk->ensureImageInitialized(contextVk));
986 vk::ImageHelper &image = textureVk->getImage();
987
988 // Ensure the image is in read-only layout
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500989 if (image.isLayoutChangeNecessary(vk::ImageLayout::FragmentShaderReadOnly))
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100990 {
Shahbaz Youssefi2660b502019-03-21 12:08:40 -0400991 vk::CommandBuffer *srcLayoutChange;
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100992 ANGLE_TRY(image.recordCommands(contextVk, &srcLayoutChange));
993
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500994 image.changeLayout(VK_IMAGE_ASPECT_COLOR_BIT,
995 vk::ImageLayout::FragmentShaderReadOnly, srcLayoutChange);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100996 }
997
998 image.addReadDependency(framebuffer);
Jamie Madill4cc753e2018-06-13 13:25:33 -0400999
1000 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
1001
1002 imageInfo.sampler = textureVk->getSampler().getHandle();
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +01001003 imageInfo.imageView = textureVk->getReadImageView().getHandle();
Jamie Madill4cc753e2018-06-13 13:25:33 -04001004 imageInfo.imageLayout = image.getCurrentLayout();
1005
1006 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
1007
1008 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
1009 writeInfo.pNext = nullptr;
1010 writeInfo.dstSet = descriptorSet;
1011 writeInfo.dstBinding = textureIndex;
1012 writeInfo.dstArrayElement = arrayElement;
1013 writeInfo.descriptorCount = 1;
1014 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
1015 writeInfo.pImageInfo = &imageInfo;
1016 writeInfo.pBufferInfo = nullptr;
1017 writeInfo.pTexelBufferView = nullptr;
1018
1019 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -04001020 }
Jamie Madill5547b382017-10-23 18:16:01 -04001021 }
1022
1023 VkDevice device = contextVk->getDevice();
1024
Jamie Madill4cc753e2018-06-13 13:25:33 -04001025 ASSERT(writeCount > 0);
1026 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -04001027
Jamie Madill7c985f52018-11-29 18:16:17 -05001028 return angle::Result::Continue;
Jamie Madill5547b382017-10-23 18:16:01 -04001029}
1030
Luc Ferron7a06ac12018-03-15 10:17:04 -04001031void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
1032{
1033 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
1034 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -04001035 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -04001036 }
1037}
Jamie Madill242c4fe2018-07-12 15:56:56 -04001038
Shahbaz Youssefi2660b502019-03-21 12:08:40 -04001039angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
1040 vk::CommandBuffer *commandBuffer)
Jamie Madill242c4fe2018-07-12 15:56:56 -04001041{
Jamie Madill242c4fe2018-07-12 15:56:56 -04001042 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -04001043
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001044 if (mDescriptorSets.empty())
Jamie Madill7c985f52018-11-29 18:16:17 -05001045 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -04001046
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001047 // Find the maximum non-null descriptor set. This is used in conjunction with a driver
1048 // workaround to bind empty descriptor sets only for gaps in between 0 and max and avoid
1049 // binding unnecessary empty descriptor sets for the sets beyond max.
1050 size_t descriptorSetRange = 0;
1051 for (size_t descriptorSetIndex = 0; descriptorSetIndex < mDescriptorSets.size();
1052 ++descriptorSetIndex)
Jamie Madill242c4fe2018-07-12 15:56:56 -04001053 {
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001054 if (mDescriptorSets[descriptorSetIndex] != VK_NULL_HANDLE)
1055 {
1056 descriptorSetRange = descriptorSetIndex + 1;
1057 }
1058 }
1059
1060 for (size_t descriptorSetIndex = 0; descriptorSetIndex < descriptorSetRange;
1061 ++descriptorSetIndex)
1062 {
1063 VkDescriptorSet descSet = mDescriptorSets[descriptorSetIndex];
1064 if (descSet == VK_NULL_HANDLE)
1065 {
Jonah Ryan-Davis776694c2019-05-08 10:28:55 -04001066 if (!contextVk->getRenderer()->getFeatures().bindEmptyForUnusedDescriptorSets.enabled)
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001067 {
1068 continue;
1069 }
1070
1071 // Workaround a driver bug where missing (though unused) descriptor sets indices cause
1072 // later sets to misbehave.
1073 if (mEmptyDescriptorSets[descriptorSetIndex] == VK_NULL_HANDLE)
1074 {
1075 vk::DynamicDescriptorPool *dynamicDescriptorPool =
1076 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
1077 const vk::DescriptorSetLayout &descriptorSetLayout =
1078 mDescriptorSetLayouts[descriptorSetIndex].get();
1079
1080 ANGLE_TRY(dynamicDescriptorPool->allocateSets(
1081 contextVk, descriptorSetLayout.ptr(), 1,
1082 &mDescriptorPoolBindings[descriptorSetIndex],
1083 &mEmptyDescriptorSets[descriptorSetIndex]));
1084 }
1085 descSet = mEmptyDescriptorSets[descriptorSetIndex];
1086 }
1087
Shahbaz Youssefidf066e92018-11-13 15:39:47 -05001088 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
1089 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Shahbaz Youssefi216f73d2019-04-12 13:32:30 -04001090 constexpr uint32_t kShaderTypeCount = kShaderTypeMax - kShaderTypeMin + 1;
1091
1092 // Default uniforms are encompassed in a block per shader stage, and they are assigned
1093 // through dynamic uniform buffers (requiring dynamic offsets). No other descriptor
1094 // requires a dynamic offset.
1095 const uint32_t uniformBlockOffsetCount =
1096 descriptorSetIndex == kUniformsDescriptorSetIndex ? kShaderTypeCount : 0;
1097
1098 commandBuffer->bindGraphicsDescriptorSets(mPipelineLayout.get(), descriptorSetIndex, 1,
1099 &descSet, uniformBlockOffsetCount,
1100 mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -04001101 }
1102
Jamie Madill7c985f52018-11-29 18:16:17 -05001103 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -04001104}
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001105} // namespace rx