blob: a40ae69f0505c599357d9806bc82fcf4624ac3c0 [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.
141ProgramVk::ShaderInfo::ShaderInfo()
142{
143}
144
145ProgramVk::ShaderInfo::~ShaderInfo() = default;
146
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500147angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk,
148 const std::string &vertexSource,
149 const std::string &fragmentSource,
150 bool enableLineRasterEmulation)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400151{
152 if (!valid())
153 {
154 std::vector<uint32_t> vertexCode;
155 std::vector<uint32_t> fragmentCode;
Jamie Madillb36a4812018-09-25 10:15:11 -0400156 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
157 enableLineRasterEmulation, vertexSource,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400158 fragmentSource, &vertexCode, &fragmentCode));
159
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500160 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(),
161 vertexCode.data(), vertexCode.size() * sizeof(uint32_t)));
162 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(),
163 fragmentCode.data(),
Jamie Madill242c4fe2018-07-12 15:56:56 -0400164 fragmentCode.size() * sizeof(uint32_t)));
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500165
166 mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]);
167 mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400168 }
169
Jamie Madill242c4fe2018-07-12 15:56:56 -0400170 return angle::Result::Continue();
171}
172
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500173void ProgramVk::ShaderInfo::release(RendererVk *renderer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400174{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500175 mProgramHelper.release(renderer);
176
177 for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders)
178 {
179 shader.get().destroy(renderer->getDevice());
180 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400181}
182
183bool ProgramVk::ShaderInfo::valid() const
184{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500185 return mShaders[gl::ShaderType::Vertex].get().valid();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400186}
187
188// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400189ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400190 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Jamie Madill2b858c22018-09-03 13:58:14 -0400191 kUniformBlockDynamicBufferMinSize)
Jamie Madill76e471e2017-10-21 09:56:01 -0400192{
193}
194
Jamie Madill242c4fe2018-07-12 15:56:56 -0400195ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500196
Jamie Madill88fc6da2018-08-30 16:18:36 -0400197ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400198{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500199 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400200}
201
Jamie Madill242c4fe2018-07-12 15:56:56 -0400202ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400203
Jamie Madillf4a789f2018-10-18 16:56:20 -0400204void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500205{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400206 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500207 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400208}
Jamie Madill5deea722017-02-16 10:44:46 -0500209
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500210void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400211{
Jamie Madill9b168d02018-06-13 13:25:32 -0400212 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
213 {
214 descriptorSetLayout.reset();
215 }
216 mPipelineLayout.reset();
217
Jamie Madill76e471e2017-10-21 09:56:01 -0400218 for (auto &uniformBlock : mDefaultUniformBlocks)
219 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400220 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400221 }
222
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500223 mDefaultShaderInfo.release(renderer);
224 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400225
Jamie Madillcaaff162018-06-22 08:55:37 -0400226 Serial currentSerial = renderer->getCurrentQueueSerial();
227 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
228 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400229
Jamie Madill5547b382017-10-23 18:16:01 -0400230 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500231 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500232
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400233 for (vk::SharedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
234 {
235 binding.reset();
236 }
Jamie Madill5deea722017-02-16 10:44:46 -0500237}
238
Jamie Madill785e8a02018-10-04 17:42:00 -0400239angle::Result ProgramVk::load(const gl::Context *context,
240 gl::InfoLog &infoLog,
241 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400242{
243 UNIMPLEMENTED();
Jamie Madill785e8a02018-10-04 17:42:00 -0400244 return angle::Result::Stop();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400245}
246
Jamie Madill27a60632017-06-30 15:12:01 -0400247void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400248{
249 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400250}
251
252void ProgramVk::setBinaryRetrievableHint(bool retrievable)
253{
254 UNIMPLEMENTED();
255}
256
Yunchao He61afff12017-03-14 15:34:03 +0800257void ProgramVk::setSeparable(bool separable)
258{
259 UNIMPLEMENTED();
260}
261
Jamie Madill785e8a02018-10-04 17:42:00 -0400262std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800263 const gl::ProgramLinkedResources &resources,
264 gl::InfoLog &infoLog)
265{
266 // TODO(jie.a.chen@intel.com): Parallelize linking.
267 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400268 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800269}
270
Jamie Madill785e8a02018-10-04 17:42:00 -0400271angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
272 const gl::ProgramLinkedResources &resources,
273 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400274{
Jamie Madill06ca6342018-07-12 15:56:53 -0400275 ContextVk *contextVk = vk::GetImpl(glContext);
276 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400277
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500278 reset(renderer);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500279
jchen103fd614d2018-08-13 12:21:58 +0800280 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500281
Jamie Madill76e471e2017-10-21 09:56:01 -0400282 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500283
Jamie Madill8c3988c2017-12-21 14:44:56 -0500284 if (!mState.getSamplerUniformRange().empty())
285 {
286 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400287 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500288 }
289
Jamie Madill9b168d02018-06-13 13:25:32 -0400290 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
291 // don't already exist in the cache.
292 vk::DescriptorSetLayoutDesc uniformsSetDesc;
293 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
294 1);
295 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
296 1);
297
298 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400299 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400300
Jamie Madill9b168d02018-06-13 13:25:32 -0400301 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400302
303 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
304 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400305 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400306 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
307
308 // The front-end always binds array sampler units sequentially.
309 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
310 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400311 }
312
Jamie Madill21061022018-07-12 23:56:30 -0400313 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400314 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
315
Jamie Madillb01b4802018-07-10 12:43:57 -0400316 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
317 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
318 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400319 contextVk, driverUniformsSetDesc,
320 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400321
Jamie Madill9b168d02018-06-13 13:25:32 -0400322 vk::PipelineLayoutDesc pipelineLayoutDesc;
323 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
324 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400325 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
326 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400327
Jamie Madill21061022018-07-12 23:56:30 -0400328 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
329 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400330
Jamie Madill242c4fe2018-07-12 15:56:56 -0400331 if (!mState.getUniforms().empty())
332 {
333 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
334
335 if (mState.getUniforms().size() > samplerRange.length())
336 {
337 // Ensure the descriptor set range includes the uniform buffers at position 0.
338 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
339 }
340
341 if (!samplerRange.empty())
342 {
343 // Ensure the descriptor set range includes the textures at position 1.
344 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400345 }
346 }
347
Jamie Madill785e8a02018-10-04 17:42:00 -0400348 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400349}
350
Jamie Madill242c4fe2018-07-12 15:56:56 -0400351angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400352{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400353 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500354 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400355
356 // Process vertex and fragment uniforms into std140 packing.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500357 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
358 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400359 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400360
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500361 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400362 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500363 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800364 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400365 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
366 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400367 }
368
369 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400370 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400371 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400372 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500373 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400374
Jamie Madill76e471e2017-10-21 09:56:01 -0400375 if (location.used() && !location.ignored)
376 {
Jamie Madillde03e002017-10-21 14:04:20 -0400377 const auto &uniform = uniforms[location.index];
Geoff Langbeb0c942018-09-27 11:22:23 -0400378 if (!uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400379 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400380 std::string uniformName = uniform.name;
381 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400382 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400383 // Gets the uniform name without the [0] at the end.
384 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400385 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400386
Geoff Langbeb0c942018-09-27 11:22:23 -0400387 bool found = false;
388
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500389 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400390 {
391 auto it = layoutMap[shaderType].find(uniformName);
392 if (it != layoutMap[shaderType].end())
393 {
394 found = true;
395 layoutInfo[shaderType] = it->second;
396 }
397 }
398
399 ASSERT(found);
400 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400401 }
402
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500403 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400404 {
Jamie Madill33318de2018-05-01 11:22:54 -0400405 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400406 }
407 }
408
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500409 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400410 {
Jamie Madill33318de2018-05-01 11:22:54 -0400411 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400412 {
Jamie Madill33318de2018-05-01 11:22:54 -0400413 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
414 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400415 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400416 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400417 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400418 size_t minAlignment = static_cast<size_t>(
419 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
420
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400421 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400422
423 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400424 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400425 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400426 }
427 }
428
Jamie Madill2b858c22018-09-03 13:58:14 -0400429 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400430 {
431 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400432 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400433 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400434 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400435 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400436 uniformBufferInfo.flags = 0;
437 uniformBufferInfo.size = 1;
438 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
439 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
440 uniformBufferInfo.queueFamilyIndexCount = 0;
441 uniformBufferInfo.pQueueFamilyIndices = nullptr;
442
Yuly Novikov27780292018-11-09 11:19:49 -0500443 ANGLE_VK_TRY(contextVk, mEmptyUniformBlockStorage.buffer.init(contextVk->getDevice(),
444 uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400445
Luc Ferron7a06ac12018-03-15 10:17:04 -0400446 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500447 VkMemoryPropertyFlags flags =
448 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tobin Ehlisf5aad062018-09-19 14:39:00 -0600449 VkMemoryPropertyFlags flagsOut = 0;
450 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &flagsOut,
451 &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400452 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400453 }
Jamie Madill5547b382017-10-23 18:16:01 -0400454 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400455
Jamie Madill242c4fe2018-07-12 15:56:56 -0400456 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400457}
458
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400459GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
460{
Luc Ferronfba1f612018-06-04 14:37:17 -0400461 // No-op. The spec is very vague about the behavior of validation.
462 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400463}
464
Jamie Madill76e471e2017-10-21 09:56:01 -0400465template <typename T>
466void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
467{
468 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
469 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
470
Luc Ferron7cec3352018-03-13 13:29:34 -0400471 if (linkedUniform.isSampler())
472 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400473 // We could potentially cache some indexing here. For now this is a no-op since the mapping
474 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400475 return;
476 }
477
Luc Ferron24a31372018-04-04 11:49:14 -0400478 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400479 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500480 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400481 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400482 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400483 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400484
Luc Ferron24a31372018-04-04 11:49:14 -0400485 // Assume an offset of -1 means the block is unused.
486 if (layoutInfo.offset == -1)
487 {
488 continue;
489 }
490
491 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400492 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
493 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400494 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400495 }
Luc Ferron24a31372018-04-04 11:49:14 -0400496 }
497 else
498 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500499 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400500 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400501 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400502 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
503
504 // Assume an offset of -1 means the block is unused.
505 if (layoutInfo.offset == -1)
506 {
507 continue;
508 }
509
510 const GLint componentCount = linkedUniform.typeInfo->componentCount;
511
Luc Ferron62059a52018-03-29 07:01:35 -0400512 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
513
Luc Ferrond91c3792018-04-06 09:36:36 -0400514 GLint initialArrayOffset =
515 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400516 for (GLint i = 0; i < count; i++)
517 {
518 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
519 GLint *dest =
520 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
521 const T *source = v + i * componentCount;
522
523 for (int c = 0; c < componentCount; c++)
524 {
525 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
526 }
527 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400528
529 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400530 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400531 }
532}
533
Luc Ferron7cec3352018-03-13 13:29:34 -0400534template <typename T>
535void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
536{
537 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
538 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
539
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400540 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400541
Olli Etuaho107c7242018-03-20 15:45:35 +0200542 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800543 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400544
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500545 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400546 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400547
548 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
549 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400550
551 if (gl::IsMatrixType(linkedUniform.type))
552 {
553 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400554 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400555 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
556 }
557 else
558 {
559 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
560 v, layoutInfo, &uniformBlock.uniformData);
561 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400562}
563
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400564void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
565{
Jamie Madill76e471e2017-10-21 09:56:01 -0400566 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400567}
568
569void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
570{
Jamie Madill76e471e2017-10-21 09:56:01 -0400571 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400572}
573
574void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
575{
Jamie Madill76e471e2017-10-21 09:56:01 -0400576 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400577}
578
579void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
580{
Jamie Madill76e471e2017-10-21 09:56:01 -0400581 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400582}
583
584void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
585{
Luc Ferron7cec3352018-03-13 13:29:34 -0400586 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400587}
588
589void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
590{
Luc Ferron489243f2018-03-28 16:55:28 -0400591 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400592}
593
594void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
595{
Luc Ferron489243f2018-03-28 16:55:28 -0400596 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400597}
598
599void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
600{
Luc Ferron489243f2018-03-28 16:55:28 -0400601 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400602}
603
604void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
605{
606 UNIMPLEMENTED();
607}
608
609void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
610{
611 UNIMPLEMENTED();
612}
613
614void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
615{
616 UNIMPLEMENTED();
617}
618
619void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
620{
621 UNIMPLEMENTED();
622}
623
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400624template <int cols, int rows>
625void ProgramVk::setUniformMatrixfv(GLint location,
626 GLsizei count,
627 GLboolean transpose,
628 const GLfloat *value)
629{
630 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
631 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
632
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500633 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400634 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400635 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400636 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
637
638 // Assume an offset of -1 means the block is unused.
639 if (layoutInfo.offset == -1)
640 {
641 continue;
642 }
643
Luc Ferronc8fbff32018-06-04 10:30:48 -0400644 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400645 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
646 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400647
648 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
649 // setter did not update any data. We still want the uniform to be included when we'll
650 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400651 if (updated)
652 {
653 mDefaultUniformBlocksDirty.set(shaderType);
654 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400655 }
656}
657
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400658void ProgramVk::setUniformMatrix2fv(GLint location,
659 GLsizei count,
660 GLboolean transpose,
661 const GLfloat *value)
662{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400663 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400664}
665
666void ProgramVk::setUniformMatrix3fv(GLint location,
667 GLsizei count,
668 GLboolean transpose,
669 const GLfloat *value)
670{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400671 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400672}
673
674void ProgramVk::setUniformMatrix4fv(GLint location,
675 GLsizei count,
676 GLboolean transpose,
677 const GLfloat *value)
678{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400679 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400680}
681
682void ProgramVk::setUniformMatrix2x3fv(GLint location,
683 GLsizei count,
684 GLboolean transpose,
685 const GLfloat *value)
686{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400687 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400688}
689
690void ProgramVk::setUniformMatrix3x2fv(GLint location,
691 GLsizei count,
692 GLboolean transpose,
693 const GLfloat *value)
694{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400695 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400696}
697
698void ProgramVk::setUniformMatrix2x4fv(GLint location,
699 GLsizei count,
700 GLboolean transpose,
701 const GLfloat *value)
702{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400703 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400704}
705
706void ProgramVk::setUniformMatrix4x2fv(GLint location,
707 GLsizei count,
708 GLboolean transpose,
709 const GLfloat *value)
710{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400711 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400712}
713
714void ProgramVk::setUniformMatrix3x4fv(GLint location,
715 GLsizei count,
716 GLboolean transpose,
717 const GLfloat *value)
718{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400719 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400720}
721
722void ProgramVk::setUniformMatrix4x3fv(GLint location,
723 GLsizei count,
724 GLboolean transpose,
725 const GLfloat *value)
726{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400727 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400728}
729
Sami Väisänen46eaa942016-06-29 10:26:37 +0300730void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
731 GLenum genMode,
732 GLint components,
733 const GLfloat *coeffs)
734{
735 UNIMPLEMENTED();
736}
737
Jamie Madill242c4fe2018-07-12 15:56:56 -0400738angle::Result ProgramVk::initShaders(ContextVk *contextVk,
Jamie Madillbfe31c42018-10-25 17:03:47 -0400739 gl::PrimitiveMode mode,
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500740 vk::ShaderProgramHelper **programOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500741{
Jamie Madillbfe31c42018-10-25 17:03:47 -0400742 if (UseLineRaster(contextVk, mode))
Jamie Madillb36a4812018-09-25 10:15:11 -0400743 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500744 ANGLE_TRY(
745 mLineRasterShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, true));
Jamie Madillb36a4812018-09-25 10:15:11 -0400746 ASSERT(mLineRasterShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500747 *programOut = &mLineRasterShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400748 }
749 else
750 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500751 ANGLE_TRY(mDefaultShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, false));
Jamie Madillb36a4812018-09-25 10:15:11 -0400752 ASSERT(mDefaultShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500753 *programOut = &mDefaultShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400754 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400755
Jamie Madill242c4fe2018-07-12 15:56:56 -0400756 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500757}
758
Jamie Madill21061022018-07-12 23:56:30 -0400759angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400760{
Jamie Madill76e471e2017-10-21 09:56:01 -0400761 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400762 vk::DynamicDescriptorPool *dynamicDescriptorPool =
763 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400764
Luc Ferron6ea1b412018-03-21 16:13:01 -0400765 uint32_t potentialNewCount = descriptorSetIndex + 1;
766 if (potentialNewCount > mDescriptorSets.size())
767 {
768 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
769 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400770
Jamie Madillc7918ce2018-06-13 13:25:31 -0400771 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400772 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400773 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400774 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400775 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400776 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400777}
778
Jamie Madill54164b02017-08-28 15:17:37 -0400779void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
780{
Luc Ferron7cec3352018-03-13 13:29:34 -0400781 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400782}
783
784void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
785{
Luc Ferron7cec3352018-03-13 13:29:34 -0400786 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400787}
788
789void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
790{
791 UNIMPLEMENTED();
792}
793
Jamie Madill21061022018-07-12 23:56:30 -0400794angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400795{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400796 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400797
Jamie Madill76e471e2017-10-21 09:56:01 -0400798 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400799 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500800 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400801 {
Jamie Madill33318de2018-05-01 11:22:54 -0400802 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400803
Jamie Madill2b858c22018-09-03 13:58:14 -0400804 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400805 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400806 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400807 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400808 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400809 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400810 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400811
812 if (bufferModified)
813 {
814 anyNewBufferAllocated = true;
815 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400816 }
817 }
818
Luc Ferron7a06ac12018-03-15 10:17:04 -0400819 if (anyNewBufferAllocated)
820 {
821 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
822 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400823 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400824 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
825 }
826
Jamie Madill21061022018-07-12 23:56:30 -0400827 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400828}
829
Jamie Madill21061022018-07-12 23:56:30 -0400830angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400831{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500832 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
833 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400834
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500835 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400836 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400837 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
838 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
839 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400840
841 if (!uniformBlock.uniformData.empty())
842 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400843 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400844 }
845 else
846 {
847 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
848 }
849
850 bufferInfo.offset = 0;
851 bufferInfo.range = VK_WHOLE_SIZE;
852
Jamie Madill76e471e2017-10-21 09:56:01 -0400853 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
854 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400855 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400856 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400857 writeInfo.dstArrayElement = 0;
858 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400859 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400860 writeInfo.pImageInfo = nullptr;
861 writeInfo.pBufferInfo = &bufferInfo;
862 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400863 }
864
865 VkDevice device = contextVk->getDevice();
866
Jamie Madill33318de2018-05-01 11:22:54 -0400867 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400868
Jamie Madill21061022018-07-12 23:56:30 -0400869 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400870}
871
Jamie Madill84c662b2018-07-12 15:56:55 -0400872angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400873{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400874 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400875 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400876
Jamie Madill8c3988c2017-12-21 14:44:56 -0500877 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400878 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400879
Jamie Madill84c662b2018-07-12 15:56:55 -0400880 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
881 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400882 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400883
Jamie Madill84c662b2018-07-12 15:56:55 -0400884 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400885
Jamie Madill4cc753e2018-06-13 13:25:33 -0400886 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
887 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400888 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400889 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
890
Jamie Madill5547b382017-10-23 18:16:01 -0400891 ASSERT(!samplerBinding.unreferenced);
892
Jamie Madill4cc753e2018-06-13 13:25:33 -0400893 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
894 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400895 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400896 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
897 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400898 const vk::ImageHelper &image = textureVk->getImage();
899
900 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
901
902 imageInfo.sampler = textureVk->getSampler().getHandle();
903 imageInfo.imageView = textureVk->getImageView().getHandle();
904 imageInfo.imageLayout = image.getCurrentLayout();
905
906 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
907
908 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
909 writeInfo.pNext = nullptr;
910 writeInfo.dstSet = descriptorSet;
911 writeInfo.dstBinding = textureIndex;
912 writeInfo.dstArrayElement = arrayElement;
913 writeInfo.descriptorCount = 1;
914 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
915 writeInfo.pImageInfo = &imageInfo;
916 writeInfo.pBufferInfo = nullptr;
917 writeInfo.pTexelBufferView = nullptr;
918
919 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400920 }
Jamie Madill5547b382017-10-23 18:16:01 -0400921 }
922
923 VkDevice device = contextVk->getDevice();
924
Jamie Madill4cc753e2018-06-13 13:25:33 -0400925 ASSERT(writeCount > 0);
926 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400927
Jamie Madill84c662b2018-07-12 15:56:55 -0400928 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400929}
930
Luc Ferron7a06ac12018-03-15 10:17:04 -0400931void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
932{
933 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
934 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400935 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400936 }
937}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400938
939angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400940 vk::CommandBuffer *commandBuffer)
941{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400942 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400943
944 if (mUsedDescriptorSetRange.empty())
945 return angle::Result::Continue();
946
947 ASSERT(!mDescriptorSets.empty());
948
949 unsigned int low = mUsedDescriptorSetRange.low();
950
951 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
952 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
953 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500954 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
955 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400956 commandBuffer->bindDescriptorSets(
957 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
958 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500959 kShaderTypeMax - kShaderTypeMin + 1, mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400960 }
961 else
962 {
963 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
964 low, mUsedDescriptorSetRange.length(),
965 &mDescriptorSets[low], 0, nullptr);
966 }
967
968 return angle::Result::Continue();
969}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400970} // namespace rx