Using SPIR-V in practice with SPIRV-Cross

Similar documents
Introduction to SPIR-V Shaders

Achieving High-performance Graphics on Mobile With the Vulkan API

Vulkan Multipass mobile deferred done right

Vulkan on Mobile. Daniele Di Donato, ARM GDC 2016

Moving to Vulkan: How to make your 3D graphics more explicit

Porting Roblox to Vulkan. Arseny

Going to cover; - Why we have SPIR-V - Brief history of SPIR-V - Some of the core required features we wanted - How OpenCL will use SPIR-V - How

Programming shaders & GPUs Christian Miller CS Fall 2011

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Vulkan 1.1 March Copyright Khronos Group Page 1

Profiling and Debugging Games on Mobile Platforms

Working with Metal Overview

Shading Language Update

PowerVR Framework. October 2015

Vulkan (including Vulkan Fast Paths)

Mobile Application Programing: Android. OpenGL Operation

GDC 2014 Barthold Lichtenbelt OpenGL ARB chair

Understanding M3G 2.0 and its Effect on Producing Exceptional 3D Java-Based Graphics. Sean Ellis Consultant Graphics Engineer ARM, Maidenhead

Copyright Khronos Group Page 1. Vulkan Overview. June 2015

Mali Offline Compiler User Guide

Vulkan API 杨瑜, 资深工程师

Mali Developer Resources. Kevin Ho ARM Taiwan FAE

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Shaders. Slide credit to Prof. Zwicker

Khronos Connects Software to Silicon

EECS 487: Interactive Computer Graphics

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

DEVELOPER DAY. Shader Toolchain: HLSL in Vulkan Lei Zhang, Google MONTRÉAL APRIL Copyright Khronos Group Page 1

Dave Shreiner, ARM March 2009

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Press Briefing SIGGRAPH 2015 Neil Trevett Khronos President NVIDIA Vice President Mobile Ecosystem. Copyright Khronos Group Page 1

Copyright Khronos Group Page 1

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Shaders CSCI 4229/5229 Computer Graphics Fall 2017

Vulkan: Architecture positive How Vulkan maps to PowerVR GPUs Kevin sun Lead Developer Support Engineer, APAC PowerVR Graphics.

Press Briefing SIGGRAPH 2015 Neil Trevett Khronos President NVIDIA Vice President Mobile Ecosystem. Copyright Khronos Group Page 1

Mobile Graphics Ecosystem. Tom Olson OpenGL ES working group chair

SIGGRAPH Briefing August 2014

Copyright Khronos Group, Page Graphic Remedy. All Rights Reserved

Mobile Application Programing: Android. OpenGL Operation

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

More frames per second. Alex Kan and Jean-François Roy GPU Software

Lecture 13: OpenGL Shading Language (GLSL)

CPSC 436D Video Game Programming

SPIRV-Cross. The war stories. Hans-Kristian Arntzen Vulkanise Arm Limited

Shaders CSCI 4239/5239 Advanced Computer Graphics Spring 2014

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

Prospects for a more robust, simpler and more efficient shader cross-compilation pipeline in Unity with SPIR-V

Get the most out of the new OpenGL ES 3.1 API. Hans-Kristian Arntzen Software Engineer

12.2 Programmable Graphics Hardware

EDAF80 Introduction to Computer Graphics. Seminar 3. Shaders. Michael Doggett. Slides by Carl Johan Gribel,

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Sign up for crits! Announcments

Bifrost - The GPU architecture for next five billion

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

LPGPU Workshop on Power-Efficient GPU and Many-core Computing (PEGPUM 2014)

OPENGL RENDERING PIPELINE

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

last time put back pipeline figure today will be very codey OpenGL API library of routines to control graphics calls to compile and load shaders

Compute Shaders. Christian Hafner. Institute of Computer Graphics and Algorithms Vienna University of Technology

Overview. Technology Details. D/AVE NX Preliminary Product Brief

GPU Offline Shader Compiler. Mali. User Guide. Version: 2.2. Copyright 2009 ARM. All rights reserved. ARM DUI 0513A (ID101409)

Vulkan Subpasses. or The Frame Buffer is Lava. Andrew Garrard Samsung R&D Institute UK. UK Khronos Chapter meet, May 2016

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Open API Standards for Mobile Graphics, Compute and Vision Processing GTC, March 2014

Next Generation OpenGL Neil Trevett Khronos President NVIDIA VP Mobile Copyright Khronos Group Page 1

OpenGL BOF Siggraph 2011

VULKAN AND NVIDIA: THE ESSENTIALS

Vulkan: Mark My DWORDS

GLSL Programming. Nicolas Holzschuch

OPENGL AND GLSL. Computer Graphics

Mention driver developers in the room. Because of time this will be fairly high level, feel free to come talk to us afterwards

DEVELOPER DAY. Vulkan Subgroup Explained Daniel Koch NVIDIA MONTRÉAL APRIL Copyright Khronos Group Page 1

Optimizing and Profiling Unity Games for Mobile Platforms. Angelo Theodorou Senior Software Engineer, MPG Gamelab 2014, 25 th -27 th June

More performance options

Engine Development & Support Team Lead for Korea UE4 Mobile Team Lead

D3D12 & Vulkan: Lessons learned. Dr. Matthäus G. Chajdas Developer Technology Engineer, AMD

GPU Quality and Application Portability

Vulkan Launch Webinar 18 th February Copyright Khronos Group Page 1

Investigating real-time rendering techniques approaching realism using the Vulkan API

OpenGL on Android. Lecture 7. Android and Low-level Optimizations Summer School. 27 July 2015

NVIDIA RTX: Enabling Ray Tracing in Vulkan. Nuno Subtil, Sr. Devtech Engineer / Eric Werness, Sr. System Software Engineer March 27, 2018

Programmable Graphics Hardware

Computer Graphics with OpenGL ES (J. Han) Chapter 6 Fragment shader

Programming Graphics Hardware

GLSL Introduction. Fu-Chung Huang. Thanks for materials from many other people

X. GPU Programming. Jacobs University Visualization and Computer Graphics Lab : Advanced Graphics - Chapter X 1

Bringing AAA graphics to mobile platforms. Niklas Smedberg Senior Engine Programmer, Epic Games

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

Today. Rendering - III. Outline. Texturing: The 10,000m View. Texture Coordinates. Specifying Texture Coordinates in GL

Free Downloads OpenGL ES 3.0 Programming Guide

CS475/CS675 - Computer Graphics. OpenGL Drawing

Introduction to Shaders.

Computergraphics Exercise 15/ Shading & Texturing

Information Coding / Computer Graphics, ISY, LiTH GLSL. OpenGL Shading Language. Language with syntax similar to C

Introduction to Computer Graphics. Hardware Acceleration Review

OpenGL Status - November 2013 G-Truc Creation

Mali-400 MP: A Scalable GPU for Mobile Devices Tom Olson

Transcription:

Copyright Khronos Group 2016 - Page 60 Using SPIR-V in practice with SPIRV-Cross Hans-Kristian Arntzen Engineer, ARM

Copyright Khronos Group 2016 - Page 61 Contents Moving to offline compilation of SPIR-V Creating pipeline layouts with SPIRV-Cross - Descriptor sets - Push constants - Multipass input attachments Making SPIR-V portable to other graphics APIs Debugging complex shaders with your C++ debugger of choice

Copyright Khronos Group 2016 - Page 62 Offline Compilation to SPIR-V Shader compilation can be part of your build system Catching compilation bugs in build time is always a plus Strict, mature GLSL frontends available - glslang: https://github.com/khronosgroup/glslang - shaderc: https://github.com/google/shaderc Full freedom for other languages in the future # Makefile rules FRAG_SHADERS := $(wildcard *.frag) SPIRV_FILES := $(FRAG_SHADERS:.frag=.frag.spv) shaders: $(SPIRV_FILES) %.frag.spv: %.frag glslc o $@ $< $(GLSL_FLAGS) std=310es

Copyright Khronos Group 2016 - Page 63 Vulkan Pipeline Layouts Need to know the function signature of our shaders pipelineinfo.layout = <layout goes here>; vkcreategraphicspipelines(..., &pipelineinfo,..., &pipeline);

Copyright Khronos Group 2016 - Page 64 The Contents of a Pipeline Layout layout(set = 0, binding = 1) uniform UBO { mat4 MVP; }; layout(set = 1, binding = 2) uniform sampler2d utexture; layout(push_constant) uniform PushConstants { vec4 FastConstant; } Signature constants; - 16 bytes of push constant space - Two descriptor sets - Set #0 has one UBO at binding #1 - Set #1 has one combined image sampler at binding #2 Need to figure this out automatically, or write every layout by hand - Latter is fine for tiny applications - Vulkan does not provide reflection here, after all, this is vendor neutral information

Copyright Khronos Group 2016 - Page 65 Introducing SPIRV-Cross SPIRV-Cross is a new tool hosted by Khronos - https://github.com/khronosgroup/spirv-cross Extensive reflection Decompilation to high level languages Khronos SPIR-V Toolbox glslang SPIRV- Tools SPIRV- LLVM SPIRV- Cross

Copyright Khronos Group 2016 - Page 66 Reflecting Uniforms and Samplers SPIRV-Cross has a simple API to retrieve resources using namespace spirv_cross; vector<uint32_t> spirv_binary = load_spirv_file(); Compiler comp(move(spirv_binary)); // The SPIR-V is now parsed, and we can perform reflection on it. ShaderResources resources = comp.get_shader_resources(); for (auto &u : resources.uniform_buffers) { uint32_t set = comp.get_decoration(u.id, spv::decorationdescriptorset); uint32_t binding = comp.get_decoration(u.id, spv::decorationbinding); printf( Found UBO %s at set = %u, binding = %u!\n, u.name.c_str(), set, binding); }

Copyright Khronos Group 2016 - Page 67 Stepping it up with Push Constants SPIRV-Cross can figure out which push constant elements are in use - Push constant blocks are typically shared across the various stages - Only parts of the push constant block are referenced in a single stage layout(push_constant) uniform PushConstants { mat4 MVPInVertex; vec4 ColorInFragment; } constants; FragColor = constants.colorinfragment; // Fragment only uses element #1. uint32_t id = resources.push_constant_buffers[0].id; vector<bufferrange> ranges = comp.get_active_buffer_ranges(id); for (auto &range : ranges) { printf( Accessing member #%u, offset %u, size %u\n, range.index, range.offset, range.range); } // Possible to get names for struct members as well

Copyright Khronos Group 2016 - Page 68 Subpass Input Attachments Subpass attachments are similar to regular images - Set - Binding - Input attachment index layout(set = 0, binding = 0, input_attachment_index = 0) uniform subpassinput ualbedo; layout(set = 0, binding = 1, input_attachment_index = 1) uniform subpassinput unormal; vec4 lastcolor = subpassload(ulastpass); for (auto &attachment : resources.subpass_inputs) { //... }

Copyright Khronos Group 2016 - Page 69 Taking SPIR-V Beyond Vulkan SPIR-V is a great format to rally around - Makes sense to be able to use it in older graphics APIs as well Will take some time before exclusive Vulkan support is mainstream How to make use of Vulkan features while being compatible? - Push constants - Subpass - Descriptor sets Without tools, Vulkan features will be harder to take advantage of

Copyright Khronos Group 2016 - Page 70 GL + GLES + Vulkan Pipeline Implemented in our internal demo engine Write shaders in Vulkan GLSL Use Vulkan features directly No need for platform #ifdefs Can target mobile and desktop GL from same SPIR-V binary

Copyright Khronos Group 2016 - Page 71 Subpasses in OpenGL The subpass attachment is really just a texture read from gl_fragcoord - Enables reading directly from tile memory on tiled architectures - Great for deferred rendering and programmable blending // Vulkan GLSL uniform subpassinput ualbedo;... FragColor = accumulatelight( subpassload(ualbedo), subpassload(unormal).xyz, subpassload(udepth).x); // Translated to GLSL in SPIRV-Cross uniform sampler2d ualbedo;... FragColor = accumulatelight( texelfetch(ualbedo, ivec2(gl_fragcoord.xy), 0), texelfetch(unormal, ivec2(gl_fragcoord.xy), 0).xyz, texelfetch(udepth, ivec2(gl_fragcoord.xy), 0).x);

Copyright Khronos Group 2016 - Page 72 Push Constants in OpenGL Push constants bundle up old-style uniforms into buffer blocks - Translates directly to uniform structs - Use reflection to stamp out a list of gluniform() calls // Vulkan GLSL layout(push_constant) uniform PushConstants { vec4 Material; } constants; FragColor = constants.material; // Translated to GLSL in SPIRV-Cross struct PushConstants { vec4 Material; }; uniform PushConstants constants; FragColor = constants.material;

Copyright Khronos Group 2016 - Page 73 Descriptor Sets in OpenGL OpenGL has a binding space per type Find some remapping scheme that fits your application SPIRV-Cross can tweak bindings before decompiling to GLSL // Vulkan GLSL layout(set = 1, binding = 1) uniform sampler2d utexture; // SPIRV-Cross uint32_t newbinding = 4; glsl.set_decoration(texture.id, spv::decorationbinding, newbinding); glsl.unset_decoration(texture.id, spv::decorationdescriptorset); string glslsource = glsl.compile(); // GLSL layout(binding = 4) uniform sampler2d utexture;

Copyright Khronos Group 2016 - Page 74 gl_instanceindex in OpenGL Vulkan adds the base instance to the instance ID - GL does not - Workaround is to have GL backend pass in the base index as a uniform // Vulkan GLSL layout(set = 0, binding = 0) uniform UBO { mat4 MVPs[MAX_INSTANCES]; }; gl_position = MVPs[gl_InstanceIndex] * Position; // GLSL through SPIRV-Cross layout(binding = 0) uniform UBO { mat4 MVPs[MAX_INSTANCES]; }; uniform int SPIRV_Cross_BaseInstance; // Supplied by application gl_position = MVPs[(gl_InstanceID + SPIRV_Cross_BaseInstance)] * Position;

Copyright Khronos Group 2016 - Page 75 Debugging Shaders in C++ If you have thought - I wish I could assert() in a compute shader - I wish I could instrument a shader with logging - I wish I could use clang address sanitizer to debug out-of-bounds access - I want to reproduce a shader bug outside the driver - I want to run regression tests when optimizing a shader - I want to step through a compute thread in <insert C++ debugger here> the C++ backend in SPIRV-Cross could be interesting Still a very experimental feature Hope to expand this further in the future

Copyright Khronos Group 2016 - Page 76 Basic Idea With GLM, C++ can be near GLSL compatible Reuse the GLSL backend to emit code which also works in C++ - Minor differences like references vs. in/out, etc Add some scaffolding to redirect shader resources - Easily done with macros, the actual C++ output is kept clean The C++ output implements a simple C-compatible interface Add instrumentation to the C++ file as desired Compile C++ file to a dynamic library with debug symbols Instantiate from test program, bind buffers and invoke - And have fun running shadertoy raymarchers at seconds per frame

Copyright Khronos Group 2016 - Page 77 On the Command Line # Compile to SPIR-V glslc o test.spv test.comp # Create C++ interface spirv-cross --output test.cpp test.spv --cpp # Add some instrumentation to the shader if you want $EDITOR test.cpp # Build library g++ -o test.so shared test.cpp O0 g Iinclude/spirv_cross # Run your test app./<my app> --shader test.so

Copyright Khronos Group 2016 - Page 78 Another tool supporting Vulkan: Mali Graphics Debugger is an advanced API tracer tool for Vulkan, OpenGL ES, EGL and OpenCL. It allows developers to trace their graphics and compute applications to debug issues and analyze the performance. Vulkan Support - Trace all the function calls in the SPEC. - Allows you to see exactly what calls compose your application. - Contact the Mali forums and we would love to get you setup. https://community.arm.com/groups/ arm-mali-graphics

Copyright Khronos Group 2016 - Page 79 Investigation with the Mali Graphics Debugger Assets View Frame Statistics Frame Outline API Trace Frame Capture: Framebuffers States Uniforms Vertex Attributes Buffers Dynamic Help Textures Shaders

Copyright Khronos Group 2016 - Page 80 References SPIRV-Cross - https://github.com/khronosgroup/spirv-cross Glslang - https://github.com/khronosgroup/glslang Shaderc - https://github.com/google/shaderc SPIRV-Tools - https://github.com/khronosgroup/spirv-tools Mali Graphics Debugger - http://malideveloper.arm.com/resources/tools/mali-graphics-debugger/