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_ContextUtils_DEFINED 9 #define skgpu_ContextUtils_DEFINED 10 11 #include "experimental/graphite/include/Context.h" 12 #include "include/core/SkBlendMode.h" 13 #include "include/core/SkRefCnt.h" 14 #include "include/core/SkTileMode.h" 15 16 class SkPaint; 17 18 namespace skgpu { 19 20 class Uniform; 21 class UniformCache; 22 23 // A single, fully specified combination resulting from a PaintCombo (i.e., it corresponds to a 24 // specific SkPaint) 25 struct Combination { 26 bool operator==(const Combination& other) const { 27 return fShaderType == other.fShaderType && 28 fTileMode == other.fTileMode && 29 fBlendMode == other.fBlendMode; 30 } 31 32 ShaderCombo::ShaderType fShaderType = ShaderCombo::ShaderType::kNone; 33 SkTileMode fTileMode = SkTileMode::kRepeat; 34 SkBlendMode fBlendMode = SkBlendMode::kSrc; 35 }; 36 37 class UniformData : public SkRefCnt { 38 public: 39 static constexpr uint32_t kInvalidUniformID = 0; 40 41 // TODO: should we require a name (e.g., "gradient_uniforms") for each uniform block so 42 // we can better name the Metal FS uniform struct? 43 static sk_sp<UniformData> Make(int count, 44 const Uniform* uniforms, 45 size_t dataSize); 46 ~UniformData()47 ~UniformData() override { 48 // TODO: fOffsets and fData should just be allocated right after UniformData in an arena 49 delete [] fOffsets; 50 delete [] fData; 51 } 52 setID(uint32_t id)53 void setID(uint32_t id) { // TODO: maybe make privileged for only UniformCache 54 SkASSERT(fID == kInvalidUniformID); 55 fID = id; 56 } id()57 uint32_t id() const { return fID; } count()58 int count() const { return fCount; } uniforms()59 const Uniform* uniforms() const { return fUniforms; } offsets()60 uint32_t* offsets() { return fOffsets; } offset(int index)61 uint32_t offset(int index) { 62 SkASSERT(index >= 0 && index < fCount); 63 return fOffsets[index]; 64 } data()65 char* data() { return fData; } dataSize()66 size_t dataSize() const { return fDataSize; } 67 68 private: UniformData(int count,const Uniform * uniforms,uint32_t * offsets,char * data,size_t dataSize)69 UniformData(int count, 70 const Uniform* uniforms, 71 uint32_t* offsets, 72 char* data, 73 size_t dataSize) 74 : fCount(count) 75 , fUniforms(uniforms) 76 , fOffsets(offsets) 77 , fData(data) 78 , fDataSize(dataSize) { 79 } 80 81 uint32_t fID = kInvalidUniformID; 82 const int fCount; 83 const Uniform* fUniforms; 84 uint32_t* fOffsets; // offset of each uniform in 'fData' 85 char* fData; 86 const size_t fDataSize; 87 }; 88 89 std::tuple<Combination, sk_sp<UniformData>> ExtractCombo(UniformCache*, const SkPaint&); 90 std::string GetMSLUniformStruct(ShaderCombo::ShaderType); 91 92 } // namespace skgpu 93 94 #endif // skgpu_ContextUtils_DEFINED 95