blob: 15fa0ed85052ca0ad92843e8b5f23f5c626c843d [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 Madill242c4fe2018-07-12 15:56:56 -040029void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms,
Jamie Madill21061022018-07-12 23:56:30 -040030 gl::Shader *shader,
31 sh::BlockLayoutMap *blockLayoutMapOut,
32 size_t *blockSizeOut)
Jamie Madill76e471e2017-10-21 09:56:01 -040033{
Jamie Madill76e471e2017-10-21 09:56:01 -040034 if (uniforms.empty())
35 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040036 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040037 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040038 }
39
40 sh::Std140BlockEncoder blockEncoder;
Olli Etuaho3de27032017-11-30 12:16:47 +020041 sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
Jamie Madill76e471e2017-10-21 09:56:01 -040042
43 size_t blockSize = blockEncoder.getBlockSize();
44
45 // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized.
46 if (blockSize == 0)
47 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040048 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040049 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040050 }
51
Luc Ferron7a06ac12018-03-15 10:17:04 -040052 *blockSizeOut = blockSize;
Jamie Madill21061022018-07-12 23:56:30 -040053 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040054}
55
56template <typename T>
57void UpdateDefaultUniformBlock(GLsizei count,
Luc Ferron2371aca2018-03-27 16:03:03 -040058 uint32_t arrayIndex,
Jamie Madill76e471e2017-10-21 09:56:01 -040059 int componentCount,
60 const T *v,
61 const sh::BlockMemberInfo &layoutInfo,
62 angle::MemoryBuffer *uniformData)
63{
Luc Ferron2371aca2018-03-27 16:03:03 -040064 const int elementSize = sizeof(T) * componentCount;
65
66 uint8_t *dst = uniformData->data() + layoutInfo.offset;
Jamie Madill76e471e2017-10-21 09:56:01 -040067 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
68 {
Luc Ferron2371aca2018-03-27 16:03:03 -040069 uint32_t arrayOffset = arrayIndex * layoutInfo.arrayStride;
70 uint8_t *writePtr = dst + arrayOffset;
Jamie Madill76e471e2017-10-21 09:56:01 -040071 memcpy(writePtr, v, elementSize * count);
72 }
73 else
74 {
Luc Ferron2371aca2018-03-27 16:03:03 -040075 // Have to respect the arrayStride between each element of the array.
76 int maxIndex = arrayIndex + count;
77 for (int writeIndex = arrayIndex, readIndex = 0; writeIndex < maxIndex;
78 writeIndex++, readIndex++)
79 {
80 const int arrayOffset = writeIndex * layoutInfo.arrayStride;
81 uint8_t *writePtr = dst + arrayOffset;
Luc Ferrone9465a62018-06-04 10:41:52 -040082 const T *readPtr = v + (readIndex * componentCount);
Luc Ferron2371aca2018-03-27 16:03:03 -040083 memcpy(writePtr, readPtr, elementSize);
84 }
Jamie Madill76e471e2017-10-21 09:56:01 -040085 }
86}
87
Luc Ferron7cec3352018-03-13 13:29:34 -040088template <typename T>
89void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040090 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040091 T *dst,
92 const sh::BlockMemberInfo &layoutInfo,
93 const angle::MemoryBuffer *uniformData)
94{
95 ASSERT(layoutInfo.offset != -1);
96
Luc Ferron2371aca2018-03-27 16:03:03 -040097 const int elementSize = sizeof(T) * componentCount;
98 const uint8_t *source = uniformData->data() + layoutInfo.offset;
99
Luc Ferron7cec3352018-03-13 13:29:34 -0400100 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
101 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400102 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400103 memcpy(dst, readPtr, elementSize);
104 }
105 else
106 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400107 // Have to respect the arrayStride between each element of the array.
108 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
109 const uint8_t *readPtr = source + arrayOffset;
110 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400111 }
112}
113
Jamie Madill21061022018-07-12 23:56:30 -0400114angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
115 vk::DynamicBuffer *dynamicBuffer,
116 const angle::MemoryBuffer &bufferData,
117 uint32_t *outOffset,
118 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400119{
Luc Ferron7a06ac12018-03-15 10:17:04 -0400120 ASSERT(!bufferData.empty());
121 uint8_t *data = nullptr;
122 VkBuffer *outBuffer = nullptr;
123 uint32_t offset;
Jamie Madill21061022018-07-12 23:56:30 -0400124 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400125 outBufferModified));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400126 *outOffset = offset;
127 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400128 ANGLE_TRY(dynamicBuffer->flush(contextVk));
129 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400130}
Jamie Madill76e471e2017-10-21 09:56:01 -0400131} // anonymous namespace
132
Jamie Madill242c4fe2018-07-12 15:56:56 -0400133// ProgramVk::ShaderInfo implementation.
134ProgramVk::ShaderInfo::ShaderInfo()
135{
136}
137
138ProgramVk::ShaderInfo::~ShaderInfo() = default;
139
140angle::Result ProgramVk::ShaderInfo::getShaders(
141 ContextVk *contextVk,
142 const std::string &vertexSource,
143 const std::string &fragmentSource,
144 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
145 const vk::ShaderAndSerial **fragmentShaderAndSerialOut)
146{
147 if (!valid())
148 {
149 std::vector<uint32_t> vertexCode;
150 std::vector<uint32_t> fragmentCode;
151 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(), vertexSource,
152 fragmentSource, &vertexCode, &fragmentCode));
153
154 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mVertexShaderAndSerial, vertexCode.data(),
155 vertexCode.size() * sizeof(uint32_t)));
156 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mFragmentShaderAndSerial, fragmentCode.data(),
157 fragmentCode.size() * sizeof(uint32_t)));
158 }
159
160 *fragmentShaderAndSerialOut = &mFragmentShaderAndSerial;
161 *vertexShaderAndSerialOut = &mVertexShaderAndSerial;
162 return angle::Result::Continue();
163}
164
165void ProgramVk::ShaderInfo::destroy(VkDevice device)
166{
167 mVertexShaderAndSerial.destroy(device);
168 mFragmentShaderAndSerial.destroy(device);
169}
170
171bool ProgramVk::ShaderInfo::valid() const
172{
173 return mVertexShaderAndSerial.valid();
174}
175
176// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400177ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400178 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Jamie Madillf3614372018-03-31 14:19:14 -0400179 kUniformBlockDynamicBufferMinSize),
Jamie Madill242c4fe2018-07-12 15:56:56 -0400180 uniformsDirty(false)
Jamie Madill76e471e2017-10-21 09:56:01 -0400181{
182}
183
Jamie Madill242c4fe2018-07-12 15:56:56 -0400184ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500185
Jamie Madill76e471e2017-10-21 09:56:01 -0400186ProgramVk::ProgramVk(const gl::ProgramState &state)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400187 : ProgramImpl(state), mUniformBlocksOffsets{}, mDirtyTextures(false)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400188{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500189 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400190}
191
Jamie Madill242c4fe2018-07-12 15:56:56 -0400192ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400193
Jamie Madill242c4fe2018-07-12 15:56:56 -0400194gl::Error ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500195{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400196 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500197 return reset(contextVk);
Jamie Madillc5143482017-10-15 20:20:06 -0400198}
Jamie Madill5deea722017-02-16 10:44:46 -0500199
Jamie Madill21061022018-07-12 23:56:30 -0400200angle::Result ProgramVk::reset(ContextVk *contextVk)
Jamie Madillc5143482017-10-15 20:20:06 -0400201{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500202 VkDevice device = contextVk->getDevice();
203
Jamie Madill9b168d02018-06-13 13:25:32 -0400204 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
205 {
206 descriptorSetLayout.reset();
207 }
208 mPipelineLayout.reset();
209
Jamie Madillcaaff162018-06-22 08:55:37 -0400210 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400211 for (auto &uniformBlock : mDefaultUniformBlocks)
212 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400213 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400214 }
215
Jamie Madill242c4fe2018-07-12 15:56:56 -0400216 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
217 mDefaultShaderInfo.destroy(device);
218
Jamie Madillcaaff162018-06-22 08:55:37 -0400219 Serial currentSerial = renderer->getCurrentQueueSerial();
220 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
221 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400222
Jamie Madill5547b382017-10-23 18:16:01 -0400223 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500224 mUsedDescriptorSetRange.invalidate();
Jamie Madill50cf2be2018-06-15 09:46:57 -0400225 mDirtyTextures = false;
Jamie Madillb7d924a2018-03-10 11:16:54 -0500226
Jamie Madill21061022018-07-12 23:56:30 -0400227 return angle::Result::Continue();
Jamie Madill5deea722017-02-16 10:44:46 -0500228}
229
Jamie Madill242c4fe2018-07-12 15:56:56 -0400230gl::LinkResult ProgramVk::load(const gl::Context *context,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400231 gl::InfoLog &infoLog,
232 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400233{
234 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500235 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400236}
237
Jamie Madill27a60632017-06-30 15:12:01 -0400238void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400239{
240 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400241}
242
243void ProgramVk::setBinaryRetrievableHint(bool retrievable)
244{
245 UNIMPLEMENTED();
246}
247
Yunchao He61afff12017-03-14 15:34:03 +0800248void ProgramVk::setSeparable(bool separable)
249{
250 UNIMPLEMENTED();
251}
252
Jamie Madill9cf9e872017-06-05 12:59:25 -0400253gl::LinkResult ProgramVk::link(const gl::Context *glContext,
Jamie Madillc9727f32017-11-07 12:37:07 -0500254 const gl::ProgramLinkedResources &resources,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400255 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400256{
Jamie Madill06ca6342018-07-12 15:56:53 -0400257 ContextVk *contextVk = vk::GetImpl(glContext);
258 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400259
Jamie Madillb7d924a2018-03-10 11:16:54 -0500260 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500261
Jamie Madill242c4fe2018-07-12 15:56:56 -0400262 GlslangWrapper::GetShaderSource(glContext, mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500263
Jamie Madill76e471e2017-10-21 09:56:01 -0400264 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500265
Jamie Madill8c3988c2017-12-21 14:44:56 -0500266 if (!mState.getSamplerUniformRange().empty())
267 {
268 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400269 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500270 mDirtyTextures = true;
271 }
272
Jamie Madill9b168d02018-06-13 13:25:32 -0400273 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
274 // don't already exist in the cache.
275 vk::DescriptorSetLayoutDesc uniformsSetDesc;
276 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
277 1);
278 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
279 1);
280
281 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400282 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400283
Jamie Madill9b168d02018-06-13 13:25:32 -0400284 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400285
286 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
287 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400288 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400289 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
290
291 // The front-end always binds array sampler units sequentially.
292 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
293 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400294 }
295
Jamie Madill21061022018-07-12 23:56:30 -0400296 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400297 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
298
Jamie Madillb01b4802018-07-10 12:43:57 -0400299 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
300 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
301 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400302 contextVk, driverUniformsSetDesc,
303 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400304
Jamie Madill9b168d02018-06-13 13:25:32 -0400305 vk::PipelineLayoutDesc pipelineLayoutDesc;
306 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
307 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400308 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
309 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400310
Jamie Madill21061022018-07-12 23:56:30 -0400311 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
312 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400313
Jamie Madill242c4fe2018-07-12 15:56:56 -0400314 if (!mState.getUniforms().empty())
315 {
316 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
317
318 if (mState.getUniforms().size() > samplerRange.length())
319 {
320 // Ensure the descriptor set range includes the uniform buffers at position 0.
321 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
322 }
323
324 if (!samplerRange.empty())
325 {
326 // Ensure the descriptor set range includes the textures at position 1.
327 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
328 mDirtyTextures = true;
329 }
330 }
331
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500332 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400333}
334
Jamie Madill242c4fe2018-07-12 15:56:56 -0400335angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400336{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400337 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500338 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400339
340 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400341 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
342 vk::ShaderMap<size_t> requiredBufferSize;
343 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400344
Jamie Madill33318de2018-05-01 11:22:54 -0400345 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400346 {
Jamie Madill33318de2018-05-01 11:22:54 -0400347 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400348 gl::Shader *shader = mState.getAttachedShader(glShaderType);
349 const std::vector<sh::Uniform> &uniforms = shader->getUniforms(glContext);
350 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
351 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400352 }
353
354 // Init the default block layout info.
Jamie Madill76e471e2017-10-21 09:56:01 -0400355 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400356 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400357 {
Jamie Madill33318de2018-05-01 11:22:54 -0400358 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400359
Jamie Madill76e471e2017-10-21 09:56:01 -0400360 if (location.used() && !location.ignored)
361 {
Jamie Madillde03e002017-10-21 14:04:20 -0400362 const auto &uniform = uniforms[location.index];
363
364 if (uniform.isSampler())
365 continue;
366
Jamie Madill76e471e2017-10-21 09:56:01 -0400367 std::string uniformName = uniform.name;
368 if (uniform.isArray())
369 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400370 // Gets the uniform name without the [0] at the end.
371 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400372 }
373
374 bool found = false;
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 auto it = layoutMap[shaderType].find(uniformName);
379 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400380 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400381 found = true;
382 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400383 }
384 }
385
386 ASSERT(found);
387 }
388
Jamie Madill33318de2018-05-01 11:22:54 -0400389 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400390 {
Jamie Madill33318de2018-05-01 11:22:54 -0400391 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400392 }
393 }
394
395 bool anyDirty = false;
396 bool allDirty = true;
397
Jamie Madill33318de2018-05-01 11:22:54 -0400398 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400399 {
Jamie Madill33318de2018-05-01 11:22:54 -0400400 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400401 {
Jamie Madill33318de2018-05-01 11:22:54 -0400402 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
403 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400404 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400405 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400406 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400407 size_t minAlignment = static_cast<size_t>(
408 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
409
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400410 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400411
412 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400413 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
414 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400415
416 anyDirty = true;
417 }
418 else
419 {
420 allDirty = false;
421 }
422 }
423
424 if (anyDirty)
425 {
426 // Initialize the "empty" uniform block if necessary.
427 if (!allDirty)
428 {
429 VkBufferCreateInfo uniformBufferInfo;
430 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
431 uniformBufferInfo.pNext = nullptr;
432 uniformBufferInfo.flags = 0;
433 uniformBufferInfo.size = 1;
434 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
435 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
436 uniformBufferInfo.queueFamilyIndexCount = 0;
437 uniformBufferInfo.pQueueFamilyIndices = nullptr;
438
Jamie Madill21061022018-07-12 23:56:30 -0400439 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400440
Luc Ferron7a06ac12018-03-15 10:17:04 -0400441 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500442 VkMemoryPropertyFlags flags =
443 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill21061022018-07-12 23:56:30 -0400444 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400445 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400446 }
Jamie Madill5547b382017-10-23 18:16:01 -0400447 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400448
Jamie Madill242c4fe2018-07-12 15:56:56 -0400449 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400450}
451
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400452GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
453{
Luc Ferronfba1f612018-06-04 14:37:17 -0400454 // No-op. The spec is very vague about the behavior of validation.
455 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400456}
457
Jamie Madill76e471e2017-10-21 09:56:01 -0400458template <typename T>
459void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
460{
461 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
462 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
463
Luc Ferron7cec3352018-03-13 13:29:34 -0400464 if (linkedUniform.isSampler())
465 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400466 // We could potentially cache some indexing here. For now this is a no-op since the mapping
467 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400468 return;
469 }
470
Luc Ferron24a31372018-04-04 11:49:14 -0400471 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400472 {
Luc Ferron24a31372018-04-04 11:49:14 -0400473 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400474 {
Luc Ferron24a31372018-04-04 11:49:14 -0400475 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400476
Luc Ferron24a31372018-04-04 11:49:14 -0400477 // Assume an offset of -1 means the block is unused.
478 if (layoutInfo.offset == -1)
479 {
480 continue;
481 }
482
483 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400484 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
485 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400486 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400487 }
Luc Ferron24a31372018-04-04 11:49:14 -0400488 }
489 else
490 {
491 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400492 {
Luc Ferron24a31372018-04-04 11:49:14 -0400493 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
494
495 // Assume an offset of -1 means the block is unused.
496 if (layoutInfo.offset == -1)
497 {
498 continue;
499 }
500
501 const GLint componentCount = linkedUniform.typeInfo->componentCount;
502
Luc Ferron62059a52018-03-29 07:01:35 -0400503 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
504
Luc Ferrond91c3792018-04-06 09:36:36 -0400505 GLint initialArrayOffset =
506 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400507 for (GLint i = 0; i < count; i++)
508 {
509 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
510 GLint *dest =
511 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
512 const T *source = v + i * componentCount;
513
514 for (int c = 0; c < componentCount; c++)
515 {
516 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
517 }
518 }
Luc Ferron24a31372018-04-04 11:49:14 -0400519 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400520 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400521 }
522}
523
Luc Ferron7cec3352018-03-13 13:29:34 -0400524template <typename T>
525void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
526{
527 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
528 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
529
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400530 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400531
Olli Etuaho107c7242018-03-20 15:45:35 +0200532 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800533 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400534
Jiawei Shao385b3e02018-03-21 09:43:28 +0800535 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400536 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400537 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400538
539 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
540 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400541
542 if (gl::IsMatrixType(linkedUniform.type))
543 {
544 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400545 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400546 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
547 }
548 else
549 {
550 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
551 v, layoutInfo, &uniformBlock.uniformData);
552 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400553}
554
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400555void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
556{
Jamie Madill76e471e2017-10-21 09:56:01 -0400557 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400558}
559
560void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
561{
Jamie Madill76e471e2017-10-21 09:56:01 -0400562 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400563}
564
565void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
566{
Jamie Madill76e471e2017-10-21 09:56:01 -0400567 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400568}
569
570void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
571{
Jamie Madill76e471e2017-10-21 09:56:01 -0400572 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400573}
574
575void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
576{
Luc Ferron7cec3352018-03-13 13:29:34 -0400577 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400578}
579
580void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
581{
Luc Ferron489243f2018-03-28 16:55:28 -0400582 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400583}
584
585void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
586{
Luc Ferron489243f2018-03-28 16:55:28 -0400587 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400588}
589
590void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
591{
Luc Ferron489243f2018-03-28 16:55:28 -0400592 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400593}
594
595void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
596{
597 UNIMPLEMENTED();
598}
599
600void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
601{
602 UNIMPLEMENTED();
603}
604
605void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
606{
607 UNIMPLEMENTED();
608}
609
610void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
611{
612 UNIMPLEMENTED();
613}
614
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400615template <int cols, int rows>
616void ProgramVk::setUniformMatrixfv(GLint location,
617 GLsizei count,
618 GLboolean transpose,
619 const GLfloat *value)
620{
621 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
622 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
623
624 for (auto &uniformBlock : mDefaultUniformBlocks)
625 {
626 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
627
628 // Assume an offset of -1 means the block is unused.
629 if (layoutInfo.offset == -1)
630 {
631 continue;
632 }
633
Luc Ferronc8fbff32018-06-04 10:30:48 -0400634 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400635 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
636 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400637
638 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
639 // setter did not update any data. We still want the uniform to be included when we'll
640 // update the descriptor sets.
641 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400642 }
643}
644
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400645void ProgramVk::setUniformMatrix2fv(GLint location,
646 GLsizei count,
647 GLboolean transpose,
648 const GLfloat *value)
649{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400650 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400651}
652
653void ProgramVk::setUniformMatrix3fv(GLint location,
654 GLsizei count,
655 GLboolean transpose,
656 const GLfloat *value)
657{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400658 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400659}
660
661void ProgramVk::setUniformMatrix4fv(GLint location,
662 GLsizei count,
663 GLboolean transpose,
664 const GLfloat *value)
665{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400666 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400667}
668
669void ProgramVk::setUniformMatrix2x3fv(GLint location,
670 GLsizei count,
671 GLboolean transpose,
672 const GLfloat *value)
673{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400674 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400675}
676
677void ProgramVk::setUniformMatrix3x2fv(GLint location,
678 GLsizei count,
679 GLboolean transpose,
680 const GLfloat *value)
681{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400682 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400683}
684
685void ProgramVk::setUniformMatrix2x4fv(GLint location,
686 GLsizei count,
687 GLboolean transpose,
688 const GLfloat *value)
689{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400690 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400691}
692
693void ProgramVk::setUniformMatrix4x2fv(GLint location,
694 GLsizei count,
695 GLboolean transpose,
696 const GLfloat *value)
697{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400698 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400699}
700
701void ProgramVk::setUniformMatrix3x4fv(GLint location,
702 GLsizei count,
703 GLboolean transpose,
704 const GLfloat *value)
705{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400706 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400707}
708
709void ProgramVk::setUniformMatrix4x3fv(GLint location,
710 GLsizei count,
711 GLboolean transpose,
712 const GLfloat *value)
713{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400714 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400715}
716
717void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
718{
719 UNIMPLEMENTED();
720}
721
Sami Väisänen46eaa942016-06-29 10:26:37 +0300722void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
723 GLenum genMode,
724 GLint components,
725 const GLfloat *coeffs)
726{
727 UNIMPLEMENTED();
728}
729
Jamie Madill242c4fe2018-07-12 15:56:56 -0400730angle::Result ProgramVk::initShaders(ContextVk *contextVk,
731 const gl::DrawCallParams &drawCallParams,
732 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
733 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
734 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500735{
Jamie Madill06ca6342018-07-12 15:56:53 -0400736 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
Jamie Madill242c4fe2018-07-12 15:56:56 -0400737 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource,
738 vertexShaderAndSerialOut, fragmentShaderAndSerialOut));
739 ASSERT(mDefaultShaderInfo.valid());
740
741 *pipelineLayoutOut = &mPipelineLayout.get();
742
743 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500744}
745
Jamie Madill21061022018-07-12 23:56:30 -0400746angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400747{
Jamie Madill76e471e2017-10-21 09:56:01 -0400748 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400749 vk::DynamicDescriptorPool *dynamicDescriptorPool =
750 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400751
Luc Ferron6ea1b412018-03-21 16:13:01 -0400752 uint32_t potentialNewCount = descriptorSetIndex + 1;
753 if (potentialNewCount > mDescriptorSets.size())
754 {
755 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
756 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400757
Jamie Madillc7918ce2018-06-13 13:25:31 -0400758 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400759 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400760 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400761 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400762 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400763}
764
Jamie Madill54164b02017-08-28 15:17:37 -0400765void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
766{
Luc Ferron7cec3352018-03-13 13:29:34 -0400767 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400768}
769
770void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
771{
Luc Ferron7cec3352018-03-13 13:29:34 -0400772 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400773}
774
775void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
776{
777 UNIMPLEMENTED();
778}
779
Jamie Madill21061022018-07-12 23:56:30 -0400780angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400781{
Jamie Madill33318de2018-05-01 11:22:54 -0400782 if (!mDefaultUniformBlocks[vk::ShaderType::VertexShader].uniformsDirty &&
783 !mDefaultUniformBlocks[vk::ShaderType::FragmentShader].uniformsDirty)
Jamie Madill76e471e2017-10-21 09:56:01 -0400784 {
Jamie Madill21061022018-07-12 23:56:30 -0400785 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400786 }
787
Jamie Madill76e471e2017-10-21 09:56:01 -0400788 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400789 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400790 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400791 {
Jamie Madill33318de2018-05-01 11:22:54 -0400792 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400793
Jamie Madill76e471e2017-10-21 09:56:01 -0400794 if (uniformBlock.uniformsDirty)
795 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400796 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400797 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400798 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400799 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400800 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400801
802 if (bufferModified)
803 {
804 anyNewBufferAllocated = true;
805 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400806 }
807 }
808
Luc Ferron7a06ac12018-03-15 10:17:04 -0400809 if (anyNewBufferAllocated)
810 {
811 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
812 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400813 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400814 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
815 }
816
Jamie Madill21061022018-07-12 23:56:30 -0400817 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400818}
819
Jamie Madill21061022018-07-12 23:56:30 -0400820angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400821{
Jamie Madill33318de2018-05-01 11:22:54 -0400822 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
823 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400824
Jamie Madill33318de2018-05-01 11:22:54 -0400825 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400826 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400827 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
828 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
829 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400830
831 if (!uniformBlock.uniformData.empty())
832 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400833 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400834 }
835 else
836 {
837 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
838 }
839
840 bufferInfo.offset = 0;
841 bufferInfo.range = VK_WHOLE_SIZE;
842
Jamie Madill76e471e2017-10-21 09:56:01 -0400843 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
844 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400845 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400846 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400847 writeInfo.dstArrayElement = 0;
848 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400849 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400850 writeInfo.pImageInfo = nullptr;
851 writeInfo.pBufferInfo = &bufferInfo;
852 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400853 }
854
855 VkDevice device = contextVk->getDevice();
856
Jamie Madill33318de2018-05-01 11:22:54 -0400857 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400858
Jamie Madill21061022018-07-12 23:56:30 -0400859 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400860}
861
Jamie Madill84c662b2018-07-12 15:56:55 -0400862angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400863{
864 if (mState.getSamplerBindings().empty() || !mDirtyTextures)
865 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400866 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400867 }
868
Jamie Madillc7918ce2018-06-13 13:25:31 -0400869 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400870
Jamie Madill8c3988c2017-12-21 14:44:56 -0500871 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400872 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400873
Jamie Madill84c662b2018-07-12 15:56:55 -0400874 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
875 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400876 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400877
Jamie Madill84c662b2018-07-12 15:56:55 -0400878 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400879
Jamie Madill4cc753e2018-06-13 13:25:33 -0400880 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
881 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400882 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400883 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
884
Jamie Madill5547b382017-10-23 18:16:01 -0400885 ASSERT(!samplerBinding.unreferenced);
886
Jamie Madill4cc753e2018-06-13 13:25:33 -0400887 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
888 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400889 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400890 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
891 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400892 const vk::ImageHelper &image = textureVk->getImage();
893
894 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
895
896 imageInfo.sampler = textureVk->getSampler().getHandle();
897 imageInfo.imageView = textureVk->getImageView().getHandle();
898 imageInfo.imageLayout = image.getCurrentLayout();
899
900 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
901
902 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
903 writeInfo.pNext = nullptr;
904 writeInfo.dstSet = descriptorSet;
905 writeInfo.dstBinding = textureIndex;
906 writeInfo.dstArrayElement = arrayElement;
907 writeInfo.descriptorCount = 1;
908 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
909 writeInfo.pImageInfo = &imageInfo;
910 writeInfo.pBufferInfo = nullptr;
911 writeInfo.pTexelBufferView = nullptr;
912
913 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400914 }
Jamie Madill5547b382017-10-23 18:16:01 -0400915 }
916
917 VkDevice device = contextVk->getDevice();
918
Jamie Madill4cc753e2018-06-13 13:25:33 -0400919 ASSERT(writeCount > 0);
920 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400921
922 mDirtyTextures = false;
Jamie Madill84c662b2018-07-12 15:56:55 -0400923 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400924}
925
926void ProgramVk::invalidateTextures()
927{
928 mDirtyTextures = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400929}
930
Luc Ferron7a06ac12018-03-15 10:17:04 -0400931void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
932{
933 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
934 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400935 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400936 }
937}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400938
939angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
940 const gl::DrawCallParams &drawCallParams,
941 VkDescriptorSet driverUniformsDescriptorSet,
942 vk::CommandBuffer *commandBuffer)
943{
944 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
945 // Can probably use better dirty bits here.
946 ANGLE_TRY(updateUniforms(contextVk));
947 ANGLE_TRY(updateTexturesDescriptorSet(contextVk));
948
949 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
950 kDriverUniformsDescriptorSetIndex, 1,
951 &driverUniformsDescriptorSet, 0, nullptr);
952
953 if (mUsedDescriptorSetRange.empty())
954 return angle::Result::Continue();
955
956 ASSERT(!mDescriptorSets.empty());
957
958 unsigned int low = mUsedDescriptorSetRange.low();
959
960 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
961 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
962 {
963 commandBuffer->bindDescriptorSets(
964 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
965 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
966 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
967 }
968 else
969 {
970 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
971 low, mUsedDescriptorSetRange.length(),
972 &mDescriptorSets[low], 0, nullptr);
973 }
974
975 return angle::Result::Continue();
976}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400977} // namespace rx