| 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 |
|
| 8 |
#include "src/gpu/graphite/ContextUtils.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkM44.h" |
| 12 |
#include "include/core/SkRect.h" |
| 13 |
#include "include/core/SkString.h" |
| 14 |
#include "include/gpu/GpuTypes.h" |
| 15 |
#include "include/gpu/graphite/Recorder.h" |
| 16 |
#include "include/private/base/SkAssert.h" |
| 17 |
#include "include/private/base/SkDebug.h" |
| 18 |
#include "include/private/base/SkSpan_impl.h" |
| 19 |
#include "src/base/SkEnumBitMask.h" |
| 20 |
#include "src/gpu/BlendFormula.h" |
| 21 |
#include "src/gpu/graphite/Caps.h" |
| 22 |
#include "src/gpu/graphite/ComputeTypes.h" |
| 23 |
#include "src/gpu/graphite/KeyContext.h" |
| 24 |
#include "src/gpu/graphite/PaintParams.h" |
| 25 |
#include "src/gpu/graphite/PaintParamsKey.h" |
| 26 |
#include "src/gpu/graphite/PipelineData.h" |
| 27 |
#include "src/gpu/graphite/RecorderPriv.h" |
| 28 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 29 |
#include "src/gpu/graphite/Renderer.h" |
| 30 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 31 |
#include "src/gpu/graphite/ShaderCodeDictionary.h" |
| 32 |
#include "src/gpu/graphite/TextureFormat.h" |
| 33 |
#include "src/gpu/graphite/UniformManager.h" |
| 34 |
#include "src/gpu/graphite/UniquePaintParamsID.h" |
| 35 |
#include "src/gpu/graphite/compute/ComputeStep.h" |
| 36 |
#include "src/gpu/graphite/geom/Geometry.h" |
| 37 |
#include "src/sksl/SkSLString.h" |
| 38 |
#include "src/sksl/SkSLUtil.h" |
| 39 |
|
| 40 |
#include <string> |
| 41 |
|
| 42 |
namespace skgpu::graphite { |
| 43 |
|
| 44 |
bool CanUseHardwareBlending(const Caps* caps, |
| 45 |
TextureFormat targetFormat, |
| 46 |
SkBlendMode bm, |
| 47 |
Coverage coverage) { |
| 48 |
// Check for special cases that would prevent the usage of direct hardware blending and |
| 49 |
// require us to fall back to using shader-based blending. |
| 50 |
const bool hasCoverage = coverage != Coverage::kNone; |
| 51 |
const bool dstIsFast = caps->getDstReadStrategy() != DstReadStrategy::kTextureCopy; |
| 52 |
if (// Using LCD coverage (which must be applied after the blend equation) with any blend mode |
| 53 |
// besides SkBlendMode::kSrcOver |
| 54 |
// TODO(b/414597217): Add support to use dual-source blending with LCD coverage. |
| 55 |
(coverage == Coverage::kLCD && bm != SkBlendMode::kSrcOver) || |
| 56 |
|
| 57 |
// SkBlendMode::kPlus clamps its output to [0,1], e.g. clamp(D+S,0,1), which is then |
| 58 |
// combined with coverage (f) for a final written value of: |
| 59 |
// (1-f)*D + f*clamp(D+S,0,1) |
| 60 |
// |
| 61 |
// This can be rewritten to min(D+f*S, D+f*(1-D)), which is not representable with *any* |
| 62 |
// hardware blend configuration. However, when the target format clamps to [0,1], we can |
| 63 |
// approximate the output as min(D+f*S, 1) with a slight degradation in AA quality. |
| 64 |
// |
| 65 |
// If access to D doesn't require a texture copy, prefer shader blending for the quality. |
| 66 |
(bm == SkBlendMode::kPlus && (dstIsFast || !TextureFormatAutoClamps(targetFormat))) || |
| 67 |
|
| 68 |
// Using an advanced blend mode but the hardware does not support them |
| 69 |
(bm > SkBlendMode::kLastCoeffMode && !caps->supportsHardwareAdvancedBlending()) || |
| 70 |
|
| 71 |
// The blend formula requires dual-source blending, but it is not supported by hardware |
| 72 |
(bm <= SkBlendMode::kLastCoeffMode && |
| 73 |
(coverage == Coverage::kLCD ? skgpu::GetLCDBlendFormula(bm).hasSecondaryOutput() |
| 74 |
: skgpu::GetBlendFormula(/*isOpaque=*/false, |
| 75 |
hasCoverage, |
| 76 |
bm).hasSecondaryOutput()) && |
| 77 |
!caps->shaderCaps()->fDualSourceBlendingSupport)) { |
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
// In all other cases (which are more commonly encountered; e.g. using a simple blend mode), |
| 82 |
// we can use direct HW blending. |
| 83 |
return true; |
| 84 |
} |
| 85 |
|
| 86 |
void CollectIntrinsicUniforms(const Caps* caps, |
| 87 |
SkIRect viewport, |
| 88 |
SkIRect dstReadBounds, |
| 89 |
UniformManager* uniforms) { |
| 90 |
SkDEBUGCODE(uniforms->setExpectedUniforms(kIntrinsicUniforms, /*isSubstruct=*/false);) |
| 91 |
|
| 92 |
// viewport |
| 93 |
{ |
| 94 |
// The vertex shader needs to divide by the dimension and then multiply by 2, so do this |
| 95 |
// once on the CPU. This is because viewport normalization wants to range from -1 to 1, and |
| 96 |
// not 0 to 1. If any other user of the viewport uniform requires the true reciprocal or |
| 97 |
// original dimensions, this can be adjusted. |
| 98 |
SkASSERT(!viewport.isEmpty()); |
| 99 |
float invTwoW = 2.f / viewport.width(); |
| 100 |
float invTwoH = 2.f / viewport.height(); |
| 101 |
|
| 102 |
// If the NDC Y axis points up (opposite normal skia convention and the underlying view |
| 103 |
// convention), upload the inverse height as a negative value. See ShaderInfo::Make |
| 104 |
// for how this is used. |
| 105 |
if (!caps->ndcYAxisPointsDown()) { |
| 106 |
invTwoH *= -1.f; |
| 107 |
} |
| 108 |
uniforms->write(SkV4{(float) viewport.left(), (float) viewport.top(), invTwoW, invTwoH}); |
| 109 |
} |
| 110 |
|
| 111 |
// dstReadBounds |
| 112 |
{ |
| 113 |
// Unlike viewport, dstReadBounds can be empty so check for 0 dimensions and set the |
| 114 |
// reciprocal to 0. It is also not doubled since its purpose is to normalize texture coords |
| 115 |
// to 0 to 1, and not -1 to 1. |
| 116 |
int width = dstReadBounds.width(); |
| 117 |
int height = dstReadBounds.height(); |
| 118 |
uniforms->write(SkV4{(float) dstReadBounds.left(), (float) dstReadBounds.top(), |
| 119 |
width ? 1.f / width : 0.f, height ? 1.f / height : 0.f}); |
| 120 |
} |
| 121 |
|
| 122 |
SkDEBUGCODE(uniforms->doneWithExpectedUniforms()); |
| 123 |
} |
| 124 |
|
| 125 |
std::string EmitSamplerLayout(const ResourceBindingRequirements& bindingReqs, int* binding) { |
| 126 |
std::string result; |
| 127 |
|
| 128 |
if (bindingReqs.fSeparateTextureAndSamplerBinding) { |
| 129 |
int samplerIndex = (*binding)++; |
| 130 |
int textureIndex = (*binding)++; |
| 131 |
result = SkSL::String::printf("layout(webgpu, set=%d, sampler=%d, texture=%d)", |
| 132 |
bindingReqs.fTextureSamplerSetIdx, |
| 133 |
samplerIndex, |
| 134 |
textureIndex); |
| 135 |
} else { |
| 136 |
int samplerIndex = (*binding)++; |
| 137 |
result = SkSL::String::printf("layout(set=%d, binding=%d)", |
| 138 |
bindingReqs.fTextureSamplerSetIdx, |
| 139 |
samplerIndex); |
| 140 |
} |
| 141 |
return result; |
| 142 |
} |
| 143 |
|
| 144 |
std::string GetPipelineLabel(const Caps* caps, |
| 145 |
const ShaderCodeDictionary* dict, |
| 146 |
const RenderPassDesc& renderPassDesc, |
| 147 |
const RenderStep* renderStep, |
| 148 |
UniquePaintParamsID paintID) { |
| 149 |
// KEEP IN SYNC with ShaderInfo::pipelineLabel() |
| 150 |
std::string label = renderPassDesc.toPipelineLabel().c_str(); // includes the write swizzle |
| 151 |
label += " + "; |
| 152 |
label += renderStep->name(); |
| 153 |
label += " + "; |
| 154 |
// the shader portion will be "(empty)" for depth-only draws |
| 155 |
label += dict->idToString(caps, paintID).c_str(); |
| 156 |
return label; |
| 157 |
} |
| 158 |
|
| 159 |
std::string BuildComputeSkSL(const Caps* caps, const ComputeStep* step, BackendApi backend) { |
| 160 |
std::string sksl = |
| 161 |
SkSL::String::printf("layout(local_size_x=%u, local_size_y=%u, local_size_z=%u) in;\n", |
| 162 |
step->localDispatchSize().fWidth, |
| 163 |
step->localDispatchSize().fHeight, |
| 164 |
step->localDispatchSize().fDepth); |
| 165 |
|
| 166 |
const auto& bindingReqs = caps->resourceBindingRequirements(); |
| 167 |
const bool texturesUseDistinctIdxRanges = bindingReqs.fComputeUsesDistinctIdxRangesForTextures; |
| 168 |
int index = 0; |
| 169 |
// NOTE: SkSL Metal codegen always assigns the same binding index to a texture and its sampler. |
| 170 |
// TODO: This could cause sampler indices to not be tightly packed if the sampler2D declaration |
| 171 |
// comes after 1 or more storage texture declarations (which don't have samplers). An optional |
| 172 |
// "layout(msl, sampler=T, texture=T)" syntax to count them separately (like we do for WGSL) |
| 173 |
// could come in handy here but it's not supported in MSL codegen yet. |
| 174 |
int texIdx = 0; |
| 175 |
for (const ComputeStep::ResourceDesc& r : step->resources()) { |
| 176 |
using Type = ComputeStep::ResourceType; |
| 177 |
switch (r.fType) { |
| 178 |
case Type::kUniformBuffer: |
| 179 |
SkSL::String::appendf(&sksl, "layout(binding=%d) uniform ", index++); |
| 180 |
sksl += r.fSkSL; |
| 181 |
break; |
| 182 |
case Type::kStorageBuffer: |
| 183 |
case Type::kIndirectBuffer: |
| 184 |
SkSL::String::appendf(&sksl, "layout(binding=%d) buffer ", index++); |
| 185 |
sksl += r.fSkSL; |
| 186 |
break; |
| 187 |
case Type::kReadOnlyStorageBuffer: |
| 188 |
SkSL::String::appendf(&sksl, "layout(binding=%d) readonly buffer ", index++); |
| 189 |
sksl += r.fSkSL; |
| 190 |
break; |
| 191 |
case Type::kWriteOnlyStorageTexture: |
| 192 |
SkSL::String::appendf(&sksl, "layout(binding=%d, rgba8) writeonly texture2D ", |
| 193 |
texturesUseDistinctIdxRanges ? texIdx++ : index++); |
| 194 |
sksl += r.fSkSL; |
| 195 |
break; |
| 196 |
case Type::kReadOnlyTexture: |
| 197 |
SkSL::String::appendf(&sksl, "layout(binding=%d, rgba8) readonly texture2D ", |
| 198 |
texturesUseDistinctIdxRanges ? texIdx++ : index++); |
| 199 |
sksl += r.fSkSL; |
| 200 |
break; |
| 201 |
case Type::kSampledTexture: |
| 202 |
// The following SkSL expects specific backends to have certain resource binding |
| 203 |
// requirements. Before appending the SkSL, assert that these assumptions hold true. |
| 204 |
// TODO(b/396420770): Have this method be more backend-agnostic. |
| 205 |
if (backend == BackendApi::kMetal) { |
| 206 |
// Metal is expected to use combined texture/samplers. |
| 207 |
SkASSERT(!bindingReqs.fSeparateTextureAndSamplerBinding); |
| 208 |
SkSL::String::appendf(&sksl, |
| 209 |
"layout(metal, binding=%d) ", |
| 210 |
texturesUseDistinctIdxRanges ? texIdx++ : index++); |
| 211 |
} else if (backend == BackendApi::kDawn) { |
| 212 |
// Dawn is expected to use separate texture/samplers and not use distinct |
| 213 |
// index ranges for texture resources. |
| 214 |
SkASSERT(bindingReqs.fSeparateTextureAndSamplerBinding && |
| 215 |
!texturesUseDistinctIdxRanges); |
| 216 |
SkSL::String::appendf( |
| 217 |
&sksl, "layout(webgpu, sampler=%d, texture=%d) ", index, index + 1); |
| 218 |
index += 2; |
| 219 |
} else { |
| 220 |
// This SkSL depends upon the assumption that we are using combined texture/ |
| 221 |
// samplers and that we are not using separate resource indices for textures. |
| 222 |
SkASSERT(!bindingReqs.fSeparateTextureAndSamplerBinding && |
| 223 |
!texturesUseDistinctIdxRanges); |
| 224 |
SkSL::String::appendf(&sksl, "layout(binding=%d) ", index++); |
| 225 |
} |
| 226 |
sksl += "sampler2D "; |
| 227 |
sksl += r.fSkSL; |
| 228 |
break; |
| 229 |
} |
| 230 |
sksl += ";\n"; |
| 231 |
} |
| 232 |
|
| 233 |
sksl += step->computeSkSL(); |
| 234 |
return sksl; |
| 235 |
} |
| 236 |
|
| 237 |
} // namespace skgpu::graphite |