Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 Google LLC. |
| 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 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 8 | #ifndef AtlasInstancedHelper_DEFINED |
| 9 | #define AtlasInstancedHelper_DEFINED |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 10 | |
| 11 | #include "src/core/SkIPoint16.h" |
| 12 | #include "src/gpu/GrGeometryProcessor.h" |
| 13 | #include "src/gpu/GrSurfaceProxyView.h" |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 14 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 15 | |
| 16 | struct GrVertexWriter; |
| 17 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 18 | namespace skgpu::v1 { |
| 19 | |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 20 | // This class encapsulates all the necessary steps for an instanced GrGeometryProcessor to clip |
| 21 | // against a path mask from an atlas. |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 22 | class AtlasInstancedHelper { |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 23 | public: |
| 24 | enum class ShaderFlags { |
| 25 | kNone = 0, |
| 26 | kInvertCoverage = 1 << 0, |
| 27 | kCheckBounds = 1 << 1 |
| 28 | }; |
| 29 | |
| 30 | GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(ShaderFlags); |
| 31 | |
| 32 | constexpr static int kNumShaderFlags = 2; |
| 33 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 34 | AtlasInstancedHelper(GrSurfaceProxyView atlasView, ShaderFlags shaderFlags) |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 35 | : fAtlasProxy(atlasView.detachProxy()) |
| 36 | , fAtlasSwizzle(atlasView.swizzle()) |
| 37 | , fShaderFlags(shaderFlags) { |
| 38 | // Bottom left origin is not supported. |
| 39 | SkASSERT(atlasView.origin() == kTopLeft_GrSurfaceOrigin); |
| 40 | } |
| 41 | |
| 42 | GrSurfaceProxy* proxy() const { return fAtlasProxy.get(); } |
| 43 | const GrSwizzle& atlasSwizzle() const { return fAtlasSwizzle; } |
| 44 | |
| 45 | // Returns whether the two helpers can be batched together in a single draw. |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 46 | bool isCompatible(const AtlasInstancedHelper& helper) { |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 47 | // TODO: We may want to consider two helpers compatible if they only differ in the |
| 48 | // kCheckBounds flag -- we can always promote one to checking its bounds. |
| 49 | SkASSERT(fAtlasProxy != helper.fAtlasProxy || fAtlasSwizzle == helper.fAtlasSwizzle); |
| 50 | return fAtlasProxy == helper.fAtlasProxy && fShaderFlags == helper.fShaderFlags; |
| 51 | } |
| 52 | |
| 53 | // Adds bits to the shader key that uniquely identify this specific helper's shader code. |
| 54 | void getKeyBits(GrProcessorKeyBuilder* b) const { |
| 55 | b->addBits(kNumShaderFlags, (int)fShaderFlags, "atlasFlags"); |
| 56 | } |
| 57 | |
| 58 | // Appends the instanced input attribs to the back of the array that we will need in order to |
| 59 | // locate our path in the atlas. |
| 60 | void appendInstanceAttribs(SkTArray<GrGeometryProcessor::Attribute>* instanceAttribs) const; |
| 61 | |
| 62 | struct Instance { |
| 63 | Instance(SkIPoint16 locationInAtlas, const SkIRect& pathDevIBounds, bool transposedInAtlas) |
| 64 | : fLocationInAtlas(locationInAtlas) |
| 65 | , fPathDevIBounds(pathDevIBounds) |
| 66 | , fTransposedInAtlas(transposedInAtlas) { |
| 67 | SkASSERT(fLocationInAtlas.x() >= 0); |
| 68 | SkASSERT(fLocationInAtlas.y() >= 0); |
| 69 | } |
| 70 | SkIPoint16 fLocationInAtlas; |
| 71 | SkIRect fPathDevIBounds; |
| 72 | bool fTransposedInAtlas; |
| 73 | }; |
| 74 | |
| 75 | // Writes out the given instance data, formatted for the specific attribs that we added during |
| 76 | // appendInstanceAttribs(). |
| 77 | void writeInstanceData(GrVertexWriter* instanceWriter, const Instance*) const; |
| 78 | |
| 79 | // Injects vertex code, fragment code, varyings, and uniforms to ultimately multiply |
| 80 | // "args.fOutputCoverage" in the fragment shader by the atlas coverage. |
| 81 | // |
| 82 | // The caller is responsible to store "atlasAdjustUniformHandle" and pass it to |
| 83 | // setUniformData(). |
Brian Salomon | f95940b | 2021-08-09 15:56:24 -0400 | [diff] [blame] | 84 | void injectShaderCode(const GrGeometryProcessor::ProgramImpl::EmitArgs&, |
| 85 | const GrShaderVar& devCoord, |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 86 | GrGLSLUniformHandler::UniformHandle* atlasAdjustUniformHandle) const; |
| 87 | |
| 88 | // The atlas clip requires one uniform value -- "atlasAdjustUniform". The caller should have |
| 89 | // stored this handle after its call to injectShaderCode(). This method sets its value prior to |
| 90 | // drawing. |
| 91 | void setUniformData(const GrGLSLProgramDataManager&, |
| 92 | const GrGLSLUniformHandler::UniformHandle& atlasAdjustUniformHandle) const; |
| 93 | |
| 94 | private: |
| 95 | const sk_sp<GrSurfaceProxy> fAtlasProxy; |
| 96 | const GrSwizzle fAtlasSwizzle; |
| 97 | const ShaderFlags fShaderFlags; |
| 98 | }; |
| 99 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 100 | GR_MAKE_BITFIELD_CLASS_OPS(AtlasInstancedHelper::ShaderFlags); |
Chris Dalton | b1fd64e | 2021-07-08 15:38:51 -0600 | [diff] [blame] | 101 | |
Robert Phillips | ef80d7b | 2021-09-14 16:10:56 -0400 | [diff] [blame^] | 102 | } // namespace skgpu::v1 |
| 103 | |
| 104 | #endif // AtlasInstancedHelper_DEFINED |