qt 3d node editor and shader generator - kdab · 2020-01-31 · graphics api in use ... (c++, glsl,...

23
Qt 3D Node Editor and Shader Generator Paul Lemire – [email protected]

Upload: others

Post on 21-Apr-2020

42 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Qt 3D Node Editor and Shader GeneratorPaul Lemire – [email protected]

Page 2: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

A quick recap about shaders

Page 3: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

What are shaders?• A program that runs on the GPU• Different types of shaders

– Vertex transforms points in space→ transforms points in space– Fragment compute pixels’ colors→ transforms points in space– Geometry, Compute, Tessellation …

• Written in different programming languages depending on the Graphics API in use

– OpenGL uses a language called GLSL– DirectX uses a language called HLSL

3

Page 4: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

What are shaders?

Page 5: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

What does a shader look like?

#version 150

in vec3 vertexPosition;

uniform mat4 mvp;

void main()

{

gl_Position = mvp * vec4(vertexPosition, 1.0);

}

#version 150 core

out vec4 fragColor;

void main()

{

fragColor = vec4(1.0, 0.0, 0.0, 1.0);

}

Vertex Shader Fragment Shader

Page 6: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

In Practice

Page 7: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Using shaders in Qt 3DMaterial { effect: Effect { techniques: [ Technique { // Specify the Graphics API and Version we target graphicsApiFilter { api: GraphicsApiFilter.OpenGL profile: GraphicsApiFilter.CoreProfile majorVersion: 4; minorVersion: 4 } renderPasses: RenderPass { shaderProgram: ShaderProgram { vertexShaderCode: loadSource("qrc:/shaders/phong.vert") fragmentShaderCode: loadSource("qrc:/shaders/phong.frag") } } } ] }} 7

Page 8: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Shaders with OpenGL● Multiple desktop versions (GL 2.*, GL 3.*, GL 4.*)● and embedded versions (ES 2, ES 3.*)

– Versions don’t all support the same features or use the same exact syntax– If you want to support multiple GL versions, you need to provide shader code

for each version● OpenGL expects shaders to be provided as GLSL code*● The OpenGL Driver takes care of compiling the GLSL code

to a program that can be executed by the GPU

8

Page 9: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Shaders with Vulkan● Vulkan expects SPIR-V shaders● SPIR-V is a bytecode● The glslang tool convert shaders written in various

languages (C++, GLSL, OpenCL) to SPIR-V– Shader compilation is expected to be a step that takes place at application

build time rather than runtime

9

Page 10: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Handling multiple APIs/versions● Two options:

– Provide a shader for each version we target● More assets to handle● Selection is made at runtime based on which rendering backend was selected● Makes it hard to test all possible versions

– Abstract the shader code into a set of inputs, outputs and operations● Provide translation rules for input, output, operations● Convert shader code description into actual shader code

10

Page 11: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Abstracting shader code with nodes

Page 12: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

The Node Editor● Builds a graph of nodes

– Nodes can either be● An input● An output● An operation/function

● Exports .graph files which contains the graph structure + node prototypes

● Part of Kuesa / available as QtCreator plugin

12

Page 13: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Prototypes and translations● The prototype is the definition of a specific node● Translations define how a node has to be converted● The prototype specifies:

– Whether the node is an input, output or operation– If node is an operation, the number of inputs/outputs– Translations for each Graphics API that needs to be supported– Header declaration (for uniforms, includes ...)

13

Page 14: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

14

Simple Prototypes

"add": { "inputs": ["first", "second"], "outputs": ["sum"], "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3"} }, "rules": [ { "format": { "api": "OpenGLES", "major": 2,"minor": 0}, "substitution": "highp $type $sum = $first + $second;" }, { "format": { "api": "OpenGLCoreProfile", "major": 3, "minor": 0}, "substitution": "$type $sum = $first + $second;" } ]}

Page 15: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

15

More complex Prototypes

"customFunction": { "inputs": ["first", "second"], "outputs": ["result"], "parameters": { "type": { "type": "QShaderLanguage::VariableType", "value": "QShaderLanguage::Vec3"} }, "rules": [ { "format": { "api": "OpenGLCoreProfile", "major": 3,"minor": 0 }, "headerSnippets": [ "#pragma include :shaders/es2/myCustomFunction.inc.frag" ], "substitution": "vec3 $result = myCustomFunction($first, $second);" } ]}

Page 16: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Layers

● Not to be confused with Qt 3D Layers● Allows to create different views of a given graph

● To handle different type of inputs

Page 17: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Loading graphs with Qt 3D

Page 18: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

QShaderProgramBuilder● Recreates shader code by traversing the graph● Selects translations that match the rendering backend● Relies on QShaderGenerator (private API of QtGui)● Does some optimizations:

– Cache results of operations which are referenced more than once– Inlines operations otherwise

18

Page 19: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Using shaders in Qt 3DMaterial { effect: Effect { techniques: [ Technique { GraphicsApiFilter { ... } renderPasses: RenderPass { shaderProgram: ShaderProgram { id: prog ShaderProgramBuilder { shaderProgram: prog fragmentShaderGraph:"qrc:/shaders/graphs/graph.frag.json" enabledLayers: [] } ShaderProgramBuilder { shaderProgram: prog fragmentShaderGraph:"qrc:/shaders/graphs/graph.vert.json" } } } } ] }} 19

Page 20: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

What’s next?

Page 21: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Extending the use of graphs to more than shaders

● FrameGraph● LogicalDevice● Particle Systems● ...

Page 22: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Generate Shader Bytecode

● Would allow to create SpirV byte code● Required for Vulkan / RHI

Page 23: Qt 3D Node Editor and Shader Generator - KDAB · 2020-01-31 · Graphics API in use ... (C++, GLSL, OpenCL) to SPIR-V – Shader compilation is expected to be a step that takes place

Questions?