opengl es 3.0 & 3.1 shading language
DESCRIPTION
An Overview of the OpenGL Shading Language (GLSL). OpenGL ES 3.0 & 3.1 Shading Language. Presenters: Ron Fosner & Rudy Cazabón. Agenda. Difference between “classic” and “modern” OpenGL OpenGL Shading Language (GLSL) Review of syntax and semantics Views of the Processing Pipeline - PowerPoint PPT PresentationTRANSCRIPT
OpenGL ES 3.0 & 3.1 Shading Language
Presenters: Ron Fosner & Rudy Cazabón
An Overview of the OpenGL Shading Language (GLSL)
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201422
Agenda
Difference between “classic” and “modern” OpenGLOpenGL Shading Language (GLSL)• Review of syntax and semanticsViews of the Processing PipelineReview of Shader Types Within Processing PipelineVertex Shaders In 3.0 and 3.1Tessellation Shaders No – only in 3.1 via HW vendor EXTsGeometry Shaders No – only in 3.1 via HW vendor EXTsFragment/Pixel Shaders In 3.0 and 3.1Compute Shaders In 3.1
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201433
Difference between “classic” and “modern” OpenGL
“Classic” OpenGL defines fixed-function operations – e.g. transform and lighting –
“Modern” OpenGL supersedes ALL stages of the graphics pipeline with programmable shaders
From OGL ES 2.0 onwards ALL applications must use shaders for their graphics processing• At minimum a vertex shader for T&L• a fragment shader to paint to framebuffer
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201444
GLSL – Variable Types
Strongly typed language; strict rules on type conversions
Variable Class Types Description
Scalars float, int, uint, boolScalar-based data types for floating-point, integer, unsigned integer, and boolean values
Floating-point vectors float, vec2, vec3,vec4
Floating-point–based vector types of one, two, three, or four components
Integer vector int, ivec2, ivec3, ivec4 Integer-based vector types of one, two, three, or four components
Unsigned integer vector uint, uvec2, uvec3, uvec4
Unsigned integer-based vector types of one, two, three, or four components
Boolean vector bool, bvec2, bvec3, bvec4 Boolean-based vector types of one, two, three, or four components
Matricesmat2 (or mat2x2),mat2x3, mat2x4,mat3x2, mat3 (or mat3x3), mat3x4, mat4x2, mat4x3, mat4 (or mat4x4)
Floating-point based matrices of size 2 × 2, 2 × 3, 2 × 4, 3 × 2, 3 × 3, 3 × 4, 4 × 2, 4 × 3, or 4 × 4
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201455
mat2, mat3, mat4→ 2x2, 3x3, 4x4
mat
vec3 → 3-element struct
Example: Manipulation of Vector and Matrix Fields
vec4 → 4-element struct
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201466
#version 300 esin vec4 v_Position;in vec4 v_Color;out vec4 o_Color;uniform mat4 ModelViewProjectionMatrix;void main() {
o_Color = v_Color;gl_Position =
ModelViewProjectionMatrix * v_Position;}
Variables Declaration Syntax and KeywordsEvery shader has its execution entry-point in main()
Keywords in/out def’ns data that OpenGL passes into/out-of shaders from/to the pipelineKeyword uniform does not change within the pipeline – it is updated by application
Into shader
Out of shader
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201477
10,000ft Simplified Model
GPU Data FlowApplication
Framebuffer
VertexProcessing Rasterizer Fragment
Processing
VertexShader
FragmentShader
vertices
vertices
fragments
MinimumRequired
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201488
FragmentGeometry
Tess
ella
tion
Vert
ex
View of All Stages of the Processing Pipeline
DrawProcessing
Draw
VertexProcessing
Vertex
Tesselator
Primitive
PrimitiveProcessing
Stream
Xform FeedbackProcessing
FragmentProcessing
Fragment PixelProcessing
Pixel
Image
Rasterization
Skip Draw
No streamgenerated
Rasterizationdiscard
Fail testdiscard
NoWrite
Tessellation Control
Tessellation Evaluation
Compute (OGLES 3.1)
1
23 4 5
No primitivegenerated
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 201499
OpenGL ES 3.0 and 3.1 Shading Language “Cookbook Approach”
1. Create Shader Programs
2. Create buffer objects
3. Load data into the buffers
4. “Connect” data locations with shader variables
5. Make shader program current
6. Update shader uniforms (application/host-side data)
7. Set attribute inputs
8. Render
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141010
OpenGL ES 3.0 and 3.1 Shading Language Applying the Shaders
• Shaders need to be compiled
• Linked to form an executable shader program
• OpenGL provides the compiler and linker
Create Program
Create Shader
Load Shader Source
Compile Shader
Attach shader to program
Link shader
program
Use shader
program
glCreateProgram()
glCreateShader()
glShaderSource()
glCompileShader()
glAttachShader()
glUseProgram()
glLinkProgram()
These steps are repeated for each shader program type
Compile ALL shaders THEN link them into one program
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141111
Vertex Shader
Basic uses of vertex shaders • Transformations – e.g. translate, rotate, scale• Lighting – e.g. per-vertex lighting, cartoon shading• Moving vertex positions – e.g. skinning, height
fieldsIts output must be a position in clip coordinates to the rasterizer
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141212
Tessellation ShadersTwo (2) conjoined processing steps in a tessellation shader1. Tessellation control shader (TCS)• Controls how much tessellation a particular control shape surface
(patch) is to receive and defines a size
N.b. Only available in OpenGL ES 3.1 by way of hardware vendor EXTs
2. Tessellation evaluation shader (TES)• Consumes the results of the TCS and computes interpolated per-
vertex data from them, e.g. positions, texture coords, and normals
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141313
Geometry Shaders
Geometry Shaders precede the primitive assembly and fragment/pixel shadersGeometry shader invocations take a single Primitive as input and may output zero or more primitives.Usages for geometry shaders• Layered rendering: taking one primitive and rendering it to multiple images
without having to change bound rendertargets and so forth• Transform feedback: employed for doing computational tasks on the GPUInstancing - Major win of geometry shaders
• allows multiple invocations to operate over the same input primitive• Enables layered rendering easier to implement and possibly faster performing,
as each layer's primitive(s) can be computed by a separate GS instanceN.b. Only available in OpenGL ES 3.1 by way of hardware vendor EXTs
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141414
Fragment Shaders
Fragment shader is the pipeline stage after the primitive rasterizer
A fragment shader is executed for each “potential” pixel • fragments still need to pass several tests before making it to the
framebuffer Common outputs of fragment shaders• RGB colors, depth values, stencil values TESTsEffects that can be done with fragment shaders • Per-fragment lighting• Bump Mapping• Use a “sampler” inside to enable environment (reflection) maps
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141616
Compute ShadersCommon Usages
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141717
Use-Case – Image Processing
Gaussian blurs with large kernels• Common in photo processing apps such as Photoshop• Expensive, particular for large neighborhoodsCompute Shaders ideal for such image processing operations• Fast shared memory and hundreds executing threadsNo way to perform these in OpenGL ES prior at these processing rates• Results immediately available in OpenGL for display
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141818
Compute ShadersWorkgroups – local, globalWork item fundamental unit of work inside a work-group; executes once per invocation
Workgroup collection of work-items launched simultaneouslyLocal workgroup – collection of workgroups that have topological computational neighbors Global workgroup – full aggregate of (local) workgroups
5 Work Groups
4 work-items
20 total items to compute
Computationalneighbors
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20141919
Compute Topology
The invocation space for compute shaders can be 1-D (previous slide), 2-D, or 3-DThe size and dimensionality of a workgroup is defined in compute shader source-code via input layout qualifierWhenever an input layout qualifier is omitted it automatically defaults to one (1)
#version 300 es// local input layout qualifierlayout (local_size_x = 4) in;
#version 300 es// local input layout qualifierLayout (local_size_x = 4, local_size_y = 4) in;
#version 300 es// local input layout qualifierLayout (local_size_x = 4, local_size_y = 4,Local_size_z = 4) in;
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20142020
Shared State Variables and Parallel Compute Model
Communication Achieved via GPU-Internal Shared Memory
Use of the “shared” keyword signals the driver to place the annotated data in special storage that is visible to all compute shader invocations within the same local workgroup
Parallelism Achieved via GPU-Internal HW Threading
• Each thread can share data with other members of the workgroup via shared memory (variables)
• Each thread can issue memory and control barriers to synchronise with other members of the workgroup
• Data cannot be shared between workgroups, unless via images, buffer objects or atomic counters
• Each thread can uniquely identify itself within a workgroup and globally with builtin variables. This is the only method for a thread to determine where to get its input and where to write its output
ComputeThread N
...?...
ComputeThread 1
Shared Variables
Workgroup1
ComputeThread N
...?...
ComputeThread 1
Shared Variables
Workgroup M
AnDevCon Boston 201421 AnDevCon Boston 2014
Back up slides
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20142222
OpenGL ES 3.0 and 3.1 Shading10K-Foot View of the Processing Pipeline
Primitive Assembly?
OpenGL-ES 3.0 And Beyond Boston 2014OpenGL-ES 3.0 And Beyond Boston 20142323
Vert
ex S
tage