This document discusses using shaders in cocos2d-x. It begins with an introduction to the speaker and overview of the cocos2d family of frameworks. It then explains the graphics rendering pipeline and key steps like vertex processing and rasterization. The document defines what a shader is and that it is executed for each vertex and pixel. Examples of using shaders in projects like The School of Games and Lines98 are provided to improve performance over traditional techniques. In summary, shaders can enhance games by processing graphics in real-time on the GPU for better memory usage and higher frame rates.
Rendering Pipeline
Position: Identifiesit 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.
What is shader?
Micro-program
Executedfor each “vertex” or each “pixel”
Run on GPU
Languages
GLSL (OpenGL)
HLSL (DirectX)
2 kinds of shader: Vertex & Fragment
#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?