blob: efb8625b8f1ed286f8b38255789cdb09dd16ca1b [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"
Jamie Madill8ecf7f92017-01-13 17:29:52 -050015#include "libANGLE/renderer/vulkan/ContextVk.h"
16#include "libANGLE/renderer/vulkan/GlslangWrapper.h"
17#include "libANGLE/renderer/vulkan/RendererVk.h"
Jamie Madill9e54b5a2016-05-25 12:57:39 -040018
19namespace rx
20{
21
Jamie Madill76e471e2017-10-21 09:56:01 -040022namespace
23{
24
25gl::Error InitDefaultUniformBlock(const gl::Context *context,
26 VkDevice device,
27 gl::Shader *shader,
28 vk::BufferAndMemory *storageOut,
29 sh::BlockLayoutMap *blockLayoutMapOut,
30 size_t *requiredSizeOut)
31{
32 const auto &uniforms = shader->getUniforms(context);
33
34 if (uniforms.empty())
35 {
36 *requiredSizeOut = 0;
37 return gl::NoError();
38 }
39
40 sh::Std140BlockEncoder blockEncoder;
41 sh::GetUniformBlockInfo(uniforms, "", &blockEncoder, false, blockLayoutMapOut);
42
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 {
48 *requiredSizeOut = 0;
49 return gl::NoError();
50 }
51
52 VkBufferCreateInfo uniformBufferInfo;
53 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
54 uniformBufferInfo.pNext = nullptr;
55 uniformBufferInfo.flags = 0;
56 uniformBufferInfo.size = blockSize;
57 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
58 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
59 uniformBufferInfo.queueFamilyIndexCount = 0;
60 uniformBufferInfo.pQueueFamilyIndices = nullptr;
61
62 ANGLE_TRY(storageOut->buffer.init(device, uniformBufferInfo));
63
64 ANGLE_TRY(AllocateBufferMemory(GetImplAs<ContextVk>(context), blockSize, &storageOut->buffer,
65 &storageOut->memory, requiredSizeOut));
66
67 return gl::NoError();
68}
69
70template <typename T>
71void UpdateDefaultUniformBlock(GLsizei count,
72 int componentCount,
73 const T *v,
74 const sh::BlockMemberInfo &layoutInfo,
75 angle::MemoryBuffer *uniformData)
76{
77 // Assume an offset of -1 means the block is unused.
78 if (layoutInfo.offset == -1)
79 {
80 return;
81 }
82
83 int elementSize = sizeof(T) * componentCount;
84 if (layoutInfo.arrayStride == 0 || layoutInfo.arrayStride == elementSize)
85 {
86 uint8_t *writePtr = uniformData->data() + layoutInfo.offset;
87 memcpy(writePtr, v, elementSize * count);
88 }
89 else
90 {
91 UNIMPLEMENTED();
92 }
93}
94
95vk::Error SyncDefaultUniformBlock(VkDevice device,
96 vk::DeviceMemory *bufferMemory,
97 const angle::MemoryBuffer &bufferData)
98{
99 ASSERT(bufferMemory->valid() && !bufferData.empty());
100 uint8_t *mapPointer = nullptr;
101 ANGLE_TRY(bufferMemory->map(device, 0, bufferData.size(), 0, &mapPointer));
102 memcpy(mapPointer, bufferData.data(), bufferData.size());
103 bufferMemory->unmap(device);
104 return vk::NoError();
105}
106
107enum ShaderIndex : uint32_t
108{
109 MinShaderIndex = 0,
110 VertexShader = MinShaderIndex,
111 FragmentShader = 1,
112 MaxShaderIndex = 2,
113};
114
115gl::Shader *GetShader(const gl::ProgramState &programState, uint32_t shaderIndex)
116{
117 switch (shaderIndex)
118 {
119 case VertexShader:
120 return programState.getAttachedVertexShader();
121 case FragmentShader:
122 return programState.getAttachedFragmentShader();
123 default:
124 UNREACHABLE();
125 return nullptr;
126 }
127}
128
129} // anonymous namespace
130
131ProgramVk::DefaultUniformBlock::DefaultUniformBlock()
132 : storage(), uniformData(), uniformsDirty(false), uniformLayout()
133{
134}
135
136ProgramVk::ProgramVk(const gl::ProgramState &state)
137 : ProgramImpl(state), mDefaultUniformBlocks(), mDescriptorSet(VK_NULL_HANDLE)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400138{
139}
140
141ProgramVk::~ProgramVk()
142{
143}
144
Jamie Madillc564c072017-06-01 12:45:42 -0400145void ProgramVk::destroy(const gl::Context *contextImpl)
Jamie Madill5deea722017-02-16 10:44:46 -0500146{
Jamie Madillc564c072017-06-01 12:45:42 -0400147 VkDevice device = GetImplAs<ContextVk>(contextImpl)->getDevice();
Jamie Madillc5143482017-10-15 20:20:06 -0400148 reset(device);
149}
Jamie Madill5deea722017-02-16 10:44:46 -0500150
Jamie Madillc5143482017-10-15 20:20:06 -0400151void ProgramVk::reset(VkDevice device)
152{
Jamie Madill76e471e2017-10-21 09:56:01 -0400153 for (auto &uniformBlock : mDefaultUniformBlocks)
154 {
155 uniformBlock.storage.memory.destroy(device);
156 uniformBlock.storage.buffer.destroy(device);
157 }
158
159 mEmptyUniformBlockStorage.memory.destroy(device);
160 mEmptyUniformBlockStorage.buffer.destroy(device);
161
162 for (auto &descriptorSetLayout : mDescriptorSetLayouts)
163 {
164 descriptorSetLayout.destroy(device);
165 }
166
Jamie Madill5deea722017-02-16 10:44:46 -0500167 mLinkedFragmentModule.destroy(device);
168 mLinkedVertexModule.destroy(device);
169 mPipelineLayout.destroy(device);
Jamie Madill76e471e2017-10-21 09:56:01 -0400170
171 // Descriptor Sets are pool allocated, so do not need to be explicitly freed.
172 mDescriptorSet = VK_NULL_HANDLE;
Jamie Madill5deea722017-02-16 10:44:46 -0500173}
174
Jamie Madill9cf9e872017-06-05 12:59:25 -0400175gl::LinkResult ProgramVk::load(const gl::Context *contextImpl,
176 gl::InfoLog &infoLog,
177 gl::BinaryInputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400178{
179 UNIMPLEMENTED();
Yuly Novikovc4d18aa2017-03-09 18:45:02 -0500180 return gl::InternalError();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400181}
182
Jamie Madill27a60632017-06-30 15:12:01 -0400183void ProgramVk::save(const gl::Context *context, gl::BinaryOutputStream *stream)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400184{
185 UNIMPLEMENTED();
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400186}
187
188void ProgramVk::setBinaryRetrievableHint(bool retrievable)
189{
190 UNIMPLEMENTED();
191}
192
Yunchao He61afff12017-03-14 15:34:03 +0800193void ProgramVk::setSeparable(bool separable)
194{
195 UNIMPLEMENTED();
196}
197
Jamie Madill9cf9e872017-06-05 12:59:25 -0400198gl::LinkResult ProgramVk::link(const gl::Context *glContext,
199 const gl::VaryingPacking &packing,
200 gl::InfoLog &infoLog)
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400201{
Jamie Madillc5143482017-10-15 20:20:06 -0400202 ContextVk *contextVk = GetImplAs<ContextVk>(glContext);
203 RendererVk *renderer = contextVk->getRenderer();
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500204 GlslangWrapper *glslangWrapper = renderer->getGlslangWrapper();
Jamie Madillc5143482017-10-15 20:20:06 -0400205 VkDevice device = renderer->getDevice();
206
207 reset(device);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500208
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500209 std::vector<uint32_t> vertexCode;
210 std::vector<uint32_t> fragmentCode;
211 bool linkSuccess = false;
Jamie Madill2a9e1072017-09-22 11:31:57 -0400212 ANGLE_TRY_RESULT(glslangWrapper->linkProgram(glContext, mState, &vertexCode, &fragmentCode),
213 linkSuccess);
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500214 if (!linkSuccess)
215 {
216 return false;
217 }
218
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500219 {
220 VkShaderModuleCreateInfo vertexShaderInfo;
221 vertexShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
222 vertexShaderInfo.pNext = nullptr;
223 vertexShaderInfo.flags = 0;
224 vertexShaderInfo.codeSize = vertexCode.size() * sizeof(uint32_t);
225 vertexShaderInfo.pCode = vertexCode.data();
Jamie Madillc5143482017-10-15 20:20:06 -0400226
227 ANGLE_TRY(mLinkedVertexModule.init(device, vertexShaderInfo));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500228 }
229
230 {
231 VkShaderModuleCreateInfo fragmentShaderInfo;
232 fragmentShaderInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
233 fragmentShaderInfo.pNext = nullptr;
234 fragmentShaderInfo.flags = 0;
235 fragmentShaderInfo.codeSize = fragmentCode.size() * sizeof(uint32_t);
236 fragmentShaderInfo.pCode = fragmentCode.data();
237
Jamie Madillc5143482017-10-15 20:20:06 -0400238 ANGLE_TRY(mLinkedFragmentModule.init(device, fragmentShaderInfo));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500239 }
240
Jamie Madillc5143482017-10-15 20:20:06 -0400241 ANGLE_TRY(initPipelineLayout(contextVk));
Jamie Madill76e471e2017-10-21 09:56:01 -0400242 ANGLE_TRY(initDescriptorSets(contextVk));
243 ANGLE_TRY(initDefaultUniformBlocks(glContext));
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500244
245 return true;
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400246}
247
Jamie Madill76e471e2017-10-21 09:56:01 -0400248gl::Error ProgramVk::initDefaultUniformBlocks(const gl::Context *glContext)
249{
250 ContextVk *contextVk = GetImplAs<ContextVk>(glContext);
251 VkDevice device = contextVk->getDevice();
252
253 // Process vertex and fragment uniforms into std140 packing.
254 std::array<sh::BlockLayoutMap, 2> layoutMap;
255 std::array<size_t, 2> requiredBufferSize = {{0, 0}};
256
257 for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex)
258 {
259 ANGLE_TRY(InitDefaultUniformBlock(glContext, device, GetShader(mState, shaderIndex),
260 &mDefaultUniformBlocks[shaderIndex].storage,
261 &layoutMap[shaderIndex],
262 &requiredBufferSize[shaderIndex]));
263 }
264
265 // Init the default block layout info.
266 const auto &locations = mState.getUniformLocations();
267 const auto &uniforms = mState.getUniforms();
268 for (size_t locationIndex = 0; locationIndex < locations.size(); ++locationIndex)
269 {
270 std::array<sh::BlockMemberInfo, 2> layoutInfo;
271
272 const auto &location = locations[locationIndex];
273 if (location.used() && !location.ignored)
274 {
275 const auto &uniform = uniforms[location.index];
276 std::string uniformName = uniform.name;
277 if (uniform.isArray())
278 {
279 uniformName += ArrayIndexString(location.arrayIndices);
280 }
281
282 bool found = false;
283
284 for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex)
285 {
286 auto it = layoutMap[shaderIndex].find(uniformName);
287 if (it != layoutMap[shaderIndex].end())
288 {
289 found = true;
290 layoutInfo[shaderIndex] = it->second;
291 }
292 }
293
294 ASSERT(found);
295 }
296
297 for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex)
298 {
299 mDefaultUniformBlocks[shaderIndex].uniformLayout.push_back(layoutInfo[shaderIndex]);
300 }
301 }
302
303 bool anyDirty = false;
304 bool allDirty = true;
305
306 for (uint32_t shaderIndex = MinShaderIndex; shaderIndex < MaxShaderIndex; ++shaderIndex)
307 {
308 if (requiredBufferSize[shaderIndex] > 0)
309 {
310 if (!mDefaultUniformBlocks[shaderIndex].uniformData.resize(
311 requiredBufferSize[shaderIndex]))
312 {
313 return gl::OutOfMemory() << "Memory allocation failure.";
314 }
315 mDefaultUniformBlocks[shaderIndex].uniformData.fill(0);
316 mDefaultUniformBlocks[shaderIndex].uniformsDirty = true;
317
318 anyDirty = true;
319 }
320 else
321 {
322 allDirty = false;
323 }
324 }
325
326 if (anyDirty)
327 {
328 // Initialize the "empty" uniform block if necessary.
329 if (!allDirty)
330 {
331 VkBufferCreateInfo uniformBufferInfo;
332 uniformBufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
333 uniformBufferInfo.pNext = nullptr;
334 uniformBufferInfo.flags = 0;
335 uniformBufferInfo.size = 1;
336 uniformBufferInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
337 uniformBufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
338 uniformBufferInfo.queueFamilyIndexCount = 0;
339 uniformBufferInfo.pQueueFamilyIndices = nullptr;
340
341 ANGLE_TRY(mEmptyUniformBlockStorage.buffer.init(device, uniformBufferInfo));
342
343 size_t requiredSize = 0;
344 ANGLE_TRY(AllocateBufferMemory(contextVk, 1, &mEmptyUniformBlockStorage.buffer,
345 &mEmptyUniformBlockStorage.memory, &requiredSize));
346 }
347
348 ANGLE_TRY(updateDefaultUniformsDescriptorSet(contextVk));
349 }
350
351 return gl::NoError();
352}
353
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400354GLboolean ProgramVk::validate(const gl::Caps &caps, gl::InfoLog *infoLog)
355{
356 UNIMPLEMENTED();
357 return GLboolean();
358}
359
Jamie Madill76e471e2017-10-21 09:56:01 -0400360template <typename T>
361void ProgramVk::setUniformImpl(GLint location, GLsizei count, const T *v, GLenum entryPointType)
362{
363 const gl::VariableLocation &locationInfo = mState.getUniformLocations()[location];
364 const gl::LinkedUniform &linkedUniform = mState.getUniforms()[locationInfo.index];
365
366 if (linkedUniform.type == entryPointType)
367 {
368 for (auto &uniformBlock : mDefaultUniformBlocks)
369 {
370 const sh::BlockMemberInfo &layoutInfo = uniformBlock.uniformLayout[location];
371 UpdateDefaultUniformBlock(count, linkedUniform.typeInfo->componentCount, v, layoutInfo,
372 &uniformBlock.uniformData);
373 }
374 }
375 else
376 {
377 ASSERT(linkedUniform.type == gl::VariableBoolVectorType(entryPointType));
378 UNIMPLEMENTED();
379 }
380}
381
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400382void ProgramVk::setUniform1fv(GLint location, GLsizei count, const GLfloat *v)
383{
Jamie Madill76e471e2017-10-21 09:56:01 -0400384 setUniformImpl(location, count, v, GL_FLOAT);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400385}
386
387void ProgramVk::setUniform2fv(GLint location, GLsizei count, const GLfloat *v)
388{
Jamie Madill76e471e2017-10-21 09:56:01 -0400389 setUniformImpl(location, count, v, GL_FLOAT_VEC2);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400390}
391
392void ProgramVk::setUniform3fv(GLint location, GLsizei count, const GLfloat *v)
393{
Jamie Madill76e471e2017-10-21 09:56:01 -0400394 setUniformImpl(location, count, v, GL_FLOAT_VEC3);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400395}
396
397void ProgramVk::setUniform4fv(GLint location, GLsizei count, const GLfloat *v)
398{
Jamie Madill76e471e2017-10-21 09:56:01 -0400399 setUniformImpl(location, count, v, GL_FLOAT_VEC4);
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400400}
401
402void ProgramVk::setUniform1iv(GLint location, GLsizei count, const GLint *v)
403{
404 UNIMPLEMENTED();
405}
406
407void ProgramVk::setUniform2iv(GLint location, GLsizei count, const GLint *v)
408{
409 UNIMPLEMENTED();
410}
411
412void ProgramVk::setUniform3iv(GLint location, GLsizei count, const GLint *v)
413{
414 UNIMPLEMENTED();
415}
416
417void ProgramVk::setUniform4iv(GLint location, GLsizei count, const GLint *v)
418{
419 UNIMPLEMENTED();
420}
421
422void ProgramVk::setUniform1uiv(GLint location, GLsizei count, const GLuint *v)
423{
424 UNIMPLEMENTED();
425}
426
427void ProgramVk::setUniform2uiv(GLint location, GLsizei count, const GLuint *v)
428{
429 UNIMPLEMENTED();
430}
431
432void ProgramVk::setUniform3uiv(GLint location, GLsizei count, const GLuint *v)
433{
434 UNIMPLEMENTED();
435}
436
437void ProgramVk::setUniform4uiv(GLint location, GLsizei count, const GLuint *v)
438{
439 UNIMPLEMENTED();
440}
441
442void ProgramVk::setUniformMatrix2fv(GLint location,
443 GLsizei count,
444 GLboolean transpose,
445 const GLfloat *value)
446{
447 UNIMPLEMENTED();
448}
449
450void ProgramVk::setUniformMatrix3fv(GLint location,
451 GLsizei count,
452 GLboolean transpose,
453 const GLfloat *value)
454{
455 UNIMPLEMENTED();
456}
457
458void ProgramVk::setUniformMatrix4fv(GLint location,
459 GLsizei count,
460 GLboolean transpose,
461 const GLfloat *value)
462{
463 UNIMPLEMENTED();
464}
465
466void ProgramVk::setUniformMatrix2x3fv(GLint location,
467 GLsizei count,
468 GLboolean transpose,
469 const GLfloat *value)
470{
471 UNIMPLEMENTED();
472}
473
474void ProgramVk::setUniformMatrix3x2fv(GLint location,
475 GLsizei count,
476 GLboolean transpose,
477 const GLfloat *value)
478{
479 UNIMPLEMENTED();
480}
481
482void ProgramVk::setUniformMatrix2x4fv(GLint location,
483 GLsizei count,
484 GLboolean transpose,
485 const GLfloat *value)
486{
487 UNIMPLEMENTED();
488}
489
490void ProgramVk::setUniformMatrix4x2fv(GLint location,
491 GLsizei count,
492 GLboolean transpose,
493 const GLfloat *value)
494{
495 UNIMPLEMENTED();
496}
497
498void ProgramVk::setUniformMatrix3x4fv(GLint location,
499 GLsizei count,
500 GLboolean transpose,
501 const GLfloat *value)
502{
503 UNIMPLEMENTED();
504}
505
506void ProgramVk::setUniformMatrix4x3fv(GLint location,
507 GLsizei count,
508 GLboolean transpose,
509 const GLfloat *value)
510{
511 UNIMPLEMENTED();
512}
513
514void ProgramVk::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding)
515{
516 UNIMPLEMENTED();
517}
518
Olli Etuaho855d9642017-05-17 14:05:06 +0300519bool ProgramVk::getUniformBlockSize(const std::string &blockName,
520 const std::string &blockMappedName,
521 size_t *sizeOut) const
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400522{
523 UNIMPLEMENTED();
524 return bool();
525}
526
527bool ProgramVk::getUniformBlockMemberInfo(const std::string &memberUniformName,
Olli Etuaho855d9642017-05-17 14:05:06 +0300528 const std::string &memberUniformMappedName,
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400529 sh::BlockMemberInfo *memberInfoOut) const
530{
531 UNIMPLEMENTED();
532 return bool();
533}
534
Sami Väisänen46eaa942016-06-29 10:26:37 +0300535void ProgramVk::setPathFragmentInputGen(const std::string &inputName,
536 GLenum genMode,
537 GLint components,
538 const GLfloat *coeffs)
539{
540 UNIMPLEMENTED();
541}
542
Jamie Madill8ecf7f92017-01-13 17:29:52 -0500543const vk::ShaderModule &ProgramVk::getLinkedVertexModule() const
544{
545 ASSERT(mLinkedVertexModule.getHandle() != VK_NULL_HANDLE);
546 return mLinkedVertexModule;
547}
548
549const vk::ShaderModule &ProgramVk::getLinkedFragmentModule() const
550{
551 ASSERT(mLinkedFragmentModule.getHandle() != VK_NULL_HANDLE);
552 return mLinkedFragmentModule;
553}
554
Jamie Madillc5143482017-10-15 20:20:06 -0400555const vk::PipelineLayout &ProgramVk::getPipelineLayout() const
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500556{
Jamie Madillc5143482017-10-15 20:20:06 -0400557 return mPipelineLayout;
558}
559
560vk::Error ProgramVk::initPipelineLayout(ContextVk *context)
561{
562 ASSERT(!mPipelineLayout.valid());
563
564 VkDevice device = context->getDevice();
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500565
Jamie Madill76e471e2017-10-21 09:56:01 -0400566 // Create two descriptor set layouts: one for default uniform info, and one for textures.
567 // Skip one or both if there are no uniforms.
568 VkDescriptorSetLayoutBinding uniformBindings[2];
569 uint32_t blockCount = 0;
570
571 {
572 auto &layoutBinding = uniformBindings[blockCount];
573
574 layoutBinding.binding = blockCount;
575 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
576 layoutBinding.descriptorCount = 1;
577 layoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
578 layoutBinding.pImmutableSamplers = nullptr;
579
580 blockCount++;
581 }
582
583 {
584 auto &layoutBinding = uniformBindings[blockCount];
585
586 layoutBinding.binding = blockCount;
587 layoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
588 layoutBinding.descriptorCount = 1;
589 layoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
590 layoutBinding.pImmutableSamplers = nullptr;
591
592 blockCount++;
593 }
594
595 {
596 VkDescriptorSetLayoutCreateInfo uniformInfo;
597 uniformInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
598 uniformInfo.pNext = nullptr;
599 uniformInfo.flags = 0;
600 uniformInfo.bindingCount = blockCount;
601 uniformInfo.pBindings = uniformBindings;
602
603 vk::DescriptorSetLayout uniformLayout;
604 ANGLE_TRY(uniformLayout.init(device, uniformInfo));
605 mDescriptorSetLayouts.push_back(std::move(uniformLayout));
606 }
607
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500608 VkPipelineLayoutCreateInfo createInfo;
609 createInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
610 createInfo.pNext = nullptr;
611 createInfo.flags = 0;
Jamie Madill76e471e2017-10-21 09:56:01 -0400612 createInfo.setLayoutCount = static_cast<uint32_t>(mDescriptorSetLayouts.size());
613 createInfo.pSetLayouts = mDescriptorSetLayouts[0].ptr();
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500614 createInfo.pushConstantRangeCount = 0;
615 createInfo.pPushConstantRanges = nullptr;
616
Jamie Madillc5143482017-10-15 20:20:06 -0400617 ANGLE_TRY(mPipelineLayout.init(device, createInfo));
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500618
Jamie Madillc5143482017-10-15 20:20:06 -0400619 return vk::NoError();
Jamie Madilldf68a6f2017-01-13 17:29:53 -0500620}
621
Jamie Madill76e471e2017-10-21 09:56:01 -0400622vk::Error ProgramVk::initDescriptorSets(ContextVk *contextVk)
623{
624 ASSERT(mDescriptorSet == VK_NULL_HANDLE);
625
626 VkDevice device = contextVk->getDevice();
627
628 // Write out to a new a descriptor set.
629 // TODO(jmadill): Handle descriptor set lifetime.
630 vk::DescriptorPool *descriptorPool = contextVk->getDescriptorPool();
631
632 VkDescriptorSetAllocateInfo allocInfo;
633 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
634 allocInfo.pNext = nullptr;
635 allocInfo.descriptorPool = descriptorPool->getHandle();
636
637 // TODO(jmadill): Handle descriptor set layouts for textures.
638 allocInfo.descriptorSetCount = 1;
639 allocInfo.pSetLayouts = mDescriptorSetLayouts[0].ptr();
640
641 ANGLE_TRY(descriptorPool->allocateDescriptorSets(device, allocInfo, &mDescriptorSet));
642 return vk::NoError();
643}
644
Jamie Madill54164b02017-08-28 15:17:37 -0400645void ProgramVk::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const
646{
647 UNIMPLEMENTED();
648}
649
650void ProgramVk::getUniformiv(const gl::Context *context, GLint location, GLint *params) const
651{
652 UNIMPLEMENTED();
653}
654
655void ProgramVk::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const
656{
657 UNIMPLEMENTED();
658}
659
Jamie Madill76e471e2017-10-21 09:56:01 -0400660vk::Error ProgramVk::updateUniforms(ContextVk *contextVk)
661{
662 if (!mDefaultUniformBlocks[VertexShader].uniformsDirty &&
663 !mDefaultUniformBlocks[FragmentShader].uniformsDirty)
664 {
665 return vk::NoError();
666 }
667
668 VkDevice device = contextVk->getDevice();
669
670 // Update buffer memory by immediate mapping. This immediate update only works once.
671 // TODO(jmadill): Handle inserting updates into the command stream, or use dynamic buffers.
672 for (auto &uniformBlock : mDefaultUniformBlocks)
673 {
674 if (uniformBlock.uniformsDirty)
675 {
676 ANGLE_TRY(SyncDefaultUniformBlock(device, &uniformBlock.storage.memory,
677 uniformBlock.uniformData));
678 uniformBlock.uniformsDirty = false;
679 }
680 }
681
682 return vk::NoError();
683}
684
685vk::Error ProgramVk::updateDefaultUniformsDescriptorSet(ContextVk *contextVk)
686{
687 std::array<VkDescriptorBufferInfo, 2> descriptorBufferInfo;
688 std::array<VkWriteDescriptorSet, 2> writeDescriptorInfo;
689 uint32_t bufferCount = 0;
690
691 for (auto &uniformBlock : mDefaultUniformBlocks)
692 {
693 auto &bufferInfo = descriptorBufferInfo[bufferCount];
694
695 if (!uniformBlock.uniformData.empty())
696 {
697 bufferInfo.buffer = uniformBlock.storage.buffer.getHandle();
698 }
699 else
700 {
701 bufferInfo.buffer = mEmptyUniformBlockStorage.buffer.getHandle();
702 }
703
704 bufferInfo.offset = 0;
705 bufferInfo.range = VK_WHOLE_SIZE;
706
707 auto &writeInfo = writeDescriptorInfo[bufferCount];
708
709 writeInfo.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
710 writeInfo.pNext = nullptr;
711 writeInfo.dstSet = mDescriptorSet;
712 writeInfo.dstBinding = bufferCount;
713 writeInfo.dstArrayElement = 0;
714 writeInfo.descriptorCount = 1;
715 writeInfo.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
716 writeInfo.pImageInfo = nullptr;
717 writeInfo.pBufferInfo = &bufferInfo;
718 writeInfo.pTexelBufferView = nullptr;
719
720 bufferCount++;
721 }
722
723 VkDevice device = contextVk->getDevice();
724
725 vkUpdateDescriptorSets(device, bufferCount, writeDescriptorInfo.data(), 0, nullptr);
726
727 return vk::NoError();
728}
729
730VkDescriptorSet ProgramVk::getDescriptorSet() const
731{
732 return mDescriptorSet;
733}
734
Jamie Madill9e54b5a2016-05-25 12:57:39 -0400735} // namespace rx