KEMBAR78
Using Shader in cocos2d-x | PPTX
Using Shader in cocos2d-x
Dong Xuan Thuy, Fuji Technology
2017/03/09
About myself
- Đồng Xuân Thủy (Nick), 1992
- Game/Full-stack Developer (FujiTech)
- TechLead (WeenGame Studio)
https://github.com/thuydx55
nick@weengame.com
thuy.dong@fujitechjsc.com
Cocos2d family
Wiki: https://en.wikipedia.org/wiki/Cocos2d
Cocos2d (Python)
Cocos2d-ObjC (ObjC)
Cocos2d-x (C++, Javascript, Lua)
Cocos2d-html5 (deprecated) (Javascript)
Cocos2d-XNA (C#)
Rendering Pipeline
Position: Identifies it in a 3D space (x, y, z).
Color: Holds an RGBA value (R, G and B for the red,
green, and blue channels, alpha for transparency
— all values range from 0.0 to 1.0).
Normal: A way to describe the direction the vertex is
facing.
Texture: A 2D image that the vertex can use to
decorate the surface it is part of instead of a simple
color.
Rendering Pipeline
Ref: https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_on_the_web/Basic_theory
Rendering Pipeline
Vertex processing
Rendering Pipeline
Rasterization
Rendering Pipeline
Fragment processing
What is shader?
Micro-program
Executed for each “vertex” or each “pixel”
Run on GPU
Languages
GLSL (OpenGL)
HLSL (DirectX)
2 kinds of shader: Vertex & Fragment
Example (What I’ve done)
TSOG (The School Of Games)
Lines98
TSOG (The School Of Games)
- http://www.theschoolofgames.org/
- Source Code: https://github.com/theschoolofgames/tsog-mainapp/
- Open Source Project
- Education
TSOG
Problem
200 objects difference images
Create animations
Create shade images
Create outline images
TSOG
TSOG
TSOG
Edge Detection
Lines98
- http://lines98.net/
- Board Game
Lines98
- OpenCV?
Lines98
Clipping Node - Aliasing
Lines98
R
G
B
A
Lines98
Lines98
Barrel Distort
Lines98
Lines98
QA
Example of shader in cocos2d-x
Old way to create effect like that
60 FPS
189 Frames / 30.6MB
Memory: 129.8 MB
30 FPS
95 Frames / 15.4MB
Memory: 65.2 MB
Old way to create effect like that
15 FPS
47 Frames / 7.6MB
Memory: 32.3 MB
10 FPS
31 Frames / 5MB
Memory: 21.3 MB
60 FPS
60 FPS 30 FPS
15 FPS 10 FPS
Using Shader: Better
Only one image
Implement shader and apply to that image
Comparison
Memory
FPS

Using Shader in cocos2d-x

Editor's Notes

  • #7 Vertex processing is about combining the information about individual vertices into primitives and setting their coordinates in the 3D space for the viewer to see. It's like taking a photo of the given scenery you have prepared — you have to place the objects first, configure the camera, and then take the shot.
  • #8 Rasterization converts primitives (which are connected vertices) to a set of fragments. Those fragments — which are 3D projections of the 2D pixels — are aligned to the pixel grid, so eventually they can be printed out as pixels on a 2D screen display during the output merging stage.
  • #9 Fragment processing focuses on textures and lighting — it calculates final colors based on the given parameters.
  • #10 Of course! These days, even the cheapest GPU will bring you at least 300 or so cores (with monster cards having over 3000 of them), and performing the same thing across all the cores, is what GPUs are really good with. Those 30M shader runs per second, when distributed across 300 GPU cores running @ 1GHz each, will mean that we have 10000 GPU clocks to run our shader. And while GPU cores are weaker than CPU ones7 for generic calculations, they have hardware support for those things which matter for graphics and shaders. Just as one example, GPUs tend to have sin/cos operations8 at mere 8x of the cost of integer addition operation, while for CPU the ratio is more like 50-200x Fragment: returns the color of the pixel. Vertex: what vertices we’re speaking about?