joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrDefaultGeoProcFactory.h" |
| 9 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 10 | #include "GrCaps.h" |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 11 | #include "SkRefCnt.h" |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 12 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
egdaniel | 2d721d3 | 2015-11-11 13:06:05 -0800 | [diff] [blame] | 13 | #include "glsl/GrGLSLFragmentShaderBuilder.h" |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 14 | #include "glsl/GrGLSLGeometryProcessor.h" |
Chris Dalton | c17bf32 | 2017-10-24 10:59:03 -0600 | [diff] [blame] | 15 | #include "glsl/GrGLSLVertexGeoBuilder.h" |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 16 | #include "glsl/GrGLSLVarying.h" |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 17 | #include "glsl/GrGLSLUniformHandler.h" |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 18 | #include "glsl/GrGLSLUtil.h" |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * The default Geometry Processor simply takes position and multiplies it by the uniform view |
| 22 | * matrix. It also leaves coverage untouched. Behind the scenes, we may add per vertex color or |
| 23 | * local coords. |
| 24 | */ |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 25 | |
| 26 | enum GPFlag { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 27 | kColorAttribute_GPFlag = 0x1, |
| 28 | kColorAttributeIsSkColor_GPFlag = 0x2, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 29 | kColorAttributeIsWide_GPFlag = 0x4, |
| 30 | kLocalCoordAttribute_GPFlag = 0x8, |
| 31 | kCoverageAttribute_GPFlag = 0x10, |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 32 | kCoverageAttributeTweak_GPFlag = 0x20, |
| 33 | kBonesAttribute_GPFlag = 0x40, |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 34 | }; |
| 35 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 36 | static constexpr int kNumVec2sPerBone = 3; // Our bone matrices are 3x2 matrices passed in as |
| 37 | // vec2s in column major order, and thus there are 3 |
| 38 | // vec2s per bone. |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 39 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 40 | class DefaultGeoProc : public GrGeometryProcessor { |
| 41 | public: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 42 | static sk_sp<GrGeometryProcessor> Make(const GrShaderCaps* shaderCaps, |
| 43 | uint32_t gpTypeFlags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 44 | const SkPMColor4f& color, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 45 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 46 | const SkMatrix& viewMatrix, |
| 47 | const SkMatrix& localMatrix, |
| 48 | bool localCoordsWillBeRead, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 49 | uint8_t coverage, |
| 50 | const float* bones, |
| 51 | int boneCount) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 52 | return sk_sp<GrGeometryProcessor>(new DefaultGeoProc( |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 53 | shaderCaps, gpTypeFlags, color, std::move(colorSpaceXform), viewMatrix, localMatrix, |
| 54 | coverage, localCoordsWillBeRead, bones, boneCount)); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | const char* name() const override { return "DefaultGeometryProcessor"; } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 58 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 59 | const SkPMColor4f& color() const { return fColor; } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 60 | bool hasVertexColor() const { return fInColor.isInitialized(); } |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 61 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 62 | const SkMatrix& localMatrix() const { return fLocalMatrix; } |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 63 | bool localCoordsWillBeRead() const { return fLocalCoordsWillBeRead; } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 64 | uint8_t coverage() const { return fCoverage; } |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 65 | bool hasVertexCoverage() const { return fInCoverage.isInitialized(); } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 66 | const float* bones() const { return fBones; } |
| 67 | int boneCount() const { return fBoneCount; } |
| 68 | bool hasBones() const { return SkToBool(fBones); } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 69 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 70 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 71 | public: |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 72 | GLSLProcessor() |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 73 | : fViewMatrix(SkMatrix::InvalidMatrix()) |
| 74 | , fColor(SK_PMColor4fILLEGAL) |
| 75 | , fCoverage(0xff) {} |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 76 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 77 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 78 | const DefaultGeoProc& gp = args.fGP.cast<DefaultGeoProc>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 79 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 80 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 81 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 82 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 83 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 84 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 85 | varyingHandler->emitAttributes(gp); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 86 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 87 | bool tweakAlpha = SkToBool(gp.fFlags & kCoverageAttributeTweak_GPFlag); |
| 88 | SkASSERT(!tweakAlpha || gp.hasVertexCoverage()); |
| 89 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 90 | // Setup pass through color |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 91 | if (gp.hasVertexColor() || tweakAlpha) { |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 92 | GrGLSLVarying varying(kHalf4_GrSLType); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 93 | varyingHandler->addVarying("color", &varying); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 94 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 95 | // There are several optional steps to process the color. Start with the attribute, |
| 96 | // or with uniform color (in the case of folding coverage into a uniform color): |
| 97 | if (gp.hasVertexColor()) { |
| 98 | vertBuilder->codeAppendf("half4 color = %s;", gp.fInColor.name()); |
| 99 | } else { |
| 100 | const char* colorUniformName; |
| 101 | fColorUniform = uniformHandler->addUniform(kVertex_GrShaderFlag, |
| 102 | kHalf4_GrSLType, |
| 103 | "Color", |
| 104 | &colorUniformName); |
| 105 | vertBuilder->codeAppendf("half4 color = %s;", colorUniformName); |
| 106 | } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 107 | |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 108 | // For SkColor, do a red/blue swap, possible color space conversion, and premul |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 109 | if (gp.fFlags & kColorAttributeIsSkColor_GPFlag) { |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 110 | vertBuilder->codeAppend("color = color.bgra;"); |
| 111 | |
| 112 | if (gp.fColorSpaceXform) { |
| 113 | fColorSpaceHelper.emitCode(uniformHandler, gp.fColorSpaceXform.get(), |
| 114 | kVertex_GrShaderFlag); |
| 115 | SkString xformedColor; |
| 116 | vertBuilder->appendColorGamutXform(&xformedColor, "color", |
| 117 | &fColorSpaceHelper); |
| 118 | vertBuilder->codeAppendf("color = %s;", xformedColor.c_str()); |
| 119 | } |
| 120 | |
| 121 | vertBuilder->codeAppend("color = half4(color.rgb * color.a, color.a);"); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 122 | } |
| 123 | |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 124 | // Optionally fold coverage into alpha (color). |
| 125 | if (tweakAlpha) { |
| 126 | vertBuilder->codeAppendf("color = color * %s;", gp.fInCoverage.name()); |
| 127 | } |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 128 | vertBuilder->codeAppendf("%s = color;\n", varying.vsOut()); |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 129 | fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, varying.fsIn()); |
Brian Salomon | bfd5183 | 2017-01-04 13:22:08 -0500 | [diff] [blame] | 130 | } else { |
| 131 | this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, |
| 132 | &fColorUniform); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 135 | // Setup bone transforms |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 136 | // NOTE: This code path is currently unused. Benchmarks have found that for all |
| 137 | // reasonable cases of skinned vertices, the overhead involved in copying and uploading |
| 138 | // bone data makes performing the transformations on the CPU faster than doing so on |
| 139 | // the GPU. This is being kept here in case that changes. |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 140 | const char* transformedPositionName = gp.fInPosition.name(); |
| 141 | if (gp.hasBones()) { |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 142 | // Set up the uniform for the bones. |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 143 | const char* vertBonesUniformName; |
| 144 | fBonesUniform = uniformHandler->addUniformArray(kVertex_GrShaderFlag, |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 145 | kFloat2_GrSLType, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 146 | "Bones", |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 147 | kMaxBones * kNumVec2sPerBone, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 148 | &vertBonesUniformName); |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 149 | |
| 150 | // Set up the bone application function. |
| 151 | SkString applyBoneFunctionName; |
| 152 | this->emitApplyBoneFunction(vertBuilder, |
| 153 | vertBonesUniformName, |
| 154 | &applyBoneFunctionName); |
| 155 | |
| 156 | // Apply the world transform to the position first. |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 157 | vertBuilder->codeAppendf( |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 158 | "float2 worldPosition = %s(0, %s);" |
| 159 | "float2 transformedPosition = float2(0, 0);" |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 160 | "for (int i = 0; i < 4; i++) {", |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 161 | applyBoneFunctionName.c_str(), |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 162 | gp.fInPosition.name()); |
| 163 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 164 | // If the GPU supports unsigned integers, then we can read the index. Otherwise, |
| 165 | // we have to estimate it given the float representation. |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 166 | if (args.fShaderCaps->unsignedSupport()) { |
| 167 | vertBuilder->codeAppendf( |
| 168 | " byte index = %s[i];", |
| 169 | gp.fInBoneIndices.name()); |
| 170 | } else { |
| 171 | vertBuilder->codeAppendf( |
| 172 | " byte index = byte(floor(%s[i] * 255 + 0.5));", |
| 173 | gp.fInBoneIndices.name()); |
| 174 | } |
| 175 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 176 | // Get the weight and apply the transformation. |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 177 | vertBuilder->codeAppendf( |
| 178 | " float weight = %s[i];" |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 179 | " transformedPosition += %s(index, worldPosition) * weight;" |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 180 | "}", |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 181 | gp.fInBoneWeights.name(), |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 182 | applyBoneFunctionName.c_str()); |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 183 | transformedPositionName = "transformedPosition"; |
| 184 | } |
| 185 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 186 | // Setup position |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 187 | this->writeOutputPosition(vertBuilder, |
| 188 | uniformHandler, |
| 189 | gpArgs, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 190 | transformedPositionName, |
Brian Salomon | 7f23543 | 2017-08-16 09:41:48 -0400 | [diff] [blame] | 191 | gp.viewMatrix(), |
| 192 | &fViewMatrixUniform); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 193 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 194 | if (gp.fInLocalCoords.isInitialized()) { |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 195 | // emit transforms with explicit local coords |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 196 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 197 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 198 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 199 | gp.fInLocalCoords.asShaderVar(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 200 | gp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 201 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 202 | } else { |
| 203 | // emit transforms with position |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 204 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 205 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 206 | uniformHandler, |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 207 | gp.fInPosition.asShaderVar(), |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 208 | gp.localMatrix(), |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 209 | args.fFPCoordTransformHandler); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 210 | } |
| 211 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 212 | // Setup coverage as pass through |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 213 | if (gp.hasVertexCoverage() && !tweakAlpha) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 214 | fragBuilder->codeAppendf("half alpha = 1.0;"); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 215 | varyingHandler->addPassThroughAttribute(gp.fInCoverage, "alpha"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 216 | fragBuilder->codeAppendf("%s = half4(alpha);", args.fOutputCoverage); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 217 | } else if (gp.coverage() == 0xff) { |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 218 | fragBuilder->codeAppendf("%s = half4(1);", args.fOutputCoverage); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 219 | } else { |
| 220 | const char* fragCoverage; |
| 221 | fCoverageUniform = uniformHandler->addUniform(kFragment_GrShaderFlag, |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 222 | kHalf_GrSLType, |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 223 | "Coverage", |
| 224 | &fragCoverage); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 225 | fragBuilder->codeAppendf("%s = half4(%s);", args.fOutputCoverage, fragCoverage); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 226 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 227 | } |
| 228 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 229 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 230 | const GrShaderCaps&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 231 | GrProcessorKeyBuilder* b) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 232 | const DefaultGeoProc& def = gp.cast<DefaultGeoProc>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 233 | uint32_t key = def.fFlags; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 234 | key |= (def.coverage() == 0xff) ? 0x80 : 0; |
| 235 | key |= (def.localCoordsWillBeRead() && def.localMatrix().hasPerspective()) ? 0x100 : 0; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 236 | key |= ComputePosKey(def.viewMatrix()) << 20; |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 237 | b->add32(key); |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 238 | b->add32(GrColorSpaceXform::XformKey(def.fColorSpaceXform.get())); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 239 | } |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 240 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 241 | void setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 242 | const GrPrimitiveProcessor& gp, |
| 243 | FPCoordTransformIter&& transformIter) override { |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 244 | const DefaultGeoProc& dgp = gp.cast<DefaultGeoProc>(); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 245 | |
| 246 | if (!dgp.viewMatrix().isIdentity() && !fViewMatrix.cheapEqualTo(dgp.viewMatrix())) { |
| 247 | fViewMatrix = dgp.viewMatrix(); |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 248 | float viewMatrix[3 * 3]; |
egdaniel | 64c4728 | 2015-11-13 06:54:19 -0800 | [diff] [blame] | 249 | GrGLSLGetMatrix<3>(viewMatrix, fViewMatrix); |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 250 | pdman.setMatrix3f(fViewMatrixUniform, viewMatrix); |
| 251 | } |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 252 | |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 253 | if (!dgp.hasVertexColor() && dgp.color() != fColor) { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 254 | pdman.set4fv(fColorUniform, 1, dgp.color().vec()); |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 255 | fColor = dgp.color(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 256 | } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 257 | |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 258 | if (dgp.coverage() != fCoverage && !dgp.hasVertexCoverage()) { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 259 | pdman.set1f(fCoverageUniform, GrNormalizeByteToFloat(dgp.coverage())); |
| 260 | fCoverage = dgp.coverage(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 261 | } |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 262 | this->setTransformDataHelper(dgp.fLocalMatrix, pdman, &transformIter); |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 263 | |
Brian Osman | 08a50e0 | 2018-06-15 15:06:48 -0400 | [diff] [blame] | 264 | fColorSpaceHelper.setData(pdman, dgp.fColorSpaceXform.get()); |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 265 | |
| 266 | if (dgp.hasBones()) { |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 267 | pdman.set2fv(fBonesUniform, dgp.boneCount() * kNumVec2sPerBone, dgp.bones()); |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 268 | } |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 269 | } |
| 270 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 271 | private: |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 272 | void emitApplyBoneFunction(GrGLSLVertexBuilder* vertBuilder, |
| 273 | const char* vertBonesUniformName, |
| 274 | SkString* funcName) { |
| 275 | // The bone matrices are passed in as 3x2 matrices in column-major order as groups |
| 276 | // of 3 float2s. This code takes those float2s and performs the matrix operation on |
| 277 | // a given matrix and float2. |
Nico Weber | e50efdf | 2018-10-01 14:40:44 -0400 | [diff] [blame] | 278 | const GrShaderVar gApplyBoneArgs[] = { |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 279 | GrShaderVar("index", kByte_GrSLType), |
| 280 | GrShaderVar("vec", kFloat2_GrSLType), |
| 281 | }; |
| 282 | SkString body; |
| 283 | body.appendf( |
| 284 | " float2 c0 = %s[index * 3];" |
| 285 | " float2 c1 = %s[index * 3 + 1];" |
| 286 | " float2 c2 = %s[index * 3 + 2];" |
| 287 | " float x = c0.x * vec.x + c1.x * vec.y + c2.x;" |
| 288 | " float y = c0.y * vec.x + c1.y * vec.y + c2.y;" |
| 289 | " return float2(x, y);", |
| 290 | vertBonesUniformName, |
| 291 | vertBonesUniformName, |
| 292 | vertBonesUniformName); |
| 293 | vertBuilder->emitFunction(kFloat2_GrSLType, |
| 294 | "applyBone", |
| 295 | SK_ARRAY_COUNT(gApplyBoneArgs), |
| 296 | gApplyBoneArgs, |
| 297 | body.c_str(), |
| 298 | funcName); |
| 299 | } |
| 300 | |
| 301 | private: |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 302 | SkMatrix fViewMatrix; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 303 | SkPMColor4f fColor; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 304 | uint8_t fCoverage; |
joshualitt | 5559ca2 | 2015-05-21 15:50:36 -0700 | [diff] [blame] | 305 | UniformHandle fViewMatrixUniform; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 306 | UniformHandle fColorUniform; |
| 307 | UniformHandle fCoverageUniform; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 308 | UniformHandle fBonesUniform; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 309 | GrGLSLColorSpaceXformHelper fColorSpaceHelper; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 310 | |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 311 | typedef GrGLSLGeometryProcessor INHERITED; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 312 | }; |
| 313 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 314 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 315 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 318 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 319 | return new GLSLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 320 | } |
| 321 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 322 | private: |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 323 | DefaultGeoProc(const GrShaderCaps* shaderCaps, |
| 324 | uint32_t gpTypeFlags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 325 | const SkPMColor4f& color, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 326 | sk_sp<GrColorSpaceXform> colorSpaceXform, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 327 | const SkMatrix& viewMatrix, |
| 328 | const SkMatrix& localMatrix, |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 329 | uint8_t coverage, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 330 | bool localCoordsWillBeRead, |
| 331 | const float* bones, |
| 332 | int boneCount) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 333 | : INHERITED(kDefaultGeoProc_ClassID) |
| 334 | , fColor(color) |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 335 | , fViewMatrix(viewMatrix) |
| 336 | , fLocalMatrix(localMatrix) |
| 337 | , fCoverage(coverage) |
| 338 | , fFlags(gpTypeFlags) |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 339 | , fLocalCoordsWillBeRead(localCoordsWillBeRead) |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 340 | , fColorSpaceXform(std::move(colorSpaceXform)) |
| 341 | , fBones(bones) |
| 342 | , fBoneCount(boneCount) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 343 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 344 | if (fFlags & kColorAttribute_GPFlag) { |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 345 | fInColor = MakeColorAttribute("inColor", |
| 346 | SkToBool(fFlags & kColorAttributeIsWide_GPFlag)); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 347 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 348 | if (fFlags & kLocalCoordAttribute_GPFlag) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 349 | fInLocalCoords = {"inLocalCoord", kFloat2_GrVertexAttribType, |
| 350 | kFloat2_GrSLType}; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 351 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 352 | if (fFlags & kCoverageAttribute_GPFlag) { |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 353 | fInCoverage = {"inCoverage", kFloat_GrVertexAttribType, kHalf_GrSLType}; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 354 | } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 355 | if (fFlags & kBonesAttribute_GPFlag) { |
| 356 | SkASSERT(bones && (boneCount > 0)); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 357 | // GLSL 1.10 and 1.20 don't support integer attributes. |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 358 | GrVertexAttribType indicesCPUType = kByte4_GrVertexAttribType; |
| 359 | GrSLType indicesGPUType = kByte4_GrSLType; |
| 360 | if (!shaderCaps->unsignedSupport()) { |
| 361 | indicesCPUType = kUByte4_norm_GrVertexAttribType; |
| 362 | indicesGPUType = kHalf4_GrSLType; |
| 363 | } |
| 364 | fInBoneIndices = {"inBoneIndices", indicesCPUType, indicesGPUType}; |
Brian Osman | d4c2970 | 2018-09-14 16:16:55 -0400 | [diff] [blame] | 365 | fInBoneWeights = {"inBoneWeights", kUByte4_norm_GrVertexAttribType, |
| 366 | kHalf4_GrSLType}; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 367 | } |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 368 | this->setVertexAttributes(&fInPosition, 6); |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | Attribute fInPosition; |
| 372 | Attribute fInColor; |
| 373 | Attribute fInLocalCoords; |
| 374 | Attribute fInCoverage; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 375 | Attribute fInBoneIndices; |
| 376 | Attribute fInBoneWeights; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 377 | SkPMColor4f fColor; |
joshualitt | e578a95 | 2015-05-14 10:09:13 -0700 | [diff] [blame] | 378 | SkMatrix fViewMatrix; |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 379 | SkMatrix fLocalMatrix; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 380 | uint8_t fCoverage; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 381 | uint32_t fFlags; |
bsalomon | 7765a47 | 2015-07-08 11:26:37 -0700 | [diff] [blame] | 382 | bool fLocalCoordsWillBeRead; |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 383 | sk_sp<GrColorSpaceXform> fColorSpaceXform; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 384 | const float* fBones; |
| 385 | int fBoneCount; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 386 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 387 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 388 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 389 | typedef GrGeometryProcessor INHERITED; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DefaultGeoProc); |
| 393 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 394 | #if GR_TEST_UTILS |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 395 | static constexpr int kNumFloatsPerBone = 6; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 396 | static constexpr int kTestBoneCount = 4; |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 397 | static constexpr float kTestBones[kTestBoneCount * kNumFloatsPerBone] = { |
| 398 | 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, |
| 399 | 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, |
| 400 | 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, |
| 401 | 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 402 | }; |
| 403 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 404 | sk_sp<GrGeometryProcessor> DefaultGeoProc::TestCreate(GrProcessorTestData* d) { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 405 | uint32_t flags = 0; |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 406 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 407 | flags |= kColorAttribute_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 408 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 409 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 410 | flags |= kColorAttributeIsSkColor_GPFlag; |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 411 | } |
joshualitt | 0067ff5 | 2015-07-08 14:26:19 -0700 | [diff] [blame] | 412 | if (d->fRandom->nextBool()) { |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 413 | flags |= kColorAttributeIsWide_GPFlag; |
| 414 | } |
| 415 | if (d->fRandom->nextBool()) { |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 416 | flags |= kCoverageAttribute_GPFlag; |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 417 | if (d->fRandom->nextBool()) { |
| 418 | flags |= kCoverageAttributeTweak_GPFlag; |
| 419 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 420 | } |
| 421 | if (d->fRandom->nextBool()) { |
| 422 | flags |= kLocalCoordAttribute_GPFlag; |
joshualitt | b2aa7cb | 2015-08-05 11:05:22 -0700 | [diff] [blame] | 423 | } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 424 | if (d->fRandom->nextBool()) { |
| 425 | flags |= kBonesAttribute_GPFlag; |
| 426 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 427 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 428 | return DefaultGeoProc::Make(d->caps()->shaderCaps(), |
| 429 | flags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 430 | SkPMColor4f::FromBytes_RGBA(GrRandomColor(d->fRandom)), |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 431 | GrTest::TestColorXform(d->fRandom), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 432 | GrTest::TestMatrix(d->fRandom), |
| 433 | GrTest::TestMatrix(d->fRandom), |
| 434 | d->fRandom->nextBool(), |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 435 | GrRandomCoverage(d->fRandom), |
| 436 | kTestBones, |
| 437 | kTestBoneCount); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 438 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 439 | #endif |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 440 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 441 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::Make(const GrShaderCaps* shaderCaps, |
| 442 | const Color& color, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 443 | const Coverage& coverage, |
| 444 | const LocalCoords& localCoords, |
| 445 | const SkMatrix& viewMatrix) { |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 446 | uint32_t flags = 0; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 447 | if (Color::kPremulGrColorAttribute_Type == color.fType) { |
| 448 | flags |= kColorAttribute_GPFlag; |
| 449 | } else if (Color::kUnpremulSkColorAttribute_Type == color.fType) { |
| 450 | flags |= kColorAttribute_GPFlag | kColorAttributeIsSkColor_GPFlag; |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 451 | } else if (Color::kPremulWideColorAttribute_Type == color.fType) { |
| 452 | flags |= kColorAttribute_GPFlag | kColorAttributeIsWide_GPFlag; |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 453 | } |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 454 | if (Coverage::kAttribute_Type == coverage.fType) { |
| 455 | flags |= kCoverageAttribute_GPFlag; |
| 456 | } else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) { |
| 457 | flags |= kCoverageAttribute_GPFlag | kCoverageAttributeTweak_GPFlag; |
| 458 | } |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 459 | flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 460 | |
| 461 | uint8_t inCoverage = coverage.fCoverage; |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 462 | bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type; |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 463 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 464 | return DefaultGeoProc::Make(shaderCaps, |
| 465 | flags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 466 | color.fColor, |
Brian Osman | fa6d865 | 2017-05-31 09:37:27 -0400 | [diff] [blame] | 467 | color.fColorSpaceXform, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 468 | viewMatrix, |
| 469 | localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(), |
| 470 | localCoordsWillBeRead, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 471 | inCoverage, |
| 472 | nullptr, |
| 473 | 0); |
joshualitt | e9d6095 | 2015-07-27 12:13:14 -0700 | [diff] [blame] | 474 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 475 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 476 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::MakeForDeviceSpace( |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 477 | const GrShaderCaps* shaderCaps, |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 478 | const Color& color, |
| 479 | const Coverage& coverage, |
| 480 | const LocalCoords& localCoords, |
| 481 | const SkMatrix& viewMatrix) { |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 482 | SkMatrix invert = SkMatrix::I(); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 483 | if (LocalCoords::kUnused_Type != localCoords.fType) { |
| 484 | SkASSERT(LocalCoords::kUsePosition_Type == localCoords.fType); |
| 485 | if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 486 | return nullptr; |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 487 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 488 | |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 489 | if (localCoords.hasLocalMatrix()) { |
Michael Ludwig | ef77604 | 2018-11-01 11:07:51 -0400 | [diff] [blame] | 490 | invert.postConcat(*localCoords.fMatrix); |
joshualitt | df0c557 | 2015-08-03 11:35:28 -0700 | [diff] [blame] | 491 | } |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | LocalCoords inverted(LocalCoords::kUsePosition_Type, &invert); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 495 | return Make(shaderCaps, color, coverage, inverted, SkMatrix::I()); |
joshualitt | 0d986d8 | 2015-07-28 10:01:18 -0700 | [diff] [blame] | 496 | } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 497 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 498 | sk_sp<GrGeometryProcessor> GrDefaultGeoProcFactory::MakeWithBones(const GrShaderCaps* shaderCaps, |
| 499 | const Color& color, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 500 | const Coverage& coverage, |
| 501 | const LocalCoords& localCoords, |
| 502 | const Bones& bones, |
| 503 | const SkMatrix& viewMatrix) { |
| 504 | uint32_t flags = 0; |
| 505 | if (Color::kPremulGrColorAttribute_Type == color.fType) { |
| 506 | flags |= kColorAttribute_GPFlag; |
| 507 | } else if (Color::kUnpremulSkColorAttribute_Type == color.fType) { |
| 508 | flags |= kColorAttribute_GPFlag | kColorAttributeIsSkColor_GPFlag; |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 509 | } else if (Color::kPremulWideColorAttribute_Type == color.fType) { |
| 510 | flags |= kColorAttribute_GPFlag | kColorAttributeIsWide_GPFlag; |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 511 | } |
Brian Osman | 80879d4 | 2019-01-07 16:15:27 -0500 | [diff] [blame] | 512 | if (Coverage::kAttribute_Type == coverage.fType) { |
| 513 | flags |= kCoverageAttribute_GPFlag; |
| 514 | } else if (Coverage::kAttributeTweakAlpha_Type == coverage.fType) { |
| 515 | flags |= kCoverageAttribute_GPFlag | kCoverageAttributeTweak_GPFlag; |
| 516 | } |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 517 | flags |= localCoords.fType == LocalCoords::kHasExplicit_Type ? kLocalCoordAttribute_GPFlag : 0; |
| 518 | flags |= kBonesAttribute_GPFlag; |
| 519 | |
| 520 | uint8_t inCoverage = coverage.fCoverage; |
| 521 | bool localCoordsWillBeRead = localCoords.fType != LocalCoords::kUnused_Type; |
| 522 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 523 | return DefaultGeoProc::Make(shaderCaps, |
| 524 | flags, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 525 | color.fColor, |
Ruiqi Mao | 4ec72f7 | 2018-07-10 17:21:07 -0400 | [diff] [blame] | 526 | color.fColorSpaceXform, |
| 527 | viewMatrix, |
| 528 | localCoords.fMatrix ? *localCoords.fMatrix : SkMatrix::I(), |
| 529 | localCoordsWillBeRead, |
| 530 | inCoverage, |
| 531 | bones.fBones, |
| 532 | bones.fBoneCount); |
| 533 | } |