blob: 9493b4044933d09f6d09a357010acd0410b2c6c5 [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;
Geoff Langbeb0c942018-09-27 11:22:23 -040071 ASSERT(writePtr + (elementSize * count) <= uniformData->data() + uniformData->size());
Jamie Madill76e471e2017-10-21 09:56:01 -040072 memcpy(writePtr, v, elementSize * count);
73 }
74 else
75 {
Luc Ferron2371aca2018-03-27 16:03:03 -040076 // Have to respect the arrayStride between each element of the array.
77 int maxIndex = arrayIndex + count;
78 for (int writeIndex = arrayIndex, readIndex = 0; writeIndex < maxIndex;
79 writeIndex++, readIndex++)
80 {
81 const int arrayOffset = writeIndex * layoutInfo.arrayStride;
82 uint8_t *writePtr = dst + arrayOffset;
Luc Ferrone9465a62018-06-04 10:41:52 -040083 const T *readPtr = v + (readIndex * componentCount);
Geoff Langbeb0c942018-09-27 11:22:23 -040084 ASSERT(writePtr + elementSize <= uniformData->data() + uniformData->size());
Luc Ferron2371aca2018-03-27 16:03:03 -040085 memcpy(writePtr, readPtr, elementSize);
86 }
Jamie Madill76e471e2017-10-21 09:56:01 -040087 }
88}
89
Luc Ferron7cec3352018-03-13 13:29:34 -040090template <typename T>
91void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040092 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040093 T *dst,
94 const sh::BlockMemberInfo &layoutInfo,
95 const angle::MemoryBuffer *uniformData)
96{
97 ASSERT(layoutInfo.offset != -1);
98
Luc Ferron2371aca2018-03-27 16:03:03 -040099 const int elementSize = sizeof(T) * componentCount;
100 const uint8_t *source = uniformData->data() + layoutInfo.offset;
101
Luc Ferron7cec3352018-03-13 13:29:34 -0400102 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
103 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400104 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400105 memcpy(dst, readPtr, elementSize);
106 }
107 else
108 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400109 // Have to respect the arrayStride between each element of the array.
110 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
111 const uint8_t *readPtr = source + arrayOffset;
112 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400113 }
114}
115
Jamie Madill21061022018-07-12 23:56:30 -0400116angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
117 vk::DynamicBuffer *dynamicBuffer,
118 const angle::MemoryBuffer &bufferData,
119 uint32_t *outOffset,
120 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400121{
Luc Ferron7a06ac12018-03-15 10:17:04 -0400122 ASSERT(!bufferData.empty());
123 uint8_t *data = nullptr;
124 VkBuffer *outBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -0400125 VkDeviceSize offset = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400126 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400127 outBufferModified));
Jamie Madill4c310832018-08-29 13:43:17 -0400128 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400129 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400130 ANGLE_TRY(dynamicBuffer->flush(contextVk));
131 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400132}
Jamie Madillb36a4812018-09-25 10:15:11 -0400133
Jamie Madillbfe31c42018-10-25 17:03:47 -0400134bool UseLineRaster(const ContextVk *contextVk, gl::PrimitiveMode mode)
Jamie Madillb36a4812018-09-25 10:15:11 -0400135{
Jamie Madillbfe31c42018-10-25 17:03:47 -0400136 return contextVk->getFeatures().basicGLLineRasterization && gl::IsLineMode(mode);
Jamie Madillb36a4812018-09-25 10:15:11 -0400137}
Jamie Madill76e471e2017-10-21 09:56:01 -0400138} // anonymous namespace
139
Jamie Madill242c4fe2018-07-12 15:56:56 -0400140// ProgramVk::ShaderInfo implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500141ProgramVk::ShaderInfo::ShaderInfo() {}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400142
143ProgramVk::ShaderInfo::~ShaderInfo() = default;
144
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500145angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk,
146 const std::string &vertexSource,
147 const std::string &fragmentSource,
148 bool enableLineRasterEmulation)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400149{
150 if (!valid())
151 {
152 std::vector<uint32_t> vertexCode;
153 std::vector<uint32_t> fragmentCode;
Jamie Madillb36a4812018-09-25 10:15:11 -0400154 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
155 enableLineRasterEmulation, vertexSource,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400156 fragmentSource, &vertexCode, &fragmentCode));
157
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500158 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(),
159 vertexCode.data(), vertexCode.size() * sizeof(uint32_t)));
160 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(),
161 fragmentCode.data(),
Jamie Madill242c4fe2018-07-12 15:56:56 -0400162 fragmentCode.size() * sizeof(uint32_t)));
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500163
164 mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]);
165 mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400166 }
167
Jamie Madill242c4fe2018-07-12 15:56:56 -0400168 return angle::Result::Continue();
169}
170
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500171void ProgramVk::ShaderInfo::release(RendererVk *renderer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400172{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500173 mProgramHelper.release(renderer);
174
175 for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders)
176 {
177 shader.get().destroy(renderer->getDevice());
178 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400179}
180
181bool ProgramVk::ShaderInfo::valid() const
182{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500183 return mShaders[gl::ShaderType::Vertex].get().valid();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400184}
185
186// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400187ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400188 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500189 kUniformBlockDynamicBufferMinSize,
190 true)
Jamie Madillb980c562018-11-27 11:34:27 -0500191{}
Jamie Madill76e471e2017-10-21 09:56:01 -0400192
Jamie Madill242c4fe2018-07-12 15:56:56 -0400193ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500194
Jamie Madill88fc6da2018-08-30 16:18:36 -0400195ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400196{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500197 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400198}
199
Jamie Madill242c4fe2018-07-12 15:56:56 -0400200ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400201
Jamie Madillf4a789f2018-10-18 16:56:20 -0400202void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500203{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400204 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500205 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400206}
Jamie Madill5deea722017-02-16 10:44:46 -0500207
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500208void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400209{
Jamie Madill9b168d02018-06-13 13:25:32 -0400210 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
211 {
212 descriptorSetLayout.reset();
213 }
214 mPipelineLayout.reset();
215
Jamie Madill76e471e2017-10-21 09:56:01 -0400216 for (auto &uniformBlock : mDefaultUniformBlocks)
217 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400218 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400219 }
220
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500221 mDefaultShaderInfo.release(renderer);
222 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400223
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500224 mEmptyUniformBlockStorage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400225
Jamie Madill5547b382017-10-23 18:16:01 -0400226 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500227 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500228
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400229 for (vk::SharedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
230 {
231 binding.reset();
232 }
Jamie Madill5deea722017-02-16 10:44:46 -0500233}
234
Jamie Madill785e8a02018-10-04 17:42:00 -0400235angle::Result ProgramVk::load(const gl::Context *context,
236 gl::InfoLog &infoLog,
237 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400238{
239 UNIMPLEMENTED();
Jamie Madill785e8a02018-10-04 17:42:00 -0400240 return angle::Result::Stop();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400241}
242
Jamie Madill27a60632017-06-30 15:12:01 -0400243void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400244{
245 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400246}
247
248void ProgramVk::setBinaryRetrievableHint(bool retrievable)
249{
250 UNIMPLEMENTED();
251}
252
Yunchao He61afff12017-03-14 15:34:03 +0800253void ProgramVk::setSeparable(bool separable)
254{
255 UNIMPLEMENTED();
256}
257
Jamie Madill785e8a02018-10-04 17:42:00 -0400258std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800259 const gl::ProgramLinkedResources &resources,
260 gl::InfoLog &infoLog)
261{
262 // TODO(jie.a.chen@intel.com): Parallelize linking.
263 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400264 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800265}
266
Jamie Madill785e8a02018-10-04 17:42:00 -0400267angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
268 const gl::ProgramLinkedResources &resources,
269 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400270{
Jamie Madill06ca6342018-07-12 15:56:53 -0400271 ContextVk *contextVk = vk::GetImpl(glContext);
272 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400273
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500274 reset(renderer);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500275
jchen103fd614d2018-08-13 12:21:58 +0800276 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500277
Jamie Madill76e471e2017-10-21 09:56:01 -0400278 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500279
Jamie Madill8c3988c2017-12-21 14:44:56 -0500280 if (!mState.getSamplerUniformRange().empty())
281 {
282 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400283 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500284 }
285
Jamie Madill9b168d02018-06-13 13:25:32 -0400286 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
287 // don't already exist in the cache.
288 vk::DescriptorSetLayoutDesc uniformsSetDesc;
289 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
290 1);
291 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
292 1);
293
294 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400295 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400296
Jamie Madill9b168d02018-06-13 13:25:32 -0400297 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400298
299 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
300 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400301 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400302 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
303
304 // The front-end always binds array sampler units sequentially.
305 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
306 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400307 }
308
Jamie Madill21061022018-07-12 23:56:30 -0400309 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400310 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
311
Jamie Madillb01b4802018-07-10 12:43:57 -0400312 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
313 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
314 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400315 contextVk, driverUniformsSetDesc,
316 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400317
Jamie Madill9b168d02018-06-13 13:25:32 -0400318 vk::PipelineLayoutDesc pipelineLayoutDesc;
319 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
320 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400321 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
322 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400323
Jamie Madill21061022018-07-12 23:56:30 -0400324 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
325 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400326
Jamie Madill242c4fe2018-07-12 15:56:56 -0400327 if (!mState.getUniforms().empty())
328 {
329 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
330
331 if (mState.getUniforms().size() > samplerRange.length())
332 {
333 // Ensure the descriptor set range includes the uniform buffers at position 0.
334 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
335 }
336
337 if (!samplerRange.empty())
338 {
339 // Ensure the descriptor set range includes the textures at position 1.
340 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400341 }
342 }
343
Jamie Madill785e8a02018-10-04 17:42:00 -0400344 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400345}
346
Jamie Madill242c4fe2018-07-12 15:56:56 -0400347angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400348{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400349 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500350 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400351
352 // Process vertex and fragment uniforms into std140 packing.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500353 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
354 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400355 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400356
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500357 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400358 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500359 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800360 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400361 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
362 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400363 }
364
365 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400366 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400367 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400368 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500369 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400370
Jamie Madill76e471e2017-10-21 09:56:01 -0400371 if (location.used() && !location.ignored)
372 {
Jamie Madillde03e002017-10-21 14:04:20 -0400373 const auto &uniform = uniforms[location.index];
Geoff Langbeb0c942018-09-27 11:22:23 -0400374 if (!uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400375 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400376 std::string uniformName = uniform.name;
377 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400378 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400379 // Gets the uniform name without the [0] at the end.
380 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400381 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400382
Geoff Langbeb0c942018-09-27 11:22:23 -0400383 bool found = false;
384
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500385 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400386 {
387 auto it = layoutMap[shaderType].find(uniformName);
388 if (it != layoutMap[shaderType].end())
389 {
390 found = true;
391 layoutInfo[shaderType] = it->second;
392 }
393 }
394
395 ASSERT(found);
396 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400397 }
398
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500399 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400400 {
Jamie Madill33318de2018-05-01 11:22:54 -0400401 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400402 }
403 }
404
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500405 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400406 {
Jamie Madill33318de2018-05-01 11:22:54 -0400407 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400408 {
Jamie Madill33318de2018-05-01 11:22:54 -0400409 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
410 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400411 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400412 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400413 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400414 size_t minAlignment = static_cast<size_t>(
415 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
416
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400417 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400418
419 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400420 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400421 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400422 }
423 }
424
Jamie Madill2b858c22018-09-03 13:58:14 -0400425 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400426 {
427 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400428 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400429 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400430 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400431 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400432 uniformBufferInfo.flags = 0;
433 uniformBufferInfo.size = 1;
434 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
435 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
436 uniformBufferInfo.queueFamilyIndexCount = 0;
437 uniformBufferInfo.pQueueFamilyIndices = nullptr;
438
Luc Ferron7a06ac12018-03-15 10:17:04 -0400439 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500440 VkMemoryPropertyFlags flags =
441 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500442 ANGLE_TRY(mEmptyUniformBlockStorage.init(contextVk, uniformBufferInfo, flags));
Jamie Madill76e471e2017-10-21 09:56:01 -0400443 }
Jamie Madill5547b382017-10-23 18:16:01 -0400444 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400445
Jamie Madill242c4fe2018-07-12 15:56:56 -0400446 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400447}
448
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400449GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
450{
Luc Ferronfba1f612018-06-04 14:37:17 -0400451 // No-op. The spec is very vague about the behavior of validation.
452 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400453}
454
Jamie Madill76e471e2017-10-21 09:56:01 -0400455template <typename T>
456void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
457{
458 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
459 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
460
Luc Ferron7cec3352018-03-13 13:29:34 -0400461 if (linkedUniform.isSampler())
462 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400463 // We could potentially cache some indexing here. For now this is a no-op since the mapping
464 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400465 return;
466 }
467
Luc Ferron24a31372018-04-04 11:49:14 -0400468 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400469 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500470 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400471 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400472 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400473 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400474
Luc Ferron24a31372018-04-04 11:49:14 -0400475 // Assume an offset of -1 means the block is unused.
476 if (layoutInfo.offset == -1)
477 {
478 continue;
479 }
480
481 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400482 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
483 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400484 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400485 }
Luc Ferron24a31372018-04-04 11:49:14 -0400486 }
487 else
488 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500489 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400490 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400491 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400492 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
493
494 // Assume an offset of -1 means the block is unused.
495 if (layoutInfo.offset == -1)
496 {
497 continue;
498 }
499
500 const GLint componentCount = linkedUniform.typeInfo->componentCount;
501
Luc Ferron62059a52018-03-29 07:01:35 -0400502 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
503
Luc Ferrond91c3792018-04-06 09:36:36 -0400504 GLint initialArrayOffset =
505 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400506 for (GLint i = 0; i < count; i++)
507 {
508 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
509 GLint *dest =
510 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
511 const T *source = v + i * componentCount;
512
513 for (int c = 0; c < componentCount; c++)
514 {
515 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
516 }
517 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400518
519 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400520 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400521 }
522}
523
Luc Ferron7cec3352018-03-13 13:29:34 -0400524template <typename T>
525void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
526{
527 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
528 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
529
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400530 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400531
Olli Etuaho107c7242018-03-20 15:45:35 +0200532 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800533 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400534
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500535 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madillb980c562018-11-27 11:34:27 -0500536 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400537
538 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
539 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400540
541 if (gl::IsMatrixType(linkedUniform.type))
542 {
543 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400544 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400545 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
546 }
547 else
548 {
549 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
550 v, layoutInfo, &uniformBlock.uniformData);
551 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400552}
553
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400554void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
555{
Jamie Madill76e471e2017-10-21 09:56:01 -0400556 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400557}
558
559void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
560{
Jamie Madill76e471e2017-10-21 09:56:01 -0400561 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400562}
563
564void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
565{
Jamie Madill76e471e2017-10-21 09:56:01 -0400566 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400567}
568
569void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
570{
Jamie Madill76e471e2017-10-21 09:56:01 -0400571 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400572}
573
574void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
575{
Luc Ferron7cec3352018-03-13 13:29:34 -0400576 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400577}
578
579void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
580{
Luc Ferron489243f2018-03-28 16:55:28 -0400581 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400582}
583
584void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
585{
Luc Ferron489243f2018-03-28 16:55:28 -0400586 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400587}
588
589void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
590{
Luc Ferron489243f2018-03-28 16:55:28 -0400591 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400592}
593
594void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
595{
596 UNIMPLEMENTED();
597}
598
599void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
600{
601 UNIMPLEMENTED();
602}
603
604void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
605{
606 UNIMPLEMENTED();
607}
608
609void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
610{
611 UNIMPLEMENTED();
612}
613
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400614template <int cols, int rows>
615void ProgramVk::setUniformMatrixfv(GLint location,
616 GLsizei count,
617 GLboolean transpose,
618 const GLfloat *value)
619{
620 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
621 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
622
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500623 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400624 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400625 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400626 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
627
628 // Assume an offset of -1 means the block is unused.
629 if (layoutInfo.offset == -1)
630 {
631 continue;
632 }
633
Luc Ferronc8fbff32018-06-04 10:30:48 -0400634 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400635 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
636 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400637
638 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
639 // setter did not update any data. We still want the uniform to be included when we'll
640 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400641 if (updated)
642 {
643 mDefaultUniformBlocksDirty.set(shaderType);
644 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400645 }
646}
647
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400648void ProgramVk::setUniformMatrix2fv(GLint location,
649 GLsizei count,
650 GLboolean transpose,
651 const GLfloat *value)
652{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400653 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400654}
655
656void ProgramVk::setUniformMatrix3fv(GLint location,
657 GLsizei count,
658 GLboolean transpose,
659 const GLfloat *value)
660{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400661 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400662}
663
664void ProgramVk::setUniformMatrix4fv(GLint location,
665 GLsizei count,
666 GLboolean transpose,
667 const GLfloat *value)
668{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400669 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400670}
671
672void ProgramVk::setUniformMatrix2x3fv(GLint location,
673 GLsizei count,
674 GLboolean transpose,
675 const GLfloat *value)
676{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400677 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400678}
679
680void ProgramVk::setUniformMatrix3x2fv(GLint location,
681 GLsizei count,
682 GLboolean transpose,
683 const GLfloat *value)
684{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400685 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400686}
687
688void ProgramVk::setUniformMatrix2x4fv(GLint location,
689 GLsizei count,
690 GLboolean transpose,
691 const GLfloat *value)
692{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400693 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400694}
695
696void ProgramVk::setUniformMatrix4x2fv(GLint location,
697 GLsizei count,
698 GLboolean transpose,
699 const GLfloat *value)
700{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400701 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400702}
703
704void ProgramVk::setUniformMatrix3x4fv(GLint location,
705 GLsizei count,
706 GLboolean transpose,
707 const GLfloat *value)
708{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400709 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400710}
711
712void ProgramVk::setUniformMatrix4x3fv(GLint location,
713 GLsizei count,
714 GLboolean transpose,
715 const GLfloat *value)
716{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400717 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400718}
719
Sami Väisänen46eaa942016-06-29 10:26:37 +0300720void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
721 GLenum genMode,
722 GLint components,
723 const GLfloat *coeffs)
724{
725 UNIMPLEMENTED();
726}
727
Jamie Madill242c4fe2018-07-12 15:56:56 -0400728angle::Result ProgramVk::initShaders(ContextVk *contextVk,
Jamie Madillbfe31c42018-10-25 17:03:47 -0400729 gl::PrimitiveMode mode,
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500730 vk::ShaderProgramHelper **programOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500731{
Jamie Madillbfe31c42018-10-25 17:03:47 -0400732 if (UseLineRaster(contextVk, mode))
Jamie Madillb36a4812018-09-25 10:15:11 -0400733 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500734 ANGLE_TRY(
735 mLineRasterShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, true));
Jamie Madillb36a4812018-09-25 10:15:11 -0400736 ASSERT(mLineRasterShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500737 *programOut = &mLineRasterShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400738 }
739 else
740 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500741 ANGLE_TRY(mDefaultShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, false));
Jamie Madillb36a4812018-09-25 10:15:11 -0400742 ASSERT(mDefaultShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500743 *programOut = &mDefaultShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400744 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400745
Jamie Madill242c4fe2018-07-12 15:56:56 -0400746 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500747}
748
Jamie Madill21061022018-07-12 23:56:30 -0400749angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400750{
Jamie Madill76e471e2017-10-21 09:56:01 -0400751 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400752 vk::DynamicDescriptorPool *dynamicDescriptorPool =
753 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400754
Luc Ferron6ea1b412018-03-21 16:13:01 -0400755 uint32_t potentialNewCount = descriptorSetIndex + 1;
756 if (potentialNewCount > mDescriptorSets.size())
757 {
758 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
759 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400760
Jamie Madillc7918ce2018-06-13 13:25:31 -0400761 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400762 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400763 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400764 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400765 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400766 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400767}
768
Jamie Madill54164b02017-08-28 15:17:37 -0400769void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
770{
Luc Ferron7cec3352018-03-13 13:29:34 -0400771 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400772}
773
774void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
775{
Luc Ferron7cec3352018-03-13 13:29:34 -0400776 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400777}
778
779void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
780{
781 UNIMPLEMENTED();
782}
783
Jamie Madill21061022018-07-12 23:56:30 -0400784angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400785{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400786 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400787
Jamie Madill76e471e2017-10-21 09:56:01 -0400788 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400789 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500790 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400791 {
Jamie Madill33318de2018-05-01 11:22:54 -0400792 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400793
Jamie Madill2b858c22018-09-03 13:58:14 -0400794 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400795 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400796 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400797 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400798 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400799 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400800 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400801
802 if (bufferModified)
803 {
804 anyNewBufferAllocated = true;
805 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400806 }
807 }
808
Luc Ferron7a06ac12018-03-15 10:17:04 -0400809 if (anyNewBufferAllocated)
810 {
811 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
812 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400813 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400814 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
815 }
816
Jamie Madill21061022018-07-12 23:56:30 -0400817 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400818}
819
Jamie Madill21061022018-07-12 23:56:30 -0400820angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400821{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500822 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
823 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400824
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500825 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400826 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400827 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
828 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
829 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400830
831 if (!uniformBlock.uniformData.empty())
832 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500833 bufferInfo.buffer = uniformBlock.storage.getCurrentBuffer()->getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400834 }
835 else
836 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500837 bufferInfo.buffer = mEmptyUniformBlockStorage.getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400838 }
839
840 bufferInfo.offset = 0;
841 bufferInfo.range = VK_WHOLE_SIZE;
842
Jamie Madill76e471e2017-10-21 09:56:01 -0400843 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
844 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400845 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400846 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400847 writeInfo.dstArrayElement = 0;
848 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400849 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400850 writeInfo.pImageInfo = nullptr;
851 writeInfo.pBufferInfo = &bufferInfo;
852 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400853 }
854
855 VkDevice device = contextVk->getDevice();
856
Jamie Madill33318de2018-05-01 11:22:54 -0400857 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400858
Jamie Madill21061022018-07-12 23:56:30 -0400859 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400860}
861
Jamie Madill84c662b2018-07-12 15:56:55 -0400862angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400863{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400864 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400865 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400866
Jamie Madill8c3988c2017-12-21 14:44:56 -0500867 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400868 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400869
Jamie Madill84c662b2018-07-12 15:56:55 -0400870 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
871 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400872 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400873
Jamie Madill84c662b2018-07-12 15:56:55 -0400874 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400875
Jamie Madill4cc753e2018-06-13 13:25:33 -0400876 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
877 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400878 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400879 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
880
Jamie Madill5547b382017-10-23 18:16:01 -0400881 ASSERT(!samplerBinding.unreferenced);
882
Jamie Madill4cc753e2018-06-13 13:25:33 -0400883 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
884 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400885 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400886 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
887 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400888 const vk::ImageHelper &image = textureVk->getImage();
889
890 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
891
892 imageInfo.sampler = textureVk->getSampler().getHandle();
893 imageInfo.imageView = textureVk->getImageView().getHandle();
894 imageInfo.imageLayout = image.getCurrentLayout();
895
896 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
897
898 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
899 writeInfo.pNext = nullptr;
900 writeInfo.dstSet = descriptorSet;
901 writeInfo.dstBinding = textureIndex;
902 writeInfo.dstArrayElement = arrayElement;
903 writeInfo.descriptorCount = 1;
904 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
905 writeInfo.pImageInfo = &imageInfo;
906 writeInfo.pBufferInfo = nullptr;
907 writeInfo.pTexelBufferView = nullptr;
908
909 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400910 }
Jamie Madill5547b382017-10-23 18:16:01 -0400911 }
912
913 VkDevice device = contextVk->getDevice();
914
Jamie Madill4cc753e2018-06-13 13:25:33 -0400915 ASSERT(writeCount > 0);
916 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400917
Jamie Madill84c662b2018-07-12 15:56:55 -0400918 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400919}
920
Luc Ferron7a06ac12018-03-15 10:17:04 -0400921void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
922{
923 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
924 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400925 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400926 }
927}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400928
929angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400930 vk::CommandBuffer *commandBuffer)
931{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400932 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400933
934 if (mUsedDescriptorSetRange.empty())
935 return angle::Result::Continue();
936
937 ASSERT(!mDescriptorSets.empty());
938
939 unsigned int low = mUsedDescriptorSetRange.low();
940
941 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
942 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
943 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500944 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
945 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400946 commandBuffer->bindDescriptorSets(
947 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
948 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500949 kShaderTypeMax - kShaderTypeMin + 1, mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400950 }
951 else
952 {
953 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
954 low, mUsedDescriptorSetRange.length(),
955 &mDescriptorSets[low], 0, nullptr);
956 }
957
958 return angle::Result::Continue();
959}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400960} // namespace rx