Kevin Lubick | f3d6c36 | 2020-01-06 08:11:52 -0500 | [diff] [blame] | 1 | CanvasKit._extraInitializations = CanvasKit._extraInitializations || []; |
| 2 | CanvasKit._extraInitializations.push(function() { |
Kevin Lubick | 70b6729 | 2021-02-03 11:26:12 -0500 | [diff] [blame] | 3 | |
| 4 | // sksl is the shader code. |
| 5 | // errorCallback is a function that will be called with an error string if the |
| 6 | // effect cannot be made. If not provided, the error will be logged. |
| 7 | CanvasKit.RuntimeEffect.Make = function(sksl, errorCallback) { |
| 8 | // The easiest way to pass a function into C++ code is to wrap it in an object and |
| 9 | // treat it as an emscripten::val on the other side. |
| 10 | var callbackObj = { |
| 11 | 'onError': errorCallback || function(err) { |
| 12 | console.log('RuntimeEffect error', err); |
| 13 | }, |
| 14 | }; |
| 15 | return CanvasKit.RuntimeEffect._Make(sksl, callbackObj); |
| 16 | }; |
| 17 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 18 | CanvasKit.RuntimeEffect.prototype.makeShader = function(floats, isOpaque, localMatrix) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 19 | // We don't need to free these floats because they will become owned by the shader. |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 20 | var fptr = copy1dArray(floats, "HEAPF32"); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 21 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Kevin Lubick | f3d6c36 | 2020-01-06 08:11:52 -0500 | [diff] [blame] | 22 | // Our array has 4 bytes per float, so be sure to account for that before |
| 23 | // sending it over the wire. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 24 | return this._makeShader(fptr, floats.length * 4, !!isOpaque, localMatrixPtr); |
Kevin Lubick | f3d6c36 | 2020-01-06 08:11:52 -0500 | [diff] [blame] | 25 | } |
Kevin Lubick | ecd8762 | 2020-02-22 07:37:33 -0500 | [diff] [blame] | 26 | |
Kevin Lubick | 54c1b3d | 2020-10-07 16:09:22 -0400 | [diff] [blame] | 27 | // childrenWithShaders is an array of other shaders (e.g. Image.makeShader()) |
| 28 | CanvasKit.RuntimeEffect.prototype.makeShaderWithChildren = function(floats, isOpaque, childrenShaders, localMatrix) { |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 29 | // We don't need to free these floats because they will become owned by the shader. |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 30 | var fptr = copy1dArray(floats, "HEAPF32"); |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 31 | var localMatrixPtr = copy3x3MatrixToWasm(localMatrix); |
Kevin Lubick | ecd8762 | 2020-02-22 07:37:33 -0500 | [diff] [blame] | 32 | var barePointers = []; |
Kevin Lubick | 6bffe39 | 2020-04-02 15:24:15 -0400 | [diff] [blame] | 33 | for (var i = 0; i < childrenShaders.length; i++) { |
Kevin Lubick | ecd8762 | 2020-02-22 07:37:33 -0500 | [diff] [blame] | 34 | // childrenShaders are emscriptens smart pointer type. We want to get the bare pointer |
| 35 | // and send that over the wire, so it can be re-wrapped as an sk_sp. |
| 36 | barePointers.push(childrenShaders[i].$$.ptr); |
| 37 | } |
Kevin Lubick | 69e46da | 2020-06-05 07:13:48 -0400 | [diff] [blame] | 38 | var childrenPointers = copy1dArray(barePointers, "HEAPU32"); |
Kevin Lubick | ecd8762 | 2020-02-22 07:37:33 -0500 | [diff] [blame] | 39 | // Our array has 4 bytes per float, so be sure to account for that before |
| 40 | // sending it over the wire. |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 41 | return this._makeShaderWithChildren(fptr, floats.length * 4, !!isOpaque, childrenPointers, |
| 42 | barePointers.length, localMatrixPtr); |
Kevin Lubick | ecd8762 | 2020-02-22 07:37:33 -0500 | [diff] [blame] | 43 | } |
Kevin Lubick | 6aa3869 | 2020-06-01 11:25:47 -0400 | [diff] [blame] | 44 | }); |