README 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. Overview
  2. ========
  3. SkSL ("Skia Shading Language") is a variant of GLSL which is used as Skia's
  4. internal shading language. SkSL is, at its heart, a single standardized version
  5. of GLSL which avoids all of the various version and dialect differences found
  6. in GLSL "in the wild", but it does bring a few of its own changes to the table.
  7. Skia uses the SkSL compiler to convert SkSL code to GLSL, GLSL ES, or SPIR-V
  8. before handing it over to the graphics driver.
  9. Differences from GLSL
  10. =====================
  11. * Precision modifiers are not used. 'float', 'int', and 'uint' are always high
  12. precision. New types 'half', 'short', and 'ushort' are medium precision (we
  13. do not use low precision).
  14. * Vector types are named <base type><columns>, so float2 instead of vec2 and
  15. bool4 instead of bvec4
  16. * Matrix types are named <base type><columns>x<rows>, so float2x3 instead of
  17. mat2x3 and double4x4 instead of dmat4
  18. * "@if" and "@switch" are static versions of if and switch. They behave exactly
  19. the same as if and switch in all respects other than it being a compile-time
  20. error to use a non-constant expression as a test.
  21. * GLSL caps can be referenced via the syntax 'sk_Caps.<name>', e.g.
  22. sk_Caps.sampleVariablesSupport. The value will be a constant boolean or int,
  23. as appropriate. As SkSL supports constant folding and branch elimination, this
  24. means that an 'if' statement which statically queries a cap will collapse down
  25. to the chosen branch, meaning that:
  26. if (sk_Caps.externalTextureSupport)
  27. do_something();
  28. else
  29. do_something_else();
  30. will compile as if you had written either 'do_something();' or
  31. 'do_something_else();', depending on whether that cap is enabled or not.
  32. * no #version statement is required, and it will be ignored if present
  33. * the output color is sk_FragColor (do not declare it)
  34. * use sk_Position instead of gl_Position. sk_Position is in device coordinates
  35. rather than normalized coordinates.
  36. * use sk_PointSize instead of gl_PointSize
  37. * use sk_VertexID instead of gl_VertexID
  38. * use sk_InstanceID instead of gl_InstanceID
  39. * the fragment coordinate is sk_FragCoord, and is always relative to the upper
  40. left.
  41. * use sk_Clockwise instead of gl_FrontFacing. This is always relative to an
  42. upper left origin.
  43. * you do not need to include ".0" to make a number a float (meaning that
  44. "float2(x, y) * 4" is perfectly legal in SkSL, unlike GLSL where it would
  45. often have to be expressed "float2(x, y) * 4.0". There is no performance
  46. penalty for this, as the number is converted to a float at compile time)
  47. * type suffixes on numbers (1.0f, 0xFFu) are both unnecessary and unsupported
  48. * creating a smaller vector from a larger vector (e.g. float2(float3(1))) is
  49. intentionally disallowed, as it is just a wordier way of performing a swizzle.
  50. Use swizzles instead.
  51. * The last swizzle component, in addition to the normal rgba / xyzw components,
  52. can also be the constants '0' or '1'. foo.rgb1 is equivalent to
  53. float4(foo.rgb, 1).
  54. * Use texture() instead of textureProj(), e.g. texture(sampler2D, float3) is
  55. equivalent to GLSL's textureProj(sampler2D, float3)
  56. * Render target width and height are available via sk_Width and sk_Height
  57. * some built-in functions and one or two rarely-used language features are not
  58. yet supported (sorry!)
  59. SkSL is still under development, and is expected to diverge further from GLSL
  60. over time.
  61. SkSL Fragment Processors
  62. ========================
  63. ********************************************************************************
  64. *** IMPORTANT: You must set gn arg "skia_compile_processors = true" to cause ***
  65. *** .fp files to be recompiled! In order for compilation to succeed, you ***
  66. *** must run bin/fetch-clang-format (once) to install our blessed version. ***
  67. ********************************************************************************
  68. An extension of SkSL allows for the creation of fragment processors in pure
  69. SkSL. The program defines its inputs similarly to a normal SkSL program (with
  70. 'in' and 'uniform' variables), but the 'main()' function represents only this
  71. fragment processor's portion of the overall fragment shader.
  72. Within an '.fp' fragment processor file:
  73. * C++ code can be embedded in sections of the form:
  74. @section_name { <arbitrary C++ code> }
  75. Supported section are:
  76. @header (in the .h file, outside the class declaration)
  77. @headerEnd (at the end of the .h file)
  78. @class (in the .h file, inside the class declaration)
  79. @cpp (in the .cpp file)
  80. @cppEnd (at the end of the .cpp file)
  81. @constructorParams (extra parameters to the constructor, comma-separated)
  82. @constructor (replaces the default constructor)
  83. @initializers (constructor initializer list, comma-separated)
  84. @emitCode (extra code for the emitCode function)
  85. @fields (extra private fields, each terminated with a semicolon)
  86. @make (replaces the default Make function)
  87. @clone (replaces the default clone() function)
  88. @setData(<pdman>) (extra code for the setData function, where <pdman> is
  89. the name of the GrGLSLProgramDataManager)
  90. @test(<testData>) (the body of the TestCreate function, where <testData> is
  91. the name of the GrProcessorTestData* parameter)
  92. @coordTransform(<sampler>)
  93. (the matrix to attach to the named sampler2D's
  94. GrCoordTransform)
  95. @samplerParams(<sampler>)
  96. (the sampler params to attach to the named sampler2D)
  97. * global 'in' variables represent data passed to the fragment processor at
  98. construction time. These variables become constructor parameters and are
  99. stored in fragment processor fields. By default float2/half2 maps to SkPoints,
  100. and float4/half4 maps to SkRects (in x, y, width, height) order. Similarly,
  101. int2/short2 maps to SkIPoint and int4/half4 maps to SkIRect. Use ctype
  102. (below) to override this default mapping.
  103. * global variables support an additional 'ctype' layout key, providing the type
  104. they should be represented as from within the C++ code. For instance, you can
  105. use 'layout(ctype=SkPMColor4f) in half4 color;' to create a variable that looks
  106. like a half4 on the SkSL side of things, and a SkPMColor4f on the C++ side of
  107. things.
  108. * 'uniform' variables become, as one would expect, top-level uniforms. By
  109. default they do not have any data provided to them; you will need to provide
  110. them with data via the @setData section.
  111. * 'in uniform' variables are uniforms that are automatically wired up to
  112. fragment processor constructor parameters. The fragment processor will accept
  113. a parameter representing the uniform's value, and automatically plumb it
  114. through to the uniform's value in its generated setData() function.
  115. * 'in uniform' variables support a 'tracked' flag in the layout that will
  116. have the generated code automatically implement state tracking on the uniform
  117. value to minimize GPU calls.
  118. * the 'sk_TransformedCoords2D' array provides access to 2D transformed
  119. coordinates. sk_TransformedCoords2D[0] is equivalent to calling
  120. fragBuilder->ensureCoords2D(args.fTransformedCoords[0]) (and the result is
  121. cached, so you need not worry about using the value repeatedly).
  122. * Uniform variables support an additional 'when' layout key.
  123. 'layout(when=foo) uniform int x;' means that this uniform will only be
  124. emitted when the 'foo' expression is true.
  125. * 'in' variables support an additional 'key' layout key.
  126. 'layout(key) uniform int x;' means that this uniform should be included in
  127. the program's key. Matrix variables additionally support 'key=identity',
  128. which causes the key to consider only whether or not the matrix is an
  129. identity matrix.
  130. * child processors can be declared with 'in fragmentProcessor <name>;', and can
  131. be invoked by calling 'process(<name>)' or 'process(<name>, <inputColor>)'.
  132. The first variant emits the child with a solid white input color. The second
  133. variant emits the child with the result of the 2nd argument's expression,
  134. which must evaluate to a half4. The process function returns a half4.
  135. * By default, fragment processors must be non-null. The type for a nullable
  136. fragment processor is 'fragmentProcessor?', as in
  137. 'in fragmentProcessor? <name>'. You can check for the presence of such a
  138. fragment processor by comparing it to 'null'.
  139. Creating a new .fp file
  140. =======================
  141. 1. Ensure that you have set gn arg "skia_compile_processors = true"
  142. 2. Create your new .fp file, generally under src/gpu/effects.
  143. 3. Add the .fp file to sksl.gni.
  144. 4. Build Skia. This will cause the .fp file to be compiled, resulting in a new
  145. .cpp and .h file for the fragment processor.
  146. 5. Add the .cpp and .h files to gpu.gni.
  147. 6. Add the new processor's ClassID (k<ProcessorName>_ClassID) to
  148. GrProcessor::ClassID.
  149. 7. At this point you can reference the new fragment processor from within Skia.
  150. Once you have done this initial setup, simply re-build Skia to pick up any
  151. changes to the .fp file.