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

30
High Fidelity Games Real Examples, Best Practices, Vulkan Expertize & Excellences Oleksii Vasylenko Graphics Engineer Samsung Electronics

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

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

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

Oleksii VasylenkoGraphics EngineerSamsung Electronics

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

GameDev Team

UK Ukraine

USA

Korea

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

Eternal battle

Quality Performance

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

Push the limits!

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

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

Push the limits!

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

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

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

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

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

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

1st demos

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

S7 Unpack: Protostar

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

2016: next step

First real Vulkan games

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

2016: first real games

• Vulkan gives consistent 60 FPS even in thermal throttling situation

• Console quality graphics with cinematic post-processing in Mobile

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

2017: more and more

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

Best practices

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

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

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

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

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

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

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

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

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

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

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

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

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

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

... ...

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

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

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

Best practices: pipeline barriers

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

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

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

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!

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

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);

}

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

Complicated API?

X

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

• 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

[email protected]

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

Thank you!