Motivation
What is OpenGL
Using OpenGL with Perl
Future improvements
There are lots of pretty pictures and animations in this talk
They have all been programmatically generated
But not by me
Max Maischein
Frankfurt.pm
Perl since 2000
HTC Vive + NVidia 1080 since 2016
CGI
Computer Generated Images
BBS / Demoscene
New hardware must be mastered
Video postprocessing (App::VideoPlayer)
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
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
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
GLSL is very restricted C
Runs on the GPU
No recursion
No variable length loops
Compiler included in OpenGL
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
1: use OpenGL ':all';
only OpenGL 1.x and 2.0 functions
Perl is not good at number pushing
PDL, SDL help
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
OpenGL is like Perl
Battle tested
Available even on Linux, HTML5, ...
Much bad/outdated documentation
Read the OpenGL FAQ
Do calculations as much outside of Perl as possible
GLSL can be very terse
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: }
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
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
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
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
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
)
Fragment shader live editor
Based on an idea by Inigo Quilez and Pol Jeremias
But in Perl
Prima, Prima::OpenGL, OpenGL::Glew
See here ->
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
Thank you!
Questions?
Questions?
App::ShaderToy - https://github.com/Corion/app-shadertoy
corion@cpan.org
Slides will be at http://corion.net/talks/
My second XS module
Move OpenGL::Glew into OpenGL::Modern (Chris Marshall and Mithaldu)
Real world usages for compute shaders (parallel Regexp execution?)
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?
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
Nobody likes OpenGL programming
Everybody writes their own wrapper
I like GLSL programming
Trying to keep wrappers/frameworks at bay
Raw framebuffer access
Abstractions (X11, GDI, OpenGL, DirectX)
HTML + Javascript in the browser
HTML5 <canvas>
WebGL
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)