blob: 64dccb716e0214dba22f02c61946bf1af9785201 [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 Madillc564c072017-06-01 12:45:42 -040013#include "libANGLE/Context.h"
Luc Ferron48cdc2e2018-05-31 09:58:34 -040014#include "libANGLE/renderer/renderer_utils.h"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050015#include "libANGLE/renderer/vulkan/ContextVk.h"
16#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
17#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill5547b382017-10-23 18:16:01 -040018#include "libANGLE/renderer/vulkan/TextureVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040019
20namespace rx
21{
22
Jamie Madill76e471e2017-10-21 09:56:01 -040023namespace
24{
25
Jamie Madillf3614372018-03-31 14:19:14 -040026constexpr size_t kUniformBlockDynamicBufferMinSize = 256 * 128;
Luc Ferron7a06ac12018-03-15 10:17:04 -040027
Jamie Madill242c4fe2018-07-12 15:56:56 -040028void InitDefaultUniformBlock(const std::vector<sh::Uniform> &uniforms,
Jamie Madill21061022018-07-12 23:56:30 -040029 gl::Shader *shader,
30 sh::BlockLayoutMap *blockLayoutMapOut,
31 size_t *blockSizeOut)
Jamie Madill76e471e2017-10-21 09:56:01 -040032{
Jamie Madill76e471e2017-10-21 09:56:01 -040033 if (uniforms.empty())
34 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040035 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040036 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040037 }
38
39 sh::Std140BlockEncoder blockEncoder;
Olli Etuaho3de27032017-11-30 12:16:47 +020040 sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, blockLayoutMapOut);
Jamie Madill76e471e2017-10-21 09:56:01 -040041
Jamie Madill4e712be2019-01-03 13:53:59 -050042 size_t blockSize = blockEncoder.getCurrentOffset();
Jamie Madill76e471e2017-10-21 09:56:01 -040043
44 // TODO(jmadill): I think we still need a valid block for the pipeline even if zero sized.
45 if (blockSize == 0)
46 {
Luc Ferron7a06ac12018-03-15 10:17:04 -040047 *blockSizeOut = 0;
Jamie Madill21061022018-07-12 23:56:30 -040048 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040049 }
50
Luc Ferron7a06ac12018-03-15 10:17:04 -040051 *blockSizeOut = blockSize;
Jamie Madill21061022018-07-12 23:56:30 -040052 return;
Jamie Madill76e471e2017-10-21 09:56:01 -040053}
54
55template <typename T>
56void UpdateDefaultUniformBlock(GLsizei count,
Luc Ferron2371aca2018-03-27 16:03:03 -040057 uint32_t arrayIndex,
Jamie Madill76e471e2017-10-21 09:56:01 -040058 int componentCount,
59 const T *v,
60 const sh::BlockMemberInfo &layoutInfo,
61 angle::MemoryBuffer *uniformData)
62{
Luc Ferron2371aca2018-03-27 16:03:03 -040063 const int elementSize = sizeof(T) * componentCount;
64
65 uint8_t *dst = uniformData->data() + layoutInfo.offset;
Jamie Madill76e471e2017-10-21 09:56:01 -040066 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
67 {
Luc Ferron2371aca2018-03-27 16:03:03 -040068 uint32_t arrayOffset = arrayIndex * layoutInfo.arrayStride;
69 uint8_t *writePtr = dst + arrayOffset;
Geoff Langbeb0c942018-09-27 11:22:23 -040070 ASSERT(writePtr + (elementSize * count) <= uniformData->data() + uniformData->size());
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);
Geoff Langbeb0c942018-09-27 11:22:23 -040083 ASSERT(writePtr + elementSize <= uniformData->data() + uniformData->size());
Luc Ferron2371aca2018-03-27 16:03:03 -040084 memcpy(writePtr, readPtr, elementSize);
85 }
Jamie Madill76e471e2017-10-21 09:56:01 -040086 }
87}
88
Luc Ferron7cec3352018-03-13 13:29:34 -040089template <typename T>
90void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040091 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040092 T *dst,
93 const sh::BlockMemberInfo &layoutInfo,
94 const angle::MemoryBuffer *uniformData)
95{
96 ASSERT(layoutInfo.offset != -1);
97
Luc Ferron2371aca2018-03-27 16:03:03 -040098 const int elementSize = sizeof(T) * componentCount;
99 const uint8_t *source = uniformData->data() + layoutInfo.offset;
100
Luc Ferron7cec3352018-03-13 13:29:34 -0400101 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
102 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400103 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400104 memcpy(dst, readPtr, elementSize);
105 }
106 else
107 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400108 // Have to respect the arrayStride between each element of the array.
109 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
110 const uint8_t *readPtr = source + arrayOffset;
111 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400112 }
113}
114
Jamie Madill21061022018-07-12 23:56:30 -0400115angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
116 vk::DynamicBuffer *dynamicBuffer,
117 const angle::MemoryBuffer &bufferData,
118 uint32_t *outOffset,
119 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400120{
Shahbaz Youssefi5829c032018-11-28 10:39:41 -0500121 dynamicBuffer->releaseRetainedBuffers(contextVk->getRenderer());
122
Luc Ferron7a06ac12018-03-15 10:17:04 -0400123 ASSERT(!bufferData.empty());
124 uint8_t *data = nullptr;
125 VkBuffer *outBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -0400126 VkDeviceSize offset = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400127 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400128 outBufferModified));
Jamie Madill4c310832018-08-29 13:43:17 -0400129 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400130 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400131 ANGLE_TRY(dynamicBuffer->flush(contextVk));
Jamie Madill7c985f52018-11-29 18:16:17 -0500132 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400133}
Jamie Madill76e471e2017-10-21 09:56:01 -0400134} // anonymous namespace
135
Jamie Madill242c4fe2018-07-12 15:56:56 -0400136// ProgramVk::ShaderInfo implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500137ProgramVk::ShaderInfo::ShaderInfo() {}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400138
139ProgramVk::ShaderInfo::~ShaderInfo() = default;
140
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500141angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk,
142 const std::string &vertexSource,
143 const std::string &fragmentSource,
144 bool enableLineRasterEmulation)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400145{
Jamie Madilldbc605c2019-01-04 16:39:14 -0500146 ASSERT(!valid());
Jamie Madill242c4fe2018-07-12 15:56:56 -0400147
Jamie Madilldbc605c2019-01-04 16:39:14 -0500148 std::vector<uint32_t> vertexCode;
149 std::vector<uint32_t> fragmentCode;
150 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
151 enableLineRasterEmulation, vertexSource, fragmentSource,
152 &vertexCode, &fragmentCode));
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500153
Jamie Madilldbc605c2019-01-04 16:39:14 -0500154 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(),
155 vertexCode.data(), vertexCode.size() * sizeof(uint32_t)));
156 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(),
157 fragmentCode.data(), fragmentCode.size() * sizeof(uint32_t)));
158
159 mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]);
160 mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400161
Jamie Madill7c985f52018-11-29 18:16:17 -0500162 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400163}
164
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500165void ProgramVk::ShaderInfo::release(RendererVk *renderer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400166{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500167 mProgramHelper.release(renderer);
168
169 for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders)
170 {
171 shader.get().destroy(renderer->getDevice());
172 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400173}
174
Jamie Madill242c4fe2018-07-12 15:56:56 -0400175// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400176ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400177 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500178 kUniformBlockDynamicBufferMinSize,
179 true)
Jamie Madillb980c562018-11-27 11:34:27 -0500180{}
Jamie Madill76e471e2017-10-21 09:56:01 -0400181
Jamie Madill242c4fe2018-07-12 15:56:56 -0400182ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500183
Jamie Madill88fc6da2018-08-30 16:18:36 -0400184ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400185{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500186 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400187}
188
Jamie Madill242c4fe2018-07-12 15:56:56 -0400189ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400190
Jamie Madillf4a789f2018-10-18 16:56:20 -0400191void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500192{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400193 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500194 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400195}
Jamie Madill5deea722017-02-16 10:44:46 -0500196
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500197void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400198{
Jamie Madill9b168d02018-06-13 13:25:32 -0400199 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
200 {
201 descriptorSetLayout.reset();
202 }
203 mPipelineLayout.reset();
204
Jamie Madill76e471e2017-10-21 09:56:01 -0400205 for (auto &uniformBlock : mDefaultUniformBlocks)
206 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400207 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400208 }
209
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500210 mDefaultShaderInfo.release(renderer);
211 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400212
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500213 mEmptyUniformBlockStorage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400214
Jamie Madill5547b382017-10-23 18:16:01 -0400215 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500216 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500217
Shahbaz Youssefid8381782019-03-04 11:07:47 -0500218 for (vk::RefCountedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400219 {
220 binding.reset();
221 }
Jamie Madill5deea722017-02-16 10:44:46 -0500222}
223
Geoff Lange332e622019-02-14 12:53:04 -0500224std::unique_ptr<rx::LinkEvent> ProgramVk::load(const gl::Context *context,
225 gl::BinaryInputStream *stream,
226 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400227{
228 UNIMPLEMENTED();
Geoff Lange332e622019-02-14 12:53:04 -0500229 return std::make_unique<LinkEventDone>(angle::Result::Stop);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400230}
231
Jamie Madill27a60632017-06-30 15:12:01 -0400232void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400233{
234 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400235}
236
237void ProgramVk::setBinaryRetrievableHint(bool retrievable)
238{
239 UNIMPLEMENTED();
240}
241
Yunchao He61afff12017-03-14 15:34:03 +0800242void ProgramVk::setSeparable(bool separable)
243{
244 UNIMPLEMENTED();
245}
246
Jamie Madill785e8a02018-10-04 17:42:00 -0400247std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800248 const gl::ProgramLinkedResources &resources,
249 gl::InfoLog &infoLog)
250{
251 // TODO(jie.a.chen@intel.com): Parallelize linking.
252 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400253 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800254}
255
Jamie Madill785e8a02018-10-04 17:42:00 -0400256angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
257 const gl::ProgramLinkedResources &resources,
258 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400259{
Jamie Madill06ca6342018-07-12 15:56:53 -0400260 ContextVk *contextVk = vk::GetImpl(glContext);
261 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400262
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500263 reset(renderer);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500264
jchen103fd614d2018-08-13 12:21:58 +0800265 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500266
Jamie Madill76e471e2017-10-21 09:56:01 -0400267 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500268
Jamie Madill8c3988c2017-12-21 14:44:56 -0500269 if (!mState.getSamplerUniformRange().empty())
270 {
271 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400272 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500273 }
274
Jamie Madill9b168d02018-06-13 13:25:32 -0400275 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
276 // don't already exist in the cache.
277 vk::DescriptorSetLayoutDesc uniformsSetDesc;
278 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
279 1);
280 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
281 1);
282
283 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400284 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400285
Jamie Madill9b168d02018-06-13 13:25:32 -0400286 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400287
288 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
289 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400290 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400291 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
292
293 // The front-end always binds array sampler units sequentially.
294 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
295 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400296 }
297
Jamie Madill21061022018-07-12 23:56:30 -0400298 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400299 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
300
Jamie Madillb01b4802018-07-10 12:43:57 -0400301 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
302 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
303 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400304 contextVk, driverUniformsSetDesc,
305 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400306
Jamie Madill9b168d02018-06-13 13:25:32 -0400307 vk::PipelineLayoutDesc pipelineLayoutDesc;
308 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
309 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400310 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
311 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400312
Jamie Madill21061022018-07-12 23:56:30 -0400313 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
314 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400315
Jamie Madill242c4fe2018-07-12 15:56:56 -0400316 if (!mState.getUniforms().empty())
317 {
318 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
319
320 if (mState.getUniforms().size() > samplerRange.length())
321 {
322 // Ensure the descriptor set range includes the uniform buffers at position 0.
323 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
324 }
325
326 if (!samplerRange.empty())
327 {
328 // Ensure the descriptor set range includes the textures at position 1.
329 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400330 }
331 }
332
Jamie Madill7c985f52018-11-29 18:16:17 -0500333 return angle::Result::Continue;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400334}
335
Jamie Madill242c4fe2018-07-12 15:56:56 -0400336angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400337{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400338 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500339 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400340
341 // Process vertex and fragment uniforms into std140 packing.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500342 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
343 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400344 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400345
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500346 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400347 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500348 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800349 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400350 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
351 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400352 }
353
354 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400355 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400356 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400357 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500358 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400359
Jamie Madill76e471e2017-10-21 09:56:01 -0400360 if (location.used() && !location.ignored)
361 {
Jamie Madillde03e002017-10-21 14:04:20 -0400362 const auto &uniform = uniforms[location.index];
Geoff Langbeb0c942018-09-27 11:22:23 -0400363 if (!uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400365 std::string uniformName = uniform.name;
366 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400367 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400368 // Gets the uniform name without the [0] at the end.
369 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400370 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400371
Geoff Langbeb0c942018-09-27 11:22:23 -0400372 bool found = false;
373
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500374 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400375 {
376 auto it = layoutMap[shaderType].find(uniformName);
377 if (it != layoutMap[shaderType].end())
378 {
379 found = true;
380 layoutInfo[shaderType] = it->second;
381 }
382 }
383
384 ASSERT(found);
385 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400386 }
387
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500388 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400389 {
Jamie Madill33318de2018-05-01 11:22:54 -0400390 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400391 }
392 }
393
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500394 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400395 {
Jamie Madill33318de2018-05-01 11:22:54 -0400396 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400397 {
Jamie Madill33318de2018-05-01 11:22:54 -0400398 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
399 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400400 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400401 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400402 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400403 size_t minAlignment = static_cast<size_t>(
404 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
405
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400406 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400407
408 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400409 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400410 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400411 }
412 }
413
Jamie Madill2b858c22018-09-03 13:58:14 -0400414 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400415 {
416 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400417 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400418 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400419 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400420 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400421 uniformBufferInfo.flags = 0;
422 uniformBufferInfo.size = 1;
423 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
424 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
425 uniformBufferInfo.queueFamilyIndexCount = 0;
426 uniformBufferInfo.pQueueFamilyIndices = nullptr;
427
Luc Ferron7a06ac12018-03-15 10:17:04 -0400428 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500429 VkMemoryPropertyFlags flags =
430 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500431 ANGLE_TRY(mEmptyUniformBlockStorage.init(contextVk, uniformBufferInfo, flags));
Jamie Madill76e471e2017-10-21 09:56:01 -0400432 }
Jamie Madill5547b382017-10-23 18:16:01 -0400433 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400434
Jamie Madill7c985f52018-11-29 18:16:17 -0500435 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400436}
437
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400438GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
439{
Luc Ferronfba1f612018-06-04 14:37:17 -0400440 // No-op. The spec is very vague about the behavior of validation.
441 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400442}
443
Jamie Madill76e471e2017-10-21 09:56:01 -0400444template <typename T>
445void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
446{
447 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
448 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
449
Luc Ferron7cec3352018-03-13 13:29:34 -0400450 if (linkedUniform.isSampler())
451 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400452 // We could potentially cache some indexing here. For now this is a no-op since the mapping
453 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400454 return;
455 }
456
Luc Ferron24a31372018-04-04 11:49:14 -0400457 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400458 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500459 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400460 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400461 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400462 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400463
Luc Ferron24a31372018-04-04 11:49:14 -0400464 // Assume an offset of -1 means the block is unused.
465 if (layoutInfo.offset == -1)
466 {
467 continue;
468 }
469
470 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400471 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
472 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400473 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400474 }
Luc Ferron24a31372018-04-04 11:49:14 -0400475 }
476 else
477 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500478 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400479 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400480 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400481 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
482
483 // Assume an offset of -1 means the block is unused.
484 if (layoutInfo.offset == -1)
485 {
486 continue;
487 }
488
489 const GLint componentCount = linkedUniform.typeInfo->componentCount;
490
Luc Ferron62059a52018-03-29 07:01:35 -0400491 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
492
Luc Ferrond91c3792018-04-06 09:36:36 -0400493 GLint initialArrayOffset =
494 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400495 for (GLint i = 0; i < count; i++)
496 {
497 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
498 GLint *dest =
499 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
500 const T *source = v + i * componentCount;
501
502 for (int c = 0; c < componentCount; c++)
503 {
504 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
505 }
506 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400507
508 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400509 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400510 }
511}
512
Luc Ferron7cec3352018-03-13 13:29:34 -0400513template <typename T>
514void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
515{
516 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
517 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
518
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400519 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400520
Olli Etuaho107c7242018-03-20 15:45:35 +0200521 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800522 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400523
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500524 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madillb980c562018-11-27 11:34:27 -0500525 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400526
527 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
528 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400529
530 if (gl::IsMatrixType(linkedUniform.type))
531 {
532 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400533 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400534 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
535 }
536 else
537 {
538 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
539 v, layoutInfo, &uniformBlock.uniformData);
540 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400541}
542
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400543void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
544{
Jamie Madill76e471e2017-10-21 09:56:01 -0400545 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400546}
547
548void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
549{
Jamie Madill76e471e2017-10-21 09:56:01 -0400550 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400551}
552
553void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
554{
Jamie Madill76e471e2017-10-21 09:56:01 -0400555 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400556}
557
558void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
559{
Jamie Madill76e471e2017-10-21 09:56:01 -0400560 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400561}
562
563void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
564{
Luc Ferron7cec3352018-03-13 13:29:34 -0400565 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400566}
567
568void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
569{
Luc Ferron489243f2018-03-28 16:55:28 -0400570 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400571}
572
573void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
574{
Luc Ferron489243f2018-03-28 16:55:28 -0400575 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400576}
577
578void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
579{
Luc Ferron489243f2018-03-28 16:55:28 -0400580 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400581}
582
583void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
584{
585 UNIMPLEMENTED();
586}
587
588void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
589{
590 UNIMPLEMENTED();
591}
592
593void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
594{
595 UNIMPLEMENTED();
596}
597
598void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
599{
600 UNIMPLEMENTED();
601}
602
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400603template <int cols, int rows>
604void ProgramVk::setUniformMatrixfv(GLint location,
605 GLsizei count,
606 GLboolean transpose,
607 const GLfloat *value)
608{
609 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
610 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
611
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500612 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400613 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400614 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400615 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
616
617 // Assume an offset of -1 means the block is unused.
618 if (layoutInfo.offset == -1)
619 {
620 continue;
621 }
622
Luc Ferronc8fbff32018-06-04 10:30:48 -0400623 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400624 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
625 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400626
627 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
628 // setter did not update any data. We still want the uniform to be included when we'll
629 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400630 if (updated)
631 {
632 mDefaultUniformBlocksDirty.set(shaderType);
633 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400634 }
635}
636
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400637void ProgramVk::setUniformMatrix2fv(GLint location,
638 GLsizei count,
639 GLboolean transpose,
640 const GLfloat *value)
641{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400642 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400643}
644
645void ProgramVk::setUniformMatrix3fv(GLint location,
646 GLsizei count,
647 GLboolean transpose,
648 const GLfloat *value)
649{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400650 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400651}
652
653void ProgramVk::setUniformMatrix4fv(GLint location,
654 GLsizei count,
655 GLboolean transpose,
656 const GLfloat *value)
657{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400658 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400659}
660
661void ProgramVk::setUniformMatrix2x3fv(GLint location,
662 GLsizei count,
663 GLboolean transpose,
664 const GLfloat *value)
665{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400666 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400667}
668
669void ProgramVk::setUniformMatrix3x2fv(GLint location,
670 GLsizei count,
671 GLboolean transpose,
672 const GLfloat *value)
673{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400674 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400675}
676
677void ProgramVk::setUniformMatrix2x4fv(GLint location,
678 GLsizei count,
679 GLboolean transpose,
680 const GLfloat *value)
681{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400682 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400683}
684
685void ProgramVk::setUniformMatrix4x2fv(GLint location,
686 GLsizei count,
687 GLboolean transpose,
688 const GLfloat *value)
689{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400690 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400691}
692
693void ProgramVk::setUniformMatrix3x4fv(GLint location,
694 GLsizei count,
695 GLboolean transpose,
696 const GLfloat *value)
697{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400698 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400699}
700
701void ProgramVk::setUniformMatrix4x3fv(GLint location,
702 GLsizei count,
703 GLboolean transpose,
704 const GLfloat *value)
705{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400706 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400707}
708
Sami Väisänen46eaa942016-06-29 10:26:37 +0300709void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
710 GLenum genMode,
711 GLint components,
712 const GLfloat *coeffs)
713{
714 UNIMPLEMENTED();
715}
716
Jamie Madill21061022018-07-12 23:56:30 -0400717angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400718{
Jamie Madill76e471e2017-10-21 09:56:01 -0400719 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400720 vk::DynamicDescriptorPool *dynamicDescriptorPool =
721 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400722
Luc Ferron6ea1b412018-03-21 16:13:01 -0400723 uint32_t potentialNewCount = descriptorSetIndex + 1;
724 if (potentialNewCount > mDescriptorSets.size())
725 {
726 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
727 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400728
Jamie Madillc7918ce2018-06-13 13:25:31 -0400729 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400730 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400731 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400732 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400733 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill7c985f52018-11-29 18:16:17 -0500734 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400735}
736
Jamie Madill54164b02017-08-28 15:17:37 -0400737void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
738{
Luc Ferron7cec3352018-03-13 13:29:34 -0400739 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400740}
741
742void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
743{
Luc Ferron7cec3352018-03-13 13:29:34 -0400744 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400745}
746
747void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
748{
749 UNIMPLEMENTED();
750}
751
Jamie Madill21061022018-07-12 23:56:30 -0400752angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400753{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400754 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400755
Jamie Madill76e471e2017-10-21 09:56:01 -0400756 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400757 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500758 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400759 {
Jamie Madill33318de2018-05-01 11:22:54 -0400760 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400761
Jamie Madill2b858c22018-09-03 13:58:14 -0400762 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400763 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400764 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400765 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400766 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400767 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400768 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400769
770 if (bufferModified)
771 {
772 anyNewBufferAllocated = true;
773 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400774 }
775 }
776
Luc Ferron7a06ac12018-03-15 10:17:04 -0400777 if (anyNewBufferAllocated)
778 {
779 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
780 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400781 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400782 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
783 }
784
Jamie Madill7c985f52018-11-29 18:16:17 -0500785 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400786}
787
Jamie Madill21061022018-07-12 23:56:30 -0400788angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400789{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500790 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
791 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400792
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500793 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400794 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400795 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
796 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
797 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400798
799 if (!uniformBlock.uniformData.empty())
800 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500801 bufferInfo.buffer = uniformBlock.storage.getCurrentBuffer()->getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400802 }
803 else
804 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500805 bufferInfo.buffer = mEmptyUniformBlockStorage.getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400806 }
807
808 bufferInfo.offset = 0;
809 bufferInfo.range = VK_WHOLE_SIZE;
810
Jamie Madill76e471e2017-10-21 09:56:01 -0400811 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
812 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400813 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400814 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400815 writeInfo.dstArrayElement = 0;
816 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400817 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400818 writeInfo.pImageInfo = nullptr;
819 writeInfo.pBufferInfo = &bufferInfo;
820 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400821 }
822
823 VkDevice device = contextVk->getDevice();
824
Jamie Madill33318de2018-05-01 11:22:54 -0400825 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400826
Jamie Madill7c985f52018-11-29 18:16:17 -0500827 return angle::Result::Continue;
Jamie Madill76e471e2017-10-21 09:56:01 -0400828}
829
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100830angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk,
831 vk::FramebufferHelper *framebuffer)
Jamie Madill5547b382017-10-23 18:16:01 -0400832{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400833 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400834 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400835
Jamie Madill8c3988c2017-12-21 14:44:56 -0500836 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400837 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400838
Jamie Madill84c662b2018-07-12 15:56:55 -0400839 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
840 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400841 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400842
Jamie Madill84c662b2018-07-12 15:56:55 -0400843 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400844
Jamie Madill4cc753e2018-06-13 13:25:33 -0400845 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
846 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400847 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400848 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
849
Jamie Madill5547b382017-10-23 18:16:01 -0400850 ASSERT(!samplerBinding.unreferenced);
851
Jamie Madill4cc753e2018-06-13 13:25:33 -0400852 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
853 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400854 {
Jamie Madilldbc605c2019-01-04 16:39:14 -0500855 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
856 TextureVk *textureVk = activeTextures[textureUnit];
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100857
858 // Ensure any writes to the textures are flushed before we read from them.
859 ANGLE_TRY(textureVk->ensureImageInitialized(contextVk));
860 vk::ImageHelper &image = textureVk->getImage();
861
862 // Ensure the image is in read-only layout
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500863 if (image.isLayoutChangeNecessary(vk::ImageLayout::FragmentShaderReadOnly))
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100864 {
Tobin Ehlis134425c2019-03-15 17:02:17 -0600865 CommandBufferT *srcLayoutChange;
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100866 ANGLE_TRY(image.recordCommands(contextVk, &srcLayoutChange));
867
Shahbaz Youssefi7dafe3e2019-01-28 11:39:15 -0500868 image.changeLayout(VK_IMAGE_ASPECT_COLOR_BIT,
869 vk::ImageLayout::FragmentShaderReadOnly, srcLayoutChange);
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100870 }
871
872 image.addReadDependency(framebuffer);
Jamie Madill4cc753e2018-06-13 13:25:33 -0400873
874 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
875
876 imageInfo.sampler = textureVk->getSampler().getHandle();
Shahbaz Youssefif83a28a2018-12-09 03:48:34 +0100877 imageInfo.imageView = textureVk->getReadImageView().getHandle();
Jamie Madill4cc753e2018-06-13 13:25:33 -0400878 imageInfo.imageLayout = image.getCurrentLayout();
879
880 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
881
882 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
883 writeInfo.pNext = nullptr;
884 writeInfo.dstSet = descriptorSet;
885 writeInfo.dstBinding = textureIndex;
886 writeInfo.dstArrayElement = arrayElement;
887 writeInfo.descriptorCount = 1;
888 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
889 writeInfo.pImageInfo = &imageInfo;
890 writeInfo.pBufferInfo = nullptr;
891 writeInfo.pTexelBufferView = nullptr;
892
893 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400894 }
Jamie Madill5547b382017-10-23 18:16:01 -0400895 }
896
897 VkDevice device = contextVk->getDevice();
898
Jamie Madill4cc753e2018-06-13 13:25:33 -0400899 ASSERT(writeCount > 0);
900 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400901
Jamie Madill7c985f52018-11-29 18:16:17 -0500902 return angle::Result::Continue;
Jamie Madill5547b382017-10-23 18:16:01 -0400903}
904
Luc Ferron7a06ac12018-03-15 10:17:04 -0400905void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
906{
907 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
908 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400909 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400910 }
911}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400912
Tobin Ehlis134425c2019-03-15 17:02:17 -0600913angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk, CommandBufferT *commandBuffer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400914{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400915 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400916
917 if (mUsedDescriptorSetRange.empty())
Jamie Madill7c985f52018-11-29 18:16:17 -0500918 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400919
920 ASSERT(!mDescriptorSets.empty());
921
922 unsigned int low = mUsedDescriptorSetRange.low();
923
924 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
925 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
926 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500927 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
928 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400929 commandBuffer->bindDescriptorSets(
930 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
931 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500932 kShaderTypeMax - kShaderTypeMin + 1, mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400933 }
934 else
935 {
936 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
937 low, mUsedDescriptorSetRange.length(),
938 &mDescriptorSets[low], 0, nullptr);
939 }
940
Jamie Madill7c985f52018-11-29 18:16:17 -0500941 return angle::Result::Continue;
Jamie Madill242c4fe2018-07-12 15:56:56 -0400942}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400943} // namespace rx