Skip to content

Commit ca0b0b7

Browse files
authored
Merge pull request #2011 from overte-org/fix/vulkan_improvements_1
Vulkan improvements 1
2 parents b15ee3a + 06de05e commit ca0b0b7

File tree

19 files changed

+226
-57
lines changed

19 files changed

+226
-57
lines changed

libraries/display-plugins/src/display-plugins/VulkanDisplayPlugin.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ void VulkanDisplayPlugin::customizeContext() {
433433

434434
_SRGBToLinearPipeline = gpu::Pipeline::create(gpu::Shader::createProgram(DrawTextureSRGBToLinear), scissorState);
435435

436-
_hudPipeline = gpu::Pipeline::create(gpu::Shader::createProgram(DrawTextureSRGBToLinear), blendState);
436+
_hudPipeline = gpu::Pipeline::create(gpu::Shader::createProgram(DrawTexturePremultipliedSRGBToLinear), blendState);
437437

438438
_cursorPipeline = gpu::Pipeline::create(gpu::Shader::createProgram(DrawTransformedTexture), blendState);
439439
}
@@ -750,6 +750,11 @@ void VulkanDisplayPlugin::present(const std::shared_ptr<RefreshRateController>&
750750

751751
uint32_t currentImageIndex = UINT32_MAX;
752752
//VK_CHECK_RESULT(_vkWindow->_swapchain.acquireNextImage(_vkWindow->_acquireCompleteSemaphore, &currentImageIndex));
753+
if (!_vkWindow->_acquireCompleteSemaphore) {
754+
VkSemaphoreCreateInfo semaphoreCreateInfo = vks::initializers::semaphoreCreateInfo();
755+
VK_CHECK_RESULT(vkCreateSemaphore(_vkWindow->_context.device->logicalDevice, &semaphoreCreateInfo, nullptr, &_vkWindow->_acquireCompleteSemaphore));
756+
}
757+
753758
if(_vkWindow->_swapchain.acquireNextImage(_vkWindow->_acquireCompleteSemaphore, &currentImageIndex) != VK_SUCCESS) {
754759
qDebug() << "_vkWindow->_swapchain.acquireNextImage fail";
755760
_vkWindow->resizeFramebuffer(); //VKTODO: workaround
@@ -898,6 +903,11 @@ void VulkanDisplayPlugin::present(const std::shared_ptr<RefreshRateController>&
898903
cmdEndLabel(commandBuffer);
899904
VK_CHECK_RESULT(vkEndCommandBuffer(commandBuffer));
900905

906+
if (!_vkWindow->_renderCompleteSemaphore) {
907+
VkSemaphoreCreateInfo semaphoreCreateInfo = vks::initializers::semaphoreCreateInfo();
908+
VK_CHECK_RESULT(vkCreateSemaphore(_vkWindow->_context.device->logicalDevice, &semaphoreCreateInfo, nullptr, &_vkWindow->_renderCompleteSemaphore));
909+
}
910+
901911
static const VkPipelineStageFlags waitFlags{ VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT };
902912
VkSubmitInfo submitInfo = vks::initializers::submitInfo();
903913
submitInfo.waitSemaphoreCount = 1;
@@ -925,8 +935,18 @@ void VulkanDisplayPlugin::present(const std::shared_ptr<RefreshRateController>&
925935

926936
_vkWindow->_previousFrameFence = frameFence;
927937
_vkWindow->_previousCommandBuffer = commandBuffer;
938+
if (_vkWindow->_previousAcquireCompleteSemaphore) {
939+
_vkWindow->_context.recycler.trashVkSemaphore(_vkWindow->_previousAcquireCompleteSemaphore);
940+
}
941+
if (_vkWindow->_previousRenderCompleteSemaphore) {
942+
_vkWindow->_context.recycler.trashVkSemaphore(_vkWindow->_previousRenderCompleteSemaphore);
943+
}
928944
}
929945
_vkWindow->_swapchain.queuePresent(_vkWindow->_context.graphicsQueue, currentImageIndex, _vkWindow->_renderCompleteSemaphore);
946+
_vkWindow->_previousAcquireCompleteSemaphore = _vkWindow->_acquireCompleteSemaphore;
947+
_vkWindow->_previousRenderCompleteSemaphore = _vkWindow->_renderCompleteSemaphore;
948+
_vkWindow->_acquireCompleteSemaphore = VK_NULL_HANDLE;
949+
_vkWindow->_renderCompleteSemaphore = VK_NULL_HANDLE;
930950

931951
// VKTODO
932952
gpu::Backend::freeGPUMemSize.set(gpu::gl::getFreeDedicatedMemory());

libraries/gpu-vk/src/gpu/vk/VKBackend.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,8 +1486,8 @@ VKTexture* VKBackend::syncGPUObject(const Texture *texture) {
14861486
qDebug() << " mismatch, stored: " << storedFormat.getType() << " texel: " << texelFormat.getType();
14871487
return nullptr;
14881488
}
1489-
auto storedVkFormat = evalTexelFormatInternal(texture->getStoredMipFormat());
1490-
auto texelVkFormat = evalTexelFormatInternal(texture->getTexelFormat());
1489+
auto storedVkFormat = evalTexelFormatInternal(texture->getStoredMipFormat(), _context);
1490+
auto texelVkFormat = evalTexelFormatInternal(texture->getTexelFormat(), _context);
14911491
if (storedVkFormat != texelVkFormat) {
14921492
if (!(storedVkFormat == VK_FORMAT_R8G8B8_UNORM && texelVkFormat == VK_FORMAT_R8G8B8A8_UNORM) // Adding alpha channel needed
14931493
&& !(storedVkFormat == VK_FORMAT_R8G8B8_UNORM && texelVkFormat == VK_FORMAT_R8G8B8A8_SRGB) // Adding alpha channel needed and maybe SRGB conversion?
@@ -1795,9 +1795,16 @@ void VKBackend::FrameData::createDescriptorPool() {
17951795
}
17961796

17971797
void VKBackend::FrameData::addGlUniform(size_t size, const void* data, size_t commandIndex) {
1798-
_glUniformData.resize(_glUniformBufferPosition + size);
1798+
size_t alignment = _backend->_context.device->properties.limits.minUniformBufferOffsetAlignment;
1799+
size_t newSizeUnaligned = _glUniformBufferPosition + size;
1800+
size_t newSizeAligned = newSizeUnaligned - (newSizeUnaligned % alignment);
1801+
if (newSizeAligned < newSizeUnaligned) {
1802+
newSizeAligned += alignment;
1803+
}
1804+
_glUniformData.resize(newSizeAligned);
17991805
memcpy(_glUniformData.data()+_glUniformBufferPosition, data, size);
1800-
_glUniformBufferPosition += size;
1806+
_glUniformOffsetMap.insert({commandIndex, _glUniformBufferPosition});
1807+
_glUniformBufferPosition = newSizeAligned;
18011808
}
18021809

18031810
VKBackend::FrameData::FrameData(VKBackend *backend) : _backend(backend) {
@@ -2616,7 +2623,7 @@ void VKBackend::do_clearFramebuffer(const Batch& batch, size_t paramOffset) {
26162623
auto gpuFramebuffer = syncGPUObject(framebuffer);
26172624
auto &renderBuffers = framebuffer->getRenderBuffers();
26182625

2619-
Cache::Pipeline::RenderpassKey key = _cache.pipelineState.getRenderPassKey(framebuffer);
2626+
Cache::Pipeline::RenderpassKey key = _cache.pipelineState.getRenderPassKey(framebuffer, _context);
26202627
std::vector<VkAttachmentDescription> attachments;
26212628
std::vector<VkClearValue> clearValues;
26222629
attachments.reserve(key.size());
@@ -2796,7 +2803,7 @@ void VKBackend::do_clearFramebuffer(const Batch& batch, size_t paramOffset) {
27962803

27972804
VkImageSubresourceRange mipSubRange = {};
27982805
mipSubRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
2799-
if (formatHasStencil(evalTexelFormatInternal(texture->getTexelFormat()))) {
2806+
if (formatHasStencil(evalTexelFormatInternal(texture->getTexelFormat(), _context))) {
28002807
mipSubRange.aspectMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
28012808
}
28022809
mipSubRange.baseMipLevel = 0;

libraries/gpu-vk/src/gpu/vk/VKFramebuffer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void gpu::vk::VKFramebuffer::update() {
5555
attachmentCI.width = vkTexture->_gpuObject.getWidth();
5656
attachmentCI.height = vkTexture->_gpuObject.getHeight();
5757
attachmentCI.layerCount = 1;
58-
attachmentCI.format = gpu::vk::evalTexelFormatInternal(vkTexture->_gpuObject.getTexelFormat());
58+
attachmentCI.format = gpu::vk::evalTexelFormatInternal(vkTexture->_gpuObject.getTexelFormat(), backend->getContext());
5959
attachmentCI.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
6060
attachmentCI.imageSampleCount = VK_SAMPLE_COUNT_1_BIT;
6161
addAttachment(attachmentCI, vkTexture);
@@ -101,7 +101,7 @@ void gpu::vk::VKFramebuffer::update() {
101101
attachmentCI.width = vkTexture->_gpuObject.getWidth();
102102
attachmentCI.height = vkTexture->_gpuObject.getHeight();
103103
attachmentCI.layerCount = 1;
104-
attachmentCI.format = gpu::vk::evalTexelFormatInternal(vkTexture->_gpuObject.getTexelFormat());
104+
attachmentCI.format = gpu::vk::evalTexelFormatInternal(vkTexture->_gpuObject.getTexelFormat(), backend->getContext());
105105
attachmentCI.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
106106
attachmentCI.imageSampleCount = VK_SAMPLE_COUNT_1_BIT;
107107
addAttachment(attachmentCI, vkTexture);
@@ -112,7 +112,7 @@ void gpu::vk::VKFramebuffer::update() {
112112
attachmentCI.width = vkTexture->_gpuObject.getWidth();
113113
attachmentCI.height = vkTexture->_gpuObject.getHeight();
114114
attachmentCI.layerCount = vkTexture->_gpuObject.getNumSlices();
115-
attachmentCI.format = gpu::vk::evalTexelFormatInternal(vkTexture->_gpuObject.getTexelFormat());
115+
attachmentCI.format = gpu::vk::evalTexelFormatInternal(vkTexture->_gpuObject.getTexelFormat(), backend->getContext());
116116
attachmentCI.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
117117
attachmentCI.imageSampleCount = VK_SAMPLE_COUNT_1_BIT;
118118
addAttachment(attachmentCI, vkTexture);

libraries/gpu-vk/src/gpu/vk/VKPipelineCache.cpp

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Cache::PipelineLayout Cache::Pipeline::getPipelineAndDescriptorLayout(const vks:
158158
return layout;
159159
}
160160

161-
Cache::Pipeline::RenderpassKey Cache::Pipeline::getRenderPassKey(gpu::Framebuffer* framebuffer) const {
161+
Cache::Pipeline::RenderpassKey Cache::Pipeline::getRenderPassKey(gpu::Framebuffer* framebuffer, const vks::Context &context) const {
162162
RenderpassKey result;
163163
if (!framebuffer) {
164164
result.emplace_back(VK_FORMAT_R8G8B8A8_SRGB, VK_IMAGE_LAYOUT_UNDEFINED); // VKTODO: this is definitely wrong, why is it that way?
@@ -176,7 +176,7 @@ Cache::Pipeline::RenderpassKey Cache::Pipeline::getRenderPassKey(gpu::Framebuffe
176176
layout = attachmentTexture->getVkImageLayout();
177177
}
178178
}
179-
result.emplace_back(evalTexelFormatInternal(attachment._texture->getTexelFormat()), layout);
179+
result.emplace_back(evalTexelFormatInternal(attachment._texture->getTexelFormat(), context), layout);
180180
}
181181
}
182182
if (framebuffer->hasDepthStencil()) {
@@ -190,7 +190,7 @@ Cache::Pipeline::RenderpassKey Cache::Pipeline::getRenderPassKey(gpu::Framebuffe
190190
}
191191
}
192192
}
193-
result.emplace_back(evalTexelFormatInternal(framebuffer->getDepthStencilBufferFormat()), layout);
193+
result.emplace_back(evalTexelFormatInternal(framebuffer->getDepthStencilBufferFormat(), context), layout);
194194
}
195195
}
196196
return result;
@@ -199,7 +199,7 @@ Cache::Pipeline::RenderpassKey Cache::Pipeline::getRenderPassKey(gpu::Framebuffe
199199
VkRenderPass Cache::Pipeline::getRenderPass(const vks::Context& context) {
200200
const auto framebuffer = gpu::acquire(this->framebuffer);
201201

202-
RenderpassKey key = getRenderPassKey(framebuffer);
202+
RenderpassKey key = getRenderPassKey(framebuffer, context);
203203
auto itr = _renderPassMap.find(key);
204204
if (itr == _renderPassMap.end()) {
205205
auto &renderBuffers = framebuffer->getRenderBuffers();
@@ -357,9 +357,9 @@ std::string Cache::Pipeline::getStridesKey() const {
357357
}
358358

359359
// VKTODO: use binary key if performance with text key is not good enough
360-
std::string Cache::Pipeline::getKey() const {
360+
std::string Cache::Pipeline::getKey(const vks::Context& context) const {
361361
const auto framebuffer = gpu::acquire(this->framebuffer);
362-
RenderpassKey renderpassKey = getRenderPassKey(framebuffer);
362+
RenderpassKey renderpassKey = getRenderPassKey(framebuffer, context);
363363
const gpu::Pipeline& pipeline = *gpu::acquire(this->pipeline);
364364
const gpu::State& state = *pipeline.getState();
365365
const auto& vertexShader = pipeline.getProgram()->getShaders()[0]->getSource();
@@ -406,7 +406,7 @@ VkShaderModule Cache::getShaderModule(const vks::Context& context, const shader:
406406
}
407407

408408
Cache::PipelineLayout Cache::getPipeline(const vks::Context& context) {
409-
auto key = pipelineState.getKey();
409+
auto key = pipelineState.getKey(context);
410410
auto pipelineIterator = pipelineMap.find(key);
411411
if (pipelineIterator != pipelineMap.end()) {
412412
return pipelineIterator->second;
@@ -541,13 +541,31 @@ Cache::PipelineLayout Cache::getPipeline(const vks::Context& context) {
541541
isAttributeSlotOccupied[slot] = true;
542542

543543
attributeDescriptions.push_back(
544-
{ slot, attribute._channel, evalTexelFormatInternal(attribute._element), (uint32_t)attribute._offset });
544+
{ slot, attribute._channel, evalTexelFormatInternal(attribute._element, context), (uint32_t)attribute._offset });
545545
}
546546

547547
if (!colorFound && vertexReflection.validInput(Stream::COLOR)) {
548548
attributeDescriptions.push_back({ Stream::COLOR, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
549549
}
550550

551+
if (!isAttributeSlotOccupied[Stream::TANGENT] && vertexReflection.validInput(Stream::TANGENT)) {
552+
attributeDescriptions.push_back({ Stream::TANGENT, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
553+
}
554+
555+
// VKTODO: is it safe to provide empty skin cluster data?
556+
// It would be used for meshes with blendshapes but no skinning.
557+
// Since blendshapes and skinning use the same shader, currently I added
558+
// a workaround that disables blendshapes on meshes with blendshapes but no skinning.
559+
/*if (!isAttributeSlotOccupied[Stream::SKIN_CLUSTER_INDEX] && vertexReflection.validInput(Stream::SKIN_CLUSTER_INDEX)) {
560+
// VKTODO: provide valid format here.
561+
attributeDescriptions.push_back({ Stream::SKIN_CLUSTER_INDEX, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
562+
}
563+
564+
if (!isAttributeSlotOccupied[Stream::SKIN_CLUSTER_WEIGHT] && vertexReflection.validInput(Stream::SKIN_CLUSTER_WEIGHT)) {
565+
// VKTODO: provide valid format here.
566+
attributeDescriptions.push_back({ Stream::SKIN_CLUSTER_WEIGHT, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
567+
}*/
568+
551569
if (!isAttributeSlotOccupied[Stream::TEXCOORD0] && vertexReflection.validInput(Stream::TEXCOORD0)) {
552570
attributeDescriptions.push_back({ Stream::TEXCOORD0, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
553571
}
@@ -556,6 +574,35 @@ Cache::PipelineLayout Cache::getPipeline(const vks::Context& context) {
556574
attributeDescriptions.push_back({ Stream::TEXCOORD1, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
557575
}
558576

577+
if (!isAttributeSlotOccupied[Stream::TEXCOORD2] && vertexReflection.validInput(Stream::TEXCOORD2)) {
578+
attributeDescriptions.push_back({ Stream::TEXCOORD2, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
579+
}
580+
581+
if (!isAttributeSlotOccupied[Stream::TEXCOORD3] && vertexReflection.validInput(Stream::TEXCOORD3)) {
582+
attributeDescriptions.push_back({ Stream::TEXCOORD3, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
583+
}
584+
585+
if (!isAttributeSlotOccupied[Stream::TEXCOORD4] && vertexReflection.validInput(Stream::TEXCOORD4)) {
586+
attributeDescriptions.push_back({ Stream::TEXCOORD4, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
587+
}
588+
589+
// VKTODO: Fade inputs are not set for shapes drawn with drawIndexed.
590+
// I currently don't know how to fix it, so I added a workaround.
591+
if (!isAttributeSlotOccupied[GPU_ATTR_FADEDATA5] && vertexReflection.validInput(GPU_ATTR_FADEDATA5)) {
592+
// VKTODO: provide valid format here.
593+
attributeDescriptions.push_back({ GPU_ATTR_FADEDATA5, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
594+
}
595+
596+
if (!isAttributeSlotOccupied[GPU_ATTR_FADEDATA6] && vertexReflection.validInput(GPU_ATTR_FADEDATA6)) {
597+
// VKTODO: provide valid format here.
598+
attributeDescriptions.push_back({ GPU_ATTR_FADEDATA6, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
599+
}
600+
601+
if (!isAttributeSlotOccupied[GPU_ATTR_FADEDATA7] && vertexReflection.validInput(GPU_ATTR_FADEDATA7)) {
602+
// VKTODO: provide valid format here.
603+
attributeDescriptions.push_back({ GPU_ATTR_FADEDATA7, 0, VK_FORMAT_R8G8B8A8_UNORM, 0 });
604+
}
605+
559606
// Explicitly add the draw call info slot if required
560607
if (vertexReflection.validInput(gpu::slot::attr::DrawCallInfo)) {
561608
attributeDescriptions.push_back(

libraries/gpu-vk/src/gpu/vk/VKPipelineCache.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ struct Cache {
115115
// Returns structure containing pipeline layout and descriptor set layouts
116116
PipelineLayout getPipelineAndDescriptorLayout(const vks::Context& context);
117117

118-
RenderpassKey getRenderPassKey(gpu::Framebuffer* framebuffer) const;
118+
RenderpassKey getRenderPassKey(gpu::Framebuffer* framebuffer, const vks::Context &context) const;
119119

120120
VkRenderPass getRenderPass(const vks::Context& context);
121121
static std::string getRenderpassKeyString(const RenderpassKey& renderpassKey);
122122
std::string getStridesKey() const;
123-
std::string getKey() const;
123+
std::string getKey(const vks::Context& context) const;
124124
} pipelineState;
125125

126126
static VkStencilOpState getStencilOp(const gpu::State::StencilTest& stencil);

libraries/gpu-vk/src/gpu/vk/VKShared.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ Q_LOGGING_CATEGORY(gpu_vk_logging, "hifi.gpu.vk")
1414
Q_LOGGING_CATEGORY(trace_gpu_vk, "trace.gpu.vk")
1515
Q_LOGGING_CATEGORY(trace_gpu_vk_detail, "trace.gpu.vk.detail")
1616

17-
VkFormat gpu::vk::evalTexelFormatInternal(const gpu::Element& dstFormat) {
17+
VkFormat gpu::vk::evalTexelFormatInternal(const gpu::Element& dstFormat, const vks::Context &context) {
1818
// VKTODO: add BGRA and SBGRA
1919
VkFormat result = VK_FORMAT_R8G8B8_UNORM;
2020
switch (dstFormat.getDimension()) {
2121
case gpu::SCALAR:
2222
{
2323
switch (dstFormat.getSemantic()) {
24+
case gpu::RAW:
2425
case gpu::RED:
2526
case gpu::RGB:
2627
case gpu::RGBA:
@@ -123,7 +124,7 @@ VkFormat gpu::vk::evalTexelFormatInternal(const gpu::Element& dstFormat) {
123124

124125
case gpu::DEPTH_STENCIL:
125126
// The only possible depth stencil format
126-
result = VK_FORMAT_D24_UNORM_S8_UINT;
127+
result = context.getBestDepthStencilFormat();
127128
break;
128129

129130
default:

libraries/gpu-vk/src/gpu/vk/VKShared.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ State::StencilOp stencilOpFromGL(VkStencilOp stencilOp);
3030
State::BlendOp blendOpFromGL(VkBlendOp blendOp);
3131
State::BlendArg blendArgFromGL(VkBlendFactor blendArg);
3232

33-
VkFormat evalTexelFormatInternal(const Element& dstFormat);
33+
VkFormat evalTexelFormatInternal(const Element& dstFormat, const vks::Context &context);
3434

3535
//bool isDepthStencilFormat(VkFormat format);
3636
bool formatHasStencil(VkFormat format);

0 commit comments

Comments
 (0)