blob: 2d6e644ac16b06d24d25edc4e503b925d94aa73f [file] [log] [blame]
Jamie Madill9e54b5a2016-05-25 12:57:39 -04001//
2// Copyright 2016 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6// ProgramVk.cpp:
7// Implements the class methods for ProgramVk.
8//
9
10#include "libANGLE/renderer/vulkan/ProgramVk.h"
11
12#include "common/debug.h"
Jamie Madill76e471e2017-10-21 09:56:01 -040013#include "common/utilities.h"
Jamie Madillc564c072017-06-01 12:45:42 -040014#include "libANGLE/Context.h"
Luc Ferron48cdc2e2018-05-31 09:58:34 -040015#include "libANGLE/renderer/renderer_utils.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050016#include "libANGLE/renderer/vulkan/ContextVk.h"
17#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
18#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill5547b382017-10-23 18:16:01 -040019#include "libANGLE/renderer/vulkan/TextureVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040020
21namespace rx
22{
23
Jamie Madill76e471e2017-10-21 09:56:01 -040024namespace
25{
26
Jamie Madillf3614372018-03-31 14:19:14 -040027constexpr size_t kUniformBlockDynamicBufferMinSize = 256 * 128;
Luc Ferron7a06ac12018-03-15 10:17:04 -040028
Jamie Madill76e471e2017-10-21 09:56:01 -040029gl::Error InitDefaultUniformBlock(const gl::Context *context,
Jamie Madill76e471e2017-10-21 09:56:01 -040030 gl::Shader *shader,
Jamie Madill76e471e2017-10-21 09:56:01 -040031 sh::BlockLayoutMap *blockLayoutMapOut,
Luc Ferron7a06ac12018-03-15 10:17:04 -040032 size_t *blockSizeOut)
Jamie Madill76e471e2017-10-21 09:56:01 -040033{
34 const auto &uniforms = shader->getUniforms(context);
35
36 if (uniforms.empty())
37 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040038 *blockSizeOut = 0;
Jamie Madill76e471e2017-10-21 09:56:01 -040039 return gl::NoError();
40 }
41
42 sh::Std140BlockEncoder blockEncoder;
Olli Etuaho3de27032017-11-30 12:16:47 +020043 sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
Jamie Madill76e471e2017-10-21 09:56:01 -040044
45 size_t blockSize = blockEncoder.getBlockSize();
46
47 // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized.
48 if (blockSize == 0)
49 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040050 *blockSizeOut = 0;
Jamie Madill76e471e2017-10-21 09:56:01 -040051 return gl::NoError();
52 }
53
Luc Ferron7a06ac12018-03-15 10:17:04 -040054 *blockSizeOut = blockSize;
Jamie Madill76e471e2017-10-21 09:56:01 -040055 return gl::NoError();
56}
57
58template <typename T>
59void UpdateDefaultUniformBlock(GLsizei count,
Luc Ferron2371aca2018-03-27 16:03:03 -040060 uint32_t arrayIndex,
Jamie Madill76e471e2017-10-21 09:56:01 -040061 int componentCount,
62 const T *v,
63 const sh::BlockMemberInfo &layoutInfo,
64 angle::MemoryBuffer *uniformData)
65{
Luc Ferron2371aca2018-03-27 16:03:03 -040066 const int elementSize = sizeof(T) * componentCount;
67
68 uint8_t *dst = uniformData->data() + layoutInfo.offset;
Jamie Madill76e471e2017-10-21 09:56:01 -040069 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
70 {
Luc Ferron2371aca2018-03-27 16:03:03 -040071 uint32_t arrayOffset = arrayIndex * layoutInfo.arrayStride;
72 uint8_t *writePtr = dst + arrayOffset;
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);
Luc Ferron2371aca2018-03-27 16:03:03 -040085 memcpy(writePtr, readPtr, elementSize);
86 }
Jamie Madill76e471e2017-10-21 09:56:01 -040087 }
88}
89
Luc Ferron7cec3352018-03-13 13:29:34 -040090template <typename T>
91void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040092 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040093 T *dst,
94 const sh::BlockMemberInfo &layoutInfo,
95 const angle::MemoryBuffer *uniformData)
96{
97 ASSERT(layoutInfo.offset != -1);
98
Luc Ferron2371aca2018-03-27 16:03:03 -040099 const int elementSize = sizeof(T) * componentCount;
100 const uint8_t *source = uniformData->data() + layoutInfo.offset;
101
Luc Ferron7cec3352018-03-13 13:29:34 -0400102 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
103 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400104 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400105 memcpy(dst, readPtr, elementSize);
106 }
107 else
108 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400109 // Have to respect the arrayStride between each element of the array.
110 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
111 const uint8_t *readPtr = source + arrayOffset;
112 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400113 }
114}
115
Jamie Madillc3755fc2018-04-05 08:39:13 -0400116vk::Error SyncDefaultUniformBlock(RendererVk *renderer,
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400117 vk::DynamicBuffer *dynamicBuffer,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400118 const angle::MemoryBuffer &bufferData,
119 uint32_t *outOffset,
120 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400121{
Luc Ferron7a06ac12018-03-15 10:17:04 -0400122 ASSERT(!bufferData.empty());
123 uint8_t *data = nullptr;
124 VkBuffer *outBuffer = nullptr;
125 uint32_t offset;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400126 ANGLE_TRY(dynamicBuffer->allocate(renderer, bufferData.size(), &data, outBuffer, &offset,
127 outBufferModified));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400128 *outOffset = offset;
129 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madillc3755fc2018-04-05 08:39:13 -0400130 ANGLE_TRY(dynamicBuffer->flush(renderer->getDevice()));
Jamie Madill76e471e2017-10-21 09:56:01 -0400131 return vk::NoError();
132}
Jamie Madill76e471e2017-10-21 09:56:01 -0400133} // anonymous namespace
134
135ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400136 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Jamie Madillf3614372018-03-31 14:19:14 -0400137 kUniformBlockDynamicBufferMinSize),
Luc Ferron7a06ac12018-03-15 10:17:04 -0400138 uniformData(),
139 uniformsDirty(false),
140 uniformLayout()
Jamie Madill76e471e2017-10-21 09:56:01 -0400141{
142}
143
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500144ProgramVk::DefaultUniformBlock::~DefaultUniformBlock()
145{
146}
147
Jamie Madill76e471e2017-10-21 09:56:01 -0400148ProgramVk::ProgramVk(const gl::ProgramState &state)
Luc Ferron7a06ac12018-03-15 10:17:04 -0400149 : ProgramImpl(state),
150 mDefaultUniformBlocks(),
151 mUniformBlocksOffsets(),
152 mUsedDescriptorSetRange(),
153 mDirtyTextures(true)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400154{
Luc Ferron7a06ac12018-03-15 10:17:04 -0400155 mUniformBlocksOffsets.fill(0);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500156 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400157}
158
159ProgramVk::~ProgramVk()
160{
161}
162
Jamie Madillb7d924a2018-03-10 11:16:54 -0500163gl::Error ProgramVk::destroy(const gl::Context *contextImpl)
Jamie Madill5deea722017-02-16 10:44:46 -0500164{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500165 ContextVk *contextVk = vk::GetImpl(contextImpl);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500166 return reset(contextVk);
Jamie Madillc5143482017-10-15 20:20:06 -0400167}
Jamie Madill5deea722017-02-16 10:44:46 -0500168
Jamie Madillb7d924a2018-03-10 11:16:54 -0500169vk::Error ProgramVk::reset(ContextVk *contextVk)
Jamie Madillc5143482017-10-15 20:20:06 -0400170{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500171 // TODO(jmadill): Handle re-linking a program that is in-use. http://anglebug.com/2397
172
173 VkDevice device = contextVk->getDevice();
174
Jamie Madill9b168d02018-06-13 13:25:32 -0400175 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
176 {
177 descriptorSetLayout.reset();
178 }
179 mPipelineLayout.reset();
180
Jamie Madill76e471e2017-10-21 09:56:01 -0400181 for (auto &uniformBlock : mDefaultUniformBlocks)
182 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400183 uniformBlock.storage.destroy(device);
Jamie Madill76e471e2017-10-21 09:56:01 -0400184 }
185
186 mEmptyUniformBlockStorage.memory.destroy(device);
187 mEmptyUniformBlockStorage.buffer.destroy(device);
188
Jamie Madill5deea722017-02-16 10:44:46 -0500189 mLinkedFragmentModule.destroy(device);
190 mLinkedVertexModule.destroy(device);
Jamie Madillf2f6d372018-01-10 21:37:23 -0500191 mVertexModuleSerial = Serial();
192 mFragmentModuleSerial = Serial();
Jamie Madill76e471e2017-10-21 09:56:01 -0400193
Jamie Madill5547b382017-10-23 18:16:01 -0400194 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500195 mUsedDescriptorSetRange.invalidate();
Jamie Madill50cf2be2018-06-15 09:46:57 -0400196 mDirtyTextures = false;
Jamie Madillb7d924a2018-03-10 11:16:54 -0500197
198 return vk::NoError();
Jamie Madill5deea722017-02-16 10:44:46 -0500199}
200
Jamie Madill9cf9e872017-06-05 12:59:25 -0400201gl::LinkResult ProgramVk::load(const gl::Context *contextImpl,
202 gl::InfoLog &infoLog,
203 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400204{
205 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500206 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400207}
208
Jamie Madill27a60632017-06-30 15:12:01 -0400209void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400210{
211 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400212}
213
214void ProgramVk::setBinaryRetrievableHint(bool retrievable)
215{
216 UNIMPLEMENTED();
217}
218
Yunchao He61afff12017-03-14 15:34:03 +0800219void ProgramVk::setSeparable(bool separable)
220{
221 UNIMPLEMENTED();
222}
223
Jamie Madill9cf9e872017-06-05 12:59:25 -0400224gl::LinkResult ProgramVk::link(const gl::Context *glContext,
Jamie Madillc9727f32017-11-07 12:37:07 -0500225 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400226 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400227{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400228 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madillc5143482017-10-15 20:20:06 -0400229 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500230 GlslangWrapper *glslangWrapper = renderer->getGlslangWrapper();
Jamie Madillc5143482017-10-15 20:20:06 -0400231 VkDevice device = renderer->getDevice();
232
Jamie Madillb7d924a2018-03-10 11:16:54 -0500233 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500234
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500235 std::vector<uint32_t> vertexCode;
236 std::vector<uint32_t> fragmentCode;
237 bool linkSuccess = false;
Jamie Madill4dd167f2017-11-09 13:08:31 -0500238 ANGLE_TRY_RESULT(
239 glslangWrapper->linkProgram(glContext, mState, resources, &vertexCode, &fragmentCode),
240 linkSuccess);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500241 if (!linkSuccess)
242 {
243 return false;
244 }
245
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500246 {
247 VkShaderModuleCreateInfo vertexShaderInfo;
248 vertexShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
249 vertexShaderInfo.pNext = nullptr;
250 vertexShaderInfo.flags = 0;
251 vertexShaderInfo.codeSize = vertexCode.size() * sizeof(uint32_t);
252 vertexShaderInfo.pCode = vertexCode.data();
Jamie Madillc5143482017-10-15 20:20:06 -0400253
254 ANGLE_TRY(mLinkedVertexModule.init(device, vertexShaderInfo));
Jamie Madill78feddc2018-04-27 11:45:05 -0400255 mVertexModuleSerial = renderer->issueShaderSerial();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500256 }
257
258 {
259 VkShaderModuleCreateInfo fragmentShaderInfo;
260 fragmentShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
261 fragmentShaderInfo.pNext = nullptr;
262 fragmentShaderInfo.flags = 0;
263 fragmentShaderInfo.codeSize = fragmentCode.size() * sizeof(uint32_t);
264 fragmentShaderInfo.pCode = fragmentCode.data();
265
Jamie Madillc5143482017-10-15 20:20:06 -0400266 ANGLE_TRY(mLinkedFragmentModule.init(device, fragmentShaderInfo));
Jamie Madill78feddc2018-04-27 11:45:05 -0400267 mFragmentModuleSerial = renderer->issueShaderSerial();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500268 }
269
Jamie Madill76e471e2017-10-21 09:56:01 -0400270 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500271
Jamie Madill8c3988c2017-12-21 14:44:56 -0500272 if (!mState.getSamplerUniformRange().empty())
273 {
274 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400275 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500276 mDirtyTextures = true;
277 }
278
Jamie Madill9b168d02018-06-13 13:25:32 -0400279 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
280 // don't already exist in the cache.
281 vk::DescriptorSetLayoutDesc uniformsSetDesc;
282 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
283 1);
284 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
285 1);
286
287 ANGLE_TRY(renderer->getDescriptorSetLayout(
288 uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
289
Jamie Madill9b168d02018-06-13 13:25:32 -0400290 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400291
292 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
293 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400294 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400295 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
296
297 // The front-end always binds array sampler units sequentially.
298 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
299 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400300 }
301
302 ANGLE_TRY(renderer->getDescriptorSetLayout(texturesSetDesc,
303 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
304
305 vk::PipelineLayoutDesc pipelineLayoutDesc;
306 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
307 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
308
309 ANGLE_TRY(
310 renderer->getPipelineLayout(pipelineLayoutDesc, mDescriptorSetLayouts, &mPipelineLayout));
311
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500312 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400313}
314
Jamie Madill76e471e2017-10-21 09:56:01 -0400315gl::Error ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
316{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400317 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500318 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400319 VkDevice device = contextVk->getDevice();
320
321 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400322 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
323 vk::ShaderMap<size_t> requiredBufferSize;
324 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400325
Jamie Madill33318de2018-05-01 11:22:54 -0400326 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400327 {
Jamie Madill33318de2018-05-01 11:22:54 -0400328 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
329 ANGLE_TRY(InitDefaultUniformBlock(glContext, mState.getAttachedShader(glShaderType),
330 &layoutMap[shaderType], &requiredBufferSize[shaderType]));
Jamie Madill76e471e2017-10-21 09:56:01 -0400331 }
332
333 // Init the default block layout info.
334 const auto &locations = mState.getUniformLocations();
335 const auto &uniforms = mState.getUniforms();
336 for (size_t locationIndex = 0; locationIndex < locations.size(); ++locationIndex)
337 {
Jamie Madill33318de2018-05-01 11:22:54 -0400338 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400339
340 const auto &location = locations[locationIndex];
341 if (location.used() && !location.ignored)
342 {
Jamie Madillde03e002017-10-21 14:04:20 -0400343 const auto &uniform = uniforms[location.index];
344
345 if (uniform.isSampler())
346 continue;
347
Jamie Madill76e471e2017-10-21 09:56:01 -0400348 std::string uniformName = uniform.name;
349 if (uniform.isArray())
350 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400351 // Gets the uniform name without the [0] at the end.
352 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400353 }
354
355 bool found = false;
356
Jamie Madill33318de2018-05-01 11:22:54 -0400357 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400358 {
Jamie Madill33318de2018-05-01 11:22:54 -0400359 auto it = layoutMap[shaderType].find(uniformName);
360 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400361 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400362 found = true;
363 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 }
365 }
366
367 ASSERT(found);
368 }
369
Jamie Madill33318de2018-05-01 11:22:54 -0400370 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400371 {
Jamie Madill33318de2018-05-01 11:22:54 -0400372 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400373 }
374 }
375
376 bool anyDirty = false;
377 bool allDirty = true;
378
Jamie Madill33318de2018-05-01 11:22:54 -0400379 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400380 {
Jamie Madill33318de2018-05-01 11:22:54 -0400381 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400382 {
Jamie Madill33318de2018-05-01 11:22:54 -0400383 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
384 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400385 {
386 return gl::OutOfMemory() << "Memory allocation failure.";
387 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400388 size_t minAlignment = static_cast<size_t>(
389 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
390
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400391 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400392
393 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400394 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
395 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400396
397 anyDirty = true;
398 }
399 else
400 {
401 allDirty = false;
402 }
403 }
404
405 if (anyDirty)
406 {
407 // Initialize the "empty" uniform block if necessary.
408 if (!allDirty)
409 {
410 VkBufferCreateInfo uniformBufferInfo;
411 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
412 uniformBufferInfo.pNext = nullptr;
413 uniformBufferInfo.flags = 0;
414 uniformBufferInfo.size = 1;
415 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
416 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
417 uniformBufferInfo.queueFamilyIndexCount = 0;
418 uniformBufferInfo.pQueueFamilyIndices = nullptr;
419
420 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(device, uniformBufferInfo));
421
Luc Ferron7a06ac12018-03-15 10:17:04 -0400422 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500423 VkMemoryPropertyFlags flags =
424 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill76e471e2017-10-21 09:56:01 -0400425 size_t requiredSize = 0;
Jamie Madill57fbfd82018-02-14 12:45:34 -0500426 ANGLE_TRY(AllocateBufferMemory(renderer, flags, &mEmptyUniformBlockStorage.buffer,
Jamie Madill76e471e2017-10-21 09:56:01 -0400427 &mEmptyUniformBlockStorage.memory, &requiredSize));
428 }
429
Jamie Madill8c3988c2017-12-21 14:44:56 -0500430 // Ensure the descriptor set range includes the uniform buffers at position 0.
431 mUsedDescriptorSetRange.extend(0);
Jamie Madill5547b382017-10-23 18:16:01 -0400432 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400433
434 return gl::NoError();
435}
436
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400437GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
438{
Luc Ferronfba1f612018-06-04 14:37:17 -0400439 // No-op. The spec is very vague about the behavior of validation.
440 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400441}
442
Jamie Madill76e471e2017-10-21 09:56:01 -0400443template <typename T>
444void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
445{
446 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
447 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
448
Luc Ferron7cec3352018-03-13 13:29:34 -0400449 if (linkedUniform.isSampler())
450 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400451 // We could potentially cache some indexing here. For now this is a no-op since the mapping
452 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400453 return;
454 }
455
Luc Ferron24a31372018-04-04 11:49:14 -0400456 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400457 {
Luc Ferron24a31372018-04-04 11:49:14 -0400458 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400459 {
Luc Ferron24a31372018-04-04 11:49:14 -0400460 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400461
Luc Ferron24a31372018-04-04 11:49:14 -0400462 // Assume an offset of -1 means the block is unused.
463 if (layoutInfo.offset == -1)
464 {
465 continue;
466 }
467
468 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400469 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
470 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400471 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400472 }
Luc Ferron24a31372018-04-04 11:49:14 -0400473 }
474 else
475 {
476 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400477 {
Luc Ferron24a31372018-04-04 11:49:14 -0400478 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
479
480 // Assume an offset of -1 means the block is unused.
481 if (layoutInfo.offset == -1)
482 {
483 continue;
484 }
485
486 const GLint componentCount = linkedUniform.typeInfo->componentCount;
487
Luc Ferron62059a52018-03-29 07:01:35 -0400488 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
489
Luc Ferrond91c3792018-04-06 09:36:36 -0400490 GLint initialArrayOffset =
491 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400492 for (GLint i = 0; i < count; i++)
493 {
494 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
495 GLint *dest =
496 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
497 const T *source = v + i * componentCount;
498
499 for (int c = 0; c < componentCount; c++)
500 {
501 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
502 }
503 }
Luc Ferron24a31372018-04-04 11:49:14 -0400504 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400505 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400506 }
507}
508
Luc Ferron7cec3352018-03-13 13:29:34 -0400509template <typename T>
510void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
511{
512 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
513 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
514
515 if (linkedUniform.isSampler())
516 {
517 UNIMPLEMENTED();
518 return;
519 }
520
Olli Etuaho107c7242018-03-20 15:45:35 +0200521 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800522 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400523
Jiawei Shao385b3e02018-03-21 09:43:28 +0800524 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400525 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400526 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400527
528 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
529 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400530
531 if (gl::IsMatrixType(linkedUniform.type))
532 {
533 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
534 (locationInfo.arrayIndex * linkedUniform.getElementSize());
535 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
536 }
537 else
538 {
539 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
540 v, layoutInfo, &uniformBlock.uniformData);
541 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400542}
543
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400544void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
545{
Jamie Madill76e471e2017-10-21 09:56:01 -0400546 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400547}
548
549void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
550{
Jamie Madill76e471e2017-10-21 09:56:01 -0400551 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400552}
553
554void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
555{
Jamie Madill76e471e2017-10-21 09:56:01 -0400556 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400557}
558
559void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
560{
Jamie Madill76e471e2017-10-21 09:56:01 -0400561 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400562}
563
564void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
565{
Luc Ferron7cec3352018-03-13 13:29:34 -0400566 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400567}
568
569void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
570{
Luc Ferron489243f2018-03-28 16:55:28 -0400571 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400572}
573
574void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
575{
Luc Ferron489243f2018-03-28 16:55:28 -0400576 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400577}
578
579void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
580{
Luc Ferron489243f2018-03-28 16:55:28 -0400581 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400582}
583
584void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
585{
586 UNIMPLEMENTED();
587}
588
589void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
590{
591 UNIMPLEMENTED();
592}
593
594void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
595{
596 UNIMPLEMENTED();
597}
598
599void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
600{
601 UNIMPLEMENTED();
602}
603
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400604template <int cols, int rows>
605void ProgramVk::setUniformMatrixfv(GLint location,
606 GLsizei count,
607 GLboolean transpose,
608 const GLfloat *value)
609{
610 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
611 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
612
613 for (auto &uniformBlock : mDefaultUniformBlocks)
614 {
615 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
616
617 // Assume an offset of -1 means the block is unused.
618 if (layoutInfo.offset == -1)
619 {
620 continue;
621 }
622
Luc Ferronc8fbff32018-06-04 10:30:48 -0400623 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400624 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
625 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400626
627 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
628 // setter did not update any data. We still want the uniform to be included when we'll
629 // update the descriptor sets.
630 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400631 }
632}
633
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400634void ProgramVk::setUniformMatrix2fv(GLint location,
635 GLsizei count,
636 GLboolean transpose,
637 const GLfloat *value)
638{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400639 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400640}
641
642void ProgramVk::setUniformMatrix3fv(GLint location,
643 GLsizei count,
644 GLboolean transpose,
645 const GLfloat *value)
646{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400647 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400648}
649
650void ProgramVk::setUniformMatrix4fv(GLint location,
651 GLsizei count,
652 GLboolean transpose,
653 const GLfloat *value)
654{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400655 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400656}
657
658void ProgramVk::setUniformMatrix2x3fv(GLint location,
659 GLsizei count,
660 GLboolean transpose,
661 const GLfloat *value)
662{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400663 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400664}
665
666void ProgramVk::setUniformMatrix3x2fv(GLint location,
667 GLsizei count,
668 GLboolean transpose,
669 const GLfloat *value)
670{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400671 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400672}
673
674void ProgramVk::setUniformMatrix2x4fv(GLint location,
675 GLsizei count,
676 GLboolean transpose,
677 const GLfloat *value)
678{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400679 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400680}
681
682void ProgramVk::setUniformMatrix4x2fv(GLint location,
683 GLsizei count,
684 GLboolean transpose,
685 const GLfloat *value)
686{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400687 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400688}
689
690void ProgramVk::setUniformMatrix3x4fv(GLint location,
691 GLsizei count,
692 GLboolean transpose,
693 const GLfloat *value)
694{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400695 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400696}
697
698void ProgramVk::setUniformMatrix4x3fv(GLint location,
699 GLsizei count,
700 GLboolean transpose,
701 const GLfloat *value)
702{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400703 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400704}
705
706void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
707{
708 UNIMPLEMENTED();
709}
710
Sami Väisänen46eaa942016-06-29 10:26:37 +0300711void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
712 GLenum genMode,
713 GLint components,
714 const GLfloat *coeffs)
715{
716 UNIMPLEMENTED();
717}
718
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500719const vk::ShaderModule &ProgramVk::getLinkedVertexModule() const
720{
721 ASSERT(mLinkedVertexModule.getHandle() != VK_NULL_HANDLE);
722 return mLinkedVertexModule;
723}
724
Jamie Madillf2f6d372018-01-10 21:37:23 -0500725Serial ProgramVk::getVertexModuleSerial() const
726{
727 return mVertexModuleSerial;
728}
729
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500730const vk::ShaderModule &ProgramVk::getLinkedFragmentModule() const
731{
732 ASSERT(mLinkedFragmentModule.getHandle() != VK_NULL_HANDLE);
733 return mLinkedFragmentModule;
734}
735
Jamie Madillf2f6d372018-01-10 21:37:23 -0500736Serial ProgramVk::getFragmentModuleSerial() const
737{
738 return mFragmentModuleSerial;
739}
740
Luc Ferron6ea1b412018-03-21 16:13:01 -0400741vk::Error ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400742{
Jamie Madill76e471e2017-10-21 09:56:01 -0400743 // Write out to a new a descriptor set.
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400744 vk::DynamicDescriptorPool *dynamicDescriptorPool = contextVk->getDynamicDescriptorPool();
Jamie Madill76e471e2017-10-21 09:56:01 -0400745
Luc Ferron6ea1b412018-03-21 16:13:01 -0400746 uint32_t potentialNewCount = descriptorSetIndex + 1;
747 if (potentialNewCount > mDescriptorSets.size())
748 {
749 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
750 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400751
Jamie Madillc7918ce2018-06-13 13:25:31 -0400752 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400753 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madillc7918ce2018-06-13 13:25:31 -0400754 ANGLE_TRY(dynamicDescriptorPool->allocateDescriptorSets(contextVk, descriptorSetLayout.ptr(), 1,
Luc Ferron6ea1b412018-03-21 16:13:01 -0400755 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill76e471e2017-10-21 09:56:01 -0400756 return vk::NoError();
757}
758
Jamie Madill54164b02017-08-28 15:17:37 -0400759void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
760{
Luc Ferron7cec3352018-03-13 13:29:34 -0400761 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400762}
763
764void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
765{
Luc Ferron7cec3352018-03-13 13:29:34 -0400766 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400767}
768
769void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
770{
771 UNIMPLEMENTED();
772}
773
Jamie Madill76e471e2017-10-21 09:56:01 -0400774vk::Error ProgramVk::updateUniforms(ContextVk *contextVk)
775{
Jamie Madill33318de2018-05-01 11:22:54 -0400776 if (!mDefaultUniformBlocks[vk::ShaderType::VertexShader].uniformsDirty &&
777 !mDefaultUniformBlocks[vk::ShaderType::FragmentShader].uniformsDirty)
Jamie Madill76e471e2017-10-21 09:56:01 -0400778 {
779 return vk::NoError();
780 }
781
Jamie Madill8c3988c2017-12-21 14:44:56 -0500782 ASSERT(mUsedDescriptorSetRange.contains(0));
Jamie Madill5547b382017-10-23 18:16:01 -0400783
Jamie Madill76e471e2017-10-21 09:56:01 -0400784 // Update buffer memory by immediate mapping. This immediate update only works once.
785 // TODO(jmadill): Handle inserting updates into the command stream, or use dynamic buffers.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400786 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400787 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400788 {
Jamie Madill33318de2018-05-01 11:22:54 -0400789 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400790
Jamie Madill76e471e2017-10-21 09:56:01 -0400791 if (uniformBlock.uniformsDirty)
792 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400793 bool bufferModified = false;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400794 ANGLE_TRY(SyncDefaultUniformBlock(contextVk->getRenderer(), &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400795 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400796 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400797 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400798
799 if (bufferModified)
800 {
801 anyNewBufferAllocated = true;
802 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400803 }
804 }
805
Luc Ferron7a06ac12018-03-15 10:17:04 -0400806 if (anyNewBufferAllocated)
807 {
808 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
809 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400810 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400811 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
812 }
813
Jamie Madill76e471e2017-10-21 09:56:01 -0400814 return vk::NoError();
815}
816
817vk::Error ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
818{
Jamie Madill33318de2018-05-01 11:22:54 -0400819 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
820 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400821
Jamie Madill33318de2018-05-01 11:22:54 -0400822 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400823 {
Jamie Madill33318de2018-05-01 11:22:54 -0400824 auto &uniformBlock = mDefaultUniformBlocks[shaderType];
825 auto &bufferInfo = descriptorBufferInfo[shaderType];
826 auto &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400827
828 if (!uniformBlock.uniformData.empty())
829 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400830 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400831 }
832 else
833 {
834 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
835 }
836
837 bufferInfo.offset = 0;
838 bufferInfo.range = VK_WHOLE_SIZE;
839
Jamie Madill76e471e2017-10-21 09:56:01 -0400840 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
841 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400842 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400843 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400844 writeInfo.dstArrayElement = 0;
845 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400846 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400847 writeInfo.pImageInfo = nullptr;
848 writeInfo.pBufferInfo = &bufferInfo;
849 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400850 }
851
852 VkDevice device = contextVk->getDevice();
853
Jamie Madill33318de2018-05-01 11:22:54 -0400854 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400855
856 return vk::NoError();
857}
858
Jamie Madill5547b382017-10-23 18:16:01 -0400859const std::vector<VkDescriptorSet> &ProgramVk::getDescriptorSets() const
Jamie Madill76e471e2017-10-21 09:56:01 -0400860{
Jamie Madill5547b382017-10-23 18:16:01 -0400861 return mDescriptorSets;
862}
863
Luc Ferron7a06ac12018-03-15 10:17:04 -0400864const uint32_t *ProgramVk::getDynamicOffsets()
865{
866 // If we have no descriptor set being used, we do not need to specify any offsets when binding
867 // the descriptor sets.
868 if (!mUsedDescriptorSetRange.contains(0))
869 return nullptr;
870
871 return mUniformBlocksOffsets.data();
872}
873
874uint32_t ProgramVk::getDynamicOffsetsCount()
875{
876 if (!mUsedDescriptorSetRange.contains(0))
877 return 0;
878
879 return static_cast<uint32_t>(mUniformBlocksOffsets.size());
880}
881
Jamie Madill8c3988c2017-12-21 14:44:56 -0500882const gl::RangeUI &ProgramVk::getUsedDescriptorSetRange() const
Jamie Madill5547b382017-10-23 18:16:01 -0400883{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500884 return mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400885}
886
Luc Ferron90968362018-05-04 08:47:22 -0400887gl::Error ProgramVk::updateTexturesDescriptorSet(const gl::Context *context)
Jamie Madill5547b382017-10-23 18:16:01 -0400888{
889 if (mState.getSamplerBindings().empty() || !mDirtyTextures)
890 {
Luc Ferron90968362018-05-04 08:47:22 -0400891 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400892 }
893
Luc Ferron90968362018-05-04 08:47:22 -0400894 ContextVk *contextVk = GetImplAs<ContextVk>(context);
Jamie Madillc7918ce2018-06-13 13:25:31 -0400895 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400896
Jamie Madill8c3988c2017-12-21 14:44:56 -0500897 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400898 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400899
900 // TODO(jmadill): Don't hard-code the texture limit.
901 ShaderTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
902 ShaderTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400903 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400904
905 const gl::State &glState = contextVk->getGLState();
906 const auto &completeTextures = glState.getCompleteTextureCache();
907
Jamie Madill4cc753e2018-06-13 13:25:33 -0400908 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
909 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400910 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400911 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
912
Jamie Madill5547b382017-10-23 18:16:01 -0400913 ASSERT(!samplerBinding.unreferenced);
914
Jamie Madill4cc753e2018-06-13 13:25:33 -0400915 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
916 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400917 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400918 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
919 gl::Texture *texture = completeTextures[textureUnit];
920
921 if (texture == nullptr)
922 {
923 // If we have an incomplete texture, fetch it from our renderer.
924 ANGLE_TRY(
925 contextVk->getIncompleteTexture(context, samplerBinding.textureType, &texture));
926 }
927
928 TextureVk *textureVk = vk::GetImpl(texture);
929 const vk::ImageHelper &image = textureVk->getImage();
930
931 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
932
933 imageInfo.sampler = textureVk->getSampler().getHandle();
934 imageInfo.imageView = textureVk->getImageView().getHandle();
935 imageInfo.imageLayout = image.getCurrentLayout();
936
937 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
938
939 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
940 writeInfo.pNext = nullptr;
941 writeInfo.dstSet = descriptorSet;
942 writeInfo.dstBinding = textureIndex;
943 writeInfo.dstArrayElement = arrayElement;
944 writeInfo.descriptorCount = 1;
945 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
946 writeInfo.pImageInfo = &imageInfo;
947 writeInfo.pBufferInfo = nullptr;
948 writeInfo.pTexelBufferView = nullptr;
949
950 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400951 }
Jamie Madill5547b382017-10-23 18:16:01 -0400952 }
953
954 VkDevice device = contextVk->getDevice();
955
Jamie Madill4cc753e2018-06-13 13:25:33 -0400956 ASSERT(writeCount > 0);
957 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400958
959 mDirtyTextures = false;
Luc Ferron90968362018-05-04 08:47:22 -0400960 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400961}
962
963void ProgramVk::invalidateTextures()
964{
965 mDirtyTextures = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400966}
967
Jamie Madill9b168d02018-06-13 13:25:32 -0400968const vk::PipelineLayout &ProgramVk::getPipelineLayout() const
969{
970 return mPipelineLayout.get();
971}
972
Luc Ferron7a06ac12018-03-15 10:17:04 -0400973void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
974{
975 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
976 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400977 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400978 }
979}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400980} // namespace rx