blob: 8a438db03e7c6d499f53bb2bae81a558a328af25 [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
134bool UseLineRaster(const ContextVk *contextVk, const gl::DrawCallParams &drawCallParams)
135{
136 return contextVk->getFeatures().basicGLLineRasterization &&
137 gl::IsLineMode(drawCallParams.mode());
138}
Jamie Madill76e471e2017-10-21 09:56:01 -0400139} // anonymous namespace
140
Jamie Madill242c4fe2018-07-12 15:56:56 -0400141// ProgramVk::ShaderInfo implementation.
142ProgramVk::ShaderInfo::ShaderInfo()
143{
144}
145
146ProgramVk::ShaderInfo::~ShaderInfo() = default;
147
148angle::Result ProgramVk::ShaderInfo::getShaders(
149 ContextVk *contextVk,
150 const std::string &vertexSource,
151 const std::string &fragmentSource,
Jamie Madillb36a4812018-09-25 10:15:11 -0400152 bool enableLineRasterEmulation,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400153 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
154 const vk::ShaderAndSerial **fragmentShaderAndSerialOut)
155{
156 if (!valid())
157 {
158 std::vector<uint32_t> vertexCode;
159 std::vector<uint32_t> fragmentCode;
Jamie Madillb36a4812018-09-25 10:15:11 -0400160 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
161 enableLineRasterEmulation, vertexSource,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400162 fragmentSource, &vertexCode, &fragmentCode));
163
164 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mVertexShaderAndSerial, vertexCode.data(),
165 vertexCode.size() * sizeof(uint32_t)));
166 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mFragmentShaderAndSerial, fragmentCode.data(),
167 fragmentCode.size() * sizeof(uint32_t)));
168 }
169
170 *fragmentShaderAndSerialOut = &mFragmentShaderAndSerial;
171 *vertexShaderAndSerialOut = &mVertexShaderAndSerial;
172 return angle::Result::Continue();
173}
174
175void ProgramVk::ShaderInfo::destroy(VkDevice device)
176{
177 mVertexShaderAndSerial.destroy(device);
178 mFragmentShaderAndSerial.destroy(device);
179}
180
181bool ProgramVk::ShaderInfo::valid() const
182{
183 return mVertexShaderAndSerial.valid();
184}
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 Madill76e471e2017-10-21 09:56:01 -0400190{
191}
192
Jamie Madill242c4fe2018-07-12 15:56:56 -0400193ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500194
Jamie Madill88fc6da2018-08-30 16:18:36 -0400195ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400196{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500197 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400198}
199
Jamie Madill242c4fe2018-07-12 15:56:56 -0400200ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400201
Jamie Madill242c4fe2018-07-12 15:56:56 -0400202gl::Error ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500203{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400204 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500205 return reset(contextVk);
Jamie Madillc5143482017-10-15 20:20:06 -0400206}
Jamie Madill5deea722017-02-16 10:44:46 -0500207
Jamie Madill21061022018-07-12 23:56:30 -0400208angle::Result ProgramVk::reset(ContextVk *contextVk)
Jamie Madillc5143482017-10-15 20:20:06 -0400209{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500210 VkDevice device = contextVk->getDevice();
211
Jamie Madill9b168d02018-06-13 13:25:32 -0400212 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
213 {
214 descriptorSetLayout.reset();
215 }
216 mPipelineLayout.reset();
217
Jamie Madillcaaff162018-06-22 08:55:37 -0400218 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400219 for (auto &uniformBlock : mDefaultUniformBlocks)
220 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400221 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400222 }
223
Jamie Madill242c4fe2018-07-12 15:56:56 -0400224 mDefaultShaderInfo.destroy(device);
Jamie Madillb36a4812018-09-25 10:15:11 -0400225 mLineRasterShaderInfo.destroy(device);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400226
Jamie Madillcaaff162018-06-22 08:55:37 -0400227 Serial currentSerial = renderer->getCurrentQueueSerial();
228 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
229 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400230
Jamie Madill5547b382017-10-23 18:16:01 -0400231 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500232 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500233
Jamie Madill21061022018-07-12 23:56:30 -0400234 return angle::Result::Continue();
Jamie Madill5deea722017-02-16 10:44:46 -0500235}
236
Jamie Madill785e8a02018-10-04 17:42:00 -0400237angle::Result ProgramVk::load(const gl::Context *context,
238 gl::InfoLog &infoLog,
239 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400240{
241 UNIMPLEMENTED();
Jamie Madill785e8a02018-10-04 17:42:00 -0400242 return angle::Result::Stop();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400243}
244
Jamie Madill27a60632017-06-30 15:12:01 -0400245void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400246{
247 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400248}
249
250void ProgramVk::setBinaryRetrievableHint(bool retrievable)
251{
252 UNIMPLEMENTED();
253}
254
Yunchao He61afff12017-03-14 15:34:03 +0800255void ProgramVk::setSeparable(bool separable)
256{
257 UNIMPLEMENTED();
258}
259
Jamie Madill785e8a02018-10-04 17:42:00 -0400260std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *context,
jchen107ae70d82018-07-06 13:47:01 +0800261 const gl::ProgramLinkedResources &resources,
262 gl::InfoLog &infoLog)
263{
264 // TODO(jie.a.chen@intel.com): Parallelize linking.
265 // http://crbug.com/849576
Jamie Madill785e8a02018-10-04 17:42:00 -0400266 return std::make_unique<LinkEventDone>(linkImpl(context, resources, infoLog));
jchen107ae70d82018-07-06 13:47:01 +0800267}
268
Jamie Madill785e8a02018-10-04 17:42:00 -0400269angle::Result ProgramVk::linkImpl(const gl::Context *glContext,
270 const gl::ProgramLinkedResources &resources,
271 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400272{
Jamie Madill06ca6342018-07-12 15:56:53 -0400273 ContextVk *contextVk = vk::GetImpl(glContext);
274 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400275
Jamie Madillb7d924a2018-03-10 11:16:54 -0500276 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500277
jchen103fd614d2018-08-13 12:21:58 +0800278 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500279
Jamie Madill76e471e2017-10-21 09:56:01 -0400280 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500281
Jamie Madill8c3988c2017-12-21 14:44:56 -0500282 if (!mState.getSamplerUniformRange().empty())
283 {
284 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400285 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500286 }
287
Jamie Madill9b168d02018-06-13 13:25:32 -0400288 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
289 // don't already exist in the cache.
290 vk::DescriptorSetLayoutDesc uniformsSetDesc;
291 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
292 1);
293 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
294 1);
295
296 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400297 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400298
Jamie Madill9b168d02018-06-13 13:25:32 -0400299 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400300
301 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
302 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400303 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400304 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
305
306 // The front-end always binds array sampler units sequentially.
307 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
308 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400309 }
310
Jamie Madill21061022018-07-12 23:56:30 -0400311 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400312 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
313
Jamie Madillb01b4802018-07-10 12:43:57 -0400314 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
315 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
316 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400317 contextVk, driverUniformsSetDesc,
318 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400319
Jamie Madill9b168d02018-06-13 13:25:32 -0400320 vk::PipelineLayoutDesc pipelineLayoutDesc;
321 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
322 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400323 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
324 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400325
Jamie Madill21061022018-07-12 23:56:30 -0400326 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
327 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400328
Jamie Madill242c4fe2018-07-12 15:56:56 -0400329 if (!mState.getUniforms().empty())
330 {
331 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
332
333 if (mState.getUniforms().size() > samplerRange.length())
334 {
335 // Ensure the descriptor set range includes the uniform buffers at position 0.
336 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
337 }
338
339 if (!samplerRange.empty())
340 {
341 // Ensure the descriptor set range includes the textures at position 1.
342 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400343 }
344 }
345
Jamie Madill785e8a02018-10-04 17:42:00 -0400346 return angle::Result::Continue();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400347}
348
Jamie Madill242c4fe2018-07-12 15:56:56 -0400349angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400350{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400351 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500352 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400353
354 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400355 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
356 vk::ShaderMap<size_t> requiredBufferSize;
357 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400358
Jamie Madill33318de2018-05-01 11:22:54 -0400359 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400360 {
Jamie Madill33318de2018-05-01 11:22:54 -0400361 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400362 gl::Shader *shader = mState.getAttachedShader(glShaderType);
jchen103fd614d2018-08-13 12:21:58 +0800363 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400364 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
365 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400366 }
367
368 // Init the default block layout info.
Jamie Madill76e471e2017-10-21 09:56:01 -0400369 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400370 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400371 {
Jamie Madill33318de2018-05-01 11:22:54 -0400372 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400373
Jamie Madill76e471e2017-10-21 09:56:01 -0400374 if (location.used() && !location.ignored)
375 {
Jamie Madillde03e002017-10-21 14:04:20 -0400376 const auto &uniform = uniforms[location.index];
Geoff Langbeb0c942018-09-27 11:22:23 -0400377 if (!uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400378 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400379 std::string uniformName = uniform.name;
380 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400381 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400382 // Gets the uniform name without the [0] at the end.
383 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400384 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400385
Geoff Langbeb0c942018-09-27 11:22:23 -0400386 bool found = false;
387
388 for (vk::ShaderType shaderType : vk::AllShaderTypes())
389 {
390 auto it = layoutMap[shaderType].find(uniformName);
391 if (it != layoutMap[shaderType].end())
392 {
393 found = true;
394 layoutInfo[shaderType] = it->second;
395 }
396 }
397
398 ASSERT(found);
399 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400400 }
401
Jamie Madill33318de2018-05-01 11:22:54 -0400402 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400403 {
Jamie Madill33318de2018-05-01 11:22:54 -0400404 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400405 }
406 }
407
Jamie Madill33318de2018-05-01 11:22:54 -0400408 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400409 {
Jamie Madill33318de2018-05-01 11:22:54 -0400410 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400411 {
Jamie Madill33318de2018-05-01 11:22:54 -0400412 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
413 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400414 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400415 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400416 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400417 size_t minAlignment = static_cast<size_t>(
418 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
419
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400420 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400421
422 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400423 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400424 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400425 }
426 }
427
Jamie Madill2b858c22018-09-03 13:58:14 -0400428 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400429 {
430 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400431 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400432 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400433 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400434 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400435 uniformBufferInfo.flags = 0;
436 uniformBufferInfo.size = 1;
437 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
438 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
439 uniformBufferInfo.queueFamilyIndexCount = 0;
440 uniformBufferInfo.pQueueFamilyIndices = nullptr;
441
Jamie Madill21061022018-07-12 23:56:30 -0400442 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400443
Luc Ferron7a06ac12018-03-15 10:17:04 -0400444 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500445 VkMemoryPropertyFlags flags =
446 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tobin Ehlisf5aad062018-09-19 14:39:00 -0600447 VkMemoryPropertyFlags flagsOut = 0;
448 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &flagsOut,
449 &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400450 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400451 }
Jamie Madill5547b382017-10-23 18:16:01 -0400452 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400453
Jamie Madill242c4fe2018-07-12 15:56:56 -0400454 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400455}
456
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400457GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
458{
Luc Ferronfba1f612018-06-04 14:37:17 -0400459 // No-op. The spec is very vague about the behavior of validation.
460 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400461}
462
Jamie Madill76e471e2017-10-21 09:56:01 -0400463template <typename T>
464void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
465{
466 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
467 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
468
Luc Ferron7cec3352018-03-13 13:29:34 -0400469 if (linkedUniform.isSampler())
470 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400471 // We could potentially cache some indexing here. For now this is a no-op since the mapping
472 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400473 return;
474 }
475
Luc Ferron24a31372018-04-04 11:49:14 -0400476 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400477 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400478 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400479 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400480 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400481 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400482
Luc Ferron24a31372018-04-04 11:49:14 -0400483 // Assume an offset of -1 means the block is unused.
484 if (layoutInfo.offset == -1)
485 {
486 continue;
487 }
488
489 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400490 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
491 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400492 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400493 }
Luc Ferron24a31372018-04-04 11:49:14 -0400494 }
495 else
496 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400497 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400498 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400499 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400500 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
501
502 // Assume an offset of -1 means the block is unused.
503 if (layoutInfo.offset == -1)
504 {
505 continue;
506 }
507
508 const GLint componentCount = linkedUniform.typeInfo->componentCount;
509
Luc Ferron62059a52018-03-29 07:01:35 -0400510 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
511
Luc Ferrond91c3792018-04-06 09:36:36 -0400512 GLint initialArrayOffset =
513 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400514 for (GLint i = 0; i < count; i++)
515 {
516 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
517 GLint *dest =
518 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
519 const T *source = v + i * componentCount;
520
521 for (int c = 0; c < componentCount; c++)
522 {
523 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
524 }
525 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400526
527 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400528 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400529 }
530}
531
Luc Ferron7cec3352018-03-13 13:29:34 -0400532template <typename T>
533void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
534{
535 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
536 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
537
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400538 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400539
Olli Etuaho107c7242018-03-20 15:45:35 +0200540 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800541 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400542
Jiawei Shao385b3e02018-03-21 09:43:28 +0800543 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400544 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400545 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400546
547 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
548 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400549
550 if (gl::IsMatrixType(linkedUniform.type))
551 {
552 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400553 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400554 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
555 }
556 else
557 {
558 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
559 v, layoutInfo, &uniformBlock.uniformData);
560 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400561}
562
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400563void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
564{
Jamie Madill76e471e2017-10-21 09:56:01 -0400565 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400566}
567
568void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
569{
Jamie Madill76e471e2017-10-21 09:56:01 -0400570 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400571}
572
573void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
574{
Jamie Madill76e471e2017-10-21 09:56:01 -0400575 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400576}
577
578void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
579{
Jamie Madill76e471e2017-10-21 09:56:01 -0400580 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400581}
582
583void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
584{
Luc Ferron7cec3352018-03-13 13:29:34 -0400585 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400586}
587
588void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
589{
Luc Ferron489243f2018-03-28 16:55:28 -0400590 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400591}
592
593void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
594{
Luc Ferron489243f2018-03-28 16:55:28 -0400595 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400596}
597
598void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
599{
Luc Ferron489243f2018-03-28 16:55:28 -0400600 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400601}
602
603void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
604{
605 UNIMPLEMENTED();
606}
607
608void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
609{
610 UNIMPLEMENTED();
611}
612
613void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
614{
615 UNIMPLEMENTED();
616}
617
618void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
619{
620 UNIMPLEMENTED();
621}
622
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400623template <int cols, int rows>
624void ProgramVk::setUniformMatrixfv(GLint location,
625 GLsizei count,
626 GLboolean transpose,
627 const GLfloat *value)
628{
629 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
630 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
631
Jamie Madill2b858c22018-09-03 13:58:14 -0400632 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400633 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400634 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400635 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
636
637 // Assume an offset of -1 means the block is unused.
638 if (layoutInfo.offset == -1)
639 {
640 continue;
641 }
642
Luc Ferronc8fbff32018-06-04 10:30:48 -0400643 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400644 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
645 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400646
647 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
648 // setter did not update any data. We still want the uniform to be included when we'll
649 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400650 if (updated)
651 {
652 mDefaultUniformBlocksDirty.set(shaderType);
653 }
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400654 }
655}
656
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400657void ProgramVk::setUniformMatrix2fv(GLint location,
658 GLsizei count,
659 GLboolean transpose,
660 const GLfloat *value)
661{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400662 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400663}
664
665void ProgramVk::setUniformMatrix3fv(GLint location,
666 GLsizei count,
667 GLboolean transpose,
668 const GLfloat *value)
669{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400670 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400671}
672
673void ProgramVk::setUniformMatrix4fv(GLint location,
674 GLsizei count,
675 GLboolean transpose,
676 const GLfloat *value)
677{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400678 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400679}
680
681void ProgramVk::setUniformMatrix2x3fv(GLint location,
682 GLsizei count,
683 GLboolean transpose,
684 const GLfloat *value)
685{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400686 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400687}
688
689void ProgramVk::setUniformMatrix3x2fv(GLint location,
690 GLsizei count,
691 GLboolean transpose,
692 const GLfloat *value)
693{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400694 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400695}
696
697void ProgramVk::setUniformMatrix2x4fv(GLint location,
698 GLsizei count,
699 GLboolean transpose,
700 const GLfloat *value)
701{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400702 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400703}
704
705void ProgramVk::setUniformMatrix4x2fv(GLint location,
706 GLsizei count,
707 GLboolean transpose,
708 const GLfloat *value)
709{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400710 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400711}
712
713void ProgramVk::setUniformMatrix3x4fv(GLint location,
714 GLsizei count,
715 GLboolean transpose,
716 const GLfloat *value)
717{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400718 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400719}
720
721void ProgramVk::setUniformMatrix4x3fv(GLint location,
722 GLsizei count,
723 GLboolean transpose,
724 const GLfloat *value)
725{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400726 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400727}
728
Sami Väisänen46eaa942016-06-29 10:26:37 +0300729void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
730 GLenum genMode,
731 GLint components,
732 const GLfloat *coeffs)
733{
734 UNIMPLEMENTED();
735}
736
Jamie Madill242c4fe2018-07-12 15:56:56 -0400737angle::Result ProgramVk::initShaders(ContextVk *contextVk,
738 const gl::DrawCallParams &drawCallParams,
739 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
740 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
741 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500742{
Jamie Madillb36a4812018-09-25 10:15:11 -0400743 if (UseLineRaster(contextVk, drawCallParams))
744 {
745 ANGLE_TRY(mLineRasterShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource, true,
746 vertexShaderAndSerialOut,
747 fragmentShaderAndSerialOut));
748 ASSERT(mLineRasterShaderInfo.valid());
749 }
750 else
751 {
752 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource, false,
753 vertexShaderAndSerialOut,
754 fragmentShaderAndSerialOut));
755 ASSERT(mDefaultShaderInfo.valid());
756 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400757
758 *pipelineLayoutOut = &mPipelineLayout.get();
759
760 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500761}
762
Jamie Madill21061022018-07-12 23:56:30 -0400763angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400764{
Jamie Madill76e471e2017-10-21 09:56:01 -0400765 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400766 vk::DynamicDescriptorPool *dynamicDescriptorPool =
767 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400768
Luc Ferron6ea1b412018-03-21 16:13:01 -0400769 uint32_t potentialNewCount = descriptorSetIndex + 1;
770 if (potentialNewCount > mDescriptorSets.size())
771 {
772 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
773 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400774
Jamie Madillc7918ce2018-06-13 13:25:31 -0400775 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400776 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400777 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400778 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400779 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400780}
781
Jamie Madill54164b02017-08-28 15:17:37 -0400782void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
783{
Luc Ferron7cec3352018-03-13 13:29:34 -0400784 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400785}
786
787void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
788{
Luc Ferron7cec3352018-03-13 13:29:34 -0400789 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400790}
791
792void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
793{
794 UNIMPLEMENTED();
795}
796
Jamie Madill21061022018-07-12 23:56:30 -0400797angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400798{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400799 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400800
Jamie Madill76e471e2017-10-21 09:56:01 -0400801 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400802 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400803 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400804 {
Jamie Madill33318de2018-05-01 11:22:54 -0400805 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400806
Jamie Madill2b858c22018-09-03 13:58:14 -0400807 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400808 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400809 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400810 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400811 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400812 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400813 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400814
815 if (bufferModified)
816 {
817 anyNewBufferAllocated = true;
818 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400819 }
820 }
821
Luc Ferron7a06ac12018-03-15 10:17:04 -0400822 if (anyNewBufferAllocated)
823 {
824 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
825 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400826 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400827 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
828 }
829
Jamie Madill21061022018-07-12 23:56:30 -0400830 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400831}
832
Jamie Madill21061022018-07-12 23:56:30 -0400833angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400834{
Jamie Madill33318de2018-05-01 11:22:54 -0400835 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
836 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400837
Jamie Madill33318de2018-05-01 11:22:54 -0400838 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400839 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400840 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
841 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
842 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400843
844 if (!uniformBlock.uniformData.empty())
845 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400846 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400847 }
848 else
849 {
850 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
851 }
852
853 bufferInfo.offset = 0;
854 bufferInfo.range = VK_WHOLE_SIZE;
855
Jamie Madill76e471e2017-10-21 09:56:01 -0400856 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
857 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400858 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400859 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400860 writeInfo.dstArrayElement = 0;
861 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400862 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400863 writeInfo.pImageInfo = nullptr;
864 writeInfo.pBufferInfo = &bufferInfo;
865 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400866 }
867
868 VkDevice device = contextVk->getDevice();
869
Jamie Madill33318de2018-05-01 11:22:54 -0400870 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400871
Jamie Madill21061022018-07-12 23:56:30 -0400872 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400873}
874
Jamie Madill84c662b2018-07-12 15:56:55 -0400875angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400876{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400877 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400878 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400879
Jamie Madill8c3988c2017-12-21 14:44:56 -0500880 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400881 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400882
Jamie Madill84c662b2018-07-12 15:56:55 -0400883 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
884 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400885 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400886
Jamie Madill84c662b2018-07-12 15:56:55 -0400887 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400888
Jamie Madill4cc753e2018-06-13 13:25:33 -0400889 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
890 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400891 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400892 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
893
Jamie Madill5547b382017-10-23 18:16:01 -0400894 ASSERT(!samplerBinding.unreferenced);
895
Jamie Madill4cc753e2018-06-13 13:25:33 -0400896 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
897 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400898 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400899 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
900 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400901 const vk::ImageHelper &image = textureVk->getImage();
902
903 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
904
905 imageInfo.sampler = textureVk->getSampler().getHandle();
906 imageInfo.imageView = textureVk->getImageView().getHandle();
907 imageInfo.imageLayout = image.getCurrentLayout();
908
909 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
910
911 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
912 writeInfo.pNext = nullptr;
913 writeInfo.dstSet = descriptorSet;
914 writeInfo.dstBinding = textureIndex;
915 writeInfo.dstArrayElement = arrayElement;
916 writeInfo.descriptorCount = 1;
917 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
918 writeInfo.pImageInfo = &imageInfo;
919 writeInfo.pBufferInfo = nullptr;
920 writeInfo.pTexelBufferView = nullptr;
921
922 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400923 }
Jamie Madill5547b382017-10-23 18:16:01 -0400924 }
925
926 VkDevice device = contextVk->getDevice();
927
Jamie Madill4cc753e2018-06-13 13:25:33 -0400928 ASSERT(writeCount > 0);
929 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400930
Jamie Madill84c662b2018-07-12 15:56:55 -0400931 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400932}
933
Luc Ferron7a06ac12018-03-15 10:17:04 -0400934void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
935{
936 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
937 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400938 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400939 }
940}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400941
942angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
943 const gl::DrawCallParams &drawCallParams,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400944 vk::CommandBuffer *commandBuffer)
945{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400946 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400947
948 if (mUsedDescriptorSetRange.empty())
949 return angle::Result::Continue();
950
951 ASSERT(!mDescriptorSets.empty());
952
953 unsigned int low = mUsedDescriptorSetRange.low();
954
955 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
956 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
957 {
958 commandBuffer->bindDescriptorSets(
959 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
960 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
961 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
962 }
963 else
964 {
965 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
966 low, mUsedDescriptorSetRange.length(),
967 &mDescriptorSets[low], 0, nullptr);
968 }
969
970 return angle::Result::Continue();
971}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400972} // namespace rx