blob: fc5276c35681c92c564e1ea0987eba25e5cb729c [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,
Jamie Madill2b858c22018-09-03 13:58:14 -0400189 kUniformBlockDynamicBufferMinSize)
Jamie Madillb980c562018-11-27 11:34:27 -0500190{}
Jamie Madill76e471e2017-10-21 09:56:01 -0400191
Jamie Madill242c4fe2018-07-12 15:56:56 -0400192ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500193
Jamie Madill88fc6da2018-08-30 16:18:36 -0400194ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400195{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500196 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400197}
198
Jamie Madill242c4fe2018-07-12 15:56:56 -0400199ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400200
Jamie Madillf4a789f2018-10-18 16:56:20 -0400201void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500202{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400203 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500204 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400205}
Jamie Madill5deea722017-02-16 10:44:46 -0500206
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500207void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400208{
Jamie Madill9b168d02018-06-13 13:25:32 -0400209 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
210 {
211 descriptorSetLayout.reset();
212 }
213 mPipelineLayout.reset();
214
Jamie Madill76e471e2017-10-21 09:56:01 -0400215 for (auto &uniformBlock : mDefaultUniformBlocks)
216 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400217 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400218 }
219
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500220 mDefaultShaderInfo.release(renderer);
221 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400222
Jamie Madillcaaff162018-06-22 08:55:37 -0400223 Serial currentSerial = renderer->getCurrentQueueSerial();
224 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
225 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400226
Jamie Madill5547b382017-10-23 18:16:01 -0400227 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500228 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500229
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400230 for (vk::SharedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
231 {
232 binding.reset();
233 }
Jamie Madill5deea722017-02-16 10:44:46 -0500234}
235
Jamie Madill785e8a02018-10-04 17:42:00 -0400236angle::Result ProgramVk::load(const gl::Context *context,
237 gl::InfoLog &infoLog,
238 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400239{
240 UNIMPLEMENTED();
Jamie Madill785e8a02018-10-04 17:42:00 -0400241 return angle::Result::Stop();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400242}
243
Jamie Madill27a60632017-06-30 15:12:01 -0400244void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400245{
246 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400247}
248
249void ProgramVk::setBinaryRetrievableHint(bool retrievable)
250{
251 UNIMPLEMENTED();
252}
253
Yunchao He61afff12017-03-14 15:34:03 +0800254void ProgramVk::setSeparable(bool separable)
255{
256 UNIMPLEMENTED();
257}
258
Jamie Madill785e8a02018-10-04 17:42:00 -0400259std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800260 const gl::ProgramLinkedResources &resources,
261 gl::InfoLog &infoLog)
262{
263 // TODO(jie.a.chen@intel.com): Parallelize linking.
264 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400265 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800266}
267
Jamie Madill785e8a02018-10-04 17:42:00 -0400268angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
269 const gl::ProgramLinkedResources &resources,
270 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400271{
Jamie Madill06ca6342018-07-12 15:56:53 -0400272 ContextVk *contextVk = vk::GetImpl(glContext);
273 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400274
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500275 reset(renderer);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500276
jchen103fd614d2018-08-13 12:21:58 +0800277 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500278
Jamie Madill76e471e2017-10-21 09:56:01 -0400279 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500280
Jamie Madill8c3988c2017-12-21 14:44:56 -0500281 if (!mState.getSamplerUniformRange().empty())
282 {
283 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400284 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500285 }
286
Jamie Madill9b168d02018-06-13 13:25:32 -0400287 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
288 // don't already exist in the cache.
289 vk::DescriptorSetLayoutDesc uniformsSetDesc;
290 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
291 1);
292 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
293 1);
294
295 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400296 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400297
Jamie Madill9b168d02018-06-13 13:25:32 -0400298 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400299
300 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
301 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400302 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400303 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
304
305 // The front-end always binds array sampler units sequentially.
306 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
307 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400308 }
309
Jamie Madill21061022018-07-12 23:56:30 -0400310 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400311 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
312
Jamie Madillb01b4802018-07-10 12:43:57 -0400313 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
314 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
315 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400316 contextVk, driverUniformsSetDesc,
317 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400318
Jamie Madill9b168d02018-06-13 13:25:32 -0400319 vk::PipelineLayoutDesc pipelineLayoutDesc;
320 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
321 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400322 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
323 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400324
Jamie Madill21061022018-07-12 23:56:30 -0400325 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
326 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400327
Jamie Madill242c4fe2018-07-12 15:56:56 -0400328 if (!mState.getUniforms().empty())
329 {
330 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
331
332 if (mState.getUniforms().size() > samplerRange.length())
333 {
334 // Ensure the descriptor set range includes the uniform buffers at position 0.
335 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
336 }
337
338 if (!samplerRange.empty())
339 {
340 // Ensure the descriptor set range includes the textures at position 1.
341 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400342 }
343 }
344
Jamie Madill785e8a02018-10-04 17:42:00 -0400345 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400346}
347
Jamie Madill242c4fe2018-07-12 15:56:56 -0400348angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400349{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400350 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500351 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400352
353 // Process vertex and fragment uniforms into std140 packing.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500354 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
355 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400356 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400357
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500358 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400359 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500360 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800361 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400362 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
363 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400364 }
365
366 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400367 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400368 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400369 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500370 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400371
Jamie Madill76e471e2017-10-21 09:56:01 -0400372 if (location.used() && !location.ignored)
373 {
Jamie Madillde03e002017-10-21 14:04:20 -0400374 const auto &uniform = uniforms[location.index];
Geoff Langbeb0c942018-09-27 11:22:23 -0400375 if (!uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400376 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400377 std::string uniformName = uniform.name;
378 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400379 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400380 // Gets the uniform name without the [0] at the end.
381 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400382 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400383
Geoff Langbeb0c942018-09-27 11:22:23 -0400384 bool found = false;
385
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500386 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400387 {
388 auto it = layoutMap[shaderType].find(uniformName);
389 if (it != layoutMap[shaderType].end())
390 {
391 found = true;
392 layoutInfo[shaderType] = it->second;
393 }
394 }
395
396 ASSERT(found);
397 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400398 }
399
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500400 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400401 {
Jamie Madill33318de2018-05-01 11:22:54 -0400402 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400403 }
404 }
405
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500406 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400407 {
Jamie Madill33318de2018-05-01 11:22:54 -0400408 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400409 {
Jamie Madill33318de2018-05-01 11:22:54 -0400410 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
411 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400412 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400413 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400414 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400415 size_t minAlignment = static_cast<size_t>(
416 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
417
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400418 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400419
420 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400421 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400422 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400423 }
424 }
425
Jamie Madill2b858c22018-09-03 13:58:14 -0400426 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400427 {
428 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400429 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400430 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400431 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400432 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400433 uniformBufferInfo.flags = 0;
434 uniformBufferInfo.size = 1;
435 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
436 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
437 uniformBufferInfo.queueFamilyIndexCount = 0;
438 uniformBufferInfo.pQueueFamilyIndices = nullptr;
439
Yuly Novikov27780292018-11-09 11:19:49 -0500440 ANGLE_VK_TRY(contextVk, mEmptyUniformBlockStorage.buffer.init(contextVk->getDevice(),
441 uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400442
Luc Ferron7a06ac12018-03-15 10:17:04 -0400443 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500444 VkMemoryPropertyFlags flags =
445 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tobin Ehlisf5aad062018-09-19 14:39:00 -0600446 VkMemoryPropertyFlags flagsOut = 0;
447 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &flagsOut,
448 &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400449 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400450 }
Jamie Madill5547b382017-10-23 18:16:01 -0400451 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400452
Jamie Madill242c4fe2018-07-12 15:56:56 -0400453 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400454}
455
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400456GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
457{
Luc Ferronfba1f612018-06-04 14:37:17 -0400458 // No-op. The spec is very vague about the behavior of validation.
459 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400460}
461
Jamie Madill76e471e2017-10-21 09:56:01 -0400462template <typename T>
463void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
464{
465 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
466 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
467
Luc Ferron7cec3352018-03-13 13:29:34 -0400468 if (linkedUniform.isSampler())
469 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400470 // We could potentially cache some indexing here. For now this is a no-op since the mapping
471 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400472 return;
473 }
474
Luc Ferron24a31372018-04-04 11:49:14 -0400475 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400476 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500477 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400478 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400479 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400480 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400481
Luc Ferron24a31372018-04-04 11:49:14 -0400482 // Assume an offset of -1 means the block is unused.
483 if (layoutInfo.offset == -1)
484 {
485 continue;
486 }
487
488 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400489 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
490 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400491 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400492 }
Luc Ferron24a31372018-04-04 11:49:14 -0400493 }
494 else
495 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500496 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400497 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400498 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400499 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
500
501 // Assume an offset of -1 means the block is unused.
502 if (layoutInfo.offset == -1)
503 {
504 continue;
505 }
506
507 const GLint componentCount = linkedUniform.typeInfo->componentCount;
508
Luc Ferron62059a52018-03-29 07:01:35 -0400509 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
510
Luc Ferrond91c3792018-04-06 09:36:36 -0400511 GLint initialArrayOffset =
512 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400513 for (GLint i = 0; i < count; i++)
514 {
515 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
516 GLint *dest =
517 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
518 const T *source = v + i * componentCount;
519
520 for (int c = 0; c < componentCount; c++)
521 {
522 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
523 }
524 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400525
526 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400527 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400528 }
529}
530
Luc Ferron7cec3352018-03-13 13:29:34 -0400531template <typename T>
532void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
533{
534 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
535 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
536
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400537 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400538
Olli Etuaho107c7242018-03-20 15:45:35 +0200539 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800540 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400541
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500542 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madillb980c562018-11-27 11:34:27 -0500543 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400544
545 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
546 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400547
548 if (gl::IsMatrixType(linkedUniform.type))
549 {
550 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400551 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400552 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
553 }
554 else
555 {
556 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
557 v, layoutInfo, &uniformBlock.uniformData);
558 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400559}
560
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400561void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
562{
Jamie Madill76e471e2017-10-21 09:56:01 -0400563 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400564}
565
566void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
567{
Jamie Madill76e471e2017-10-21 09:56:01 -0400568 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400569}
570
571void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
572{
Jamie Madill76e471e2017-10-21 09:56:01 -0400573 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400574}
575
576void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
577{
Jamie Madill76e471e2017-10-21 09:56:01 -0400578 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400579}
580
581void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
582{
Luc Ferron7cec3352018-03-13 13:29:34 -0400583 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400584}
585
586void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
587{
Luc Ferron489243f2018-03-28 16:55:28 -0400588 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400589}
590
591void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
592{
Luc Ferron489243f2018-03-28 16:55:28 -0400593 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400594}
595
596void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
597{
Luc Ferron489243f2018-03-28 16:55:28 -0400598 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400599}
600
601void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
602{
603 UNIMPLEMENTED();
604}
605
606void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
607{
608 UNIMPLEMENTED();
609}
610
611void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
612{
613 UNIMPLEMENTED();
614}
615
616void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
617{
618 UNIMPLEMENTED();
619}
620
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400621template <int cols, int rows>
622void ProgramVk::setUniformMatrixfv(GLint location,
623 GLsizei count,
624 GLboolean transpose,
625 const GLfloat *value)
626{
627 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
628 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
629
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500630 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400631 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400632 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400633 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
634
635 // Assume an offset of -1 means the block is unused.
636 if (layoutInfo.offset == -1)
637 {
638 continue;
639 }
640
Luc Ferronc8fbff32018-06-04 10:30:48 -0400641 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400642 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
643 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400644
645 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
646 // setter did not update any data. We still want the uniform to be included when we'll
647 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400648 if (updated)
649 {
650 mDefaultUniformBlocksDirty.set(shaderType);
651 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400652 }
653}
654
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400655void ProgramVk::setUniformMatrix2fv(GLint location,
656 GLsizei count,
657 GLboolean transpose,
658 const GLfloat *value)
659{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400660 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400661}
662
663void ProgramVk::setUniformMatrix3fv(GLint location,
664 GLsizei count,
665 GLboolean transpose,
666 const GLfloat *value)
667{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400668 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400669}
670
671void ProgramVk::setUniformMatrix4fv(GLint location,
672 GLsizei count,
673 GLboolean transpose,
674 const GLfloat *value)
675{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400676 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400677}
678
679void ProgramVk::setUniformMatrix2x3fv(GLint location,
680 GLsizei count,
681 GLboolean transpose,
682 const GLfloat *value)
683{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400684 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400685}
686
687void ProgramVk::setUniformMatrix3x2fv(GLint location,
688 GLsizei count,
689 GLboolean transpose,
690 const GLfloat *value)
691{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400692 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400693}
694
695void ProgramVk::setUniformMatrix2x4fv(GLint location,
696 GLsizei count,
697 GLboolean transpose,
698 const GLfloat *value)
699{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400700 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400701}
702
703void ProgramVk::setUniformMatrix4x2fv(GLint location,
704 GLsizei count,
705 GLboolean transpose,
706 const GLfloat *value)
707{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400708 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400709}
710
711void ProgramVk::setUniformMatrix3x4fv(GLint location,
712 GLsizei count,
713 GLboolean transpose,
714 const GLfloat *value)
715{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400716 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400717}
718
719void ProgramVk::setUniformMatrix4x3fv(GLint location,
720 GLsizei count,
721 GLboolean transpose,
722 const GLfloat *value)
723{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400724 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400725}
726
Sami Väisänen46eaa942016-06-29 10:26:37 +0300727void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
728 GLenum genMode,
729 GLint components,
730 const GLfloat *coeffs)
731{
732 UNIMPLEMENTED();
733}
734
Jamie Madill242c4fe2018-07-12 15:56:56 -0400735angle::Result ProgramVk::initShaders(ContextVk *contextVk,
Jamie Madillbfe31c42018-10-25 17:03:47 -0400736 gl::PrimitiveMode mode,
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500737 vk::ShaderProgramHelper **programOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500738{
Jamie Madillbfe31c42018-10-25 17:03:47 -0400739 if (UseLineRaster(contextVk, mode))
Jamie Madillb36a4812018-09-25 10:15:11 -0400740 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500741 ANGLE_TRY(
742 mLineRasterShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, true));
Jamie Madillb36a4812018-09-25 10:15:11 -0400743 ASSERT(mLineRasterShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500744 *programOut = &mLineRasterShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400745 }
746 else
747 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500748 ANGLE_TRY(mDefaultShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, false));
Jamie Madillb36a4812018-09-25 10:15:11 -0400749 ASSERT(mDefaultShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500750 *programOut = &mDefaultShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400751 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400752
Jamie Madill242c4fe2018-07-12 15:56:56 -0400753 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500754}
755
Jamie Madill21061022018-07-12 23:56:30 -0400756angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400757{
Jamie Madill76e471e2017-10-21 09:56:01 -0400758 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400759 vk::DynamicDescriptorPool *dynamicDescriptorPool =
760 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400761
Luc Ferron6ea1b412018-03-21 16:13:01 -0400762 uint32_t potentialNewCount = descriptorSetIndex + 1;
763 if (potentialNewCount > mDescriptorSets.size())
764 {
765 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
766 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400767
Jamie Madillc7918ce2018-06-13 13:25:31 -0400768 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400769 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400770 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400771 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400772 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400773 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400774}
775
Jamie Madill54164b02017-08-28 15:17:37 -0400776void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
777{
Luc Ferron7cec3352018-03-13 13:29:34 -0400778 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400779}
780
781void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
782{
Luc Ferron7cec3352018-03-13 13:29:34 -0400783 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400784}
785
786void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
787{
788 UNIMPLEMENTED();
789}
790
Jamie Madill21061022018-07-12 23:56:30 -0400791angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400792{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400793 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400794
Jamie Madill76e471e2017-10-21 09:56:01 -0400795 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400796 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500797 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400798 {
Jamie Madill33318de2018-05-01 11:22:54 -0400799 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400800
Jamie Madill2b858c22018-09-03 13:58:14 -0400801 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400802 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400803 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400804 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400805 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400806 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400807 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400808
809 if (bufferModified)
810 {
811 anyNewBufferAllocated = true;
812 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400813 }
814 }
815
Luc Ferron7a06ac12018-03-15 10:17:04 -0400816 if (anyNewBufferAllocated)
817 {
818 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
819 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400820 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400821 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
822 }
823
Jamie Madill21061022018-07-12 23:56:30 -0400824 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400825}
826
Jamie Madill21061022018-07-12 23:56:30 -0400827angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400828{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500829 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
830 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400831
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500832 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400833 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400834 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
835 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
836 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400837
838 if (!uniformBlock.uniformData.empty())
839 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400840 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400841 }
842 else
843 {
844 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
845 }
846
847 bufferInfo.offset = 0;
848 bufferInfo.range = VK_WHOLE_SIZE;
849
Jamie Madill76e471e2017-10-21 09:56:01 -0400850 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
851 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400852 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400853 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400854 writeInfo.dstArrayElement = 0;
855 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400856 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400857 writeInfo.pImageInfo = nullptr;
858 writeInfo.pBufferInfo = &bufferInfo;
859 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400860 }
861
862 VkDevice device = contextVk->getDevice();
863
Jamie Madill33318de2018-05-01 11:22:54 -0400864 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400865
Jamie Madill21061022018-07-12 23:56:30 -0400866 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400867}
868
Jamie Madill84c662b2018-07-12 15:56:55 -0400869angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400870{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400871 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400872 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400873
Jamie Madill8c3988c2017-12-21 14:44:56 -0500874 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400875 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400876
Jamie Madill84c662b2018-07-12 15:56:55 -0400877 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
878 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400879 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400880
Jamie Madill84c662b2018-07-12 15:56:55 -0400881 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400882
Jamie Madill4cc753e2018-06-13 13:25:33 -0400883 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
884 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400885 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400886 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
887
Jamie Madill5547b382017-10-23 18:16:01 -0400888 ASSERT(!samplerBinding.unreferenced);
889
Jamie Madill4cc753e2018-06-13 13:25:33 -0400890 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
891 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400892 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400893 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
894 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400895 const vk::ImageHelper &image = textureVk->getImage();
896
897 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
898
899 imageInfo.sampler = textureVk->getSampler().getHandle();
900 imageInfo.imageView = textureVk->getImageView().getHandle();
901 imageInfo.imageLayout = image.getCurrentLayout();
902
903 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
904
905 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
906 writeInfo.pNext = nullptr;
907 writeInfo.dstSet = descriptorSet;
908 writeInfo.dstBinding = textureIndex;
909 writeInfo.dstArrayElement = arrayElement;
910 writeInfo.descriptorCount = 1;
911 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
912 writeInfo.pImageInfo = &imageInfo;
913 writeInfo.pBufferInfo = nullptr;
914 writeInfo.pTexelBufferView = nullptr;
915
916 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400917 }
Jamie Madill5547b382017-10-23 18:16:01 -0400918 }
919
920 VkDevice device = contextVk->getDevice();
921
Jamie Madill4cc753e2018-06-13 13:25:33 -0400922 ASSERT(writeCount > 0);
923 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400924
Jamie Madill84c662b2018-07-12 15:56:55 -0400925 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400926}
927
Luc Ferron7a06ac12018-03-15 10:17:04 -0400928void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
929{
930 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
931 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400932 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400933 }
934}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400935
936angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400937 vk::CommandBuffer *commandBuffer)
938{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400939 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400940
941 if (mUsedDescriptorSetRange.empty())
942 return angle::Result::Continue();
943
944 ASSERT(!mDescriptorSets.empty());
945
946 unsigned int low = mUsedDescriptorSetRange.low();
947
948 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
949 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
950 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500951 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
952 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400953 commandBuffer->bindDescriptorSets(
954 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
955 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500956 kShaderTypeMax - kShaderTypeMin + 1, mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400957 }
958 else
959 {
960 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
961 low, mUsedDescriptorSetRange.length(),
962 &mDescriptorSets[low], 0, nullptr);
963 }
964
965 return angle::Result::Continue();
966}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400967} // namespace rx