blob: 502574a5e37bef8fbd7c54ad1307303482b270e7 [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 {
433 VkBufferCreateInfo uniformBufferInfo;
434 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
435 uniformBufferInfo.pNext = nullptr;
436 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
Jamie Madill21061022018-07-12 23:56:30 -0400443 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400444
Luc Ferron7a06ac12018-03-15 10:17:04 -0400445 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500446 VkMemoryPropertyFlags flags =
447 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Tobin Ehlisf5aad062018-09-19 14:39:00 -0600448 VkMemoryPropertyFlags flagsOut = 0;
449 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &flagsOut,
450 &mEmptyUniformBlockStorage.buffer,
Luc Ferrona72ebeb2018-06-28 11:16:58 -0400451 &mEmptyUniformBlockStorage.memory));
Jamie Madill76e471e2017-10-21 09:56:01 -0400452 }
Jamie Madill5547b382017-10-23 18:16:01 -0400453 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400454
Jamie Madill242c4fe2018-07-12 15:56:56 -0400455 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400456}
457
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400458GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
459{
Luc Ferronfba1f612018-06-04 14:37:17 -0400460 // No-op. The spec is very vague about the behavior of validation.
461 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400462}
463
Jamie Madill76e471e2017-10-21 09:56:01 -0400464template <typename T>
465void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
466{
467 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
468 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
469
Luc Ferron7cec3352018-03-13 13:29:34 -0400470 if (linkedUniform.isSampler())
471 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400472 // We could potentially cache some indexing here. For now this is a no-op since the mapping
473 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400474 return;
475 }
476
Luc Ferron24a31372018-04-04 11:49:14 -0400477 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400478 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400479 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400480 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400481 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400482 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400483
Luc Ferron24a31372018-04-04 11:49:14 -0400484 // Assume an offset of -1 means the block is unused.
485 if (layoutInfo.offset == -1)
486 {
487 continue;
488 }
489
490 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400491 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
492 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400493 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400494 }
Luc Ferron24a31372018-04-04 11:49:14 -0400495 }
496 else
497 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400498 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400499 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400500 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400501 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
502
503 // Assume an offset of -1 means the block is unused.
504 if (layoutInfo.offset == -1)
505 {
506 continue;
507 }
508
509 const GLint componentCount = linkedUniform.typeInfo->componentCount;
510
Luc Ferron62059a52018-03-29 07:01:35 -0400511 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
512
Luc Ferrond91c3792018-04-06 09:36:36 -0400513 GLint initialArrayOffset =
514 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400515 for (GLint i = 0; i < count; i++)
516 {
517 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
518 GLint *dest =
519 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
520 const T *source = v + i * componentCount;
521
522 for (int c = 0; c < componentCount; c++)
523 {
524 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
525 }
526 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400527
528 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400529 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400530 }
531}
532
Luc Ferron7cec3352018-03-13 13:29:34 -0400533template <typename T>
534void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
535{
536 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
537 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
538
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400539 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400540
Olli Etuaho107c7242018-03-20 15:45:35 +0200541 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800542 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400543
Jiawei Shao385b3e02018-03-21 09:43:28 +0800544 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400545 mDefaultUniformBlocks[static_cast<vk::ShaderType>(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
Jamie Madill2b858c22018-09-03 13:58:14 -0400633 for (vk::ShaderType shaderType : vk::AllShaderTypes())
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,
739 const gl::DrawCallParams &drawCallParams,
740 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
741 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
742 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500743{
Jamie Madillb36a4812018-09-25 10:15:11 -0400744 if (UseLineRaster(contextVk, drawCallParams))
745 {
746 ANGLE_TRY(mLineRasterShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource, true,
747 vertexShaderAndSerialOut,
748 fragmentShaderAndSerialOut));
749 ASSERT(mLineRasterShaderInfo.valid());
750 }
751 else
752 {
753 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource, false,
754 vertexShaderAndSerialOut,
755 fragmentShaderAndSerialOut));
756 ASSERT(mDefaultShaderInfo.valid());
757 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400758
759 *pipelineLayoutOut = &mPipelineLayout.get();
760
761 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500762}
763
Jamie Madill21061022018-07-12 23:56:30 -0400764angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400765{
Jamie Madill76e471e2017-10-21 09:56:01 -0400766 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400767 vk::DynamicDescriptorPool *dynamicDescriptorPool =
768 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400769
Luc Ferron6ea1b412018-03-21 16:13:01 -0400770 uint32_t potentialNewCount = descriptorSetIndex + 1;
771 if (potentialNewCount > mDescriptorSets.size())
772 {
773 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
774 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400775
Jamie Madillc7918ce2018-06-13 13:25:31 -0400776 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400777 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400778 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400779 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400780 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400781}
782
Jamie Madill54164b02017-08-28 15:17:37 -0400783void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
784{
Luc Ferron7cec3352018-03-13 13:29:34 -0400785 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400786}
787
788void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
789{
Luc Ferron7cec3352018-03-13 13:29:34 -0400790 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400791}
792
793void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
794{
795 UNIMPLEMENTED();
796}
797
Jamie Madill21061022018-07-12 23:56:30 -0400798angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400799{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400800 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400801
Jamie Madill76e471e2017-10-21 09:56:01 -0400802 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400803 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400804 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400805 {
Jamie Madill33318de2018-05-01 11:22:54 -0400806 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400807
Jamie Madill2b858c22018-09-03 13:58:14 -0400808 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400809 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400810 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400811 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400812 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400813 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400814 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400815
816 if (bufferModified)
817 {
818 anyNewBufferAllocated = true;
819 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400820 }
821 }
822
Luc Ferron7a06ac12018-03-15 10:17:04 -0400823 if (anyNewBufferAllocated)
824 {
825 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
826 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400827 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400828 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
829 }
830
Jamie Madill21061022018-07-12 23:56:30 -0400831 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400832}
833
Jamie Madill21061022018-07-12 23:56:30 -0400834angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400835{
Jamie Madill33318de2018-05-01 11:22:54 -0400836 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
837 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400838
Jamie Madill33318de2018-05-01 11:22:54 -0400839 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400840 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400841 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
842 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
843 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400844
845 if (!uniformBlock.uniformData.empty())
846 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400847 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400848 }
849 else
850 {
851 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
852 }
853
854 bufferInfo.offset = 0;
855 bufferInfo.range = VK_WHOLE_SIZE;
856
Jamie Madill76e471e2017-10-21 09:56:01 -0400857 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
858 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400859 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400860 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400861 writeInfo.dstArrayElement = 0;
862 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400863 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400864 writeInfo.pImageInfo = nullptr;
865 writeInfo.pBufferInfo = &bufferInfo;
866 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400867 }
868
869 VkDevice device = contextVk->getDevice();
870
Jamie Madill33318de2018-05-01 11:22:54 -0400871 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400872
Jamie Madill21061022018-07-12 23:56:30 -0400873 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400874}
875
Jamie Madill84c662b2018-07-12 15:56:55 -0400876angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400877{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400878 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400879 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400880
Jamie Madill8c3988c2017-12-21 14:44:56 -0500881 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400882 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400883
Jamie Madill84c662b2018-07-12 15:56:55 -0400884 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
885 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400886 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400887
Jamie Madill84c662b2018-07-12 15:56:55 -0400888 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400889
Jamie Madill4cc753e2018-06-13 13:25:33 -0400890 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
891 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400892 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400893 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
894
Jamie Madill5547b382017-10-23 18:16:01 -0400895 ASSERT(!samplerBinding.unreferenced);
896
Jamie Madill4cc753e2018-06-13 13:25:33 -0400897 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
898 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400899 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400900 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
901 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400902 const vk::ImageHelper &image = textureVk->getImage();
903
904 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
905
906 imageInfo.sampler = textureVk->getSampler().getHandle();
907 imageInfo.imageView = textureVk->getImageView().getHandle();
908 imageInfo.imageLayout = image.getCurrentLayout();
909
910 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
911
912 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
913 writeInfo.pNext = nullptr;
914 writeInfo.dstSet = descriptorSet;
915 writeInfo.dstBinding = textureIndex;
916 writeInfo.dstArrayElement = arrayElement;
917 writeInfo.descriptorCount = 1;
918 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
919 writeInfo.pImageInfo = &imageInfo;
920 writeInfo.pBufferInfo = nullptr;
921 writeInfo.pTexelBufferView = nullptr;
922
923 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400924 }
Jamie Madill5547b382017-10-23 18:16:01 -0400925 }
926
927 VkDevice device = contextVk->getDevice();
928
Jamie Madill4cc753e2018-06-13 13:25:33 -0400929 ASSERT(writeCount > 0);
930 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400931
Jamie Madill84c662b2018-07-12 15:56:55 -0400932 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400933}
934
Luc Ferron7a06ac12018-03-15 10:17:04 -0400935void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
936{
937 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
938 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400939 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400940 }
941}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400942
943angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
944 const gl::DrawCallParams &drawCallParams,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400945 vk::CommandBuffer *commandBuffer)
946{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400947 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400948
949 if (mUsedDescriptorSetRange.empty())
950 return angle::Result::Continue();
951
952 ASSERT(!mDescriptorSets.empty());
953
954 unsigned int low = mUsedDescriptorSetRange.low();
955
956 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
957 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
958 {
959 commandBuffer->bindDescriptorSets(
960 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
961 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
962 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
963 }
964 else
965 {
966 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
967 low, mUsedDescriptorSetRange.length(),
968 &mDescriptorSets[low], 0, nullptr);
969 }
970
971 return angle::Result::Continue();
972}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400973} // namespace rx