1 // 2 // Copyright 2023 The ANGLE Project Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 // 6 // blocklayoutmetal.h: 7 // Methods and classes related to uniform layout and packing in Metal 8 // 9 10 #ifndef COMMON_BLOCKLAYOUT_METAL__H_ 11 #define COMMON_BLOCKLAYOUT_METAL__H_ 12 13 #include <cstddef> 14 #include <map> 15 #include <vector> 16 17 #include <GLSLANG/ShaderLang.h> 18 #include "angle_gl.h" 19 #include "compiler/translator/blocklayout.h" 20 21 namespace rx 22 { 23 24 namespace mtl 25 { 26 27 size_t GetMetalSizeForGLType(GLenum type); 28 size_t GetMetalAlignmentForGLType(GLenum type); 29 30 size_t GetMTLBaseAlignment(GLenum variableType, bool isRowMajor); 31 32 class MetalAlignmentVisitor : public sh::ShaderVariableVisitor 33 { 34 public: 35 MetalAlignmentVisitor() = default; 36 void visitVariable(const sh::ShaderVariable &variable, bool isRowMajor) override; 37 // This is in bytes rather than components. getBaseAlignment()38 size_t getBaseAlignment() const { return mCurrentAlignment; } 39 40 private: 41 size_t mCurrentAlignment = 0; 42 }; 43 44 class BlockLayoutEncoderMTL : public sh::BlockLayoutEncoder 45 { 46 public: 47 BlockLayoutEncoderMTL(); ~BlockLayoutEncoderMTL()48 ~BlockLayoutEncoderMTL() override {} 49 50 sh::BlockMemberInfo encodeType(GLenum type, 51 const std::vector<unsigned int> &arraySizes, 52 bool isRowMajorMatrix) override; 53 // Advance the offset based on struct size and array dimensions. Size can be calculated with 54 // getShaderVariableSize() or equivalent. |enterAggregateType|/|exitAggregateType| is necessary 55 // around this call. 56 sh::BlockMemberInfo encodeArrayOfPreEncodedStructs( 57 size_t size, 58 const std::vector<unsigned int> &arraySizes) override; 59 60 size_t getCurrentOffset() const override; 61 size_t getShaderVariableSize(const sh::ShaderVariable &structVar, bool isRowMajor) override; 62 63 // Called when entering/exiting a structure variable. 64 void enterAggregateType(const sh::ShaderVariable &structVar) override; 65 void exitAggregateType(const sh::ShaderVariable &structVar) override; 66 67 private: 68 void getBlockLayoutInfo(GLenum type, 69 const std::vector<unsigned int> &arraySizes, 70 bool isRowMajorMatrix, 71 int *arrayStrideOut, 72 int *matrixStrideOut) override; 73 void advanceOffset(GLenum type, 74 const std::vector<unsigned int> &arraySizes, 75 bool isRowMajorMatrix, 76 int arrayStride, 77 int matrixStride) override; 78 79 size_t getBaseAlignment(const sh::ShaderVariable &variable) const; 80 size_t getTypeBaseAlignment(GLenum type, bool isRowMajorMatrix) const; 81 }; 82 83 } // namespace mtl 84 85 } // namespace rx 86 87 #endif // COMMON_BLOCKLAYOUT_METAL_H_ 88