blob: 44eaaf958f7e12b832b22e26dcf1c7b85be58422 [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;
Jamie Madill4c310832018-08-29 13:43:17 -0400123 VkDeviceSize offset = 0;
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));
Jamie Madill4c310832018-08-29 13:43:17 -0400126 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400127 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 Madill2b858c22018-09-03 13:58:14 -0400179 kUniformBlockDynamicBufferMinSize)
Jamie Madill76e471e2017-10-21 09:56:01 -0400180{
181}
182
Jamie Madill242c4fe2018-07-12 15:56:56 -0400183ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500184
Jamie Madill88fc6da2018-08-30 16:18:36 -0400185ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400186{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500187 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400188}
189
Jamie Madill242c4fe2018-07-12 15:56:56 -0400190ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400191
Jamie Madill242c4fe2018-07-12 15:56:56 -0400192gl::Error ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500193{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400194 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500195 return reset(contextVk);
Jamie Madillc5143482017-10-15 20:20:06 -0400196}
Jamie Madill5deea722017-02-16 10:44:46 -0500197
Jamie Madill21061022018-07-12 23:56:30 -0400198angle::Result ProgramVk::reset(ContextVk *contextVk)
Jamie Madillc5143482017-10-15 20:20:06 -0400199{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500200 VkDevice device = contextVk->getDevice();
201
Jamie Madill9b168d02018-06-13 13:25:32 -0400202 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
203 {
204 descriptorSetLayout.reset();
205 }
206 mPipelineLayout.reset();
207
Jamie Madillcaaff162018-06-22 08:55:37 -0400208 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400209 for (auto &uniformBlock : mDefaultUniformBlocks)
210 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400211 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400212 }
213
Jamie Madill242c4fe2018-07-12 15:56:56 -0400214 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
215 mDefaultShaderInfo.destroy(device);
216
Jamie Madillcaaff162018-06-22 08:55:37 -0400217 Serial currentSerial = renderer->getCurrentQueueSerial();
218 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
219 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400220
Jamie Madill5547b382017-10-23 18:16:01 -0400221 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500222 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500223
Jamie Madill21061022018-07-12 23:56:30 -0400224 return angle::Result::Continue();
Jamie Madill5deea722017-02-16 10:44:46 -0500225}
226
Jamie Madill242c4fe2018-07-12 15:56:56 -0400227gl::LinkResult ProgramVk::load(const gl::Context *context,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400228 gl::InfoLog &infoLog,
229 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400230{
231 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500232 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400233}
234
Jamie Madill27a60632017-06-30 15:12:01 -0400235void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400236{
237 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400238}
239
240void ProgramVk::setBinaryRetrievableHint(bool retrievable)
241{
242 UNIMPLEMENTED();
243}
244
Yunchao He61afff12017-03-14 15:34:03 +0800245void ProgramVk::setSeparable(bool separable)
246{
247 UNIMPLEMENTED();
248}
249
jchen107ae70d82018-07-06 13:47:01 +0800250std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *glContext,
251 const gl::ProgramLinkedResources &resources,
252 gl::InfoLog &infoLog)
253{
254 // TODO(jie.a.chen@intel.com): Parallelize linking.
255 // http://crbug.com/849576
256 return std::make_unique<LinkEventDone>(linkImpl(glContext, resources, infoLog));
257}
258
259gl::LinkResult ProgramVk::linkImpl(const gl::Context *glContext,
260 const gl::ProgramLinkedResources &resources,
261 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400262{
Jamie Madill06ca6342018-07-12 15:56:53 -0400263 ContextVk *contextVk = vk::GetImpl(glContext);
264 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400265
Jamie Madillb7d924a2018-03-10 11:16:54 -0500266 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500267
jchen103fd614d2018-08-13 12:21:58 +0800268 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500269
Jamie Madill76e471e2017-10-21 09:56:01 -0400270 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500271
Jamie Madill8c3988c2017-12-21 14:44:56 -0500272 if (!mState.getSamplerUniformRange().empty())
273 {
274 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400275 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500276 }
277
Jamie Madill9b168d02018-06-13 13:25:32 -0400278 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
279 // don't already exist in the cache.
280 vk::DescriptorSetLayoutDesc uniformsSetDesc;
281 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
282 1);
283 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
284 1);
285
286 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400287 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400288
Jamie Madill9b168d02018-06-13 13:25:32 -0400289 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400290
291 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
292 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400293 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400294 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
295
296 // The front-end always binds array sampler units sequentially.
297 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
298 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400299 }
300
Jamie Madill21061022018-07-12 23:56:30 -0400301 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400302 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
303
Jamie Madillb01b4802018-07-10 12:43:57 -0400304 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
305 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
306 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400307 contextVk, driverUniformsSetDesc,
308 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400309
Jamie Madill9b168d02018-06-13 13:25:32 -0400310 vk::PipelineLayoutDesc pipelineLayoutDesc;
311 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
312 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400313 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
314 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400315
Jamie Madill21061022018-07-12 23:56:30 -0400316 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
317 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400318
Jamie Madill242c4fe2018-07-12 15:56:56 -0400319 if (!mState.getUniforms().empty())
320 {
321 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
322
323 if (mState.getUniforms().size() > samplerRange.length())
324 {
325 // Ensure the descriptor set range includes the uniform buffers at position 0.
326 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
327 }
328
329 if (!samplerRange.empty())
330 {
331 // Ensure the descriptor set range includes the textures at position 1.
332 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400333 }
334 }
335
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500336 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400337}
338
Jamie Madill242c4fe2018-07-12 15:56:56 -0400339angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400340{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400341 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500342 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400343
344 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400345 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
346 vk::ShaderMap<size_t> requiredBufferSize;
347 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400348
Jamie Madill33318de2018-05-01 11:22:54 -0400349 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400350 {
Jamie Madill33318de2018-05-01 11:22:54 -0400351 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400352 gl::Shader *shader = mState.getAttachedShader(glShaderType);
jchen103fd614d2018-08-13 12:21:58 +0800353 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400354 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
355 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400356 }
357
358 // Init the default block layout info.
Jamie Madill76e471e2017-10-21 09:56:01 -0400359 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400360 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400361 {
Jamie Madill33318de2018-05-01 11:22:54 -0400362 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400363
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 if (location.used() && !location.ignored)
365 {
Jamie Madillde03e002017-10-21 14:04:20 -0400366 const auto &uniform = uniforms[location.index];
367
368 if (uniform.isSampler())
369 continue;
370
Jamie Madill76e471e2017-10-21 09:56:01 -0400371 std::string uniformName = uniform.name;
372 if (uniform.isArray())
373 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400374 // Gets the uniform name without the [0] at the end.
375 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400376 }
377
378 bool found = false;
379
Jamie Madill33318de2018-05-01 11:22:54 -0400380 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400381 {
Jamie Madill33318de2018-05-01 11:22:54 -0400382 auto it = layoutMap[shaderType].find(uniformName);
383 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400384 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400385 found = true;
386 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400387 }
388 }
389
390 ASSERT(found);
391 }
392
Jamie Madill33318de2018-05-01 11:22:54 -0400393 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400394 {
Jamie Madill33318de2018-05-01 11:22:54 -0400395 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400396 }
397 }
398
Jamie Madill33318de2018-05-01 11:22:54 -0400399 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400400 {
Jamie Madill33318de2018-05-01 11:22:54 -0400401 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400402 {
Jamie Madill33318de2018-05-01 11:22:54 -0400403 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
404 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400405 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400406 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400407 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400408 size_t minAlignment = static_cast<size_t>(
409 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
410
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400411 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400412
413 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400414 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400415 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400416 }
417 }
418
Jamie Madill2b858c22018-09-03 13:58:14 -0400419 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400420 {
421 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400422 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400423 {
424 VkBufferCreateInfo uniformBufferInfo;
425 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
426 uniformBufferInfo.pNext = nullptr;
427 uniformBufferInfo.flags = 0;
428 uniformBufferInfo.size = 1;
429 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
430 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
431 uniformBufferInfo.queueFamilyIndexCount = 0;
432 uniformBufferInfo.pQueueFamilyIndices = nullptr;
433
Jamie Madill21061022018-07-12 23:56:30 -0400434 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400435
Luc Ferron7a06ac12018-03-15 10:17:04 -0400436 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500437 VkMemoryPropertyFlags flags =
438 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill21061022018-07-12 23:56:30 -0400439 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400440 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400441 }
Jamie Madill5547b382017-10-23 18:16:01 -0400442 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400443
Jamie Madill242c4fe2018-07-12 15:56:56 -0400444 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400445}
446
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400447GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
448{
Luc Ferronfba1f612018-06-04 14:37:17 -0400449 // No-op. The spec is very vague about the behavior of validation.
450 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400451}
452
Jamie Madill76e471e2017-10-21 09:56:01 -0400453template <typename T>
454void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
455{
456 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
457 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
458
Luc Ferron7cec3352018-03-13 13:29:34 -0400459 if (linkedUniform.isSampler())
460 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400461 // We could potentially cache some indexing here. For now this is a no-op since the mapping
462 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400463 return;
464 }
465
Luc Ferron24a31372018-04-04 11:49:14 -0400466 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400467 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400468 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400469 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400470 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400471 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400472
Luc Ferron24a31372018-04-04 11:49:14 -0400473 // Assume an offset of -1 means the block is unused.
474 if (layoutInfo.offset == -1)
475 {
476 continue;
477 }
478
479 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400480 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
481 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400482 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400483 }
Luc Ferron24a31372018-04-04 11:49:14 -0400484 }
485 else
486 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400487 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400488 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400489 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400490 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
491
492 // Assume an offset of -1 means the block is unused.
493 if (layoutInfo.offset == -1)
494 {
495 continue;
496 }
497
498 const GLint componentCount = linkedUniform.typeInfo->componentCount;
499
Luc Ferron62059a52018-03-29 07:01:35 -0400500 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
501
Luc Ferrond91c3792018-04-06 09:36:36 -0400502 GLint initialArrayOffset =
503 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400504 for (GLint i = 0; i < count; i++)
505 {
506 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
507 GLint *dest =
508 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
509 const T *source = v + i * componentCount;
510
511 for (int c = 0; c < componentCount; c++)
512 {
513 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
514 }
515 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400516
517 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400518 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400519 }
520}
521
Luc Ferron7cec3352018-03-13 13:29:34 -0400522template <typename T>
523void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
524{
525 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
526 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
527
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400528 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400529
Olli Etuaho107c7242018-03-20 15:45:35 +0200530 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800531 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400532
Jiawei Shao385b3e02018-03-21 09:43:28 +0800533 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400534 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400535 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400536
537 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
538 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400539
540 if (gl::IsMatrixType(linkedUniform.type))
541 {
542 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400543 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400544 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
545 }
546 else
547 {
548 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
549 v, layoutInfo, &uniformBlock.uniformData);
550 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400551}
552
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400553void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
554{
Jamie Madill76e471e2017-10-21 09:56:01 -0400555 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400556}
557
558void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
559{
Jamie Madill76e471e2017-10-21 09:56:01 -0400560 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400561}
562
563void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
564{
Jamie Madill76e471e2017-10-21 09:56:01 -0400565 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400566}
567
568void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
569{
Jamie Madill76e471e2017-10-21 09:56:01 -0400570 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400571}
572
573void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
574{
Luc Ferron7cec3352018-03-13 13:29:34 -0400575 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400576}
577
578void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
579{
Luc Ferron489243f2018-03-28 16:55:28 -0400580 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400581}
582
583void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
584{
Luc Ferron489243f2018-03-28 16:55:28 -0400585 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400586}
587
588void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
589{
Luc Ferron489243f2018-03-28 16:55:28 -0400590 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400591}
592
593void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
594{
595 UNIMPLEMENTED();
596}
597
598void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
599{
600 UNIMPLEMENTED();
601}
602
603void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
604{
605 UNIMPLEMENTED();
606}
607
608void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
609{
610 UNIMPLEMENTED();
611}
612
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400613template <int cols, int rows>
614void ProgramVk::setUniformMatrixfv(GLint location,
615 GLsizei count,
616 GLboolean transpose,
617 const GLfloat *value)
618{
619 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
620 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
621
Jamie Madill2b858c22018-09-03 13:58:14 -0400622 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400623 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400624 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400625 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
626
627 // Assume an offset of -1 means the block is unused.
628 if (layoutInfo.offset == -1)
629 {
630 continue;
631 }
632
Luc Ferronc8fbff32018-06-04 10:30:48 -0400633 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400634 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
635 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400636
637 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
638 // setter did not update any data. We still want the uniform to be included when we'll
639 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400640 if (updated)
641 {
642 mDefaultUniformBlocksDirty.set(shaderType);
643 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400644 }
645}
646
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400647void ProgramVk::setUniformMatrix2fv(GLint location,
648 GLsizei count,
649 GLboolean transpose,
650 const GLfloat *value)
651{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400652 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400653}
654
655void ProgramVk::setUniformMatrix3fv(GLint location,
656 GLsizei count,
657 GLboolean transpose,
658 const GLfloat *value)
659{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400660 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400661}
662
663void ProgramVk::setUniformMatrix4fv(GLint location,
664 GLsizei count,
665 GLboolean transpose,
666 const GLfloat *value)
667{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400668 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400669}
670
671void ProgramVk::setUniformMatrix2x3fv(GLint location,
672 GLsizei count,
673 GLboolean transpose,
674 const GLfloat *value)
675{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400676 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400677}
678
679void ProgramVk::setUniformMatrix3x2fv(GLint location,
680 GLsizei count,
681 GLboolean transpose,
682 const GLfloat *value)
683{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400684 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400685}
686
687void ProgramVk::setUniformMatrix2x4fv(GLint location,
688 GLsizei count,
689 GLboolean transpose,
690 const GLfloat *value)
691{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400692 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400693}
694
695void ProgramVk::setUniformMatrix4x2fv(GLint location,
696 GLsizei count,
697 GLboolean transpose,
698 const GLfloat *value)
699{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400700 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400701}
702
703void ProgramVk::setUniformMatrix3x4fv(GLint location,
704 GLsizei count,
705 GLboolean transpose,
706 const GLfloat *value)
707{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400708 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400709}
710
711void ProgramVk::setUniformMatrix4x3fv(GLint location,
712 GLsizei count,
713 GLboolean transpose,
714 const GLfloat *value)
715{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400716 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400717}
718
Sami Väisänen46eaa942016-06-29 10:26:37 +0300719void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
720 GLenum genMode,
721 GLint components,
722 const GLfloat *coeffs)
723{
724 UNIMPLEMENTED();
725}
726
Jamie Madill242c4fe2018-07-12 15:56:56 -0400727angle::Result ProgramVk::initShaders(ContextVk *contextVk,
728 const gl::DrawCallParams &drawCallParams,
729 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
730 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
731 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500732{
Jamie Madill06ca6342018-07-12 15:56:53 -0400733 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
Jamie Madill242c4fe2018-07-12 15:56:56 -0400734 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource,
735 vertexShaderAndSerialOut, fragmentShaderAndSerialOut));
736 ASSERT(mDefaultShaderInfo.valid());
737
738 *pipelineLayoutOut = &mPipelineLayout.get();
739
740 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500741}
742
Jamie Madill21061022018-07-12 23:56:30 -0400743angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400744{
Jamie Madill76e471e2017-10-21 09:56:01 -0400745 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400746 vk::DynamicDescriptorPool *dynamicDescriptorPool =
747 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400748
Luc Ferron6ea1b412018-03-21 16:13:01 -0400749 uint32_t potentialNewCount = descriptorSetIndex + 1;
750 if (potentialNewCount > mDescriptorSets.size())
751 {
752 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
753 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400754
Jamie Madillc7918ce2018-06-13 13:25:31 -0400755 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400756 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400757 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400758 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400759 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400760}
761
Jamie Madill54164b02017-08-28 15:17:37 -0400762void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
763{
Luc Ferron7cec3352018-03-13 13:29:34 -0400764 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400765}
766
767void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
768{
Luc Ferron7cec3352018-03-13 13:29:34 -0400769 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400770}
771
772void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
773{
774 UNIMPLEMENTED();
775}
776
Jamie Madill21061022018-07-12 23:56:30 -0400777angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400778{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400779 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400780
Jamie Madill76e471e2017-10-21 09:56:01 -0400781 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400782 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400783 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400784 {
Jamie Madill33318de2018-05-01 11:22:54 -0400785 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400786
Jamie Madill2b858c22018-09-03 13:58:14 -0400787 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400788 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400789 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400790 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400791 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400792 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400793 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400794
795 if (bufferModified)
796 {
797 anyNewBufferAllocated = true;
798 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400799 }
800 }
801
Luc Ferron7a06ac12018-03-15 10:17:04 -0400802 if (anyNewBufferAllocated)
803 {
804 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
805 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400806 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400807 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
808 }
809
Jamie Madill21061022018-07-12 23:56:30 -0400810 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400811}
812
Jamie Madill21061022018-07-12 23:56:30 -0400813angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400814{
Jamie Madill33318de2018-05-01 11:22:54 -0400815 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
816 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400817
Jamie Madill33318de2018-05-01 11:22:54 -0400818 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400819 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400820 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
821 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
822 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400823
824 if (!uniformBlock.uniformData.empty())
825 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400826 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400827 }
828 else
829 {
830 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
831 }
832
833 bufferInfo.offset = 0;
834 bufferInfo.range = VK_WHOLE_SIZE;
835
Jamie Madill76e471e2017-10-21 09:56:01 -0400836 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
837 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400838 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400839 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400840 writeInfo.dstArrayElement = 0;
841 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400842 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400843 writeInfo.pImageInfo = nullptr;
844 writeInfo.pBufferInfo = &bufferInfo;
845 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400846 }
847
848 VkDevice device = contextVk->getDevice();
849
Jamie Madill33318de2018-05-01 11:22:54 -0400850 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400851
Jamie Madill21061022018-07-12 23:56:30 -0400852 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400853}
854
Jamie Madill84c662b2018-07-12 15:56:55 -0400855angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400856{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400857 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400858 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400859
Jamie Madill8c3988c2017-12-21 14:44:56 -0500860 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400861 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400862
Jamie Madill84c662b2018-07-12 15:56:55 -0400863 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
864 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400865 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400866
Jamie Madill84c662b2018-07-12 15:56:55 -0400867 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400868
Jamie Madill4cc753e2018-06-13 13:25:33 -0400869 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
870 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400871 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400872 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
873
Jamie Madill5547b382017-10-23 18:16:01 -0400874 ASSERT(!samplerBinding.unreferenced);
875
Jamie Madill4cc753e2018-06-13 13:25:33 -0400876 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
877 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400878 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400879 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
880 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400881 const vk::ImageHelper &image = textureVk->getImage();
882
883 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
884
885 imageInfo.sampler = textureVk->getSampler().getHandle();
886 imageInfo.imageView = textureVk->getImageView().getHandle();
887 imageInfo.imageLayout = image.getCurrentLayout();
888
889 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
890
891 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
892 writeInfo.pNext = nullptr;
893 writeInfo.dstSet = descriptorSet;
894 writeInfo.dstBinding = textureIndex;
895 writeInfo.dstArrayElement = arrayElement;
896 writeInfo.descriptorCount = 1;
897 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
898 writeInfo.pImageInfo = &imageInfo;
899 writeInfo.pBufferInfo = nullptr;
900 writeInfo.pTexelBufferView = nullptr;
901
902 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400903 }
Jamie Madill5547b382017-10-23 18:16:01 -0400904 }
905
906 VkDevice device = contextVk->getDevice();
907
Jamie Madill4cc753e2018-06-13 13:25:33 -0400908 ASSERT(writeCount > 0);
909 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400910
Jamie Madill84c662b2018-07-12 15:56:55 -0400911 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400912}
913
Luc Ferron7a06ac12018-03-15 10:17:04 -0400914void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
915{
916 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
917 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400918 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400919 }
920}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400921
922angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
923 const gl::DrawCallParams &drawCallParams,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400924 vk::CommandBuffer *commandBuffer)
925{
926 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
927 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400928
929 if (mUsedDescriptorSetRange.empty())
930 return angle::Result::Continue();
931
932 ASSERT(!mDescriptorSets.empty());
933
934 unsigned int low = mUsedDescriptorSetRange.low();
935
936 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
937 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
938 {
939 commandBuffer->bindDescriptorSets(
940 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
941 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
942 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
943 }
944 else
945 {
946 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
947 low, mUsedDescriptorSetRange.length(),
948 &mDescriptorSets[low], 0, nullptr);
949 }
950
951 return angle::Result::Continue();
952}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400953} // namespace rx