blob: 7346b4d093c458c4d8b32000aa43756423a3eeee [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 Madill5547b382017-10-23 18:16:01 -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
290 const uint32_t maxTextures = renderer->getMaxActiveTextures();
291
292 vk::DescriptorSetLayoutDesc texturesSetDesc;
293 for (uint32_t textureIndex = 0; textureIndex < maxTextures; ++textureIndex)
294 {
295 // TODO(jmadll): Sampler arrays. http://anglebug.com/2462
296 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1);
297 }
298
299 ANGLE_TRY(renderer->getDescriptorSetLayout(texturesSetDesc,
300 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
301
302 vk::PipelineLayoutDesc pipelineLayoutDesc;
303 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
304 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
305
306 ANGLE_TRY(
307 renderer->getPipelineLayout(pipelineLayoutDesc, mDescriptorSetLayouts, &mPipelineLayout));
308
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500309 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400310}
311
Jamie Madill76e471e2017-10-21 09:56:01 -0400312gl::Error ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
313{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400314 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500315 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400316 VkDevice device = contextVk->getDevice();
317
318 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400319 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
320 vk::ShaderMap<size_t> requiredBufferSize;
321 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400322
Jamie Madill33318de2018-05-01 11:22:54 -0400323 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400324 {
Jamie Madill33318de2018-05-01 11:22:54 -0400325 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
326 ANGLE_TRY(InitDefaultUniformBlock(glContext, mState.getAttachedShader(glShaderType),
327 &layoutMap[shaderType], &requiredBufferSize[shaderType]));
Jamie Madill76e471e2017-10-21 09:56:01 -0400328 }
329
330 // Init the default block layout info.
331 const auto &locations = mState.getUniformLocations();
332 const auto &uniforms = mState.getUniforms();
333 for (size_t locationIndex = 0; locationIndex < locations.size(); ++locationIndex)
334 {
Jamie Madill33318de2018-05-01 11:22:54 -0400335 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400336
337 const auto &location = locations[locationIndex];
338 if (location.used() && !location.ignored)
339 {
Jamie Madillde03e002017-10-21 14:04:20 -0400340 const auto &uniform = uniforms[location.index];
341
342 if (uniform.isSampler())
343 continue;
344
Jamie Madill76e471e2017-10-21 09:56:01 -0400345 std::string uniformName = uniform.name;
346 if (uniform.isArray())
347 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400348 // Gets the uniform name without the [0] at the end.
349 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400350 }
351
352 bool found = false;
353
Jamie Madill33318de2018-05-01 11:22:54 -0400354 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400355 {
Jamie Madill33318de2018-05-01 11:22:54 -0400356 auto it = layoutMap[shaderType].find(uniformName);
357 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400358 {
359 found = true;
Jamie Madill33318de2018-05-01 11:22:54 -0400360 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400361 }
362 }
363
364 ASSERT(found);
365 }
366
Jamie Madill33318de2018-05-01 11:22:54 -0400367 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400368 {
Jamie Madill33318de2018-05-01 11:22:54 -0400369 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400370 }
371 }
372
373 bool anyDirty = false;
374 bool allDirty = true;
375
Jamie Madill33318de2018-05-01 11:22:54 -0400376 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400377 {
Jamie Madill33318de2018-05-01 11:22:54 -0400378 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400379 {
Jamie Madill33318de2018-05-01 11:22:54 -0400380 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
381 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400382 {
383 return gl::OutOfMemory() << "Memory allocation failure.";
384 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400385 size_t minAlignment = static_cast<size_t>(
386 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
387
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400388 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400389
390 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400391 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
392 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400393
394 anyDirty = true;
395 }
396 else
397 {
398 allDirty = false;
399 }
400 }
401
402 if (anyDirty)
403 {
404 // Initialize the "empty" uniform block if necessary.
405 if (!allDirty)
406 {
407 VkBufferCreateInfo uniformBufferInfo;
408 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
409 uniformBufferInfo.pNext = nullptr;
410 uniformBufferInfo.flags = 0;
411 uniformBufferInfo.size = 1;
412 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
413 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
414 uniformBufferInfo.queueFamilyIndexCount = 0;
415 uniformBufferInfo.pQueueFamilyIndices = nullptr;
416
417 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(device, uniformBufferInfo));
418
Luc Ferron7a06ac12018-03-15 10:17:04 -0400419 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500420 VkMemoryPropertyFlags flags =
421 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill76e471e2017-10-21 09:56:01 -0400422 size_t requiredSize = 0;
Jamie Madill57fbfd82018-02-14 12:45:34 -0500423 ANGLE_TRY(AllocateBufferMemory(renderer, flags, &mEmptyUniformBlockStorage.buffer,
Jamie Madill76e471e2017-10-21 09:56:01 -0400424 &mEmptyUniformBlockStorage.memory, &requiredSize));
425 }
426
Jamie Madill8c3988c2017-12-21 14:44:56 -0500427 // Ensure the descriptor set range includes the uniform buffers at position 0.
428 mUsedDescriptorSetRange.extend(0);
Jamie Madill5547b382017-10-23 18:16:01 -0400429 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400430
431 return gl::NoError();
432}
433
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400434GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
435{
Luc Ferronfba1f612018-06-04 14:37:17 -0400436 // No-op. The spec is very vague about the behavior of validation.
437 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400438}
439
Jamie Madill76e471e2017-10-21 09:56:01 -0400440template <typename T>
441void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
442{
443 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
444 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
445
Luc Ferron7cec3352018-03-13 13:29:34 -0400446 if (linkedUniform.isSampler())
447 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400448 // We could potentially cache some indexing here. For now this is a no-op since the mapping
449 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400450 return;
451 }
452
Luc Ferron24a31372018-04-04 11:49:14 -0400453 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400454 {
Luc Ferron24a31372018-04-04 11:49:14 -0400455 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400456 {
Luc Ferron24a31372018-04-04 11:49:14 -0400457 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400458
Luc Ferron24a31372018-04-04 11:49:14 -0400459 // Assume an offset of -1 means the block is unused.
460 if (layoutInfo.offset == -1)
461 {
462 continue;
463 }
464
465 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400466 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
467 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400468 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400469 }
Luc Ferron24a31372018-04-04 11:49:14 -0400470 }
471 else
472 {
473 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400474 {
Luc Ferron24a31372018-04-04 11:49:14 -0400475 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
476
477 // Assume an offset of -1 means the block is unused.
478 if (layoutInfo.offset == -1)
479 {
480 continue;
481 }
482
483 const GLint componentCount = linkedUniform.typeInfo->componentCount;
484
Luc Ferron62059a52018-03-29 07:01:35 -0400485 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
486
Luc Ferrond91c3792018-04-06 09:36:36 -0400487 GLint initialArrayOffset =
488 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400489 for (GLint i = 0; i < count; i++)
490 {
491 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
492 GLint *dest =
493 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
494 const T *source = v + i * componentCount;
495
496 for (int c = 0; c < componentCount; c++)
497 {
498 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
499 }
500 }
Luc Ferron24a31372018-04-04 11:49:14 -0400501 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400502 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400503 }
504}
505
Luc Ferron7cec3352018-03-13 13:29:34 -0400506template <typename T>
507void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
508{
509 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
510 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
511
512 if (linkedUniform.isSampler())
513 {
514 UNIMPLEMENTED();
515 return;
516 }
517
Olli Etuaho107c7242018-03-20 15:45:35 +0200518 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800519 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400520
Jiawei Shao385b3e02018-03-21 09:43:28 +0800521 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400522 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Luc Ferron7cec3352018-03-13 13:29:34 -0400523 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400524
525 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
526 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400527
528 if (gl::IsMatrixType(linkedUniform.type))
529 {
530 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
531 (locationInfo.arrayIndex * linkedUniform.getElementSize());
532 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
533 }
534 else
535 {
536 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
537 v, layoutInfo, &uniformBlock.uniformData);
538 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400539}
540
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400541void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
542{
Jamie Madill76e471e2017-10-21 09:56:01 -0400543 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400544}
545
546void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
547{
Jamie Madill76e471e2017-10-21 09:56:01 -0400548 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400549}
550
551void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
552{
Jamie Madill76e471e2017-10-21 09:56:01 -0400553 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400554}
555
556void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
557{
Jamie Madill76e471e2017-10-21 09:56:01 -0400558 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400559}
560
561void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
562{
Luc Ferron7cec3352018-03-13 13:29:34 -0400563 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400564}
565
566void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
567{
Luc Ferron489243f2018-03-28 16:55:28 -0400568 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400569}
570
571void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
572{
Luc Ferron489243f2018-03-28 16:55:28 -0400573 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400574}
575
576void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
577{
Luc Ferron489243f2018-03-28 16:55:28 -0400578 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400579}
580
581void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
582{
583 UNIMPLEMENTED();
584}
585
586void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
587{
588 UNIMPLEMENTED();
589}
590
591void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
592{
593 UNIMPLEMENTED();
594}
595
596void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
597{
598 UNIMPLEMENTED();
599}
600
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400601template <int cols, int rows>
602void ProgramVk::setUniformMatrixfv(GLint location,
603 GLsizei count,
604 GLboolean transpose,
605 const GLfloat *value)
606{
607 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
608 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
609
610 for (auto &uniformBlock : mDefaultUniformBlocks)
611 {
612 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
613
614 // Assume an offset of -1 means the block is unused.
615 if (layoutInfo.offset == -1)
616 {
617 continue;
618 }
619
Luc Ferronc8fbff32018-06-04 10:30:48 -0400620 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400621 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
622 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400623
624 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
625 // setter did not update any data. We still want the uniform to be included when we'll
626 // update the descriptor sets.
627 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400628 }
629}
630
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400631void ProgramVk::setUniformMatrix2fv(GLint location,
632 GLsizei count,
633 GLboolean transpose,
634 const GLfloat *value)
635{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400636 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400637}
638
639void ProgramVk::setUniformMatrix3fv(GLint location,
640 GLsizei count,
641 GLboolean transpose,
642 const GLfloat *value)
643{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400644 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400645}
646
647void ProgramVk::setUniformMatrix4fv(GLint location,
648 GLsizei count,
649 GLboolean transpose,
650 const GLfloat *value)
651{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400652 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400653}
654
655void ProgramVk::setUniformMatrix2x3fv(GLint location,
656 GLsizei count,
657 GLboolean transpose,
658 const GLfloat *value)
659{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400660 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400661}
662
663void ProgramVk::setUniformMatrix3x2fv(GLint location,
664 GLsizei count,
665 GLboolean transpose,
666 const GLfloat *value)
667{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400668 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400669}
670
671void ProgramVk::setUniformMatrix2x4fv(GLint location,
672 GLsizei count,
673 GLboolean transpose,
674 const GLfloat *value)
675{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400676 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400677}
678
679void ProgramVk::setUniformMatrix4x2fv(GLint location,
680 GLsizei count,
681 GLboolean transpose,
682 const GLfloat *value)
683{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400684 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400685}
686
687void ProgramVk::setUniformMatrix3x4fv(GLint location,
688 GLsizei count,
689 GLboolean transpose,
690 const GLfloat *value)
691{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400692 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400693}
694
695void ProgramVk::setUniformMatrix4x3fv(GLint location,
696 GLsizei count,
697 GLboolean transpose,
698 const GLfloat *value)
699{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400700 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400701}
702
703void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
704{
705 UNIMPLEMENTED();
706}
707
Sami Väisänen46eaa942016-06-29 10:26:37 +0300708void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
709 GLenum genMode,
710 GLint components,
711 const GLfloat *coeffs)
712{
713 UNIMPLEMENTED();
714}
715
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500716const vk::ShaderModule &ProgramVk::getLinkedVertexModule() const
717{
718 ASSERT(mLinkedVertexModule.getHandle() != VK_NULL_HANDLE);
719 return mLinkedVertexModule;
720}
721
Jamie Madillf2f6d372018-01-10 21:37:23 -0500722Serial ProgramVk::getVertexModuleSerial() const
723{
724 return mVertexModuleSerial;
725}
726
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500727const vk::ShaderModule &ProgramVk::getLinkedFragmentModule() const
728{
729 ASSERT(mLinkedFragmentModule.getHandle() != VK_NULL_HANDLE);
730 return mLinkedFragmentModule;
731}
732
Jamie Madillf2f6d372018-01-10 21:37:23 -0500733Serial ProgramVk::getFragmentModuleSerial() const
734{
735 return mFragmentModuleSerial;
736}
737
Luc Ferron6ea1b412018-03-21 16:13:01 -0400738vk::Error ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400739{
Jamie Madill76e471e2017-10-21 09:56:01 -0400740 // Write out to a new a descriptor set.
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400741 vk::DynamicDescriptorPool *dynamicDescriptorPool = contextVk->getDynamicDescriptorPool();
Jamie Madill76e471e2017-10-21 09:56:01 -0400742
Luc Ferron6ea1b412018-03-21 16:13:01 -0400743 uint32_t potentialNewCount = descriptorSetIndex + 1;
744 if (potentialNewCount > mDescriptorSets.size())
745 {
746 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
747 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400748
Jamie Madillc7918ce2018-06-13 13:25:31 -0400749 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400750 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madillc7918ce2018-06-13 13:25:31 -0400751 ANGLE_TRY(dynamicDescriptorPool->allocateDescriptorSets(contextVk, descriptorSetLayout.ptr(), 1,
Luc Ferron6ea1b412018-03-21 16:13:01 -0400752 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill76e471e2017-10-21 09:56:01 -0400753 return vk::NoError();
754}
755
Jamie Madill54164b02017-08-28 15:17:37 -0400756void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
757{
Luc Ferron7cec3352018-03-13 13:29:34 -0400758 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400759}
760
761void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
762{
Luc Ferron7cec3352018-03-13 13:29:34 -0400763 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400764}
765
766void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
767{
768 UNIMPLEMENTED();
769}
770
Jamie Madill76e471e2017-10-21 09:56:01 -0400771vk::Error ProgramVk::updateUniforms(ContextVk *contextVk)
772{
Jamie Madill33318de2018-05-01 11:22:54 -0400773 if (!mDefaultUniformBlocks[vk::ShaderType::VertexShader].uniformsDirty &&
774 !mDefaultUniformBlocks[vk::ShaderType::FragmentShader].uniformsDirty)
Jamie Madill76e471e2017-10-21 09:56:01 -0400775 {
776 return vk::NoError();
777 }
778
Jamie Madill8c3988c2017-12-21 14:44:56 -0500779 ASSERT(mUsedDescriptorSetRange.contains(0));
Jamie Madill5547b382017-10-23 18:16:01 -0400780
Jamie Madill76e471e2017-10-21 09:56:01 -0400781 // Update buffer memory by immediate mapping. This immediate update only works once.
782 // TODO(jmadill): Handle inserting updates into the command stream, or use dynamic buffers.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400783 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400784 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400785 {
Jamie Madill33318de2018-05-01 11:22:54 -0400786 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400787
Jamie Madill76e471e2017-10-21 09:56:01 -0400788 if (uniformBlock.uniformsDirty)
789 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400790 bool bufferModified = false;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400791 ANGLE_TRY(SyncDefaultUniformBlock(contextVk->getRenderer(), &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400792 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400793 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400794 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400795
796 if (bufferModified)
797 {
798 anyNewBufferAllocated = true;
799 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400800 }
801 }
802
Luc Ferron7a06ac12018-03-15 10:17:04 -0400803 if (anyNewBufferAllocated)
804 {
805 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
806 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400807 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400808 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
809 }
810
Jamie Madill76e471e2017-10-21 09:56:01 -0400811 return vk::NoError();
812}
813
814vk::Error ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
815{
Jamie Madill33318de2018-05-01 11:22:54 -0400816 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
817 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400818
Jamie Madill33318de2018-05-01 11:22:54 -0400819 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400820 {
Jamie Madill33318de2018-05-01 11:22:54 -0400821 auto &uniformBlock = mDefaultUniformBlocks[shaderType];
822 auto &bufferInfo = descriptorBufferInfo[shaderType];
823 auto &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400824
825 if (!uniformBlock.uniformData.empty())
826 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400827 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400828 }
829 else
830 {
831 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
832 }
833
834 bufferInfo.offset = 0;
835 bufferInfo.range = VK_WHOLE_SIZE;
836
Jamie Madill76e471e2017-10-21 09:56:01 -0400837 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
838 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400839 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400840 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400841 writeInfo.dstArrayElement = 0;
842 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400843 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400844 writeInfo.pImageInfo = nullptr;
845 writeInfo.pBufferInfo = &bufferInfo;
846 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400847 }
848
849 VkDevice device = contextVk->getDevice();
850
Jamie Madill33318de2018-05-01 11:22:54 -0400851 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400852
853 return vk::NoError();
854}
855
Jamie Madill5547b382017-10-23 18:16:01 -0400856const std::vector<VkDescriptorSet> &ProgramVk::getDescriptorSets() const
Jamie Madill76e471e2017-10-21 09:56:01 -0400857{
Jamie Madill5547b382017-10-23 18:16:01 -0400858 return mDescriptorSets;
859}
860
Luc Ferron7a06ac12018-03-15 10:17:04 -0400861const uint32_t *ProgramVk::getDynamicOffsets()
862{
863 // If we have no descriptor set being used, we do not need to specify any offsets when binding
864 // the descriptor sets.
865 if (!mUsedDescriptorSetRange.contains(0))
866 return nullptr;
867
868 return mUniformBlocksOffsets.data();
869}
870
871uint32_t ProgramVk::getDynamicOffsetsCount()
872{
873 if (!mUsedDescriptorSetRange.contains(0))
874 return 0;
875
876 return static_cast<uint32_t>(mUniformBlocksOffsets.size());
877}
878
Jamie Madill8c3988c2017-12-21 14:44:56 -0500879const gl::RangeUI &ProgramVk::getUsedDescriptorSetRange() const
Jamie Madill5547b382017-10-23 18:16:01 -0400880{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500881 return mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400882}
883
Luc Ferron90968362018-05-04 08:47:22 -0400884gl::Error ProgramVk::updateTexturesDescriptorSet(const gl::Context *context)
Jamie Madill5547b382017-10-23 18:16:01 -0400885{
886 if (mState.getSamplerBindings().empty() || !mDirtyTextures)
887 {
Luc Ferron90968362018-05-04 08:47:22 -0400888 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400889 }
890
Luc Ferron90968362018-05-04 08:47:22 -0400891 ContextVk *contextVk = GetImplAs<ContextVk>(context);
Jamie Madillc7918ce2018-06-13 13:25:31 -0400892 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400893
Jamie Madill8c3988c2017-12-21 14:44:56 -0500894 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400895 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400896
897 // TODO(jmadill): Don't hard-code the texture limit.
898 ShaderTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
899 ShaderTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
900 uint32_t imageCount = 0;
901
902 const gl::State &glState = contextVk->getGLState();
903 const auto &completeTextures = glState.getCompleteTextureCache();
904
Jamie Madill858c1cc2018-03-31 14:19:13 -0400905 for (const gl::SamplerBinding &samplerBinding : mState.getSamplerBindings())
Jamie Madill5547b382017-10-23 18:16:01 -0400906 {
907 ASSERT(!samplerBinding.unreferenced);
908
909 // TODO(jmadill): Sampler arrays
910 ASSERT(samplerBinding.boundTextureUnits.size() == 1);
911
Luc Ferron90968362018-05-04 08:47:22 -0400912 GLuint textureUnit = samplerBinding.boundTextureUnits[0];
913 gl::Texture *texture = completeTextures[textureUnit];
Jamie Madill5547b382017-10-23 18:16:01 -0400914
Luc Ferron90968362018-05-04 08:47:22 -0400915 if (texture == nullptr)
916 {
917 // If we have an incomplete texture, fetch it from our renderer.
918 ANGLE_TRY(
919 contextVk->getIncompleteTexture(context, samplerBinding.textureType, &texture));
920 }
Jamie Madill5547b382017-10-23 18:16:01 -0400921
Jamie Madille1f3ad42017-10-28 23:00:42 -0400922 TextureVk *textureVk = vk::GetImpl(texture);
Jamie Madill858c1cc2018-03-31 14:19:13 -0400923 const vk::ImageHelper &image = textureVk->getImage();
Jamie Madill5547b382017-10-23 18:16:01 -0400924
925 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[imageCount];
926
927 imageInfo.sampler = textureVk->getSampler().getHandle();
928 imageInfo.imageView = textureVk->getImageView().getHandle();
929 imageInfo.imageLayout = image.getCurrentLayout();
930
931 auto &writeInfo = writeDescriptorInfo[imageCount];
932
933 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
934 writeInfo.pNext = nullptr;
935 writeInfo.dstSet = descriptorSet;
936 writeInfo.dstBinding = imageCount;
937 writeInfo.dstArrayElement = 0;
938 writeInfo.descriptorCount = 1;
939 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
940 writeInfo.pImageInfo = &imageInfo;
941 writeInfo.pBufferInfo = nullptr;
942 writeInfo.pTexelBufferView = nullptr;
943
944 imageCount++;
945 }
946
947 VkDevice device = contextVk->getDevice();
948
949 ASSERT(imageCount > 0);
950 vkUpdateDescriptorSets(device, imageCount, writeDescriptorInfo.data(), 0, nullptr);
951
952 mDirtyTextures = false;
Luc Ferron90968362018-05-04 08:47:22 -0400953 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400954}
955
956void ProgramVk::invalidateTextures()
957{
958 mDirtyTextures = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400959}
960
Jamie Madill9b168d02018-06-13 13:25:32 -0400961const vk::PipelineLayout &ProgramVk::getPipelineLayout() const
962{
963 return mPipelineLayout.get();
964}
965
Luc Ferron7a06ac12018-03-15 10:17:04 -0400966void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
967{
968 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
969 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400970 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400971 }
972}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400973} // namespace rx