blob: 64efdd9b1efb47e4240804da0f0756a9152b89d6 [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;
Jamie Madill76e471e2017-10-21 09:56:01 -040071 memcpy(writePtr, v, elementSize * count);
72 }
73 else
74 {
Luc Ferron2371aca2018-03-27 16:03:03 -040075 // Have to respect the arrayStride between each element of the array.
76 int maxIndex = arrayIndex + count;
77 for (int writeIndex = arrayIndex, readIndex = 0; writeIndex < maxIndex;
78 writeIndex++, readIndex++)
79 {
80 const int arrayOffset = writeIndex * layoutInfo.arrayStride;
81 uint8_t *writePtr = dst + arrayOffset;
Luc Ferrone9465a62018-06-04 10:41:52 -040082 const T *readPtr = v + (readIndex * componentCount);
Luc Ferron2371aca2018-03-27 16:03:03 -040083 memcpy(writePtr, readPtr, elementSize);
84 }
Jamie Madill76e471e2017-10-21 09:56:01 -040085 }
86}
87
Luc Ferron7cec3352018-03-13 13:29:34 -040088template <typename T>
89void ReadFromDefaultUniformBlock(int componentCount,
Luc Ferron2371aca2018-03-27 16:03:03 -040090 uint32_t arrayIndex,
Luc Ferron7cec3352018-03-13 13:29:34 -040091 T *dst,
92 const sh::BlockMemberInfo &layoutInfo,
93 const angle::MemoryBuffer *uniformData)
94{
95 ASSERT(layoutInfo.offset != -1);
96
Luc Ferron2371aca2018-03-27 16:03:03 -040097 const int elementSize = sizeof(T) * componentCount;
98 const uint8_t *source = uniformData->data() + layoutInfo.offset;
99
Luc Ferron7cec3352018-03-13 13:29:34 -0400100 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
101 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400102 const uint8_t *readPtr = source + arrayIndex * layoutInfo.arrayStride;
Luc Ferron7cec3352018-03-13 13:29:34 -0400103 memcpy(dst, readPtr, elementSize);
104 }
105 else
106 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400107 // Have to respect the arrayStride between each element of the array.
108 const int arrayOffset = arrayIndex * layoutInfo.arrayStride;
109 const uint8_t *readPtr = source + arrayOffset;
110 memcpy(dst, readPtr, elementSize);
Luc Ferron7cec3352018-03-13 13:29:34 -0400111 }
112}
113
Jamie Madill21061022018-07-12 23:56:30 -0400114angle::Result SyncDefaultUniformBlock(ContextVk *contextVk,
115 vk::DynamicBuffer *dynamicBuffer,
116 const angle::MemoryBuffer &bufferData,
117 uint32_t *outOffset,
118 bool *outBufferModified)
Jamie Madill76e471e2017-10-21 09:56:01 -0400119{
Luc Ferron7a06ac12018-03-15 10:17:04 -0400120 ASSERT(!bufferData.empty());
121 uint8_t *data = nullptr;
122 VkBuffer *outBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -0400123 VkDeviceSize offset = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400124 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400125 outBufferModified));
Jamie Madill4c310832018-08-29 13:43:17 -0400126 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400127 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400128 ANGLE_TRY(dynamicBuffer->flush(contextVk));
129 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400130}
Jamie Madill76e471e2017-10-21 09:56:01 -0400131} // anonymous namespace
132
Jamie Madill242c4fe2018-07-12 15:56:56 -0400133// ProgramVk::ShaderInfo implementation.
134ProgramVk::ShaderInfo::ShaderInfo()
135{
136}
137
138ProgramVk::ShaderInfo::~ShaderInfo() = default;
139
140angle::Result ProgramVk::ShaderInfo::getShaders(
141 ContextVk *contextVk,
142 const std::string &vertexSource,
143 const std::string &fragmentSource,
144 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
145 const vk::ShaderAndSerial **fragmentShaderAndSerialOut)
146{
147 if (!valid())
148 {
149 std::vector<uint32_t> vertexCode;
150 std::vector<uint32_t> fragmentCode;
151 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(), vertexSource,
152 fragmentSource, &vertexCode, &fragmentCode));
153
154 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mVertexShaderAndSerial, vertexCode.data(),
155 vertexCode.size() * sizeof(uint32_t)));
156 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mFragmentShaderAndSerial, fragmentCode.data(),
157 fragmentCode.size() * sizeof(uint32_t)));
158 }
159
160 *fragmentShaderAndSerialOut = &mFragmentShaderAndSerial;
161 *vertexShaderAndSerialOut = &mVertexShaderAndSerial;
162 return angle::Result::Continue();
163}
164
165void ProgramVk::ShaderInfo::destroy(VkDevice device)
166{
167 mVertexShaderAndSerial.destroy(device);
168 mFragmentShaderAndSerial.destroy(device);
169}
170
171bool ProgramVk::ShaderInfo::valid() const
172{
173 return mVertexShaderAndSerial.valid();
174}
175
176// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400177ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400178 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Jamie Madillf3614372018-03-31 14:19:14 -0400179 kUniformBlockDynamicBufferMinSize),
Jamie Madill242c4fe2018-07-12 15:56:56 -0400180 uniformsDirty(false)
Jamie Madill76e471e2017-10-21 09:56:01 -0400181{
182}
183
Jamie Madill242c4fe2018-07-12 15:56:56 -0400184ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500185
Jamie Madill88fc6da2018-08-30 16:18:36 -0400186ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400187{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500188 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400189}
190
Jamie Madill242c4fe2018-07-12 15:56:56 -0400191ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400192
Jamie Madill242c4fe2018-07-12 15:56:56 -0400193gl::Error ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500194{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400195 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madillb7d924a2018-03-10 11:16:54 -0500196 return reset(contextVk);
Jamie Madillc5143482017-10-15 20:20:06 -0400197}
Jamie Madill5deea722017-02-16 10:44:46 -0500198
Jamie Madill21061022018-07-12 23:56:30 -0400199angle::Result ProgramVk::reset(ContextVk *contextVk)
Jamie Madillc5143482017-10-15 20:20:06 -0400200{
Jamie Madill67ae6c52018-03-09 11:49:01 -0500201 VkDevice device = contextVk->getDevice();
202
Jamie Madill9b168d02018-06-13 13:25:32 -0400203 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
204 {
205 descriptorSetLayout.reset();
206 }
207 mPipelineLayout.reset();
208
Jamie Madillcaaff162018-06-22 08:55:37 -0400209 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400210 for (auto &uniformBlock : mDefaultUniformBlocks)
211 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400212 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400213 }
214
Jamie Madill242c4fe2018-07-12 15:56:56 -0400215 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
216 mDefaultShaderInfo.destroy(device);
217
Jamie Madillcaaff162018-06-22 08:55:37 -0400218 Serial currentSerial = renderer->getCurrentQueueSerial();
219 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.memory);
220 renderer->releaseObject(currentSerial, &mEmptyUniformBlockStorage.buffer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400221
Jamie Madill5547b382017-10-23 18:16:01 -0400222 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500223 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500224
Jamie Madill21061022018-07-12 23:56:30 -0400225 return angle::Result::Continue();
Jamie Madill5deea722017-02-16 10:44:46 -0500226}
227
Jamie Madill242c4fe2018-07-12 15:56:56 -0400228gl::LinkResult ProgramVk::load(const gl::Context *context,
Jamie Madill9cf9e872017-06-05 12:59:25 -0400229 gl::InfoLog &infoLog,
230 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400231{
232 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500233 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400234}
235
Jamie Madill27a60632017-06-30 15:12:01 -0400236void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400237{
238 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400239}
240
241void ProgramVk::setBinaryRetrievableHint(bool retrievable)
242{
243 UNIMPLEMENTED();
244}
245
Yunchao He61afff12017-03-14 15:34:03 +0800246void ProgramVk::setSeparable(bool separable)
247{
248 UNIMPLEMENTED();
249}
250
jchen107ae70d82018-07-06 13:47:01 +0800251std::unique_ptr<LinkEvent> ProgramVk::link(const gl::Context *glContext,
252 const gl::ProgramLinkedResources &resources,
253 gl::InfoLog &infoLog)
254{
255 // TODO(jie.a.chen@intel.com): Parallelize linking.
256 // http://crbug.com/849576
257 return std::make_unique<LinkEventDone>(linkImpl(glContext, resources, infoLog));
258}
259
260gl::LinkResult ProgramVk::linkImpl(const gl::Context *glContext,
261 const gl::ProgramLinkedResources &resources,
262 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400263{
Jamie Madill06ca6342018-07-12 15:56:53 -0400264 ContextVk *contextVk = vk::GetImpl(glContext);
265 RendererVk *renderer = contextVk->getRenderer();
Jamie Madillc5143482017-10-15 20:20:06 -0400266
Jamie Madillb7d924a2018-03-10 11:16:54 -0500267 ANGLE_TRY(reset(contextVk));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500268
jchen103fd614d2018-08-13 12:21:58 +0800269 GlslangWrapper::GetShaderSource(mState, resources, &mVertexSource, &mFragmentSource);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500270
Jamie Madill76e471e2017-10-21 09:56:01 -0400271 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500272
Jamie Madill8c3988c2017-12-21 14:44:56 -0500273 if (!mState.getSamplerUniformRange().empty())
274 {
275 // Ensure the descriptor set range includes the textures at position 1.
Jamie Madill9b168d02018-06-13 13:25:32 -0400276 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill8c3988c2017-12-21 14:44:56 -0500277 }
278
Jamie Madill9b168d02018-06-13 13:25:32 -0400279 // Store a reference to the pipeline and descriptor set layouts. This will create them if they
280 // don't already exist in the cache.
281 vk::DescriptorSetLayoutDesc uniformsSetDesc;
282 uniformsSetDesc.update(kVertexUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
283 1);
284 uniformsSetDesc.update(kFragmentUniformsBindingIndex, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC,
285 1);
286
287 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400288 contextVk, uniformsSetDesc, &mDescriptorSetLayouts[kUniformsDescriptorSetIndex]));
Jamie Madill9b168d02018-06-13 13:25:32 -0400289
Jamie Madill9b168d02018-06-13 13:25:32 -0400290 vk::DescriptorSetLayoutDesc texturesSetDesc;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400291
292 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
293 ++textureIndex)
Jamie Madill9b168d02018-06-13 13:25:32 -0400294 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400295 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
296
297 // The front-end always binds array sampler units sequentially.
298 const uint32_t count = static_cast<uint32_t>(samplerBinding.boundTextureUnits.size());
299 texturesSetDesc.update(textureIndex, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, count);
Jamie Madill9b168d02018-06-13 13:25:32 -0400300 }
301
Jamie Madill21061022018-07-12 23:56:30 -0400302 ANGLE_TRY(renderer->getDescriptorSetLayout(contextVk, texturesSetDesc,
Jamie Madill9b168d02018-06-13 13:25:32 -0400303 &mDescriptorSetLayouts[kTextureDescriptorSetIndex]));
304
Jamie Madillb01b4802018-07-10 12:43:57 -0400305 vk::DescriptorSetLayoutDesc driverUniformsSetDesc;
306 driverUniformsSetDesc.update(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1);
307 ANGLE_TRY(renderer->getDescriptorSetLayout(
Jamie Madill21061022018-07-12 23:56:30 -0400308 contextVk, driverUniformsSetDesc,
309 &mDescriptorSetLayouts[kDriverUniformsDescriptorSetIndex]));
Jamie Madillb01b4802018-07-10 12:43:57 -0400310
Jamie Madill9b168d02018-06-13 13:25:32 -0400311 vk::PipelineLayoutDesc pipelineLayoutDesc;
312 pipelineLayoutDesc.updateDescriptorSetLayout(kUniformsDescriptorSetIndex, uniformsSetDesc);
313 pipelineLayoutDesc.updateDescriptorSetLayout(kTextureDescriptorSetIndex, texturesSetDesc);
Jamie Madillb01b4802018-07-10 12:43:57 -0400314 pipelineLayoutDesc.updateDescriptorSetLayout(kDriverUniformsDescriptorSetIndex,
315 driverUniformsSetDesc);
Jamie Madill9b168d02018-06-13 13:25:32 -0400316
Jamie Madill21061022018-07-12 23:56:30 -0400317 ANGLE_TRY(renderer->getPipelineLayout(contextVk, pipelineLayoutDesc, mDescriptorSetLayouts,
318 &mPipelineLayout));
Jamie Madill9b168d02018-06-13 13:25:32 -0400319
Jamie Madill242c4fe2018-07-12 15:56:56 -0400320 if (!mState.getUniforms().empty())
321 {
322 const gl::RangeUI &samplerRange = mState.getSamplerUniformRange();
323
324 if (mState.getUniforms().size() > samplerRange.length())
325 {
326 // Ensure the descriptor set range includes the uniform buffers at position 0.
327 mUsedDescriptorSetRange.extend(kUniformsDescriptorSetIndex);
328 }
329
330 if (!samplerRange.empty())
331 {
332 // Ensure the descriptor set range includes the textures at position 1.
333 mUsedDescriptorSetRange.extend(kTextureDescriptorSetIndex);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400334 }
335 }
336
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500337 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400338}
339
Jamie Madill242c4fe2018-07-12 15:56:56 -0400340angle::Result ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
Jamie Madill76e471e2017-10-21 09:56:01 -0400341{
Jamie Madille1f3ad42017-10-28 23:00:42 -0400342 ContextVk *contextVk = vk::GetImpl(glContext);
Jamie Madill57fbfd82018-02-14 12:45:34 -0500343 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill76e471e2017-10-21 09:56:01 -0400344
345 // Process vertex and fragment uniforms into std140 packing.
Jamie Madill33318de2018-05-01 11:22:54 -0400346 vk::ShaderMap<sh::BlockLayoutMap> layoutMap;
347 vk::ShaderMap<size_t> requiredBufferSize;
348 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400349
Jamie Madill33318de2018-05-01 11:22:54 -0400350 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400351 {
Jamie Madill33318de2018-05-01 11:22:54 -0400352 gl::ShaderType glShaderType = static_cast<gl::ShaderType>(shaderType);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400353 gl::Shader *shader = mState.getAttachedShader(glShaderType);
jchen103fd614d2018-08-13 12:21:58 +0800354 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400355 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
356 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400357 }
358
359 // Init the default block layout info.
Jamie Madill76e471e2017-10-21 09:56:01 -0400360 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400361 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400362 {
Jamie Madill33318de2018-05-01 11:22:54 -0400363 vk::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400364
Jamie Madill76e471e2017-10-21 09:56:01 -0400365 if (location.used() && !location.ignored)
366 {
Jamie Madillde03e002017-10-21 14:04:20 -0400367 const auto &uniform = uniforms[location.index];
368
369 if (uniform.isSampler())
370 continue;
371
Jamie Madill76e471e2017-10-21 09:56:01 -0400372 std::string uniformName = uniform.name;
373 if (uniform.isArray())
374 {
Luc Ferron2371aca2018-03-27 16:03:03 -0400375 // Gets the uniform name without the [0] at the end.
376 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400377 }
378
379 bool found = false;
380
Jamie Madill33318de2018-05-01 11:22:54 -0400381 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400382 {
Jamie Madill33318de2018-05-01 11:22:54 -0400383 auto it = layoutMap[shaderType].find(uniformName);
384 if (it != layoutMap[shaderType].end())
Jamie Madill76e471e2017-10-21 09:56:01 -0400385 {
Jamie Madill50cf2be2018-06-15 09:46:57 -0400386 found = true;
387 layoutInfo[shaderType] = it->second;
Jamie Madill76e471e2017-10-21 09:56:01 -0400388 }
389 }
390
391 ASSERT(found);
392 }
393
Jamie Madill33318de2018-05-01 11:22:54 -0400394 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400395 {
Jamie Madill33318de2018-05-01 11:22:54 -0400396 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400397 }
398 }
399
400 bool anyDirty = false;
401 bool allDirty = true;
402
Jamie Madill33318de2018-05-01 11:22:54 -0400403 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400404 {
Jamie Madill33318de2018-05-01 11:22:54 -0400405 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400406 {
Jamie Madill33318de2018-05-01 11:22:54 -0400407 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
408 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400409 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400410 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400411 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400412 size_t minAlignment = static_cast<size_t>(
413 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
414
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400415 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400416
417 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400418 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
419 mDefaultUniformBlocks[shaderType].uniformsDirty = true;
Jamie Madill76e471e2017-10-21 09:56:01 -0400420
421 anyDirty = true;
422 }
423 else
424 {
425 allDirty = false;
426 }
427 }
428
429 if (anyDirty)
430 {
431 // Initialize the "empty" uniform block if necessary.
432 if (!allDirty)
433 {
434 VkBufferCreateInfo uniformBufferInfo;
435 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
436 uniformBufferInfo.pNext = nullptr;
437 uniformBufferInfo.flags = 0;
438 uniformBufferInfo.size = 1;
439 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
440 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
441 uniformBufferInfo.queueFamilyIndexCount = 0;
442 uniformBufferInfo.pQueueFamilyIndices = nullptr;
443
Jamie Madill21061022018-07-12 23:56:30 -0400444 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(contextVk, uniformBufferInfo));
Jamie Madill76e471e2017-10-21 09:56:01 -0400445
Luc Ferron7a06ac12018-03-15 10:17:04 -0400446 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500447 VkMemoryPropertyFlags flags =
448 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Jamie Madill21061022018-07-12 23:56:30 -0400449 ANGLE_TRY(AllocateBufferMemory(contextVk, flags, &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 {
Luc Ferron24a31372018-04-04 11:49:14 -0400478 for (auto &uniformBlock : mDefaultUniformBlocks)
Jamie Madill76e471e2017-10-21 09:56:01 -0400479 {
Luc Ferron24a31372018-04-04 11:49:14 -0400480 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400481
Luc Ferron24a31372018-04-04 11:49:14 -0400482 // Assume an offset of -1 means the block is unused.
483 if (layoutInfo.offset == -1)
484 {
485 continue;
486 }
487
488 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400489 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
490 &uniformBlock.uniformData);
Luc Ferron24a31372018-04-04 11:49:14 -0400491 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400492 }
Luc Ferron24a31372018-04-04 11:49:14 -0400493 }
494 else
495 {
496 for (auto &uniformBlock : mDefaultUniformBlocks)
Luc Ferron62059a52018-03-29 07:01:35 -0400497 {
Luc Ferron24a31372018-04-04 11:49:14 -0400498 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
499
500 // Assume an offset of -1 means the block is unused.
501 if (layoutInfo.offset == -1)
502 {
503 continue;
504 }
505
506 const GLint componentCount = linkedUniform.typeInfo->componentCount;
507
Luc Ferron62059a52018-03-29 07:01:35 -0400508 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
509
Luc Ferrond91c3792018-04-06 09:36:36 -0400510 GLint initialArrayOffset =
511 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400512 for (GLint i = 0; i < count; i++)
513 {
514 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
515 GLint *dest =
516 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
517 const T *source = v + i * componentCount;
518
519 for (int c = 0; c < componentCount; c++)
520 {
521 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
522 }
523 }
Luc Ferron24a31372018-04-04 11:49:14 -0400524 uniformBlock.uniformsDirty = true;
Luc Ferron62059a52018-03-29 07:01:35 -0400525 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400526 }
527}
528
Luc Ferron7cec3352018-03-13 13:29:34 -0400529template <typename T>
530void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
531{
532 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
533 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
534
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400535 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400536
Olli Etuaho107c7242018-03-20 15:45:35 +0200537 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800538 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400539
Jiawei Shao385b3e02018-03-21 09:43:28 +0800540 const DefaultUniformBlock &uniformBlock =
Jamie Madill33318de2018-05-01 11:22:54 -0400541 mDefaultUniformBlocks[static_cast<vk::ShaderType>(shaderType)];
Jamie Madill50cf2be2018-06-15 09:46:57 -0400542 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400543
544 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
545 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400546
547 if (gl::IsMatrixType(linkedUniform.type))
548 {
549 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400550 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400551 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
552 }
553 else
554 {
555 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
556 v, layoutInfo, &uniformBlock.uniformData);
557 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400558}
559
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400560void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
561{
Jamie Madill76e471e2017-10-21 09:56:01 -0400562 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400563}
564
565void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
566{
Jamie Madill76e471e2017-10-21 09:56:01 -0400567 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400568}
569
570void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
571{
Jamie Madill76e471e2017-10-21 09:56:01 -0400572 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400573}
574
575void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
576{
Jamie Madill76e471e2017-10-21 09:56:01 -0400577 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400578}
579
580void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
581{
Luc Ferron7cec3352018-03-13 13:29:34 -0400582 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400583}
584
585void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
586{
Luc Ferron489243f2018-03-28 16:55:28 -0400587 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400588}
589
590void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
591{
Luc Ferron489243f2018-03-28 16:55:28 -0400592 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400593}
594
595void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
596{
Luc Ferron489243f2018-03-28 16:55:28 -0400597 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400598}
599
600void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
601{
602 UNIMPLEMENTED();
603}
604
605void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
606{
607 UNIMPLEMENTED();
608}
609
610void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
611{
612 UNIMPLEMENTED();
613}
614
615void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
616{
617 UNIMPLEMENTED();
618}
619
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400620template <int cols, int rows>
621void ProgramVk::setUniformMatrixfv(GLint location,
622 GLsizei count,
623 GLboolean transpose,
624 const GLfloat *value)
625{
626 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
627 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
628
629 for (auto &uniformBlock : mDefaultUniformBlocks)
630 {
631 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
632
633 // Assume an offset of -1 means the block is unused.
634 if (layoutInfo.offset == -1)
635 {
636 continue;
637 }
638
Luc Ferronc8fbff32018-06-04 10:30:48 -0400639 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400640 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
641 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400642
643 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
644 // setter did not update any data. We still want the uniform to be included when we'll
645 // update the descriptor sets.
646 uniformBlock.uniformsDirty = uniformBlock.uniformsDirty || updated;
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400647 }
648}
649
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400650void ProgramVk::setUniformMatrix2fv(GLint location,
651 GLsizei count,
652 GLboolean transpose,
653 const GLfloat *value)
654{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400655 setUniformMatrixfv<2, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400656}
657
658void ProgramVk::setUniformMatrix3fv(GLint location,
659 GLsizei count,
660 GLboolean transpose,
661 const GLfloat *value)
662{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400663 setUniformMatrixfv<3, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400664}
665
666void ProgramVk::setUniformMatrix4fv(GLint location,
667 GLsizei count,
668 GLboolean transpose,
669 const GLfloat *value)
670{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400671 setUniformMatrixfv<4, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400672}
673
674void ProgramVk::setUniformMatrix2x3fv(GLint location,
675 GLsizei count,
676 GLboolean transpose,
677 const GLfloat *value)
678{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400679 setUniformMatrixfv<2, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400680}
681
682void ProgramVk::setUniformMatrix3x2fv(GLint location,
683 GLsizei count,
684 GLboolean transpose,
685 const GLfloat *value)
686{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400687 setUniformMatrixfv<3, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400688}
689
690void ProgramVk::setUniformMatrix2x4fv(GLint location,
691 GLsizei count,
692 GLboolean transpose,
693 const GLfloat *value)
694{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400695 setUniformMatrixfv<2, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400696}
697
698void ProgramVk::setUniformMatrix4x2fv(GLint location,
699 GLsizei count,
700 GLboolean transpose,
701 const GLfloat *value)
702{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400703 setUniformMatrixfv<4, 2>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400704}
705
706void ProgramVk::setUniformMatrix3x4fv(GLint location,
707 GLsizei count,
708 GLboolean transpose,
709 const GLfloat *value)
710{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400711 setUniformMatrixfv<3, 4>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400712}
713
714void ProgramVk::setUniformMatrix4x3fv(GLint location,
715 GLsizei count,
716 GLboolean transpose,
717 const GLfloat *value)
718{
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400719 setUniformMatrixfv<4, 3>(location, count, transpose, value);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400720}
721
Sami Väisänen46eaa942016-06-29 10:26:37 +0300722void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
723 GLenum genMode,
724 GLint components,
725 const GLfloat *coeffs)
726{
727 UNIMPLEMENTED();
728}
729
Jamie Madill242c4fe2018-07-12 15:56:56 -0400730angle::Result ProgramVk::initShaders(ContextVk *contextVk,
731 const gl::DrawCallParams &drawCallParams,
732 const vk::ShaderAndSerial **vertexShaderAndSerialOut,
733 const vk::ShaderAndSerial **fragmentShaderAndSerialOut,
734 const vk::PipelineLayout **pipelineLayoutOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500735{
Jamie Madill06ca6342018-07-12 15:56:53 -0400736 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
Jamie Madill242c4fe2018-07-12 15:56:56 -0400737 ANGLE_TRY(mDefaultShaderInfo.getShaders(contextVk, mVertexSource, mFragmentSource,
738 vertexShaderAndSerialOut, fragmentShaderAndSerialOut));
739 ASSERT(mDefaultShaderInfo.valid());
740
741 *pipelineLayoutOut = &mPipelineLayout.get();
742
743 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500744}
745
Jamie Madill21061022018-07-12 23:56:30 -0400746angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400747{
Jamie Madill76e471e2017-10-21 09:56:01 -0400748 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400749 vk::DynamicDescriptorPool *dynamicDescriptorPool =
750 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400751
Luc Ferron6ea1b412018-03-21 16:13:01 -0400752 uint32_t potentialNewCount = descriptorSetIndex + 1;
753 if (potentialNewCount > mDescriptorSets.size())
754 {
755 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
756 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400757
Jamie Madillc7918ce2018-06-13 13:25:31 -0400758 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400759 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400760 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400761 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400762 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400763}
764
Jamie Madill54164b02017-08-28 15:17:37 -0400765void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
766{
Luc Ferron7cec3352018-03-13 13:29:34 -0400767 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400768}
769
770void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
771{
Luc Ferron7cec3352018-03-13 13:29:34 -0400772 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400773}
774
775void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
776{
777 UNIMPLEMENTED();
778}
779
Jamie Madill21061022018-07-12 23:56:30 -0400780angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400781{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400782 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400783
Jamie Madill76e471e2017-10-21 09:56:01 -0400784 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400785 bool anyNewBufferAllocated = false;
Jamie Madill33318de2018-05-01 11:22:54 -0400786 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400787 {
Jamie Madill33318de2018-05-01 11:22:54 -0400788 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400789
Jamie Madill76e471e2017-10-21 09:56:01 -0400790 if (uniformBlock.uniformsDirty)
791 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400792 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400793 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400794 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400795 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill76e471e2017-10-21 09:56:01 -0400796 uniformBlock.uniformsDirty = false;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400797
798 if (bufferModified)
799 {
800 anyNewBufferAllocated = true;
801 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400802 }
803 }
804
Luc Ferron7a06ac12018-03-15 10:17:04 -0400805 if (anyNewBufferAllocated)
806 {
807 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
808 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400809 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400810 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
811 }
812
Jamie Madill21061022018-07-12 23:56:30 -0400813 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400814}
815
Jamie Madill21061022018-07-12 23:56:30 -0400816angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400817{
Jamie Madill33318de2018-05-01 11:22:54 -0400818 vk::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
819 vk::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400820
Jamie Madill33318de2018-05-01 11:22:54 -0400821 for (vk::ShaderType shaderType : vk::AllShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400822 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400823 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
824 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
825 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400826
827 if (!uniformBlock.uniformData.empty())
828 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400829 bufferInfo.buffer = uniformBlock.storage.getCurrentBufferHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400830 }
831 else
832 {
833 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
834 }
835
836 bufferInfo.offset = 0;
837 bufferInfo.range = VK_WHOLE_SIZE;
838
Jamie Madill76e471e2017-10-21 09:56:01 -0400839 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
840 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400841 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400842 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400843 writeInfo.dstArrayElement = 0;
844 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400845 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400846 writeInfo.pImageInfo = nullptr;
847 writeInfo.pBufferInfo = &bufferInfo;
848 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400849 }
850
851 VkDevice device = contextVk->getDevice();
852
Jamie Madill33318de2018-05-01 11:22:54 -0400853 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400854
Jamie Madill21061022018-07-12 23:56:30 -0400855 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400856}
857
Jamie Madill84c662b2018-07-12 15:56:55 -0400858angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400859{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400860 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400861 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400862
Jamie Madill8c3988c2017-12-21 14:44:56 -0500863 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400864 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400865
Jamie Madill84c662b2018-07-12 15:56:55 -0400866 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
867 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400868 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400869
Jamie Madill84c662b2018-07-12 15:56:55 -0400870 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400871
Jamie Madill4cc753e2018-06-13 13:25:33 -0400872 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
873 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400874 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400875 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
876
Jamie Madill5547b382017-10-23 18:16:01 -0400877 ASSERT(!samplerBinding.unreferenced);
878
Jamie Madill4cc753e2018-06-13 13:25:33 -0400879 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
880 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400881 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400882 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
883 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400884 const vk::ImageHelper &image = textureVk->getImage();
885
886 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
887
888 imageInfo.sampler = textureVk->getSampler().getHandle();
889 imageInfo.imageView = textureVk->getImageView().getHandle();
890 imageInfo.imageLayout = image.getCurrentLayout();
891
892 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
893
894 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
895 writeInfo.pNext = nullptr;
896 writeInfo.dstSet = descriptorSet;
897 writeInfo.dstBinding = textureIndex;
898 writeInfo.dstArrayElement = arrayElement;
899 writeInfo.descriptorCount = 1;
900 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
901 writeInfo.pImageInfo = &imageInfo;
902 writeInfo.pBufferInfo = nullptr;
903 writeInfo.pTexelBufferView = nullptr;
904
905 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400906 }
Jamie Madill5547b382017-10-23 18:16:01 -0400907 }
908
909 VkDevice device = contextVk->getDevice();
910
Jamie Madill4cc753e2018-06-13 13:25:33 -0400911 ASSERT(writeCount > 0);
912 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400913
Jamie Madill84c662b2018-07-12 15:56:55 -0400914 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400915}
916
Luc Ferron7a06ac12018-03-15 10:17:04 -0400917void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
918{
919 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
920 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400921 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400922 }
923}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400924
925angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
926 const gl::DrawCallParams &drawCallParams,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400927 vk::CommandBuffer *commandBuffer)
928{
929 // TODO(jmadill): Line rasterization emulation shaders. http://anglebug.com/2598
930 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400931
932 if (mUsedDescriptorSetRange.empty())
933 return angle::Result::Continue();
934
935 ASSERT(!mDescriptorSets.empty());
936
937 unsigned int low = mUsedDescriptorSetRange.low();
938
939 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
940 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
941 {
942 commandBuffer->bindDescriptorSets(
943 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
944 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
945 static_cast<uint32_t>(mUniformBlocksOffsets.size()), mUniformBlocksOffsets.data());
946 }
947 else
948 {
949 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
950 low, mUsedDescriptorSetRange.length(),
951 &mDescriptorSets[low], 0, nullptr);
952 }
953
954 return angle::Result::Continue();
955}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400956} // namespace rx