High Fidelity Games: Real Examples, Best Practices ... | Oleksii Vasylenko

Post on 21-Jan-2018

116 views 3 download

Transcript of High Fidelity Games: Real Examples, Best Practices ... | Oleksii Vasylenko

High Fidelity GamesReal Examples, Best Practices, Vulkan Expertize & Excellences

Oleksii VasylenkoGraphics EngineerSamsung Electronics

GameDev Team

UK Ukraine

USA

Korea

Eternal battle

Quality Performance

Push the limits!

More visual effects More impressive graphics Higher frame rate Longer battery life

Push the limits!

More visual effects More impressive graphics Higher frame rate Longer battery life

What is Vulkan?

o An open-standard, cross-platform graphics+compute API

• Compatibility break with OpenGL

• Start from first principles

o Goals

• Clean, modern architecture

• Multithread / multicore-friendly

• Greatly reduced CPU overhead

• Full support for both desktop and mobile GPU architectures

• More predictable performance – no driver magic

• Improved reliability and consistency between implementations

What is Vulkan?

o An open-standard, cross-platform graphics+compute API

• Compatibility break with OpenGL

• Start from first principles

o Goals

• Clean, modern architecture

• Multithread / multicore-friendly

• Greatly reduced CPU overhead

• Full support for both desktop and mobile GPU architectures

• More predictable performance – no driver magic

• Improved reliability and consistency between implementations

1st demos

S7 Unpack: Protostar

2016: next step

First real Vulkan games

2016: first real games

• Vulkan gives consistent 60 FPS even in thermal throttling situation

• Console quality graphics with cinematic post-processing in Mobile

2017: more and more

Best practices

Best practices: command buffers

Regular logic:

0 1 2 0

CB 0 CB 1 CB 2 CB 00

Fence 0WAIT FOR RENDERING COMPLETION OF CB0 (FENCE 0)

CB 1

1

1

SUBMIT SUBMIT SUBMIT SUBMIT SUBMIT

ACQUIRE / PRESENT SWAPCHAIN IMAGES

WAIT FOR FENCE 0

Best practices: command buffers

Better solution:

0 1 2 0

CB 0 CB 1 CB 2 CB 00

Get Fence 0 signal

next CB

1

1

SUBMIT SUBMIT SUBMIT SUBMIT SUBMIT

ACQUIRE / PRESENT SWAPCHAIN IMAGES

READY

new CB 4

All CBs are not READY

Best practices: command buffers

• Should use "FIFO" instead of "MAILBOX" for SwapChain creation on Android to get stable Queue/Dequeue based on vsync

• Recommend to use 3 back buffers on Android

• Avoid vkWaitForFences usage on acquire image

• Recommend to minimize command buffers count per frame

ExpectedError

Best practices: renderpasses

• Use begin/end renderpass efficiently

• Renderpass must match framebuffer

• Reduce renderpass count where it’s possible

• Recognize where function should be called: inside or outside renderpass

Pipeline - abstract scheme of how a unit of work is processed

Best practices: pipeline barriers

void vkCmdPipelineBarrier(VkCommandBuffer commandBuffer,VkPipelineStageFlags srcStageMask,VkPipelineStageFlags dstStageMask,VkDependencyFlags dependencyFlags,uint32 memoryBarrierCount,constVkMemoryBarrier* pMemoryBarrieruint32 bufferMemoryBarrierCount,constVkMemoryBarrier* pBufferMemoryBarrieruint32 imageMemoryBarrierCount,constVkMemoryBarrier* pImageMemoryBarrier);

VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT

VK_PIPELINE_STAGE_DRAW_INDIRECT_BIT

VK_PIPELINE_STAGE_VERTEX_INPUT_BIT

VK_PIPELINE_STAGE_VERTEX_SHADER_BIT

VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT

VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT

VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT

VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT

VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT

VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT

VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT

VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT

Best practices: pipeline barriers

Vertex Shader

...

Fragment Shader

Vertex Shader

...

Fragment Shader

Vertex Shader

...

Fragment Shader

- Change image layout to readable

- Wrong stage mask: bubble!

SRC: VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT

DST: VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_VERTEX_INPUT_BIT

Best practices: pipeline barriers

Vertex Shader

...

Fragment Shader

Vertex Shader

...

Fragment Shader

Vertex Shader

...

Fragment Shader

- Change image layout to readable

- Wrong stage mask: flickering!

SRC: VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT

DST: VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT

... ...

Best practices: pipeline barriers

Vertex Shader

...

Fragment Shader

Vertex Shader

...

Fragment Shader

Vertex Shader

...

Fragment Shader

- Change image layout to readable

- Correct stage mask

SRC: VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT

DST: VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT

Best practices: pipeline barriers

Best practices: uniform buffers

• Minimize vkUpdateDescriptorSets calls by using fixed uniform buffer for static objects

• Use UniformBufferPool for dynamic objects

• Minimize vkMapMemory, vkUnmapMemory calls for dynamic objects

Vertex / Index Raw Data

Staging VkBufferVK_MEMORY_PROPERTY_HOST_VISIBLE_BIT

VK_MEMORY_PROPERTY_HOST_COHERENT_BIT

VkBuffer / VkDeviceMemoryVK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT

vkCmdCopyBuffer

Vertex / Index Raw Data

VkBuffer / VkDeviceMemoryVK_MEMORY_PROPERTY_HOST_VISIBLE_BIT

VK_MEMORY_PROPERTY_HOST_COHERENT_BIT

memcpy

command

process

memcpy

static dynamic

Best practices: other recommendations

• Cache everything as much as you can

• ‘std140’ layout for uniform buffers should be used at shader side

• Avoid vkCmdSetXXX, vkCmdBindXXX duplications

• Use deferred deletion for Vulkan related resources & objects

• Use proper image layout for images (especially for presentation and copy!)

• Consider VkPhysicalDeviceLimits before doing something!

Best practices: validation layers

Log example:• Code 52 : At Draw time the active render pass (0xffffffff997f2024) is incompatible w/ gfx pipeline (0xffffffffc3b1f808) that was created w/ render pass (0xffffffffc43fac8c) due to: color a

ttachments at index 0 of subpass index 0 are not compatible.

• Code 56 : You cannot start a render pass using attachment 0 where the render pass initial layout is VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL and the previous known layout of the attachment is VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL. The layouts must match, or the render pass initial layout for the attachment must be VK_IMAGE_LAYOUT_UNDEFINED

• Code 6 : vkCmdDrawIndexed(): Cannot use image 0xffffffff97108908 with specific layout VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL that doesn't match the actual current layout VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL.

• Code 61 : Descriptor set 0xffffffffc321ee78 encountered the following validation error at vkCmdDrawIndexed() time: Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at time descriptor is used. See previous error callback for specific details.

CreateInstance(){

…VkInstanceCreateInfo InstInfo;InstInfo.enabledLayerCount = enabledLayerCount;InstInfo.ppEnabledLayerNames = enabledLayers;…

}

SetupDebugLayerCallback(){

VkDebugReportCallbackCreateInfoEXT CreateInfo;CreateInfo.pfnCallback = DebugReportFunction;vkCreateDebugReportCallbackEXT (Instance, &CreateInfo, nullptr, &MsgCallback);

}

Complicated API?

X

• Vulkan – solution for high fidelity games creation on mobile

• The most popular game engines support Vulkan

Samsung will keep go on supporting game developers and players!

If you have any questions, offers or suggestions, please contact

gamedev@samsung.com

Thank you!