blob: 42ee2d96fc92dabf23e54b020102ea2076d77b72 [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 VkDevice device = contextVk->getDevice();
172
Jamie Madill9b168d02018-06-13 13:25:32 -0400173 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
174 {
175 descriptorSetLayout.reset();
176 }
177 mPipelineLayout.reset();
178
Jamie Madill76e471e2017-10-21 09:56:01 -0400179 for (auto &uniformBlock : mDefaultUniformBlocks)
180 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400181 uniformBlock.storage.destroy(device);
Jamie Madill76e471e2017-10-21 09:56:01 -0400182 }
183
184 mEmptyUniformBlockStorage.memory.destroy(device);
185 mEmptyUniformBlockStorage.buffer.destroy(device);
186
Jamie Madill5deea722017-02-16 10:44:46 -0500187 mLinkedFragmentModule.destroy(device);
188 mLinkedVertexModule.destroy(device);
Jamie Madillf2f6d372018-01-10 21:37:23 -0500189 mVertexModuleSerial = Serial();
190 mFragmentModuleSerial = Serial();
Jamie Madill76e471e2017-10-21 09:56:01 -0400191
Jamie Madill5547b382017-10-23 18:16:01 -0400192 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500193 mUsedDescriptorSetRange.invalidate();
Jamie Madill50cf2be2018-06-15 09:46:57 -0400194 mDirtyTextures = false;
Jamie Madillb7d924a2018-03-10 11:16:54 -0500195
196 return vk::NoError();
Jamie Madill5deea722017-02-16 10:44:46 -0500197}
198
Jamie Madill9cf9e872017-06-05 12:59:25 -0400199gl::LinkResult ProgramVk::load(const gl::Context *contextImpl,
200 gl::InfoLog &infoLog,
201 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400202{
203 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500204 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400205}
206
Jamie Madill27a60632017-06-30 15:12:01 -0400207void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400208{
209 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400210}
211
212void ProgramVk::setBinaryRetrievableHint(bool retrievable)
213{
214 UNIMPLEMENTED();
215}
216
Yunchao He61afff12017-03-14 15:34:03 +0800217void ProgramVk::setSeparable(bool separable)
218{
219 UNIMPLEMENTED();
220}
221
Jamie Madill9cf9e872017-06-05 12:59:25 -0400222gl::LinkResult ProgramVk::link(const gl::Context *glContext,
Jamie Madillc9727f32017-11-07 12:37:07 -0500223 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400224 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400225{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400226 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madillc5143482017-10-15 20:20:06 -0400227 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500228 GlslangWrapper *glslangWrapper = renderer->getGlslangWrapper();
Jamie Madillc5143482017-10-15 20:20:06 -0400229 VkDevice device = renderer->getDevice();
230
Jamie Madillb7d924a2018-03-10 11:16:54 -0500231 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500232
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500233 std::vector<uint32_t> vertexCode;
234 std::vector<uint32_t> fragmentCode;
235 bool linkSuccess = false;
Luc Ferronc252d752018-06-14 09:32:40 -0400236 ANGLE_TRY_RESULT(glslangWrapper->linkProgram(glContext, mState, resources, glContext->getCaps(),
237 &vertexCode, &fragmentCode),
238 linkSuccess);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500239 if (!linkSuccess)
240 {
241 return false;
242 }
243
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500244 {
245 VkShaderModuleCreateInfo vertexShaderInfo;
246 vertexShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
247 vertexShaderInfo.pNext = nullptr;
248 vertexShaderInfo.flags = 0;
249 vertexShaderInfo.codeSize = vertexCode.size() * sizeof(uint32_t);
250 vertexShaderInfo.pCode = vertexCode.data();
Jamie Madillc5143482017-10-15 20:20:06 -0400251
252 ANGLE_TRY(mLinkedVertexModule.init(device, vertexShaderInfo));
Jamie Madill78feddc2018-04-27 11:45:05 -0400253 mVertexModuleSerial = renderer->issueShaderSerial();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500254 }
255
256 {
257 VkShaderModuleCreateInfo fragmentShaderInfo;
258 fragmentShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
259 fragmentShaderInfo.pNext = nullptr;
260 fragmentShaderInfo.flags = 0;
261 fragmentShaderInfo.codeSize = fragmentCode.size() * sizeof(uint32_t);
262 fragmentShaderInfo.pCode = fragmentCode.data();
263
Jamie Madillc5143482017-10-15 20:20:06 -0400264 ANGLE_TRY(mLinkedFragmentModule.init(device, fragmentShaderInfo));
Jamie Madill78feddc2018-04-27 11:45:05 -0400265 mFragmentModuleSerial = renderer->issueShaderSerial();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500266 }
267
Jamie Madill76e471e2017-10-21 09:56:01 -0400268 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500269
Jamie Madill8c3988c2017-12-21 14:44:56 -0500270 if (!mState.getSamplerUniformRange().empty())
271 {
272 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400273 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500274 mDirtyTextures = true;
275 }
276
Jamie Madill9b168d02018-06-13 13:25:32 -0400277 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
278 // don't already exist in the cache.
279 vk::DescriptorSetLayoutDesc uniformsSetDesc;
280 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
281 1);
282 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
283 1);
284
285 ANGLE_TRY(renderer->getDescriptorSetLayout(
286 uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
287
Jamie Madill9b168d02018-06-13 13:25:32 -0400288 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400289
290 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
291 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400292 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400293 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
294
295 // The front-end always binds array sampler units sequentially.
296 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
297 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400298 }
299
300 ANGLE_TRY(renderer->getDescriptorSetLayout(texturesSetDesc,
301 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
302
303 vk::PipelineLayoutDesc pipelineLayoutDesc;
304 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
305 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
306
307 ANGLE_TRY(
308 renderer->getPipelineLayout(pipelineLayoutDesc, mDescriptorSetLayouts, &mPipelineLayout));
309
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500310 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400311}
312
Jamie Madill76e471e2017-10-21 09:56:01 -0400313gl::Error ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
314{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400315 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500316 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400317 VkDevice device = contextVk->getDevice();
318
319 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400320 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
321 vk::ShaderMap<size_t> requiredBufferSize;
322 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400323
Jamie Madill33318de2018-05-01 11:22:54 -0400324 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400325 {
Jamie Madill33318de2018-05-01 11:22:54 -0400326 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
327 ANGLE_TRY(InitDefaultUniformBlock(glContext, mState.getAttachedShader(glShaderType),
328 &layoutMap[shaderType], &requiredBufferSize[shaderType]));
Jamie Madill76e471e2017-10-21 09:56:01 -0400329 }
330
331 // Init the default block layout info.
332 const auto &locations = mState.getUniformLocations();
333 const auto &uniforms = mState.getUniforms();
334 for (size_t locationIndex = 0; locationIndex < locations.size(); ++locationIndex)
335 {
Jamie Madill33318de2018-05-01 11:22:54 -0400336 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400337
338 const auto &location = locations[locationIndex];
339 if (location.used() && !location.ignored)
340 {
Jamie Madillde03e002017-10-21 14:04:20 -0400341 const auto &uniform = uniforms[location.index];
342
343 if (uniform.isSampler())
344 continue;
345
Jamie Madill76e471e2017-10-21 09:56:01 -0400346 std::string uniformName = uniform.name;
347 if (uniform.isArray())
348 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400349 // Gets the uniform name without the [0] at the end.
350 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400351 }
352
353 bool found = false;
354
Jamie Madill33318de2018-05-01 11:22:54 -0400355 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400356 {
Jamie Madill33318de2018-05-01 11:22:54 -0400357 auto it = layoutMap[shaderType].find(uniformName);
358 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400359 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400360 found = true;
361 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400362 }
363 }
364
365 ASSERT(found);
366 }
367
Jamie Madill33318de2018-05-01 11:22:54 -0400368 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400369 {
Jamie Madill33318de2018-05-01 11:22:54 -0400370 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400371 }
372 }
373
374 bool anyDirty = false;
375 bool allDirty = true;
376
Jamie Madill33318de2018-05-01 11:22:54 -0400377 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400378 {
Jamie Madill33318de2018-05-01 11:22:54 -0400379 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400380 {
Jamie Madill33318de2018-05-01 11:22:54 -0400381 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
382 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400383 {
384 return gl::OutOfMemory() << "Memory allocation failure.";
385 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400386 size_t minAlignment = static_cast<size_t>(
387 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
388
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400389 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400390
391 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400392 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
393 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400394
395 anyDirty = true;
396 }
397 else
398 {
399 allDirty = false;
400 }
401 }
402
403 if (anyDirty)
404 {
405 // Initialize the "empty" uniform block if necessary.
406 if (!allDirty)
407 {
408 VkBufferCreateInfo uniformBufferInfo;
409 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
410 uniformBufferInfo.pNext = nullptr;
411 uniformBufferInfo.flags = 0;
412 uniformBufferInfo.size = 1;
413 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
414 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
415 uniformBufferInfo.queueFamilyIndexCount = 0;
416 uniformBufferInfo.pQueueFamilyIndices = nullptr;
417
418 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(device, uniformBufferInfo));
419
Luc Ferron7a06ac12018-03-15 10:17:04 -0400420 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500421 VkMemoryPropertyFlags flags =
422 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill76e471e2017-10-21 09:56:01 -0400423 size_t requiredSize = 0;
Jamie Madill57fbfd82018-02-14 12:45:34 -0500424 ANGLE_TRY(AllocateBufferMemory(renderer, flags, &mEmptyUniformBlockStorage.buffer,
Jamie Madill76e471e2017-10-21 09:56:01 -0400425 &mEmptyUniformBlockStorage.memory, &requiredSize));
426 }
427
Jamie Madill8c3988c2017-12-21 14:44:56 -0500428 // Ensure the descriptor set range includes the uniform buffers at position 0.
429 mUsedDescriptorSetRange.extend(0);
Jamie Madill5547b382017-10-23 18:16:01 -0400430 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400431
432 return gl::NoError();
433}
434
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400435GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
436{
Luc Ferronfba1f612018-06-04 14:37:17 -0400437 // No-op. The spec is very vague about the behavior of validation.
438 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400439}
440
Jamie Madill76e471e2017-10-21 09:56:01 -0400441template <typename T>
442void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
443{
444 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
445 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
446
Luc Ferron7cec3352018-03-13 13:29:34 -0400447 if (linkedUniform.isSampler())
448 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400449 // We could potentially cache some indexing here. For now this is a no-op since the mapping
450 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400451 return;
452 }
453
Luc Ferron24a31372018-04-04 11:49:14 -0400454 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400455 {
Luc Ferron24a31372018-04-04 11:49:14 -0400456 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400457 {
Luc Ferron24a31372018-04-04 11:49:14 -0400458 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400459
Luc Ferron24a31372018-04-04 11:49:14 -0400460 // Assume an offset of -1 means the block is unused.
461 if (layoutInfo.offset == -1)
462 {
463 continue;
464 }
465
466 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400467 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
468 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400469 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400470 }
Luc Ferron24a31372018-04-04 11:49:14 -0400471 }
472 else
473 {
474 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400475 {
Luc Ferron24a31372018-04-04 11:49:14 -0400476 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
477
478 // Assume an offset of -1 means the block is unused.
479 if (layoutInfo.offset == -1)
480 {
481 continue;
482 }
483
484 const GLint componentCount = linkedUniform.typeInfo->componentCount;
485
Luc Ferron62059a52018-03-29 07:01:35 -0400486 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
487
Luc Ferrond91c3792018-04-06 09:36:36 -0400488 GLint initialArrayOffset =
489 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400490 for (GLint i = 0; i < count; i++)
491 {
492 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
493 GLint *dest =
494 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
495 const T *source = v + i * componentCount;
496
497 for (int c = 0; c < componentCount; c++)
498 {
499 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
500 }
501 }
Luc Ferron24a31372018-04-04 11:49:14 -0400502 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400503 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400504 }
505}
506
Luc Ferron7cec3352018-03-13 13:29:34 -0400507template <typename T>
508void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
509{
510 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
511 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
512
513 if (linkedUniform.isSampler())
514 {
515 UNIMPLEMENTED();
516 return;
517 }
518
Olli Etuaho107c7242018-03-20 15:45:35 +0200519 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800520 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400521
Jiawei Shao385b3e02018-03-21 09:43:28 +0800522 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400523 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400524 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400525
526 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
527 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400528
529 if (gl::IsMatrixType(linkedUniform.type))
530 {
531 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400532 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400533 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
534 }
535 else
536 {
537 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
538 v, layoutInfo, &uniformBlock.uniformData);
539 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400540}
541
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400542void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
543{
Jamie Madill76e471e2017-10-21 09:56:01 -0400544 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400545}
546
547void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
548{
Jamie Madill76e471e2017-10-21 09:56:01 -0400549 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400550}
551
552void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
553{
Jamie Madill76e471e2017-10-21 09:56:01 -0400554 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400555}
556
557void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
558{
Jamie Madill76e471e2017-10-21 09:56:01 -0400559 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400560}
561
562void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
563{
Luc Ferron7cec3352018-03-13 13:29:34 -0400564 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400565}
566
567void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
568{
Luc Ferron489243f2018-03-28 16:55:28 -0400569 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400570}
571
572void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
573{
Luc Ferron489243f2018-03-28 16:55:28 -0400574 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400575}
576
577void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
578{
Luc Ferron489243f2018-03-28 16:55:28 -0400579 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400580}
581
582void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
583{
584 UNIMPLEMENTED();
585}
586
587void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
588{
589 UNIMPLEMENTED();
590}
591
592void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
593{
594 UNIMPLEMENTED();
595}
596
597void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
598{
599 UNIMPLEMENTED();
600}
601
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400602template <int cols, int rows>
603void ProgramVk::setUniformMatrixfv(GLint location,
604 GLsizei count,
605 GLboolean transpose,
606 const GLfloat *value)
607{
608 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
609 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
610
611 for (auto &uniformBlock : mDefaultUniformBlocks)
612 {
613 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
614
615 // Assume an offset of -1 means the block is unused.
616 if (layoutInfo.offset == -1)
617 {
618 continue;
619 }
620
Luc Ferronc8fbff32018-06-04 10:30:48 -0400621 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400622 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
623 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400624
625 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
626 // setter did not update any data. We still want the uniform to be included when we'll
627 // update the descriptor sets.
628 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400629 }
630}
631
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400632void ProgramVk::setUniformMatrix2fv(GLint location,
633 GLsizei count,
634 GLboolean transpose,
635 const GLfloat *value)
636{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400637 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400638}
639
640void ProgramVk::setUniformMatrix3fv(GLint location,
641 GLsizei count,
642 GLboolean transpose,
643 const GLfloat *value)
644{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400645 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400646}
647
648void ProgramVk::setUniformMatrix4fv(GLint location,
649 GLsizei count,
650 GLboolean transpose,
651 const GLfloat *value)
652{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400653 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400654}
655
656void ProgramVk::setUniformMatrix2x3fv(GLint location,
657 GLsizei count,
658 GLboolean transpose,
659 const GLfloat *value)
660{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400661 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400662}
663
664void ProgramVk::setUniformMatrix3x2fv(GLint location,
665 GLsizei count,
666 GLboolean transpose,
667 const GLfloat *value)
668{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400669 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400670}
671
672void ProgramVk::setUniformMatrix2x4fv(GLint location,
673 GLsizei count,
674 GLboolean transpose,
675 const GLfloat *value)
676{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400677 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400678}
679
680void ProgramVk::setUniformMatrix4x2fv(GLint location,
681 GLsizei count,
682 GLboolean transpose,
683 const GLfloat *value)
684{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400685 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400686}
687
688void ProgramVk::setUniformMatrix3x4fv(GLint location,
689 GLsizei count,
690 GLboolean transpose,
691 const GLfloat *value)
692{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400693 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400694}
695
696void ProgramVk::setUniformMatrix4x3fv(GLint location,
697 GLsizei count,
698 GLboolean transpose,
699 const GLfloat *value)
700{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400701 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400702}
703
704void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
705{
706 UNIMPLEMENTED();
707}
708
Sami Väisänen46eaa942016-06-29 10:26:37 +0300709void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
710 GLenum genMode,
711 GLint components,
712 const GLfloat *coeffs)
713{
714 UNIMPLEMENTED();
715}
716
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500717const vk::ShaderModule &ProgramVk::getLinkedVertexModule() const
718{
719 ASSERT(mLinkedVertexModule.getHandle() != VK_NULL_HANDLE);
720 return mLinkedVertexModule;
721}
722
Jamie Madillf2f6d372018-01-10 21:37:23 -0500723Serial ProgramVk::getVertexModuleSerial() const
724{
725 return mVertexModuleSerial;
726}
727
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500728const vk::ShaderModule &ProgramVk::getLinkedFragmentModule() const
729{
730 ASSERT(mLinkedFragmentModule.getHandle() != VK_NULL_HANDLE);
731 return mLinkedFragmentModule;
732}
733
Jamie Madillf2f6d372018-01-10 21:37:23 -0500734Serial ProgramVk::getFragmentModuleSerial() const
735{
736 return mFragmentModuleSerial;
737}
738
Luc Ferron6ea1b412018-03-21 16:13:01 -0400739vk::Error ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400740{
Jamie Madill76e471e2017-10-21 09:56:01 -0400741 // Write out to a new a descriptor set.
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400742 vk::DynamicDescriptorPool *dynamicDescriptorPool = contextVk->getDynamicDescriptorPool();
Jamie Madill76e471e2017-10-21 09:56:01 -0400743
Luc Ferron6ea1b412018-03-21 16:13:01 -0400744 uint32_t potentialNewCount = descriptorSetIndex + 1;
745 if (potentialNewCount > mDescriptorSets.size())
746 {
747 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
748 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400749
Jamie Madillc7918ce2018-06-13 13:25:31 -0400750 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400751 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400752 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
753 descriptorSetIndex,
754 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill76e471e2017-10-21 09:56:01 -0400755 return vk::NoError();
756}
757
Jamie Madill54164b02017-08-28 15:17:37 -0400758void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
759{
Luc Ferron7cec3352018-03-13 13:29:34 -0400760 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400761}
762
763void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
764{
Luc Ferron7cec3352018-03-13 13:29:34 -0400765 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400766}
767
768void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
769{
770 UNIMPLEMENTED();
771}
772
Jamie Madill76e471e2017-10-21 09:56:01 -0400773vk::Error ProgramVk::updateUniforms(ContextVk *contextVk)
774{
Jamie Madill33318de2018-05-01 11:22:54 -0400775 if (!mDefaultUniformBlocks[vk::ShaderType::VertexShader].uniformsDirty &&
776 !mDefaultUniformBlocks[vk::ShaderType::FragmentShader].uniformsDirty)
Jamie Madill76e471e2017-10-21 09:56:01 -0400777 {
778 return vk::NoError();
779 }
780
Jamie Madill8c3988c2017-12-21 14:44:56 -0500781 ASSERT(mUsedDescriptorSetRange.contains(0));
Jamie Madill5547b382017-10-23 18:16:01 -0400782
Jamie Madill76e471e2017-10-21 09:56:01 -0400783 // Update buffer memory by immediate mapping. This immediate update only works once.
784 // TODO(jmadill): Handle inserting updates into the command stream, or use dynamic buffers.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400785 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400786 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400787 {
Jamie Madill33318de2018-05-01 11:22:54 -0400788 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400789
Jamie Madill76e471e2017-10-21 09:56:01 -0400790 if (uniformBlock.uniformsDirty)
791 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400792 bool bufferModified = false;
Jamie Madillc3755fc2018-04-05 08:39:13 -0400793 ANGLE_TRY(SyncDefaultUniformBlock(contextVk->getRenderer(), &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400794 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400795 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400796 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400797
798 if (bufferModified)
799 {
800 anyNewBufferAllocated = true;
801 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400802 }
803 }
804
Luc Ferron7a06ac12018-03-15 10:17:04 -0400805 if (anyNewBufferAllocated)
806 {
807 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
808 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400809 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400810 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
811 }
812
Jamie Madill76e471e2017-10-21 09:56:01 -0400813 return vk::NoError();
814}
815
816vk::Error ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
817{
Jamie Madill33318de2018-05-01 11:22:54 -0400818 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
819 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400820
Jamie Madill33318de2018-05-01 11:22:54 -0400821 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400822 {
Jamie Madill33318de2018-05-01 11:22:54 -0400823 auto &uniformBlock = mDefaultUniformBlocks[shaderType];
824 auto &bufferInfo = descriptorBufferInfo[shaderType];
825 auto &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400826
827 if (!uniformBlock.uniformData.empty())
828 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400829 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400830 }
831 else
832 {
833 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
834 }
835
836 bufferInfo.offset = 0;
837 bufferInfo.range = VK_WHOLE_SIZE;
838
Jamie Madill76e471e2017-10-21 09:56:01 -0400839 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
840 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400841 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400842 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400843 writeInfo.dstArrayElement = 0;
844 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400845 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400846 writeInfo.pImageInfo = nullptr;
847 writeInfo.pBufferInfo = &bufferInfo;
848 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400849 }
850
851 VkDevice device = contextVk->getDevice();
852
Jamie Madill33318de2018-05-01 11:22:54 -0400853 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400854
855 return vk::NoError();
856}
857
Jamie Madill5547b382017-10-23 18:16:01 -0400858const std::vector<VkDescriptorSet> &ProgramVk::getDescriptorSets() const
Jamie Madill76e471e2017-10-21 09:56:01 -0400859{
Jamie Madill5547b382017-10-23 18:16:01 -0400860 return mDescriptorSets;
861}
862
Luc Ferron7a06ac12018-03-15 10:17:04 -0400863const uint32_t *ProgramVk::getDynamicOffsets()
864{
865 // If we have no descriptor set being used, we do not need to specify any offsets when binding
866 // the descriptor sets.
867 if (!mUsedDescriptorSetRange.contains(0))
868 return nullptr;
869
870 return mUniformBlocksOffsets.data();
871}
872
873uint32_t ProgramVk::getDynamicOffsetsCount()
874{
875 if (!mUsedDescriptorSetRange.contains(0))
876 return 0;
877
878 return static_cast<uint32_t>(mUniformBlocksOffsets.size());
879}
880
Jamie Madill8c3988c2017-12-21 14:44:56 -0500881const gl::RangeUI &ProgramVk::getUsedDescriptorSetRange() const
Jamie Madill5547b382017-10-23 18:16:01 -0400882{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500883 return mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400884}
885
Luc Ferron90968362018-05-04 08:47:22 -0400886gl::Error ProgramVk::updateTexturesDescriptorSet(const gl::Context *context)
Jamie Madill5547b382017-10-23 18:16:01 -0400887{
888 if (mState.getSamplerBindings().empty() || !mDirtyTextures)
889 {
Luc Ferron90968362018-05-04 08:47:22 -0400890 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400891 }
892
Luc Ferron90968362018-05-04 08:47:22 -0400893 ContextVk *contextVk = GetImplAs<ContextVk>(context);
Jamie Madillc7918ce2018-06-13 13:25:31 -0400894 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400895
Jamie Madill8c3988c2017-12-21 14:44:56 -0500896 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400897 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400898
899 // TODO(jmadill): Don't hard-code the texture limit.
900 ShaderTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
901 ShaderTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400902 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400903
904 const gl::State &glState = contextVk->getGLState();
905 const auto &completeTextures = glState.getCompleteTextureCache();
906
Jamie Madill4cc753e2018-06-13 13:25:33 -0400907 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
908 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400909 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400910 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
911
Jamie Madill5547b382017-10-23 18:16:01 -0400912 ASSERT(!samplerBinding.unreferenced);
913
Jamie Madill4cc753e2018-06-13 13:25:33 -0400914 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
915 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400916 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400917 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
918 gl::Texture *texture = completeTextures[textureUnit];
919
920 if (texture == nullptr)
921 {
922 // If we have an incomplete texture, fetch it from our renderer.
923 ANGLE_TRY(
924 contextVk->getIncompleteTexture(context, samplerBinding.textureType, &texture));
925 }
926
927 TextureVk *textureVk = vk::GetImpl(texture);
928 const vk::ImageHelper &image = textureVk->getImage();
929
930 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
931
932 imageInfo.sampler = textureVk->getSampler().getHandle();
933 imageInfo.imageView = textureVk->getImageView().getHandle();
934 imageInfo.imageLayout = image.getCurrentLayout();
935
936 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
937
938 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
939 writeInfo.pNext = nullptr;
940 writeInfo.dstSet = descriptorSet;
941 writeInfo.dstBinding = textureIndex;
942 writeInfo.dstArrayElement = arrayElement;
943 writeInfo.descriptorCount = 1;
944 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
945 writeInfo.pImageInfo = &imageInfo;
946 writeInfo.pBufferInfo = nullptr;
947 writeInfo.pTexelBufferView = nullptr;
948
949 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400950 }
Jamie Madill5547b382017-10-23 18:16:01 -0400951 }
952
953 VkDevice device = contextVk->getDevice();
954
Jamie Madill4cc753e2018-06-13 13:25:33 -0400955 ASSERT(writeCount > 0);
956 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400957
958 mDirtyTextures = false;
Luc Ferron90968362018-05-04 08:47:22 -0400959 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400960}
961
962void ProgramVk::invalidateTextures()
963{
964 mDirtyTextures = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400965}
966
Jamie Madill9b168d02018-06-13 13:25:32 -0400967const vk::PipelineLayout &ProgramVk::getPipelineLayout() const
968{
969 return mPipelineLayout.get();
970}
971
Luc Ferron7a06ac12018-03-15 10:17:04 -0400972void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
973{
974 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
975 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400976 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400977 }
978}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400979} // namespace rx