Modern OpenGL with Perl

Max Maischein

Frankfurt.pm

Overview

  • Motivation

  • What is OpenGL

  • Using OpenGL with Perl

  • Future improvements

DISCLAIMER

There are lots of pretty pictures and animations in this talk

They have all been programmatically generated

But not by me

Who am I?

  • Max Maischein

  • Frankfurt.pm

  • Perl since 2000

  • HTC Vive + NVidia 1080 since 2016

Motivation

  • CGI

  • Computer Generated Images

  • BBS / Demoscene

  • New hardware must be mastered

  • Video postprocessing (App::VideoPlayer)

Definition of OpenGL

OpenGL is an abstraction layer and language for access to (accellerated) (graphics) (hardware)

  • Just because it's hardware, it's not necessarily accellerated

  • The hardware is not only for graphics

  • MESA is a software OpenGL implementation

Understanding OpenGL

OpenGL 1.0 - Language - released in 1992

  • OpenGL is a C API

  • glBegin(), glColor(), glDraw...(), glEnd()

  • Completely procedural, not descriptive

  • Linear functions for data/texture lookup

  • Limited by how fast the CPU can issue commands

Understanding OpenGL

OpenGL 2.0 - 4.5 - shader programs (2004-2015)

  • Let the hardware do more work

  • Allow user-supplied programs ("shaders") for data lookup

  • Shader programs combine scene setup and colouring

Understanding OpenGL

  • GLSL is very restricted C

  • Runs on the GPU

  • No recursion

  • No variable length loops

  • Compiler included in OpenGL

Understanding OpenGL for Perl

OpenGL 4.0 - "modern"

Let the hardware do even more work

  • GPUs get memory management

  • Shaders can write to GPU buffers never intended for display

  • Can do computations

  • CUDA / GPGPU

Accessing OpenGL from Perl

Accessing OpenGL from Perl

 1:  use OpenGL ':all';
  • only OpenGL 1.x and 2.0 functions

  • Perl is not good at number pushing

  • PDL, SDL help

Accessing OpenGL from Perl

 1:  # go to Github
 2:  use OpenGL::Glew;
 3:  # use OpenGL::Modern; # soon
  • All functions up to OpenGL 4.5

  • Ideally API compatible with OpenGL.pm

  • Perl as glue language

  • let GLSL do the heavy lifting

Learning OpenGL

OpenGL is like Perl

  • Battle tested

  • Available even on Linux, HTML5, ...

  • Much bad/outdated documentation

  • Read the OpenGL FAQ

Learning GLSL

  • Do calculations as much outside of Perl as possible

  • GLSL can be very terse

Sample GLSL program

 1:  void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
 2:  vec2 uv = fragCoord.xy / iResolution.xy;
 3:    fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
 4:  }
 5:  void main() {
 6:    vec4 color = vec4(0.0,0.0,0.0,1.0);
 7:    mainImage( color, gl_FragCoord.xy );
 8:    gl_FragColor = color;
 9:  }

Sample GLSL program

 1:  void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
 2:    vec2 uv = fragCoord.xy / iResolution.xy;
 3:    fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
 4:  }
 5:  void main() {
 6:    vec4 color = vec4(0.0,0.0,0.0,1.0);
 7:    mainImage( color, gl_FragCoord.xy );
 8:    gl_FragColor = color;
 9:  }

main() just like in C

Sample GLSL program

 1:  void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
 2:    vec2 uv = fragCoord.xy / iResolution.xy;
 3:    fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
 4:  }
 5:  void main() {
 6:    vec4 color = vec4(0.0,0.0,0.0,1.0);
 7:    mainImage( color, gl_FragCoord.xy );
 8:    gl_FragColor = color;
 9:  }

vec4 - four-float variable

Sample GLSL program

 1:  void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
 2:    vec2 uv = fragCoord.xy / iResolution.xy;
 3:    fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
 4:  }
 5:  void main() {
 6:    vec4 color = vec4(0.0,0.0,0.0,1.0);
 7:    mainImage( color, gl_FragCoord.xy );
 8:    gl_FragColor = color;
 9:  }

gl_FragCoord - variable for the coordinates of the current pixel (x,y,z,w)

mainImage(...) - function call

Sample GLSL program

 1:  void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
 2:    vec2 uv = fragCoord.xy / iResolution.xy;
 3:    fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
 4:  }
 5:  void main() {
 6:    vec4 color = vec4(0.0,0.0,0.0,1.0);
 7:    mainImage( color, gl_FragCoord.xy );
 8:    gl_FragColor = color;
 9:  }

iResolution - variable sent from Perl

iGlobalTime - variable sent from Perl

Sample GLSL program

 1:  void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
 2:    vec2 uv = fragCoord.xy / iResolution.xy;
 3:    fragColor = vec4(uv,0.5+0.5*sin(iGlobalTime),1.0);
 4:  }
 5:  void main() {
 6:    vec4 color = vec4(0.0,0.0,0.0,1.0);
 7:    mainImage( color, gl_FragCoord.xy );
 8:    gl_FragColor = color;
 9:  }

gl_FragColor - (output) variable for the pixel color (r,g,b,a)

App::ShaderToy

  • Fragment shader live editor

  • Based on an idea by Inigo Quilez and Pol Jeremias

  • https://www.shadertoy.com

  • But in Perl

  • Prima, Prima::OpenGL, OpenGL::Glew

Live Demo

See here ->

Installing App::Shadertoy

Not yet on CPAN

 1:    git clone https://github.com/Corion/app-shadertoy.git
 2:    cd app-shadertoy
 3:    cpanm --installdeps .
 4:    perl Makefile.PL
 5:    make
 6:    perl -Mblib -w scripts/shadertoy.pl -c shaders/demo.yml

Thanks

Thank you!

Thanks

Questions?

Thanks

Questions?

App::ShaderToy - https://github.com/Corion/app-shadertoy

corion@cpan.org

Slides will be at http://corion.net/talks/

The future of OpenGL::Glew / OpenGL::Modern

FAQ

  • Should I use Perl for my first Game / VR experience?

  • Ecosystem: Unity / Asset store / shader marketplace

  • Can you help me with OpenGL?

  • OpenGL Red Book

  • Can you help me with matrix multiplication?

What about Vulcan?

  • OpenGL+GLSL is like HTML+Javascript

  • OpenGL is far too high level

  • OpenGL is not suited towards graphics engines

  • HTML+Javascript move towards WebAssembly

  • No compiler in the OpenGL driver needed

The future

  • Nobody likes OpenGL programming

  • Everybody writes their own wrapper

  • I like GLSL programming

  • Trying to keep wrappers/frameworks at bay

Displaying Graphics on a PC

  • Raw framebuffer access

  • Abstractions (X11, GDI, OpenGL, DirectX)

  • HTML + Javascript in the browser

  • HTML5 <canvas>

  • WebGL

Understanding OpenGL

Why a programming language?

  • C is on the wrong side of the CPU / GPU divide

  • C is too powerful, not suited for parallelism (pointer aliasing)