blob: f27e92d436c1ae2d76cac9fa55b3dda490991161 [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
jchen107ae70d82018-07-06 13:47:01 +0800253std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *glContext,
254 const gl::ProgramLinkedResources &resources,
255 gl::InfoLog &infoLog)
256{
257 // TODO(jie.a.chen@intel.com): Parallelize linking.
258 // http://crbug.com/849576
259 return std::make_unique<LinkEventDone>(linkImpl(glContext, resources, infoLog));
260}
261
262gl::LinkResult ProgramVk::linkImpl(const gl::Context *glContext,
263 const gl::ProgramLinkedResources &resources,
264 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400265{
Jamie Madill06ca6342018-07-12 15:56:53 -0400266 ContextVk *contextVk = vk::GetImpl(glContext);
267 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400268
Jamie Madillb7d924a2018-03-10 11:16:54 -0500269 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500270
jchen103fd614d2018-08-13 12:21:58 +0800271 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500272
Jamie Madill76e471e2017-10-21 09:56:01 -0400273 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500274
Jamie Madill8c3988c2017-12-21 14:44:56 -0500275 if (!mState.getSamplerUniformRange().empty())
276 {
277 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400278 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500279 mDirtyTextures = true;
280 }
281
Jamie Madill9b168d02018-06-13 13:25:32 -0400282 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
283 // don't already exist in the cache.
284 vk::DescriptorSetLayoutDesc uniformsSetDesc;
285 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
286 1);
287 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
288 1);
289
290 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400291 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400292
Jamie Madill9b168d02018-06-13 13:25:32 -0400293 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400294
295 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
296 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400297 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400298 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
299
300 // The front-end always binds array sampler units sequentially.
301 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
302 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400303 }
304
Jamie Madill21061022018-07-12 23:56:30 -0400305 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400306 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
307
Jamie Madillb01b4802018-07-10 12:43:57 -0400308 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
309 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
310 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400311 contextVk, driverUniformsSetDesc,
312 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400313
Jamie Madill9b168d02018-06-13 13:25:32 -0400314 vk::PipelineLayoutDesc pipelineLayoutDesc;
315 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
316 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400317 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
318 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400319
Jamie Madill21061022018-07-12 23:56:30 -0400320 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
321 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400322
Jamie Madill242c4fe2018-07-12 15:56:56 -0400323 if (!mState.getUniforms().empty())
324 {
325 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
326
327 if (mState.getUniforms().size() > samplerRange.length())
328 {
329 // Ensure the descriptor set range includes the uniform buffers at position 0.
330 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
331 }
332
333 if (!samplerRange.empty())
334 {
335 // Ensure the descriptor set range includes the textures at position 1.
336 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
337 mDirtyTextures = true;
338 }
339 }
340
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500341 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400342}
343
Jamie Madill242c4fe2018-07-12 15:56:56 -0400344angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400345{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400346 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500347 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400348
349 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400350 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
351 vk::ShaderMap<size_t> requiredBufferSize;
352 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400353
Jamie Madill33318de2018-05-01 11:22:54 -0400354 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400355 {
Jamie Madill33318de2018-05-01 11:22:54 -0400356 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400357 gl::Shader *shader = mState.getAttachedShader(glShaderType);
jchen103fd614d2018-08-13 12:21:58 +0800358 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400359 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
360 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400361 }
362
363 // Init the default block layout info.
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400365 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400366 {
Jamie Madill33318de2018-05-01 11:22:54 -0400367 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400368
Jamie Madill76e471e2017-10-21 09:56:01 -0400369 if (location.used() && !location.ignored)
370 {
Jamie Madillde03e002017-10-21 14:04:20 -0400371 const auto &uniform = uniforms[location.index];
372
373 if (uniform.isSampler())
374 continue;
375
Jamie Madill76e471e2017-10-21 09:56:01 -0400376 std::string uniformName = uniform.name;
377 if (uniform.isArray())
378 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400379 // Gets the uniform name without the [0] at the end.
380 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400381 }
382
383 bool found = false;
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 auto it = layoutMap[shaderType].find(uniformName);
388 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400389 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400390 found = true;
391 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400392 }
393 }
394
395 ASSERT(found);
396 }
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 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400401 }
402 }
403
404 bool anyDirty = false;
405 bool allDirty = true;
406
Jamie Madill33318de2018-05-01 11:22:54 -0400407 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400408 {
Jamie Madill33318de2018-05-01 11:22:54 -0400409 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400410 {
Jamie Madill33318de2018-05-01 11:22:54 -0400411 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
412 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400413 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400414 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400415 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400416 size_t minAlignment = static_cast<size_t>(
417 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
418
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400419 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400420
421 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400422 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
423 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400424
425 anyDirty = true;
426 }
427 else
428 {
429 allDirty = false;
430 }
431 }
432
433 if (anyDirty)
434 {
435 // Initialize the "empty" uniform block if necessary.
436 if (!allDirty)
437 {
438 VkBufferCreateInfo uniformBufferInfo;
439 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
440 uniformBufferInfo.pNext = nullptr;
441 uniformBufferInfo.flags = 0;
442 uniformBufferInfo.size = 1;
443 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
444 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
445 uniformBufferInfo.queueFamilyIndexCount = 0;
446 uniformBufferInfo.pQueueFamilyIndices = nullptr;
447
Jamie Madill21061022018-07-12 23:56:30 -0400448 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400449
Luc Ferron7a06ac12018-03-15 10:17:04 -0400450 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500451 VkMemoryPropertyFlags flags =
452 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill21061022018-07-12 23:56:30 -0400453 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400454 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400455 }
Jamie Madill5547b382017-10-23 18:16:01 -0400456 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400457
Jamie Madill242c4fe2018-07-12 15:56:56 -0400458 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400459}
460
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400461GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
462{
Luc Ferronfba1f612018-06-04 14:37:17 -0400463 // No-op. The spec is very vague about the behavior of validation.
464 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400465}
466
Jamie Madill76e471e2017-10-21 09:56:01 -0400467template <typename T>
468void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
469{
470 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
471 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
472
Luc Ferron7cec3352018-03-13 13:29:34 -0400473 if (linkedUniform.isSampler())
474 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400475 // We could potentially cache some indexing here. For now this is a no-op since the mapping
476 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400477 return;
478 }
479
Luc Ferron24a31372018-04-04 11:49:14 -0400480 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400481 {
Luc Ferron24a31372018-04-04 11:49:14 -0400482 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400483 {
Luc Ferron24a31372018-04-04 11:49:14 -0400484 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400485
Luc Ferron24a31372018-04-04 11:49:14 -0400486 // Assume an offset of -1 means the block is unused.
487 if (layoutInfo.offset == -1)
488 {
489 continue;
490 }
491
492 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400493 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
494 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400495 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400496 }
Luc Ferron24a31372018-04-04 11:49:14 -0400497 }
498 else
499 {
500 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400501 {
Luc Ferron24a31372018-04-04 11:49:14 -0400502 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
503
504 // Assume an offset of -1 means the block is unused.
505 if (layoutInfo.offset == -1)
506 {
507 continue;
508 }
509
510 const GLint componentCount = linkedUniform.typeInfo->componentCount;
511
Luc Ferron62059a52018-03-29 07:01:35 -0400512 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
513
Luc Ferrond91c3792018-04-06 09:36:36 -0400514 GLint initialArrayOffset =
515 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400516 for (GLint i = 0; i < count; i++)
517 {
518 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
519 GLint *dest =
520 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
521 const T *source = v + i * componentCount;
522
523 for (int c = 0; c < componentCount; c++)
524 {
525 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
526 }
527 }
Luc Ferron24a31372018-04-04 11:49:14 -0400528 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400529 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400530 }
531}
532
Luc Ferron7cec3352018-03-13 13:29:34 -0400533template <typename T>
534void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
535{
536 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
537 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
538
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400539 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400540
Olli Etuaho107c7242018-03-20 15:45:35 +0200541 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800542 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400543
Jiawei Shao385b3e02018-03-21 09:43:28 +0800544 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400545 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400546 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400547
548 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
549 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400550
551 if (gl::IsMatrixType(linkedUniform.type))
552 {
553 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400554 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400555 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
556 }
557 else
558 {
559 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
560 v, layoutInfo, &uniformBlock.uniformData);
561 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400562}
563
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400564void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
565{
Jamie Madill76e471e2017-10-21 09:56:01 -0400566 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400567}
568
569void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
570{
Jamie Madill76e471e2017-10-21 09:56:01 -0400571 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400572}
573
574void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
575{
Jamie Madill76e471e2017-10-21 09:56:01 -0400576 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400577}
578
579void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
580{
Jamie Madill76e471e2017-10-21 09:56:01 -0400581 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400582}
583
584void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
585{
Luc Ferron7cec3352018-03-13 13:29:34 -0400586 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400587}
588
589void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
590{
Luc Ferron489243f2018-03-28 16:55:28 -0400591 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400592}
593
594void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
595{
Luc Ferron489243f2018-03-28 16:55:28 -0400596 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400597}
598
599void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
600{
Luc Ferron489243f2018-03-28 16:55:28 -0400601 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400602}
603
604void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
605{
606 UNIMPLEMENTED();
607}
608
609void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
610{
611 UNIMPLEMENTED();
612}
613
614void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
615{
616 UNIMPLEMENTED();
617}
618
619void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
620{
621 UNIMPLEMENTED();
622}
623
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400624template <int cols, int rows>
625void ProgramVk::setUniformMatrixfv(GLint location,
626 GLsizei count,
627 GLboolean transpose,
628 const GLfloat *value)
629{
630 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
631 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
632
633 for (auto &uniformBlock : mDefaultUniformBlocks)
634 {
635 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
636
637 // Assume an offset of -1 means the block is unused.
638 if (layoutInfo.offset == -1)
639 {
640 continue;
641 }
642
Luc Ferronc8fbff32018-06-04 10:30:48 -0400643 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400644 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
645 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400646
647 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
648 // setter did not update any data. We still want the uniform to be included when we'll
649 // update the descriptor sets.
650 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400651 }
652}
653
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400654void ProgramVk::setUniformMatrix2fv(GLint location,
655 GLsizei count,
656 GLboolean transpose,
657 const GLfloat *value)
658{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400659 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400660}
661
662void ProgramVk::setUniformMatrix3fv(GLint location,
663 GLsizei count,
664 GLboolean transpose,
665 const GLfloat *value)
666{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400667 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400668}
669
670void ProgramVk::setUniformMatrix4fv(GLint location,
671 GLsizei count,
672 GLboolean transpose,
673 const GLfloat *value)
674{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400675 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400676}
677
678void ProgramVk::setUniformMatrix2x3fv(GLint location,
679 GLsizei count,
680 GLboolean transpose,
681 const GLfloat *value)
682{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400683 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400684}
685
686void ProgramVk::setUniformMatrix3x2fv(GLint location,
687 GLsizei count,
688 GLboolean transpose,
689 const GLfloat *value)
690{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400691 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400692}
693
694void ProgramVk::setUniformMatrix2x4fv(GLint location,
695 GLsizei count,
696 GLboolean transpose,
697 const GLfloat *value)
698{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400699 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400700}
701
702void ProgramVk::setUniformMatrix4x2fv(GLint location,
703 GLsizei count,
704 GLboolean transpose,
705 const GLfloat *value)
706{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400707 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400708}
709
710void ProgramVk::setUniformMatrix3x4fv(GLint location,
711 GLsizei count,
712 GLboolean transpose,
713 const GLfloat *value)
714{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400715 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400716}
717
718void ProgramVk::setUniformMatrix4x3fv(GLint location,
719 GLsizei count,
720 GLboolean transpose,
721 const GLfloat *value)
722{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400723 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400724}
725
726void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
727{
728 UNIMPLEMENTED();
729}
730
Sami Väisänen46eaa942016-06-29 10:26:37 +0300731void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
732 GLenum genMode,
733 GLint components,
734 const GLfloat *coeffs)
735{
736 UNIMPLEMENTED();
737}
738
Jamie Madill242c4fe2018-07-12 15:56:56 -0400739angle::Result ProgramVk::initShaders(ContextVk *contextVk,
740 const gl::DrawCallParams &drawCallParams,
741 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
742 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
743 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500744{
Jamie Madill06ca6342018-07-12 15:56:53 -0400745 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
Jamie Madill242c4fe2018-07-12 15:56:56 -0400746 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource,
747 vertexShaderAndSerialOut, fragmentShaderAndSerialOut));
748 ASSERT(mDefaultShaderInfo.valid());
749
750 *pipelineLayoutOut = &mPipelineLayout.get();
751
752 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500753}
754
Jamie Madill21061022018-07-12 23:56:30 -0400755angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400756{
Jamie Madill76e471e2017-10-21 09:56:01 -0400757 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400758 vk::DynamicDescriptorPool *dynamicDescriptorPool =
759 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400760
Luc Ferron6ea1b412018-03-21 16:13:01 -0400761 uint32_t potentialNewCount = descriptorSetIndex + 1;
762 if (potentialNewCount > mDescriptorSets.size())
763 {
764 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
765 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400766
Jamie Madillc7918ce2018-06-13 13:25:31 -0400767 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400768 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400769 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400770 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400771 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400772}
773
Jamie Madill54164b02017-08-28 15:17:37 -0400774void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
775{
Luc Ferron7cec3352018-03-13 13:29:34 -0400776 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400777}
778
779void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
780{
Luc Ferron7cec3352018-03-13 13:29:34 -0400781 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400782}
783
784void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
785{
786 UNIMPLEMENTED();
787}
788
Jamie Madill21061022018-07-12 23:56:30 -0400789angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400790{
Jamie Madill33318de2018-05-01 11:22:54 -0400791 if (!mDefaultUniformBlocks[vk::ShaderType::VertexShader].uniformsDirty &&
792 !mDefaultUniformBlocks[vk::ShaderType::FragmentShader].uniformsDirty)
Jamie Madill76e471e2017-10-21 09:56:01 -0400793 {
Jamie Madill21061022018-07-12 23:56:30 -0400794 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400795 }
796
Jamie Madill76e471e2017-10-21 09:56:01 -0400797 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400798 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400799 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400800 {
Jamie Madill33318de2018-05-01 11:22:54 -0400801 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400802
Jamie Madill76e471e2017-10-21 09:56:01 -0400803 if (uniformBlock.uniformsDirty)
804 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400805 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400806 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400807 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400808 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400809 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400810
811 if (bufferModified)
812 {
813 anyNewBufferAllocated = true;
814 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400815 }
816 }
817
Luc Ferron7a06ac12018-03-15 10:17:04 -0400818 if (anyNewBufferAllocated)
819 {
820 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
821 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400822 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400823 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
824 }
825
Jamie Madill21061022018-07-12 23:56:30 -0400826 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400827}
828
Jamie Madill21061022018-07-12 23:56:30 -0400829angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400830{
Jamie Madill33318de2018-05-01 11:22:54 -0400831 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
832 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400833
Jamie Madill33318de2018-05-01 11:22:54 -0400834 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400835 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400836 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
837 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
838 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400839
840 if (!uniformBlock.uniformData.empty())
841 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400842 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400843 }
844 else
845 {
846 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
847 }
848
849 bufferInfo.offset = 0;
850 bufferInfo.range = VK_WHOLE_SIZE;
851
Jamie Madill76e471e2017-10-21 09:56:01 -0400852 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
853 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400854 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400855 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400856 writeInfo.dstArrayElement = 0;
857 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400858 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400859 writeInfo.pImageInfo = nullptr;
860 writeInfo.pBufferInfo = &bufferInfo;
861 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400862 }
863
864 VkDevice device = contextVk->getDevice();
865
Jamie Madill33318de2018-05-01 11:22:54 -0400866 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400867
Jamie Madill21061022018-07-12 23:56:30 -0400868 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400869}
870
Jamie Madill84c662b2018-07-12 15:56:55 -0400871angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400872{
873 if (mState.getSamplerBindings().empty() || !mDirtyTextures)
874 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400875 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400876 }
877
Jamie Madillc7918ce2018-06-13 13:25:31 -0400878 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400879
Jamie Madill8c3988c2017-12-21 14:44:56 -0500880 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400881 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400882
Jamie Madill84c662b2018-07-12 15:56:55 -0400883 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
884 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400885 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400886
Jamie Madill84c662b2018-07-12 15:56:55 -0400887 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400888
Jamie Madill4cc753e2018-06-13 13:25:33 -0400889 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
890 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400891 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400892 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
893
Jamie Madill5547b382017-10-23 18:16:01 -0400894 ASSERT(!samplerBinding.unreferenced);
895
Jamie Madill4cc753e2018-06-13 13:25:33 -0400896 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
897 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400898 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400899 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
900 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400901 const vk::ImageHelper &image = textureVk->getImage();
902
903 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
904
905 imageInfo.sampler = textureVk->getSampler().getHandle();
906 imageInfo.imageView = textureVk->getImageView().getHandle();
907 imageInfo.imageLayout = image.getCurrentLayout();
908
909 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
910
911 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
912 writeInfo.pNext = nullptr;
913 writeInfo.dstSet = descriptorSet;
914 writeInfo.dstBinding = textureIndex;
915 writeInfo.dstArrayElement = arrayElement;
916 writeInfo.descriptorCount = 1;
917 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
918 writeInfo.pImageInfo = &imageInfo;
919 writeInfo.pBufferInfo = nullptr;
920 writeInfo.pTexelBufferView = nullptr;
921
922 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400923 }
Jamie Madill5547b382017-10-23 18:16:01 -0400924 }
925
926 VkDevice device = contextVk->getDevice();
927
Jamie Madill4cc753e2018-06-13 13:25:33 -0400928 ASSERT(writeCount > 0);
929 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400930
931 mDirtyTextures = false;
Jamie Madill84c662b2018-07-12 15:56:55 -0400932 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400933}
934
935void ProgramVk::invalidateTextures()
936{
937 mDirtyTextures = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400938}
939
Luc Ferron7a06ac12018-03-15 10:17:04 -0400940void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
941{
942 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
943 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400944 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400945 }
946}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400947
948angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
949 const gl::DrawCallParams &drawCallParams,
950 VkDescriptorSet driverUniformsDescriptorSet,
951 vk::CommandBuffer *commandBuffer)
952{
953 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
954 // Can probably use better dirty bits here.
955 ANGLE_TRY(updateUniforms(contextVk));
956 ANGLE_TRY(updateTexturesDescriptorSet(contextVk));
957
958 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
959 kDriverUniformsDescriptorSetIndex, 1,
960 &driverUniformsDescriptorSet, 0, nullptr);
961
962 if (mUsedDescriptorSetRange.empty())
963 return angle::Result::Continue();
964
965 ASSERT(!mDescriptorSets.empty());
966
967 unsigned int low = mUsedDescriptorSetRange.low();
968
969 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
970 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
971 {
972 commandBuffer->bindDescriptorSets(
973 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
974 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
975 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
976 }
977 else
978 {
979 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
980 low, mUsedDescriptorSetRange.length(),
981 &mDescriptorSets[low], 0, nullptr);
982 }
983
984 return angle::Result::Continue();
985}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400986} // namespace rx