blob: b30d58a5f7d6b88cfc4cf7b21653cb417b88abc4 [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 Madill21061022018-07-12 23:56:30 -040029void InitDefaultUniformBlock(const gl::Context *context,
30 gl::Shader *shader,
31 sh::BlockLayoutMap *blockLayoutMapOut,
32 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 Madill21061022018-07-12 23:56:30 -040039 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040040 }
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 Madill21061022018-07-12 23:56:30 -040051 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040052 }
53
Luc Ferron7a06ac12018-03-15 10:17:04 -040054 *blockSizeOut = blockSize;
Jamie Madill21061022018-07-12 23:56:30 -040055 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040056}
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 Madill21061022018-07-12 23:56:30 -0400116angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
117 vk::DynamicBuffer *dynamicBuffer,
118 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 Madill21061022018-07-12 23:56:30 -0400126 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400127 outBufferModified));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400128 *outOffset = offset;
129 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400130 ANGLE_TRY(dynamicBuffer->flush(contextVk));
131 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400132}
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 Madill21061022018-07-12 23:56:30 -0400169angle::Result 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 Madillcaaff162018-06-22 08:55:37 -0400179 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400180 for (auto &uniformBlock : mDefaultUniformBlocks)
181 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400182 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400183 }
184
Jamie Madillcaaff162018-06-22 08:55:37 -0400185 Serial currentSerial = renderer->getCurrentQueueSerial();
186 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
187 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400188
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
Jamie Madill21061022018-07-12 23:56:30 -0400198 return angle::Result::Continue();
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
Jamie Madillb7d924a2018-03-10 11:16:54 -0500232 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500233
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500234 std::vector<uint32_t> vertexCode;
235 std::vector<uint32_t> fragmentCode;
236 bool linkSuccess = false;
Luc Ferronc252d752018-06-14 09:32:40 -0400237 ANGLE_TRY_RESULT(glslangWrapper->linkProgram(glContext, mState, resources, glContext->getCaps(),
238 &vertexCode, &fragmentCode),
239 linkSuccess);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500240 if (!linkSuccess)
241 {
242 return false;
243 }
244
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500245 {
246 VkShaderModuleCreateInfo vertexShaderInfo;
247 vertexShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
248 vertexShaderInfo.pNext = nullptr;
249 vertexShaderInfo.flags = 0;
250 vertexShaderInfo.codeSize = vertexCode.size() * sizeof(uint32_t);
251 vertexShaderInfo.pCode = vertexCode.data();
Jamie Madillc5143482017-10-15 20:20:06 -0400252
Jamie Madill21061022018-07-12 23:56:30 -0400253 ANGLE_TRY(mLinkedVertexModule.init(contextVk, vertexShaderInfo));
Jamie Madill78feddc2018-04-27 11:45:05 -0400254 mVertexModuleSerial = renderer->issueShaderSerial();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500255 }
256
257 {
258 VkShaderModuleCreateInfo fragmentShaderInfo;
259 fragmentShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
260 fragmentShaderInfo.pNext = nullptr;
261 fragmentShaderInfo.flags = 0;
262 fragmentShaderInfo.codeSize = fragmentCode.size() * sizeof(uint32_t);
263 fragmentShaderInfo.pCode = fragmentCode.data();
264
Jamie Madill21061022018-07-12 23:56:30 -0400265 ANGLE_TRY(mLinkedFragmentModule.init(contextVk, fragmentShaderInfo));
Jamie Madill78feddc2018-04-27 11:45:05 -0400266 mFragmentModuleSerial = renderer->issueShaderSerial();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500267 }
268
Jamie Madill76e471e2017-10-21 09:56:01 -0400269 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500270
Jamie Madill8c3988c2017-12-21 14:44:56 -0500271 if (!mState.getSamplerUniformRange().empty())
272 {
273 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400274 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500275 mDirtyTextures = true;
276 }
277
Jamie Madill9b168d02018-06-13 13:25:32 -0400278 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
279 // don't already exist in the cache.
280 vk::DescriptorSetLayoutDesc uniformsSetDesc;
281 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
282 1);
283 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
284 1);
285
286 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400287 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400288
Jamie Madill9b168d02018-06-13 13:25:32 -0400289 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400290
291 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
292 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400293 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400294 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
295
296 // The front-end always binds array sampler units sequentially.
297 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
298 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400299 }
300
Jamie Madill21061022018-07-12 23:56:30 -0400301 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400302 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
303
Jamie Madillb01b4802018-07-10 12:43:57 -0400304 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
305 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
306 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400307 contextVk, driverUniformsSetDesc,
308 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400309
Jamie Madill9b168d02018-06-13 13:25:32 -0400310 vk::PipelineLayoutDesc pipelineLayoutDesc;
311 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
312 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400313 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
314 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400315
Jamie Madill21061022018-07-12 23:56:30 -0400316 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
317 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400318
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500319 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400320}
321
Jamie Madill76e471e2017-10-21 09:56:01 -0400322gl::Error ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
323{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400324 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500325 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400326
327 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400328 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
329 vk::ShaderMap<size_t> requiredBufferSize;
330 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400331
Jamie Madill33318de2018-05-01 11:22:54 -0400332 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400333 {
Jamie Madill33318de2018-05-01 11:22:54 -0400334 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill21061022018-07-12 23:56:30 -0400335 InitDefaultUniformBlock(glContext, mState.getAttachedShader(glShaderType),
336 &layoutMap[shaderType], &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400337 }
338
339 // Init the default block layout info.
340 const auto &locations = mState.getUniformLocations();
341 const auto &uniforms = mState.getUniforms();
342 for (size_t locationIndex = 0; locationIndex < locations.size(); ++locationIndex)
343 {
Jamie Madill33318de2018-05-01 11:22:54 -0400344 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400345
346 const auto &location = locations[locationIndex];
347 if (location.used() && !location.ignored)
348 {
Jamie Madillde03e002017-10-21 14:04:20 -0400349 const auto &uniform = uniforms[location.index];
350
351 if (uniform.isSampler())
352 continue;
353
Jamie Madill76e471e2017-10-21 09:56:01 -0400354 std::string uniformName = uniform.name;
355 if (uniform.isArray())
356 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400357 // Gets the uniform name without the [0] at the end.
358 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400359 }
360
361 bool found = false;
362
Jamie Madill33318de2018-05-01 11:22:54 -0400363 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 {
Jamie Madill33318de2018-05-01 11:22:54 -0400365 auto it = layoutMap[shaderType].find(uniformName);
366 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400367 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400368 found = true;
369 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400370 }
371 }
372
373 ASSERT(found);
374 }
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 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400379 }
380 }
381
382 bool anyDirty = false;
383 bool allDirty = true;
384
Jamie Madill33318de2018-05-01 11:22:54 -0400385 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400386 {
Jamie Madill33318de2018-05-01 11:22:54 -0400387 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400388 {
Jamie Madill33318de2018-05-01 11:22:54 -0400389 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
390 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400391 {
392 return gl::OutOfMemory() << "Memory allocation failure.";
393 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400394 size_t minAlignment = static_cast<size_t>(
395 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
396
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400397 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400398
399 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400400 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
401 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400402
403 anyDirty = true;
404 }
405 else
406 {
407 allDirty = false;
408 }
409 }
410
411 if (anyDirty)
412 {
413 // Initialize the "empty" uniform block if necessary.
414 if (!allDirty)
415 {
416 VkBufferCreateInfo uniformBufferInfo;
417 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
418 uniformBufferInfo.pNext = nullptr;
419 uniformBufferInfo.flags = 0;
420 uniformBufferInfo.size = 1;
421 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
422 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
423 uniformBufferInfo.queueFamilyIndexCount = 0;
424 uniformBufferInfo.pQueueFamilyIndices = nullptr;
425
Jamie Madill21061022018-07-12 23:56:30 -0400426 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400427
Luc Ferron7a06ac12018-03-15 10:17:04 -0400428 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500429 VkMemoryPropertyFlags flags =
430 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill21061022018-07-12 23:56:30 -0400431 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400432 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400433 }
434
Jamie Madill8c3988c2017-12-21 14:44:56 -0500435 // Ensure the descriptor set range includes the uniform buffers at position 0.
436 mUsedDescriptorSetRange.extend(0);
Jamie Madill5547b382017-10-23 18:16:01 -0400437 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400438
439 return gl::NoError();
440}
441
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400442GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
443{
Luc Ferronfba1f612018-06-04 14:37:17 -0400444 // No-op. The spec is very vague about the behavior of validation.
445 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400446}
447
Jamie Madill76e471e2017-10-21 09:56:01 -0400448template <typename T>
449void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
450{
451 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
452 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
453
Luc Ferron7cec3352018-03-13 13:29:34 -0400454 if (linkedUniform.isSampler())
455 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400456 // We could potentially cache some indexing here. For now this is a no-op since the mapping
457 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400458 return;
459 }
460
Luc Ferron24a31372018-04-04 11:49:14 -0400461 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400462 {
Luc Ferron24a31372018-04-04 11:49:14 -0400463 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400464 {
Luc Ferron24a31372018-04-04 11:49:14 -0400465 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400466
Luc Ferron24a31372018-04-04 11:49:14 -0400467 // Assume an offset of -1 means the block is unused.
468 if (layoutInfo.offset == -1)
469 {
470 continue;
471 }
472
473 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400474 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
475 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400476 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400477 }
Luc Ferron24a31372018-04-04 11:49:14 -0400478 }
479 else
480 {
481 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400482 {
Luc Ferron24a31372018-04-04 11:49:14 -0400483 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
484
485 // Assume an offset of -1 means the block is unused.
486 if (layoutInfo.offset == -1)
487 {
488 continue;
489 }
490
491 const GLint componentCount = linkedUniform.typeInfo->componentCount;
492
Luc Ferron62059a52018-03-29 07:01:35 -0400493 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
494
Luc Ferrond91c3792018-04-06 09:36:36 -0400495 GLint initialArrayOffset =
496 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400497 for (GLint i = 0; i < count; i++)
498 {
499 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
500 GLint *dest =
501 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
502 const T *source = v + i * componentCount;
503
504 for (int c = 0; c < componentCount; c++)
505 {
506 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
507 }
508 }
Luc Ferron24a31372018-04-04 11:49:14 -0400509 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400510 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400511 }
512}
513
Luc Ferron7cec3352018-03-13 13:29:34 -0400514template <typename T>
515void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
516{
517 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
518 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
519
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400520 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400521
Olli Etuaho107c7242018-03-20 15:45:35 +0200522 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800523 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400524
Jiawei Shao385b3e02018-03-21 09:43:28 +0800525 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400526 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400527 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400528
529 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
530 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400531
532 if (gl::IsMatrixType(linkedUniform.type))
533 {
534 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400535 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400536 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
537 }
538 else
539 {
540 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
541 v, layoutInfo, &uniformBlock.uniformData);
542 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400543}
544
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400545void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
546{
Jamie Madill76e471e2017-10-21 09:56:01 -0400547 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400548}
549
550void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
551{
Jamie Madill76e471e2017-10-21 09:56:01 -0400552 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400553}
554
555void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
556{
Jamie Madill76e471e2017-10-21 09:56:01 -0400557 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400558}
559
560void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
561{
Jamie Madill76e471e2017-10-21 09:56:01 -0400562 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400563}
564
565void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
566{
Luc Ferron7cec3352018-03-13 13:29:34 -0400567 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400568}
569
570void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
571{
Luc Ferron489243f2018-03-28 16:55:28 -0400572 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400573}
574
575void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
576{
Luc Ferron489243f2018-03-28 16:55:28 -0400577 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400578}
579
580void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
581{
Luc Ferron489243f2018-03-28 16:55:28 -0400582 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400583}
584
585void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
586{
587 UNIMPLEMENTED();
588}
589
590void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
591{
592 UNIMPLEMENTED();
593}
594
595void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
596{
597 UNIMPLEMENTED();
598}
599
600void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
601{
602 UNIMPLEMENTED();
603}
604
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400605template <int cols, int rows>
606void ProgramVk::setUniformMatrixfv(GLint location,
607 GLsizei count,
608 GLboolean transpose,
609 const GLfloat *value)
610{
611 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
612 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
613
614 for (auto &uniformBlock : mDefaultUniformBlocks)
615 {
616 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
617
618 // Assume an offset of -1 means the block is unused.
619 if (layoutInfo.offset == -1)
620 {
621 continue;
622 }
623
Luc Ferronc8fbff32018-06-04 10:30:48 -0400624 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400625 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
626 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400627
628 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
629 // setter did not update any data. We still want the uniform to be included when we'll
630 // update the descriptor sets.
631 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400632 }
633}
634
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400635void ProgramVk::setUniformMatrix2fv(GLint location,
636 GLsizei count,
637 GLboolean transpose,
638 const GLfloat *value)
639{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400640 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400641}
642
643void ProgramVk::setUniformMatrix3fv(GLint location,
644 GLsizei count,
645 GLboolean transpose,
646 const GLfloat *value)
647{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400648 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400649}
650
651void ProgramVk::setUniformMatrix4fv(GLint location,
652 GLsizei count,
653 GLboolean transpose,
654 const GLfloat *value)
655{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400656 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400657}
658
659void ProgramVk::setUniformMatrix2x3fv(GLint location,
660 GLsizei count,
661 GLboolean transpose,
662 const GLfloat *value)
663{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400664 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400665}
666
667void ProgramVk::setUniformMatrix3x2fv(GLint location,
668 GLsizei count,
669 GLboolean transpose,
670 const GLfloat *value)
671{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400672 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400673}
674
675void ProgramVk::setUniformMatrix2x4fv(GLint location,
676 GLsizei count,
677 GLboolean transpose,
678 const GLfloat *value)
679{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400680 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400681}
682
683void ProgramVk::setUniformMatrix4x2fv(GLint location,
684 GLsizei count,
685 GLboolean transpose,
686 const GLfloat *value)
687{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400688 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400689}
690
691void ProgramVk::setUniformMatrix3x4fv(GLint location,
692 GLsizei count,
693 GLboolean transpose,
694 const GLfloat *value)
695{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400696 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400697}
698
699void ProgramVk::setUniformMatrix4x3fv(GLint location,
700 GLsizei count,
701 GLboolean transpose,
702 const GLfloat *value)
703{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400704 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400705}
706
707void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
708{
709 UNIMPLEMENTED();
710}
711
Sami Väisänen46eaa942016-06-29 10:26:37 +0300712void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
713 GLenum genMode,
714 GLint components,
715 const GLfloat *coeffs)
716{
717 UNIMPLEMENTED();
718}
719
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500720const vk::ShaderModule &ProgramVk::getLinkedVertexModule() const
721{
722 ASSERT(mLinkedVertexModule.getHandle() != VK_NULL_HANDLE);
723 return mLinkedVertexModule;
724}
725
Jamie Madillf2f6d372018-01-10 21:37:23 -0500726Serial ProgramVk::getVertexModuleSerial() const
727{
728 return mVertexModuleSerial;
729}
730
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500731const vk::ShaderModule &ProgramVk::getLinkedFragmentModule() const
732{
733 ASSERT(mLinkedFragmentModule.getHandle() != VK_NULL_HANDLE);
734 return mLinkedFragmentModule;
735}
736
Jamie Madillf2f6d372018-01-10 21:37:23 -0500737Serial ProgramVk::getFragmentModuleSerial() const
738{
739 return mFragmentModuleSerial;
740}
741
Jamie Madill21061022018-07-12 23:56:30 -0400742angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400743{
Jamie Madill76e471e2017-10-21 09:56:01 -0400744 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400745 vk::DynamicDescriptorPool *dynamicDescriptorPool =
746 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400747
Luc Ferron6ea1b412018-03-21 16:13:01 -0400748 uint32_t potentialNewCount = descriptorSetIndex + 1;
749 if (potentialNewCount > mDescriptorSets.size())
750 {
751 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
752 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400753
Jamie Madillc7918ce2018-06-13 13:25:31 -0400754 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400755 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400756 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400757 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400758 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400759}
760
Jamie Madill54164b02017-08-28 15:17:37 -0400761void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
762{
Luc Ferron7cec3352018-03-13 13:29:34 -0400763 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400764}
765
766void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
767{
Luc Ferron7cec3352018-03-13 13:29:34 -0400768 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400769}
770
771void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
772{
773 UNIMPLEMENTED();
774}
775
Jamie Madill21061022018-07-12 23:56:30 -0400776angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400777{
Jamie Madill33318de2018-05-01 11:22:54 -0400778 if (!mDefaultUniformBlocks[vk::ShaderType::VertexShader].uniformsDirty &&
779 !mDefaultUniformBlocks[vk::ShaderType::FragmentShader].uniformsDirty)
Jamie Madill76e471e2017-10-21 09:56:01 -0400780 {
Jamie Madill21061022018-07-12 23:56:30 -0400781 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400782 }
783
Jamie Madill8c3988c2017-12-21 14:44:56 -0500784 ASSERT(mUsedDescriptorSetRange.contains(0));
Jamie Madill5547b382017-10-23 18:16:01 -0400785
Jamie Madill76e471e2017-10-21 09:56:01 -0400786 // Update buffer memory by immediate mapping. This immediate update only works once.
787 // TODO(jmadill): Handle inserting updates into the command stream, or use dynamic buffers.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400788 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400789 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400790 {
Jamie Madill33318de2018-05-01 11:22:54 -0400791 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400792
Jamie Madill76e471e2017-10-21 09:56:01 -0400793 if (uniformBlock.uniformsDirty)
794 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400795 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400796 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400797 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400798 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400799 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400800
801 if (bufferModified)
802 {
803 anyNewBufferAllocated = true;
804 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400805 }
806 }
807
Luc Ferron7a06ac12018-03-15 10:17:04 -0400808 if (anyNewBufferAllocated)
809 {
810 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
811 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400812 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400813 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
814 }
815
Jamie Madill21061022018-07-12 23:56:30 -0400816 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400817}
818
Jamie Madill21061022018-07-12 23:56:30 -0400819angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400820{
Jamie Madill33318de2018-05-01 11:22:54 -0400821 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
822 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400823
Jamie Madill33318de2018-05-01 11:22:54 -0400824 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400825 {
Jamie Madill33318de2018-05-01 11:22:54 -0400826 auto &uniformBlock = mDefaultUniformBlocks[shaderType];
827 auto &bufferInfo = descriptorBufferInfo[shaderType];
828 auto &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400829
830 if (!uniformBlock.uniformData.empty())
831 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400832 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400833 }
834 else
835 {
836 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
837 }
838
839 bufferInfo.offset = 0;
840 bufferInfo.range = VK_WHOLE_SIZE;
841
Jamie Madill76e471e2017-10-21 09:56:01 -0400842 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
843 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400844 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400845 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400846 writeInfo.dstArrayElement = 0;
847 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400848 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400849 writeInfo.pImageInfo = nullptr;
850 writeInfo.pBufferInfo = &bufferInfo;
851 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400852 }
853
854 VkDevice device = contextVk->getDevice();
855
Jamie Madill33318de2018-05-01 11:22:54 -0400856 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400857
Jamie Madill21061022018-07-12 23:56:30 -0400858 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400859}
860
Jamie Madill5547b382017-10-23 18:16:01 -0400861const std::vector<VkDescriptorSet> &ProgramVk::getDescriptorSets() const
Jamie Madill76e471e2017-10-21 09:56:01 -0400862{
Jamie Madill5547b382017-10-23 18:16:01 -0400863 return mDescriptorSets;
864}
865
Luc Ferron7a06ac12018-03-15 10:17:04 -0400866const uint32_t *ProgramVk::getDynamicOffsets()
867{
868 // If we have no descriptor set being used, we do not need to specify any offsets when binding
869 // the descriptor sets.
870 if (!mUsedDescriptorSetRange.contains(0))
871 return nullptr;
872
873 return mUniformBlocksOffsets.data();
874}
875
876uint32_t ProgramVk::getDynamicOffsetsCount()
877{
878 if (!mUsedDescriptorSetRange.contains(0))
879 return 0;
880
881 return static_cast<uint32_t>(mUniformBlocksOffsets.size());
882}
883
Jamie Madill8c3988c2017-12-21 14:44:56 -0500884const gl::RangeUI &ProgramVk::getUsedDescriptorSetRange() const
Jamie Madill5547b382017-10-23 18:16:01 -0400885{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500886 return mUsedDescriptorSetRange;
Jamie Madill5547b382017-10-23 18:16:01 -0400887}
888
Luc Ferron90968362018-05-04 08:47:22 -0400889gl::Error ProgramVk::updateTexturesDescriptorSet(const gl::Context *context)
Jamie Madill5547b382017-10-23 18:16:01 -0400890{
891 if (mState.getSamplerBindings().empty() || !mDirtyTextures)
892 {
Luc Ferron90968362018-05-04 08:47:22 -0400893 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400894 }
895
Luc Ferron90968362018-05-04 08:47:22 -0400896 ContextVk *contextVk = GetImplAs<ContextVk>(context);
Jamie Madillc7918ce2018-06-13 13:25:31 -0400897 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400898
Jamie Madill8c3988c2017-12-21 14:44:56 -0500899 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400900 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400901
902 // TODO(jmadill): Don't hard-code the texture limit.
903 ShaderTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
904 ShaderTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400905 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400906
907 const gl::State &glState = contextVk->getGLState();
908 const auto &completeTextures = glState.getCompleteTextureCache();
909
Jamie Madill4cc753e2018-06-13 13:25:33 -0400910 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
911 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400912 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400913 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
914
Jamie Madill5547b382017-10-23 18:16:01 -0400915 ASSERT(!samplerBinding.unreferenced);
916
Jamie Madill4cc753e2018-06-13 13:25:33 -0400917 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
918 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400919 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400920 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
921 gl::Texture *texture = completeTextures[textureUnit];
922
923 if (texture == nullptr)
924 {
925 // If we have an incomplete texture, fetch it from our renderer.
926 ANGLE_TRY(
927 contextVk->getIncompleteTexture(context, samplerBinding.textureType, &texture));
928 }
929
930 TextureVk *textureVk = vk::GetImpl(texture);
931 const vk::ImageHelper &image = textureVk->getImage();
932
933 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
934
935 imageInfo.sampler = textureVk->getSampler().getHandle();
936 imageInfo.imageView = textureVk->getImageView().getHandle();
937 imageInfo.imageLayout = image.getCurrentLayout();
938
939 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
940
941 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
942 writeInfo.pNext = nullptr;
943 writeInfo.dstSet = descriptorSet;
944 writeInfo.dstBinding = textureIndex;
945 writeInfo.dstArrayElement = arrayElement;
946 writeInfo.descriptorCount = 1;
947 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
948 writeInfo.pImageInfo = &imageInfo;
949 writeInfo.pBufferInfo = nullptr;
950 writeInfo.pTexelBufferView = nullptr;
951
952 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400953 }
Jamie Madill5547b382017-10-23 18:16:01 -0400954 }
955
956 VkDevice device = contextVk->getDevice();
957
Jamie Madill4cc753e2018-06-13 13:25:33 -0400958 ASSERT(writeCount > 0);
959 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400960
961 mDirtyTextures = false;
Luc Ferron90968362018-05-04 08:47:22 -0400962 return gl::NoError();
Jamie Madill5547b382017-10-23 18:16:01 -0400963}
964
965void ProgramVk::invalidateTextures()
966{
967 mDirtyTextures = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400968}
969
Jamie Madill9b168d02018-06-13 13:25:32 -0400970const vk::PipelineLayout &ProgramVk::getPipelineLayout() const
971{
972 return mPipelineLayout.get();
973}
974
Luc Ferron7a06ac12018-03-15 10:17:04 -0400975void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
976{
977 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
978 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400979 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400980 }
981}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400982} // namespace rx