| 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 |
#ifndef skgpu_graphite_GraphicsPipeline_DEFINED |
| 9 |
#define skgpu_graphite_GraphicsPipeline_DEFINED |
| 10 |
|
| 11 |
#include "src/gpu/graphite/Caps.h" |
| 12 |
#include "src/gpu/graphite/Resource.h" |
| 13 |
#include "src/gpu/graphite/UniquePaintParamsID.h" |
| 14 |
|
| 15 |
namespace skgpu::graphite { |
| 16 |
|
| 17 |
class ShaderInfo; |
| 18 |
class RenderStep; |
| 19 |
|
| 20 |
enum class PipelineCreationFlags : uint8_t { |
| 21 |
kNone = 0b000, |
| 22 |
// For Dawn, this flag overrides the DawnCaps::fUseAsyncPipelineCreation |
| 23 |
// parameter and forces Synchronous Pipeline creation. |
| 24 |
kForPrecompilation = 0b001, |
| 25 |
}; |
| 26 |
|
| 27 |
/** |
| 28 |
* GraphicsPipeline corresponds to a backend specific pipeline used for rendering (vs. compute), |
| 29 |
* e.g. MTLRenderPipelineState (Metal), |
| 30 |
* CreateRenderPipeline (Dawn), |
| 31 |
* CreateGraphicsPipelineState (D3D12), |
| 32 |
* or VkGraphicsPipelineCreateInfo (Vulkan). |
| 33 |
* |
| 34 |
* A GraphicsPipeline is created from the combination of a GraphicsPipelineDesc (representing draw |
| 35 |
* specific configuration) and a RenderPassDesc (representing the target of the draw). |
| 36 |
*/ |
| 37 |
class GraphicsPipeline : public Resource { |
| 38 |
public: |
| 39 |
~GraphicsPipeline() override; |
| 40 |
|
| 41 |
const char* getResourceType() const override { return "Graphics Pipeline"; } |
| 42 |
|
| 43 |
DstReadStrategy dstReadStrategy() const { return fPipelineInfo.fDstReadStrategy; } |
| 44 |
|
| 45 |
int numFragTexturesAndSamplers() const { return fPipelineInfo.fNumFragTexturesAndSamplers; } |
| 46 |
bool hasCombinedUniforms() const { return fPipelineInfo.fHasCombinedUniforms; } |
| 47 |
bool hasGradientBuffer() const { return fPipelineInfo.fHasGradientBuffer; } |
| 48 |
|
| 49 |
struct PipelineInfo { |
| 50 |
PipelineInfo() = default; |
| 51 |
|
| 52 |
// NOTE: Subclasses must manually fill in native shader code in GPU_TEST_UTILS builds. |
| 53 |
PipelineInfo(const ShaderInfo&, SkEnumBitMask<PipelineCreationFlags>, |
| 54 |
uint32_t uniqueKeyHash, uint32_t compilationID); |
| 55 |
|
| 56 |
DstReadStrategy fDstReadStrategy = DstReadStrategy::kNoneRequired; |
| 57 |
int fNumFragTexturesAndSamplers = 0; |
| 58 |
bool fHasCombinedUniforms = false; |
| 59 |
bool fHasGradientBuffer = false; |
| 60 |
|
| 61 |
// In test-enabled builds, we preserve the generated shader code to display in the viewer |
| 62 |
// slide UI. This is not quite enough information to fully recreate the pipeline, as the |
| 63 |
// RenderPassDesc used to make the pipeline is not preserved. |
| 64 |
#if defined(GPU_TEST_UTILS) |
| 65 |
std::string fSkSLVertexShader; |
| 66 |
std::string fSkSLFragmentShader; |
| 67 |
std::string fNativeVertexShader; |
| 68 |
std::string fNativeFragmentShader; |
| 69 |
#endif |
| 70 |
const uint32_t fUniqueKeyHash = 0; |
| 71 |
// The compilation ID is used to distinguish between different compilations/instantiations |
| 72 |
// of the same unique key. If, for example, two versions were created due to threading. |
| 73 |
const uint32_t fCompilationID = 0; |
| 74 |
const bool fFromPrecompile = false; |
| 75 |
bool fWasUsed = false; |
| 76 |
uint16_t fEpoch = 0; // the last epoch in which this Pipeline was touched |
| 77 |
}; |
| 78 |
|
| 79 |
const PipelineInfo& getPipelineInfo() const { return fPipelineInfo; } |
| 80 |
bool fromPrecompile() const { return fPipelineInfo.fFromPrecompile; } |
| 81 |
|
| 82 |
void markUsed() { fPipelineInfo.fWasUsed = true; } |
| 83 |
bool wasUsed() const { return fPipelineInfo.fWasUsed; } |
| 84 |
|
| 85 |
void markEpoch(uint16_t epoch) { fPipelineInfo.fEpoch = epoch; } |
| 86 |
uint16_t epoch() const { return fPipelineInfo.fEpoch; } |
| 87 |
|
| 88 |
// GraphicsPipeline compiles can take a while. If the underlying compilation is performed |
| 89 |
// asynchronously, we may create a GraphicsPipeline object that later "fails" and need to remove |
| 90 |
// it from the GlobalCache. |
| 91 |
virtual bool didAsyncCompilationFail() const { return false; } |
| 92 |
|
| 93 |
protected: |
| 94 |
// GraphicsPipeline labels are often provided to the description of what needs to be compiled, |
| 95 |
// so it is required before the actual pipeline has been successfully created. Instead of adding |
| 96 |
// it to PipelineInfo, just use Resource's label field. |
| 97 |
GraphicsPipeline(const SharedContext*, const PipelineInfo&, std::string_view label); |
| 98 |
|
| 99 |
private: |
| 100 |
PipelineInfo fPipelineInfo; |
| 101 |
}; |
| 102 |
|
| 103 |
} // namespace skgpu::graphite |
| 104 |
|
| 105 |
#endif // skgpu_graphite_GraphicsPipeline_DEFINED |