blob: 4651a8f1b321250c340f5fd4b0837d1d920a0065 [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{
Shahbaz Youssefi5829c032018-11-28 10:39:41 -0500122 dynamicBuffer->releaseRetainedBuffers(contextVk->getRenderer());
123
Luc Ferron7a06ac12018-03-15 10:17:04 -0400124 ASSERT(!bufferData.empty());
125 uint8_t *data = nullptr;
126 VkBuffer *outBuffer = nullptr;
Jamie Madill4c310832018-08-29 13:43:17 -0400127 VkDeviceSize offset = 0;
Jamie Madill21061022018-07-12 23:56:30 -0400128 ANGLE_TRY(dynamicBuffer->allocate(contextVk, bufferData.size(), &data, outBuffer, &offset,
Jamie Madillc3755fc2018-04-05 08:39:13 -0400129 outBufferModified));
Jamie Madill4c310832018-08-29 13:43:17 -0400130 *outOffset = static_cast<uint32_t>(offset);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400131 memcpy(data, bufferData.data(), bufferData.size());
Jamie Madill21061022018-07-12 23:56:30 -0400132 ANGLE_TRY(dynamicBuffer->flush(contextVk));
133 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400134}
Jamie Madillb36a4812018-09-25 10:15:11 -0400135
Jamie Madillbfe31c42018-10-25 17:03:47 -0400136bool UseLineRaster(const ContextVk *contextVk, gl::PrimitiveMode mode)
Jamie Madillb36a4812018-09-25 10:15:11 -0400137{
Jamie Madillbfe31c42018-10-25 17:03:47 -0400138 return contextVk->getFeatures().basicGLLineRasterization && gl::IsLineMode(mode);
Jamie Madillb36a4812018-09-25 10:15:11 -0400139}
Jamie Madill76e471e2017-10-21 09:56:01 -0400140} // anonymous namespace
141
Jamie Madill242c4fe2018-07-12 15:56:56 -0400142// ProgramVk::ShaderInfo implementation.
Jamie Madillb980c562018-11-27 11:34:27 -0500143ProgramVk::ShaderInfo::ShaderInfo() {}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400144
145ProgramVk::ShaderInfo::~ShaderInfo() = default;
146
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500147angle::Result ProgramVk::ShaderInfo::initShaders(ContextVk *contextVk,
148 const std::string &vertexSource,
149 const std::string &fragmentSource,
150 bool enableLineRasterEmulation)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400151{
152 if (!valid())
153 {
154 std::vector<uint32_t> vertexCode;
155 std::vector<uint32_t> fragmentCode;
Jamie Madillb36a4812018-09-25 10:15:11 -0400156 ANGLE_TRY(GlslangWrapper::GetShaderCode(contextVk, contextVk->getCaps(),
157 enableLineRasterEmulation, vertexSource,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400158 fragmentSource, &vertexCode, &fragmentCode));
159
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500160 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Vertex].get(),
161 vertexCode.data(), vertexCode.size() * sizeof(uint32_t)));
162 ANGLE_TRY(vk::InitShaderAndSerial(contextVk, &mShaders[gl::ShaderType::Fragment].get(),
163 fragmentCode.data(),
Jamie Madill242c4fe2018-07-12 15:56:56 -0400164 fragmentCode.size() * sizeof(uint32_t)));
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500165
166 mProgramHelper.setShader(gl::ShaderType::Vertex, &mShaders[gl::ShaderType::Vertex]);
167 mProgramHelper.setShader(gl::ShaderType::Fragment, &mShaders[gl::ShaderType::Fragment]);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400168 }
169
Jamie Madill242c4fe2018-07-12 15:56:56 -0400170 return angle::Result::Continue();
171}
172
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500173void ProgramVk::ShaderInfo::release(RendererVk *renderer)
Jamie Madill242c4fe2018-07-12 15:56:56 -0400174{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500175 mProgramHelper.release(renderer);
176
177 for (vk::RefCounted<vk::ShaderAndSerial> &shader : mShaders)
178 {
179 shader.get().destroy(renderer->getDevice());
180 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400181}
182
183bool ProgramVk::ShaderInfo::valid() const
184{
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500185 return mShaders[gl::ShaderType::Vertex].get().valid();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400186}
187
188// ProgramVk implementation.
Jamie Madill76e471e2017-10-21 09:56:01 -0400189ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
Luc Ferron7a06ac12018-03-15 10:17:04 -0400190 : storage(VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500191 kUniformBlockDynamicBufferMinSize,
192 true)
Jamie Madillb980c562018-11-27 11:34:27 -0500193{}
Jamie Madill76e471e2017-10-21 09:56:01 -0400194
Jamie Madill242c4fe2018-07-12 15:56:56 -0400195ProgramVk::DefaultUniformBlock::~DefaultUniformBlock() = default;
Jamie Madillacf2f3a2017-11-21 19:22:44 -0500196
Jamie Madill88fc6da2018-08-30 16:18:36 -0400197ProgramVk::ProgramVk(const gl::ProgramState &state) : ProgramImpl(state), mUniformBlocksOffsets{}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400198{
Jamie Madill8c3988c2017-12-21 14:44:56 -0500199 mUsedDescriptorSetRange.invalidate();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400200}
201
Jamie Madill242c4fe2018-07-12 15:56:56 -0400202ProgramVk::~ProgramVk() = default;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400203
Jamie Madillf4a789f2018-10-18 16:56:20 -0400204void ProgramVk::destroy(const gl::Context *context)
Jamie Madill5deea722017-02-16 10:44:46 -0500205{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400206 ContextVk *contextVk = vk::GetImpl(context);
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500207 reset(contextVk->getRenderer());
Jamie Madillc5143482017-10-15 20:20:06 -0400208}
Jamie Madill5deea722017-02-16 10:44:46 -0500209
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500210void ProgramVk::reset(RendererVk *renderer)
Jamie Madillc5143482017-10-15 20:20:06 -0400211{
Jamie Madill9b168d02018-06-13 13:25:32 -0400212 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
213 {
214 descriptorSetLayout.reset();
215 }
216 mPipelineLayout.reset();
217
Jamie Madill76e471e2017-10-21 09:56:01 -0400218 for (auto &uniformBlock : mDefaultUniformBlocks)
219 {
Jamie Madillcaaff162018-06-22 08:55:37 -0400220 uniformBlock.storage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400221 }
222
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500223 mDefaultShaderInfo.release(renderer);
224 mLineRasterShaderInfo.release(renderer);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400225
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500226 mEmptyUniformBlockStorage.release(renderer);
Jamie Madill76e471e2017-10-21 09:56:01 -0400227
Jamie Madill5547b382017-10-23 18:16:01 -0400228 mDescriptorSets.clear();
Jamie Madill8c3988c2017-12-21 14:44:56 -0500229 mUsedDescriptorSetRange.invalidate();
Jamie Madillb7d924a2018-03-10 11:16:54 -0500230
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400231 for (vk::SharedDescriptorPoolBinding &binding : mDescriptorPoolBindings)
232 {
233 binding.reset();
234 }
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 Madilldc65c5b2018-11-21 11:07:26 -0500276 reset(renderer);
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.
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500355 gl::ShaderMap<sh::BlockLayoutMap> layoutMap;
356 gl::ShaderMap<size_t> requiredBufferSize;
Jamie Madill33318de2018-05-01 11:22:54 -0400357 requiredBufferSize.fill(0);
Jamie Madill76e471e2017-10-21 09:56:01 -0400358
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500359 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400360 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500361 gl::Shader *shader = mState.getAttachedShader(shaderType);
jchen103fd614d2018-08-13 12:21:58 +0800362 const std::vector<sh::Uniform> &uniforms = shader->getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400363 InitDefaultUniformBlock(uniforms, shader, &layoutMap[shaderType],
364 &requiredBufferSize[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400365 }
366
367 // Init the default block layout info.
Jamie Madill77abad82018-10-25 17:03:48 -0400368 const auto &uniforms = mState.getUniforms();
Jamie Madill242c4fe2018-07-12 15:56:56 -0400369 for (const gl::VariableLocation &location : mState.getUniformLocations())
Jamie Madill76e471e2017-10-21 09:56:01 -0400370 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500371 gl::ShaderMap<sh::BlockMemberInfo> layoutInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400372
Jamie Madill76e471e2017-10-21 09:56:01 -0400373 if (location.used() && !location.ignored)
374 {
Jamie Madillde03e002017-10-21 14:04:20 -0400375 const auto &uniform = uniforms[location.index];
Geoff Langbeb0c942018-09-27 11:22:23 -0400376 if (!uniform.isSampler())
Jamie Madill76e471e2017-10-21 09:56:01 -0400377 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400378 std::string uniformName = uniform.name;
379 if (uniform.isArray())
Jamie Madill76e471e2017-10-21 09:56:01 -0400380 {
Geoff Langbeb0c942018-09-27 11:22:23 -0400381 // Gets the uniform name without the [0] at the end.
382 uniformName = gl::ParseResourceName(uniformName, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400383 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400384
Geoff Langbeb0c942018-09-27 11:22:23 -0400385 bool found = false;
386
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500387 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Geoff Langbeb0c942018-09-27 11:22:23 -0400388 {
389 auto it = layoutMap[shaderType].find(uniformName);
390 if (it != layoutMap[shaderType].end())
391 {
392 found = true;
393 layoutInfo[shaderType] = it->second;
394 }
395 }
396
397 ASSERT(found);
398 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400399 }
400
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500401 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400402 {
Jamie Madill33318de2018-05-01 11:22:54 -0400403 mDefaultUniformBlocks[shaderType].uniformLayout.push_back(layoutInfo[shaderType]);
Jamie Madill76e471e2017-10-21 09:56:01 -0400404 }
405 }
406
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500407 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400408 {
Jamie Madill33318de2018-05-01 11:22:54 -0400409 if (requiredBufferSize[shaderType] > 0)
Jamie Madill76e471e2017-10-21 09:56:01 -0400410 {
Jamie Madill33318de2018-05-01 11:22:54 -0400411 if (!mDefaultUniformBlocks[shaderType].uniformData.resize(
412 requiredBufferSize[shaderType]))
Jamie Madill76e471e2017-10-21 09:56:01 -0400413 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400414 ANGLE_VK_CHECK(contextVk, false, VK_ERROR_OUT_OF_HOST_MEMORY);
Jamie Madill76e471e2017-10-21 09:56:01 -0400415 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400416 size_t minAlignment = static_cast<size_t>(
417 renderer->getPhysicalDeviceProperties().limits.minUniformBufferOffsetAlignment);
418
Luc Ferrona9ab0f32018-05-17 17:03:55 -0400419 mDefaultUniformBlocks[shaderType].storage.init(minAlignment, renderer);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400420
421 // Initialize uniform buffer memory to zero by default.
Jamie Madill33318de2018-05-01 11:22:54 -0400422 mDefaultUniformBlocks[shaderType].uniformData.fill(0);
Jamie Madill2b858c22018-09-03 13:58:14 -0400423 mDefaultUniformBlocksDirty.set(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400424 }
425 }
426
Jamie Madill2b858c22018-09-03 13:58:14 -0400427 if (mDefaultUniformBlocksDirty.any())
Jamie Madill76e471e2017-10-21 09:56:01 -0400428 {
429 // Initialize the "empty" uniform block if necessary.
Jamie Madill2b858c22018-09-03 13:58:14 -0400430 if (!mDefaultUniformBlocksDirty.all())
Jamie Madill76e471e2017-10-21 09:56:01 -0400431 {
Shahbaz Youssefi06270c92018-10-03 17:00:25 -0400432 VkBufferCreateInfo uniformBufferInfo = {};
Jamie Madill76e471e2017-10-21 09:56:01 -0400433 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
Jamie Madill76e471e2017-10-21 09:56:01 -0400434 uniformBufferInfo.flags = 0;
435 uniformBufferInfo.size = 1;
436 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
437 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
438 uniformBufferInfo.queueFamilyIndexCount = 0;
439 uniformBufferInfo.pQueueFamilyIndices = nullptr;
440
Luc Ferron7a06ac12018-03-15 10:17:04 -0400441 // Assume host visible/coherent memory available.
Jamie Madill57dd97a2018-02-06 17:10:49 -0500442 VkMemoryPropertyFlags flags =
443 (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500444 ANGLE_TRY(mEmptyUniformBlockStorage.init(contextVk, uniformBufferInfo, flags));
Jamie Madill76e471e2017-10-21 09:56:01 -0400445 }
Jamie Madill5547b382017-10-23 18:16:01 -0400446 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400447
Jamie Madill242c4fe2018-07-12 15:56:56 -0400448 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400449}
450
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400451GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
452{
Luc Ferronfba1f612018-06-04 14:37:17 -0400453 // No-op. The spec is very vague about the behavior of validation.
454 return GL_TRUE;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400455}
456
Jamie Madill76e471e2017-10-21 09:56:01 -0400457template <typename T>
458void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
459{
460 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
461 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
462
Luc Ferron7cec3352018-03-13 13:29:34 -0400463 if (linkedUniform.isSampler())
464 {
Jamie Madill0cb6dc42018-04-16 10:36:39 -0400465 // We could potentially cache some indexing here. For now this is a no-op since the mapping
466 // is handled entirely in ContextVk.
Luc Ferron7cec3352018-03-13 13:29:34 -0400467 return;
468 }
469
Luc Ferron24a31372018-04-04 11:49:14 -0400470 if (linkedUniform.typeInfo->type == entryPointType)
Jamie Madill76e471e2017-10-21 09:56:01 -0400471 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500472 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400473 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400474 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400475 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400476
Luc Ferron24a31372018-04-04 11:49:14 -0400477 // Assume an offset of -1 means the block is unused.
478 if (layoutInfo.offset == -1)
479 {
480 continue;
481 }
482
483 const GLint componentCount = linkedUniform.typeInfo->componentCount;
Luc Ferron62059a52018-03-29 07:01:35 -0400484 UpdateDefaultUniformBlock(count, locationInfo.arrayIndex, componentCount, v, layoutInfo,
485 &uniformBlock.uniformData);
Jamie Madill2b858c22018-09-03 13:58:14 -0400486 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400487 }
Luc Ferron24a31372018-04-04 11:49:14 -0400488 }
489 else
490 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500491 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron62059a52018-03-29 07:01:35 -0400492 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400493 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron24a31372018-04-04 11:49:14 -0400494 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
495
496 // Assume an offset of -1 means the block is unused.
497 if (layoutInfo.offset == -1)
498 {
499 continue;
500 }
501
502 const GLint componentCount = linkedUniform.typeInfo->componentCount;
503
Luc Ferron62059a52018-03-29 07:01:35 -0400504 ASSERT(linkedUniform.typeInfo->type == gl::VariableBoolVectorType(entryPointType));
505
Luc Ferrond91c3792018-04-06 09:36:36 -0400506 GLint initialArrayOffset =
507 locationInfo.arrayIndex * layoutInfo.arrayStride + layoutInfo.offset;
Luc Ferron62059a52018-03-29 07:01:35 -0400508 for (GLint i = 0; i < count; i++)
509 {
510 GLint elementOffset = i * layoutInfo.arrayStride + initialArrayOffset;
511 GLint *dest =
512 reinterpret_cast<GLint *>(uniformBlock.uniformData.data() + elementOffset);
513 const T *source = v + i * componentCount;
514
515 for (int c = 0; c < componentCount; c++)
516 {
517 dest[c] = (source[c] == static_cast<T>(0)) ? GL_FALSE : GL_TRUE;
518 }
519 }
Jamie Madill2b858c22018-09-03 13:58:14 -0400520
521 mDefaultUniformBlocksDirty.set(shaderType);
Luc Ferron62059a52018-03-29 07:01:35 -0400522 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400523 }
524}
525
Luc Ferron7cec3352018-03-13 13:29:34 -0400526template <typename T>
527void ProgramVk::getUniformImpl(GLint location, T *v, GLenum entryPointType) const
528{
529 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
530 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
531
Jamie Madill3bb2bbe2018-06-15 09:47:03 -0400532 ASSERT(!linkedUniform.isSampler());
Luc Ferron7cec3352018-03-13 13:29:34 -0400533
Olli Etuaho107c7242018-03-20 15:45:35 +0200534 const gl::ShaderType shaderType = linkedUniform.getFirstShaderTypeWhereActive();
Jiawei Shao385b3e02018-03-21 09:43:28 +0800535 ASSERT(shaderType != gl::ShaderType::InvalidEnum);
Luc Ferron7cec3352018-03-13 13:29:34 -0400536
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500537 const DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Jamie Madillb980c562018-11-27 11:34:27 -0500538 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
Luc Ferron62059a52018-03-29 07:01:35 -0400539
540 ASSERT(linkedUniform.typeInfo->componentType == entryPointType ||
541 linkedUniform.typeInfo->componentType == gl::VariableBoolVectorType(entryPointType));
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400542
543 if (gl::IsMatrixType(linkedUniform.type))
544 {
545 const uint8_t *ptrToElement = uniformBlock.uniformData.data() + layoutInfo.offset +
Luc Ferronf6fd48f2018-06-18 08:11:27 -0400546 (locationInfo.arrayIndex * layoutInfo.arrayStride);
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400547 GetMatrixUniform(linkedUniform.type, v, reinterpret_cast<const T *>(ptrToElement), false);
548 }
549 else
550 {
551 ReadFromDefaultUniformBlock(linkedUniform.typeInfo->componentCount, locationInfo.arrayIndex,
552 v, layoutInfo, &uniformBlock.uniformData);
553 }
Luc Ferron7cec3352018-03-13 13:29:34 -0400554}
555
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400556void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
557{
Jamie Madill76e471e2017-10-21 09:56:01 -0400558 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400559}
560
561void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
562{
Jamie Madill76e471e2017-10-21 09:56:01 -0400563 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400564}
565
566void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
567{
Jamie Madill76e471e2017-10-21 09:56:01 -0400568 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400569}
570
571void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
572{
Jamie Madill76e471e2017-10-21 09:56:01 -0400573 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400574}
575
576void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
577{
Luc Ferron7cec3352018-03-13 13:29:34 -0400578 setUniformImpl(location, count, v, GL_INT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400579}
580
581void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
582{
Luc Ferron489243f2018-03-28 16:55:28 -0400583 setUniformImpl(location, count, v, GL_INT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400584}
585
586void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
587{
Luc Ferron489243f2018-03-28 16:55:28 -0400588 setUniformImpl(location, count, v, GL_INT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400589}
590
591void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
592{
Luc Ferron489243f2018-03-28 16:55:28 -0400593 setUniformImpl(location, count, v, GL_INT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400594}
595
596void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
597{
598 UNIMPLEMENTED();
599}
600
601void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
602{
603 UNIMPLEMENTED();
604}
605
606void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
607{
608 UNIMPLEMENTED();
609}
610
611void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
612{
613 UNIMPLEMENTED();
614}
615
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400616template <int cols, int rows>
617void ProgramVk::setUniformMatrixfv(GLint location,
618 GLsizei count,
619 GLboolean transpose,
620 const GLfloat *value)
621{
622 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
623 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
624
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500625 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400626 {
Jamie Madill2b858c22018-09-03 13:58:14 -0400627 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400628 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
629
630 // Assume an offset of -1 means the block is unused.
631 if (layoutInfo.offset == -1)
632 {
633 continue;
634 }
635
Luc Ferronc8fbff32018-06-04 10:30:48 -0400636 bool updated = SetFloatUniformMatrix<cols, rows>(
Luc Ferron48cdc2e2018-05-31 09:58:34 -0400637 locationInfo.arrayIndex, linkedUniform.getArraySizeProduct(), count, transpose, value,
638 uniformBlock.uniformData.data() + layoutInfo.offset);
Luc Ferronc8fbff32018-06-04 10:30:48 -0400639
640 // If the uniformsDirty flag was true, we don't want to flip it to false here if the
641 // setter did not update any data. We still want the uniform to be included when we'll
642 // update the descriptor sets.
Jamie Madill2b858c22018-09-03 13:58:14 -0400643 if (updated)
644 {
645 mDefaultUniformBlocksDirty.set(shaderType);
646 }
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,
Jamie Madillbfe31c42018-10-25 17:03:47 -0400731 gl::PrimitiveMode mode,
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500732 vk::ShaderProgramHelper **programOut)
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500733{
Jamie Madillbfe31c42018-10-25 17:03:47 -0400734 if (UseLineRaster(contextVk, mode))
Jamie Madillb36a4812018-09-25 10:15:11 -0400735 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500736 ANGLE_TRY(
737 mLineRasterShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, true));
Jamie Madillb36a4812018-09-25 10:15:11 -0400738 ASSERT(mLineRasterShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500739 *programOut = &mLineRasterShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400740 }
741 else
742 {
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500743 ANGLE_TRY(mDefaultShaderInfo.initShaders(contextVk, mVertexSource, mFragmentSource, false));
Jamie Madillb36a4812018-09-25 10:15:11 -0400744 ASSERT(mDefaultShaderInfo.valid());
Jamie Madilldc65c5b2018-11-21 11:07:26 -0500745 *programOut = &mDefaultShaderInfo.getShaderProgram();
Jamie Madillb36a4812018-09-25 10:15:11 -0400746 }
Jamie Madill242c4fe2018-07-12 15:56:56 -0400747
Jamie Madill242c4fe2018-07-12 15:56:56 -0400748 return angle::Result::Continue();
Jamie Madillf2f6d372018-01-10 21:37:23 -0500749}
750
Jamie Madill21061022018-07-12 23:56:30 -0400751angle::Result ProgramVk::allocateDescriptorSet(ContextVk *contextVk, uint32_t descriptorSetIndex)
Jamie Madill76e471e2017-10-21 09:56:01 -0400752{
Jamie Madill76e471e2017-10-21 09:56:01 -0400753 // Write out to a new a descriptor set.
Jamie Madilledeaa832018-06-22 09:18:41 -0400754 vk::DynamicDescriptorPool *dynamicDescriptorPool =
755 contextVk->getDynamicDescriptorPool(descriptorSetIndex);
Jamie Madill76e471e2017-10-21 09:56:01 -0400756
Luc Ferron6ea1b412018-03-21 16:13:01 -0400757 uint32_t potentialNewCount = descriptorSetIndex + 1;
758 if (potentialNewCount > mDescriptorSets.size())
759 {
760 mDescriptorSets.resize(potentialNewCount, VK_NULL_HANDLE);
761 }
Luc Ferron7a06ac12018-03-15 10:17:04 -0400762
Jamie Madillc7918ce2018-06-13 13:25:31 -0400763 const vk::DescriptorSetLayout &descriptorSetLayout =
Jamie Madill9b168d02018-06-13 13:25:32 -0400764 mDescriptorSetLayouts[descriptorSetIndex].get();
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400765 ANGLE_TRY(dynamicDescriptorPool->allocateSets(contextVk, descriptorSetLayout.ptr(), 1,
Jamie Madill78bcd2b2018-10-16 15:05:20 -0400766 &mDescriptorPoolBindings[descriptorSetIndex],
Jamie Madill8a4c49f2018-06-21 15:43:06 -0400767 &mDescriptorSets[descriptorSetIndex]));
Jamie Madill21061022018-07-12 23:56:30 -0400768 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400769}
770
Jamie Madill54164b02017-08-28 15:17:37 -0400771void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
772{
Luc Ferron7cec3352018-03-13 13:29:34 -0400773 getUniformImpl(location, params, GL_FLOAT);
Jamie Madill54164b02017-08-28 15:17:37 -0400774}
775
776void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
777{
Luc Ferron7cec3352018-03-13 13:29:34 -0400778 getUniformImpl(location, params, GL_INT);
Jamie Madill54164b02017-08-28 15:17:37 -0400779}
780
781void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
782{
783 UNIMPLEMENTED();
784}
785
Jamie Madill21061022018-07-12 23:56:30 -0400786angle::Result ProgramVk::updateUniforms(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400787{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400788 ASSERT(dirtyUniforms());
Jamie Madill76e471e2017-10-21 09:56:01 -0400789
Jamie Madill76e471e2017-10-21 09:56:01 -0400790 // Update buffer memory by immediate mapping. This immediate update only works once.
Luc Ferron7a06ac12018-03-15 10:17:04 -0400791 bool anyNewBufferAllocated = false;
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500792 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400793 {
Jamie Madill33318de2018-05-01 11:22:54 -0400794 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
Luc Ferron7a06ac12018-03-15 10:17:04 -0400795
Jamie Madill2b858c22018-09-03 13:58:14 -0400796 if (mDefaultUniformBlocksDirty[shaderType])
Jamie Madill76e471e2017-10-21 09:56:01 -0400797 {
Luc Ferron7a06ac12018-03-15 10:17:04 -0400798 bool bufferModified = false;
Jamie Madill21061022018-07-12 23:56:30 -0400799 ANGLE_TRY(SyncDefaultUniformBlock(contextVk, &uniformBlock.storage,
Luc Ferron7a06ac12018-03-15 10:17:04 -0400800 uniformBlock.uniformData,
Jamie Madill33318de2018-05-01 11:22:54 -0400801 &mUniformBlocksOffsets[shaderType], &bufferModified));
Jamie Madill2b858c22018-09-03 13:58:14 -0400802 mDefaultUniformBlocksDirty.reset(shaderType);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400803
804 if (bufferModified)
805 {
806 anyNewBufferAllocated = true;
807 }
Jamie Madill76e471e2017-10-21 09:56:01 -0400808 }
809 }
810
Luc Ferron7a06ac12018-03-15 10:17:04 -0400811 if (anyNewBufferAllocated)
812 {
813 // We need to reinitialize the descriptor sets if we newly allocated buffers since we can't
814 // modify the descriptor sets once initialized.
Jamie Madillc7918ce2018-06-13 13:25:31 -0400815 ANGLE_TRY(allocateDescriptorSet(contextVk, kUniformsDescriptorSetIndex));
Luc Ferron7a06ac12018-03-15 10:17:04 -0400816 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
817 }
818
Jamie Madill21061022018-07-12 23:56:30 -0400819 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400820}
821
Jamie Madill21061022018-07-12 23:56:30 -0400822angle::Result ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
Jamie Madill76e471e2017-10-21 09:56:01 -0400823{
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500824 gl::ShaderMap<VkDescriptorBufferInfo> descriptorBufferInfo;
825 gl::ShaderMap<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill76e471e2017-10-21 09:56:01 -0400826
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500827 for (gl::ShaderType shaderType : gl::AllGLES2ShaderTypes())
Jamie Madill76e471e2017-10-21 09:56:01 -0400828 {
Jamie Madill242c4fe2018-07-12 15:56:56 -0400829 DefaultUniformBlock &uniformBlock = mDefaultUniformBlocks[shaderType];
830 VkDescriptorBufferInfo &bufferInfo = descriptorBufferInfo[shaderType];
831 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[shaderType];
Jamie Madill76e471e2017-10-21 09:56:01 -0400832
833 if (!uniformBlock.uniformData.empty())
834 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500835 bufferInfo.buffer = uniformBlock.storage.getCurrentBuffer()->getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400836 }
837 else
838 {
Shahbaz Youssefi254b32c2018-11-26 11:58:03 -0500839 bufferInfo.buffer = mEmptyUniformBlockStorage.getBuffer().getHandle();
Jamie Madill76e471e2017-10-21 09:56:01 -0400840 }
841
842 bufferInfo.offset = 0;
843 bufferInfo.range = VK_WHOLE_SIZE;
844
Jamie Madill76e471e2017-10-21 09:56:01 -0400845 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
846 writeInfo.pNext = nullptr;
Jamie Madill5547b382017-10-23 18:16:01 -0400847 writeInfo.dstSet = mDescriptorSets[0];
Jamie Madill33318de2018-05-01 11:22:54 -0400848 writeInfo.dstBinding = static_cast<uint32_t>(shaderType);
Jamie Madill76e471e2017-10-21 09:56:01 -0400849 writeInfo.dstArrayElement = 0;
850 writeInfo.descriptorCount = 1;
Luc Ferron7a06ac12018-03-15 10:17:04 -0400851 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC;
Jamie Madill76e471e2017-10-21 09:56:01 -0400852 writeInfo.pImageInfo = nullptr;
853 writeInfo.pBufferInfo = &bufferInfo;
854 writeInfo.pTexelBufferView = nullptr;
Jamie Madill76e471e2017-10-21 09:56:01 -0400855 }
856
857 VkDevice device = contextVk->getDevice();
858
Jamie Madill33318de2018-05-01 11:22:54 -0400859 vkUpdateDescriptorSets(device, 2, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill76e471e2017-10-21 09:56:01 -0400860
Jamie Madill21061022018-07-12 23:56:30 -0400861 return angle::Result::Continue();
Jamie Madill76e471e2017-10-21 09:56:01 -0400862}
863
Jamie Madill84c662b2018-07-12 15:56:55 -0400864angle::Result ProgramVk::updateTexturesDescriptorSet(ContextVk *contextVk)
Jamie Madill5547b382017-10-23 18:16:01 -0400865{
Jamie Madill88fc6da2018-08-30 16:18:36 -0400866 ASSERT(hasTextures());
Jamie Madillc7918ce2018-06-13 13:25:31 -0400867 ANGLE_TRY(allocateDescriptorSet(contextVk, kTextureDescriptorSetIndex));
Luc Ferron6ea1b412018-03-21 16:13:01 -0400868
Jamie Madill8c3988c2017-12-21 14:44:56 -0500869 ASSERT(mUsedDescriptorSetRange.contains(1));
Jamie Madillc7918ce2018-06-13 13:25:31 -0400870 VkDescriptorSet descriptorSet = mDescriptorSets[kTextureDescriptorSetIndex];
Jamie Madill5547b382017-10-23 18:16:01 -0400871
Jamie Madill84c662b2018-07-12 15:56:55 -0400872 gl::ActiveTextureArray<VkDescriptorImageInfo> descriptorImageInfo;
873 gl::ActiveTextureArray<VkWriteDescriptorSet> writeDescriptorInfo;
Jamie Madill4cc753e2018-06-13 13:25:33 -0400874 uint32_t writeCount = 0;
Jamie Madill5547b382017-10-23 18:16:01 -0400875
Jamie Madill84c662b2018-07-12 15:56:55 -0400876 const gl::ActiveTextureArray<TextureVk *> &activeTextures = contextVk->getActiveTextures();
Jamie Madill5547b382017-10-23 18:16:01 -0400877
Jamie Madill4cc753e2018-06-13 13:25:33 -0400878 for (uint32_t textureIndex = 0; textureIndex < mState.getSamplerBindings().size();
879 ++textureIndex)
Jamie Madill5547b382017-10-23 18:16:01 -0400880 {
Jamie Madill4cc753e2018-06-13 13:25:33 -0400881 const gl::SamplerBinding &samplerBinding = mState.getSamplerBindings()[textureIndex];
882
Jamie Madill5547b382017-10-23 18:16:01 -0400883 ASSERT(!samplerBinding.unreferenced);
884
Jamie Madill4cc753e2018-06-13 13:25:33 -0400885 for (uint32_t arrayElement = 0; arrayElement < samplerBinding.boundTextureUnits.size();
886 ++arrayElement)
Luc Ferron90968362018-05-04 08:47:22 -0400887 {
Jamie Madill84c662b2018-07-12 15:56:55 -0400888 GLuint textureUnit = samplerBinding.boundTextureUnits[arrayElement];
889 TextureVk *textureVk = activeTextures[textureUnit];
Jamie Madill4cc753e2018-06-13 13:25:33 -0400890 const vk::ImageHelper &image = textureVk->getImage();
891
892 VkDescriptorImageInfo &imageInfo = descriptorImageInfo[writeCount];
893
894 imageInfo.sampler = textureVk->getSampler().getHandle();
895 imageInfo.imageView = textureVk->getImageView().getHandle();
896 imageInfo.imageLayout = image.getCurrentLayout();
897
898 VkWriteDescriptorSet &writeInfo = writeDescriptorInfo[writeCount];
899
900 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
901 writeInfo.pNext = nullptr;
902 writeInfo.dstSet = descriptorSet;
903 writeInfo.dstBinding = textureIndex;
904 writeInfo.dstArrayElement = arrayElement;
905 writeInfo.descriptorCount = 1;
906 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
907 writeInfo.pImageInfo = &imageInfo;
908 writeInfo.pBufferInfo = nullptr;
909 writeInfo.pTexelBufferView = nullptr;
910
911 writeCount++;
Luc Ferron90968362018-05-04 08:47:22 -0400912 }
Jamie Madill5547b382017-10-23 18:16:01 -0400913 }
914
915 VkDevice device = contextVk->getDevice();
916
Jamie Madill4cc753e2018-06-13 13:25:33 -0400917 ASSERT(writeCount > 0);
918 vkUpdateDescriptorSets(device, writeCount, writeDescriptorInfo.data(), 0, nullptr);
Jamie Madill5547b382017-10-23 18:16:01 -0400919
Jamie Madill84c662b2018-07-12 15:56:55 -0400920 return angle::Result::Continue();
Jamie Madill5547b382017-10-23 18:16:01 -0400921}
922
Luc Ferron7a06ac12018-03-15 10:17:04 -0400923void ProgramVk::setDefaultUniformBlocksMinSizeForTesting(size_t minSize)
924{
925 for (DefaultUniformBlock &block : mDefaultUniformBlocks)
926 {
Jamie Madill6c7ab7f2018-03-31 14:19:15 -0400927 block.storage.setMinimumSizeForTesting(minSize);
Luc Ferron7a06ac12018-03-15 10:17:04 -0400928 }
929}
Jamie Madill242c4fe2018-07-12 15:56:56 -0400930
931angle::Result ProgramVk::updateDescriptorSets(ContextVk *contextVk,
Jamie Madill242c4fe2018-07-12 15:56:56 -0400932 vk::CommandBuffer *commandBuffer)
933{
Jamie Madill242c4fe2018-07-12 15:56:56 -0400934 // Can probably use better dirty bits here.
Jamie Madill242c4fe2018-07-12 15:56:56 -0400935
936 if (mUsedDescriptorSetRange.empty())
937 return angle::Result::Continue();
938
939 ASSERT(!mDescriptorSets.empty());
940
941 unsigned int low = mUsedDescriptorSetRange.low();
942
943 // No uniforms descriptor set means no need to specify dynamic buffer offsets.
944 if (mUsedDescriptorSetRange.contains(kUniformsDescriptorSetIndex))
945 {
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500946 constexpr uint32_t kShaderTypeMin = static_cast<uint32_t>(gl::kGLES2ShaderTypeMin);
947 constexpr uint32_t kShaderTypeMax = static_cast<uint32_t>(gl::kGLES2ShaderTypeMax);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400948 commandBuffer->bindDescriptorSets(
949 VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(), low,
950 mUsedDescriptorSetRange.length(), &mDescriptorSets[low],
Shahbaz Youssefidf066e92018-11-13 15:39:47 -0500951 kShaderTypeMax - kShaderTypeMin + 1, mUniformBlocksOffsets.data() + kShaderTypeMin);
Jamie Madill242c4fe2018-07-12 15:56:56 -0400952 }
953 else
954 {
955 commandBuffer->bindDescriptorSets(VK_PIPELINE_BIND_POINT_GRAPHICS, mPipelineLayout.get(),
956 low, mUsedDescriptorSetRange.length(),
957 &mDescriptorSets[low], 0, nullptr);
958 }
959
960 return angle::Result::Continue();
961}
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400962} // namespace rx