blob: c8fba509c522b207e9cc8e27dd5ef826e0970214 [file] [log] [blame]
ethannicholas5961bc92016-10-12 06:39:56 -07001Overview
2========
3
Ethan Nicholas0df1b042017-03-31 13:56:23 -04004SkSL ("Skia Shading Language") is a variant of GLSL which is used as Skia's
ethannicholas5961bc92016-10-12 06:39:56 -07005internal shading language. SkSL is, at its heart, a single standardized version
6of GLSL which avoids all of the various version and dialect differences found
7in GLSL "in the wild", but it does bring a few of its own changes to the table.
8
9Skia uses the SkSL compiler to convert SkSL code to GLSL, GLSL ES, or SPIR-V
10before handing it over to the graphics driver.
11
Ethan Nicholas762466e2017-06-29 10:03:38 -040012
ethannicholas5961bc92016-10-12 06:39:56 -070013Differences from GLSL
14=====================
15
Ethan Nicholas8aa45692017-09-20 11:24:15 -040016* Precision modifiers are not used. 'float', 'int', and 'uint' are always high
17 precision. New types 'half', 'short', and 'ushort' are medium precision (we
18 do not use low precision).
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040019* Vector types are named <base type><columns>, so float2 instead of vec2 and
20 bool4 instead of bvec4
21* Matrix types are named <base type><columns>x<rows>, so float2x3 instead of
22 mat2x3 and double4x4 instead of dmat4
Ethan Nicholas5ac13c22017-05-10 15:06:17 -040023* "@if" and "@switch" are static versions of if and switch. They behave exactly
24 the same as if and switch in all respects other than it being a compile-time
25 error to use a non-constant expression as a test.
Ethan Nicholas3605ace2016-11-21 15:59:48 -050026* GLSL caps can be referenced via the syntax 'sk_Caps.<name>', e.g.
27 sk_Caps.sampleVariablesSupport. The value will be a constant boolean or int,
28 as appropriate. As SkSL supports constant folding and branch elimination, this
29 means that an 'if' statement which statically queries a cap will collapse down
30 to the chosen branch, meaning that:
31
32 if (sk_Caps.externalTextureSupport)
33 do_something();
34 else
35 do_something_else();
36
37 will compile as if you had written either 'do_something();' or
38 'do_something_else();', depending on whether that cap is enabled or not.
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040039* no #version statement is required, and it will be ignored if present
ethannicholas5961bc92016-10-12 06:39:56 -070040* the output color is sk_FragColor (do not declare it)
Ethan Nicholasbed683a2017-09-26 14:23:59 -040041* use sk_Position instead of gl_Position
42* use sk_PointSize instead of gl_PointSize
Ethan Nicholasa51740c2017-02-07 14:53:32 -050043* use sk_VertexID instead of gl_VertexID
Ethan Nicholas38657112017-02-09 17:01:22 -050044* the fragment coordinate is sk_FragCoord, and is always relative to the upper
45 left.
ethannicholas5961bc92016-10-12 06:39:56 -070046* you do not need to include ".0" to make a number a float (meaning that
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040047 "float2x, y) * 4" is perfectly legal in SkSL, unlike GLSL where it would often
48 have to be expressed "float2x, y) * 4.0". There is no performance penalty for
ethannicholas5961bc92016-10-12 06:39:56 -070049 this, as the number is converted to a float at compile time)
50* type suffixes on numbers (1.0f, 0xFFu) are both unnecessary and unsupported
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040051* creating a smaller vector from a larger vector (e.g. float2float31))) is
Ethan Nicholas84645e32017-02-09 13:57:14 -050052 intentionally disallowed, as it is just a wordier way of performing a swizzle.
53 Use swizzles instead.
Ethan Nicholas5af9ea32017-07-28 15:19:46 -040054* Use texture() instead of textureProj(), e.g. texture(sampler2D, float3 is
55 equivalent to GLSL's textureProj(sampler2D, float3
ethannicholas5961bc92016-10-12 06:39:56 -070056* some built-in functions and one or two rarely-used language features are not
57 yet supported (sorry!)
58
59SkSL is still under development, and is expected to diverge further from GLSL
60over time.
Ethan Nicholas762466e2017-06-29 10:03:38 -040061
62
63SkSL Fragment Processors
64========================
65
66An extension of SkSL allows for the creation of fragment processors in pure
67SkSL. The program defines its inputs similarly to a normal SkSL program (with
68'in' and 'uniform' variables), but the 'main()' function represents only this
69fragment processor's portion of the overall fragment shader.
70
71Within an '.fp' fragment processor file:
72
73* C++ code can be embedded in sections of the form:
74
75 @section_name { <arbitrary C++ code> }
76
77 Supported section are:
78 @header (in the .h file, outside the class declaration)
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040079 @headerEnd (at the end of the .h file)
Ethan Nicholas762466e2017-06-29 10:03:38 -040080 @class (in the .h file, inside the class declaration)
81 @cpp (in the .cpp file)
Ethan Nicholas9fb036f2017-07-05 16:19:09 -040082 @cppEnd (at the end of the .cpp file)
Ethan Nicholas762466e2017-06-29 10:03:38 -040083 @constructorParams (extra parameters to the constructor, comma-separated)
84 @constructor (replaces the default constructor)
85 @initializers (constructor initializer list, comma-separated)
86 @emitCode (extra code for the emitCode function)
87 @fields (extra private fields, each terminated with a semicolon)
88 @make (replaces the default Make function)
Ethan Nicholasf57c0d62017-07-31 11:18:22 -040089 @clone (replaces the default clone() function)
Ethan Nicholas762466e2017-06-29 10:03:38 -040090 @setData(<pdman>) (extra code for the setData function, where <pdman> is
91 the name of the GrGLSLProgramDataManager)
92 @test(<testData>) (the body of the TestCreate function, where <testData> is
93 the name of the GrProcessorTestData* parameter)
Ethan Nicholas68990be2017-07-13 09:36:52 -040094 @coordTransform(<sampler>)
95 (the matrix to attach to the named sampler2D's
96 GrCoordTransform)
97 @samplerParams(<sampler>)
98 (the sampler params to attach to the named sampler2D)
Ethan Nicholas762466e2017-06-29 10:03:38 -040099* global 'in' variables represent data passed to the fragment processor at
100 construction time. These variables become constructor parameters and are
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400101 stored in fragment processor fields. float2 map to SkPoints, and float4 map to
Ethan Nicholas762466e2017-06-29 10:03:38 -0400102 SkRects (in x, y, width, height) order.
103* 'uniform' variables become, as one would expect, top-level uniforms. By
104 default they do not have any data provided to them; you will need to provide
105 them with data via the @setData section.
106* 'in uniform' variables are uniforms that are automatically wired up to
107 fragment processor constructor parameters
108* the 'sk_TransformedCoords2D' array provides access to 2D transformed
109 coordinates. sk_TransformedCoords2D[0] is equivalent to calling
110 fragBuilder->ensureCoords2D(args.fTransformedCoords[0]) (and the result is
111 cached, so you need not worry about using the value repeatedly).
112* 'colorSpaceXform' is a supported type. It is reflected within SkSL as a mat4,
113 and on the C++ side as sk_sp<GrColorSpaceXform>.
114* the texture() function can be passed a colorSpaceXform as an additional
115 parameter
116* Uniform variables support an additional 'when' layout key.
117 'layout(when=foo) uniform int x;' means that this uniform will only be
118 emitted when the 'foo' expression is true.
119* 'in' variables support an additional 'key' layout key.
120 'layout(key) uniform int x;' means that this uniform should be included in
121 the program's key. Matrix variables additionally support 'key=identity',
122 which causes the key to consider only whether or not the matrix is an
123 identity matrix.