1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2023 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker // blocklayoutMetal.cpp:
7*8975f5c5SAndroid Build Coastguard Worker // Implementation for metal block layout classes and methods.
8*8975f5c5SAndroid Build Coastguard Worker //
9*8975f5c5SAndroid Build Coastguard Worker
10*8975f5c5SAndroid Build Coastguard Worker #include "libANGLE/renderer/metal/blocklayoutMetal.h"
11*8975f5c5SAndroid Build Coastguard Worker #include "common/mathutil.h"
12*8975f5c5SAndroid Build Coastguard Worker #include "common/utilities.h"
13*8975f5c5SAndroid Build Coastguard Worker #include "compiler/translator/blocklayout.h"
14*8975f5c5SAndroid Build Coastguard Worker namespace rx
15*8975f5c5SAndroid Build Coastguard Worker {
16*8975f5c5SAndroid Build Coastguard Worker namespace mtl
17*8975f5c5SAndroid Build Coastguard Worker {
18*8975f5c5SAndroid Build Coastguard Worker // Sizes and types are available at
19*8975f5c5SAndroid Build Coastguard Worker // https://developer.apple.com/metal/Metal-Shading-Language-Specification.pdf
20*8975f5c5SAndroid Build Coastguard Worker // Section 2
GetMetalSizeForGLType(GLenum type)21*8975f5c5SAndroid Build Coastguard Worker size_t GetMetalSizeForGLType(GLenum type)
22*8975f5c5SAndroid Build Coastguard Worker {
23*8975f5c5SAndroid Build Coastguard Worker switch (type)
24*8975f5c5SAndroid Build Coastguard Worker {
25*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL:
26*8975f5c5SAndroid Build Coastguard Worker return 1;
27*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL_VEC2:
28*8975f5c5SAndroid Build Coastguard Worker return 2;
29*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL_VEC3:
30*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL_VEC4:
31*8975f5c5SAndroid Build Coastguard Worker return 4;
32*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT:
33*8975f5c5SAndroid Build Coastguard Worker return 4;
34*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_VEC2:
35*8975f5c5SAndroid Build Coastguard Worker return 8;
36*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_VEC3:
37*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_VEC4:
38*8975f5c5SAndroid Build Coastguard Worker return 16;
39*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT2: // 2x2
40*8975f5c5SAndroid Build Coastguard Worker return 16;
41*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT3: // 3x4
42*8975f5c5SAndroid Build Coastguard Worker return 48;
43*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT4: // 4x4
44*8975f5c5SAndroid Build Coastguard Worker return 64;
45*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT2x3: // 2x4
46*8975f5c5SAndroid Build Coastguard Worker return 32;
47*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT3x2: // 3x2
48*8975f5c5SAndroid Build Coastguard Worker return 24;
49*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT2x4: // 2x4
50*8975f5c5SAndroid Build Coastguard Worker return 32;
51*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT4x2: // 4x2
52*8975f5c5SAndroid Build Coastguard Worker return 32;
53*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT3x4: // 3x4
54*8975f5c5SAndroid Build Coastguard Worker return 48;
55*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT4x3: // 4x4
56*8975f5c5SAndroid Build Coastguard Worker return 64;
57*8975f5c5SAndroid Build Coastguard Worker case GL_INT:
58*8975f5c5SAndroid Build Coastguard Worker return 4;
59*8975f5c5SAndroid Build Coastguard Worker case GL_INT_VEC2:
60*8975f5c5SAndroid Build Coastguard Worker return 8;
61*8975f5c5SAndroid Build Coastguard Worker case GL_INT_VEC3:
62*8975f5c5SAndroid Build Coastguard Worker case GL_INT_VEC4:
63*8975f5c5SAndroid Build Coastguard Worker return 16;
64*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT:
65*8975f5c5SAndroid Build Coastguard Worker return 4;
66*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_VEC2:
67*8975f5c5SAndroid Build Coastguard Worker return 8;
68*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_VEC3:
69*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_VEC4:
70*8975f5c5SAndroid Build Coastguard Worker return 16;
71*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D:
72*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_RECT_ANGLE:
73*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_3D:
74*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_CUBE:
75*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_CUBE_MAP_ARRAY:
76*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_ARRAY:
77*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_EXTERNAL_OES:
78*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_MULTISAMPLE:
79*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
80*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_BUFFER:
81*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D:
82*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_3D:
83*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_CUBE:
84*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
85*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D_ARRAY:
86*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D_MULTISAMPLE:
87*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
88*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D:
89*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_3D:
90*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_CUBE:
91*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
92*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
93*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
94*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
95*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_SHADOW:
96*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_BUFFER:
97*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_CUBE_SHADOW:
98*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_ARRAY_SHADOW:
99*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_2D:
100*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_2D:
101*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_2D:
102*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_3D:
103*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_3D:
104*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_3D:
105*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_2D_ARRAY:
106*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_2D_ARRAY:
107*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
108*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_CUBE:
109*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_CUBE:
110*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_CUBE:
111*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_CUBE_MAP_ARRAY:
112*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_CUBE_MAP_ARRAY:
113*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY:
114*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_BUFFER:
115*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_BUFFER:
116*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_BUFFER:
117*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_BUFFER:
118*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_ATOMIC_COUNTER:
119*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_VIDEO_IMAGE_WEBGL:
120*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_EXTERNAL_2D_Y2Y_EXT:
121*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
122*8975f5c5SAndroid Build Coastguard Worker break;
123*8975f5c5SAndroid Build Coastguard Worker default:
124*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
125*8975f5c5SAndroid Build Coastguard Worker break;
126*8975f5c5SAndroid Build Coastguard Worker }
127*8975f5c5SAndroid Build Coastguard Worker return 0;
128*8975f5c5SAndroid Build Coastguard Worker }
129*8975f5c5SAndroid Build Coastguard Worker
GetMetalAlignmentForGLType(GLenum type)130*8975f5c5SAndroid Build Coastguard Worker size_t GetMetalAlignmentForGLType(GLenum type)
131*8975f5c5SAndroid Build Coastguard Worker {
132*8975f5c5SAndroid Build Coastguard Worker switch (type)
133*8975f5c5SAndroid Build Coastguard Worker {
134*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL:
135*8975f5c5SAndroid Build Coastguard Worker return 1;
136*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL_VEC2:
137*8975f5c5SAndroid Build Coastguard Worker return 2;
138*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL_VEC3:
139*8975f5c5SAndroid Build Coastguard Worker case GL_BOOL_VEC4:
140*8975f5c5SAndroid Build Coastguard Worker return 4;
141*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT:
142*8975f5c5SAndroid Build Coastguard Worker return 4;
143*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_VEC2:
144*8975f5c5SAndroid Build Coastguard Worker return 8;
145*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_VEC3:
146*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_VEC4:
147*8975f5c5SAndroid Build Coastguard Worker return 16;
148*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT2:
149*8975f5c5SAndroid Build Coastguard Worker return 8;
150*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT3:
151*8975f5c5SAndroid Build Coastguard Worker return 16;
152*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT4:
153*8975f5c5SAndroid Build Coastguard Worker return 16;
154*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT2x3:
155*8975f5c5SAndroid Build Coastguard Worker return 16;
156*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT3x2:
157*8975f5c5SAndroid Build Coastguard Worker return 8;
158*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT2x4:
159*8975f5c5SAndroid Build Coastguard Worker return 16;
160*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT4x2:
161*8975f5c5SAndroid Build Coastguard Worker return 8;
162*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT3x4:
163*8975f5c5SAndroid Build Coastguard Worker return 16;
164*8975f5c5SAndroid Build Coastguard Worker case GL_FLOAT_MAT4x3:
165*8975f5c5SAndroid Build Coastguard Worker return 16;
166*8975f5c5SAndroid Build Coastguard Worker case GL_INT:
167*8975f5c5SAndroid Build Coastguard Worker return 4;
168*8975f5c5SAndroid Build Coastguard Worker case GL_INT_VEC2:
169*8975f5c5SAndroid Build Coastguard Worker return 8;
170*8975f5c5SAndroid Build Coastguard Worker case GL_INT_VEC3:
171*8975f5c5SAndroid Build Coastguard Worker return 16;
172*8975f5c5SAndroid Build Coastguard Worker case GL_INT_VEC4:
173*8975f5c5SAndroid Build Coastguard Worker return 16;
174*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT:
175*8975f5c5SAndroid Build Coastguard Worker return 4;
176*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_VEC2:
177*8975f5c5SAndroid Build Coastguard Worker return 8;
178*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_VEC3:
179*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_VEC4:
180*8975f5c5SAndroid Build Coastguard Worker return 16;
181*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D:
182*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_RECT_ANGLE:
183*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_3D:
184*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_CUBE:
185*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_CUBE_MAP_ARRAY:
186*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_ARRAY:
187*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_EXTERNAL_OES:
188*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_MULTISAMPLE:
189*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_MULTISAMPLE_ARRAY:
190*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_BUFFER:
191*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D:
192*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_3D:
193*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_CUBE:
194*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_CUBE_MAP_ARRAY:
195*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D_ARRAY:
196*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D_MULTISAMPLE:
197*8975f5c5SAndroid Build Coastguard Worker case GL_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
198*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D:
199*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_3D:
200*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_CUBE:
201*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_CUBE_MAP_ARRAY:
202*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D_ARRAY:
203*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE:
204*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_2D_MULTISAMPLE_ARRAY:
205*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_SHADOW:
206*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_BUFFER:
207*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_CUBE_SHADOW:
208*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_2D_ARRAY_SHADOW:
209*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_2D:
210*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_2D:
211*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_2D:
212*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_3D:
213*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_3D:
214*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_3D:
215*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_2D_ARRAY:
216*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_2D_ARRAY:
217*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_2D_ARRAY:
218*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_CUBE:
219*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_CUBE:
220*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_CUBE:
221*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_CUBE_MAP_ARRAY:
222*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_CUBE_MAP_ARRAY:
223*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY:
224*8975f5c5SAndroid Build Coastguard Worker case GL_IMAGE_BUFFER:
225*8975f5c5SAndroid Build Coastguard Worker case GL_INT_IMAGE_BUFFER:
226*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_SAMPLER_BUFFER:
227*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_IMAGE_BUFFER:
228*8975f5c5SAndroid Build Coastguard Worker case GL_UNSIGNED_INT_ATOMIC_COUNTER:
229*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_VIDEO_IMAGE_WEBGL:
230*8975f5c5SAndroid Build Coastguard Worker case GL_SAMPLER_EXTERNAL_2D_Y2Y_EXT:
231*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
232*8975f5c5SAndroid Build Coastguard Worker break;
233*8975f5c5SAndroid Build Coastguard Worker default:
234*8975f5c5SAndroid Build Coastguard Worker UNREACHABLE();
235*8975f5c5SAndroid Build Coastguard Worker break;
236*8975f5c5SAndroid Build Coastguard Worker }
237*8975f5c5SAndroid Build Coastguard Worker return 0;
238*8975f5c5SAndroid Build Coastguard Worker }
239*8975f5c5SAndroid Build Coastguard Worker
GetMTLBaseAlignment(GLenum variableType,bool isRowMajor)240*8975f5c5SAndroid Build Coastguard Worker size_t GetMTLBaseAlignment(GLenum variableType, bool isRowMajor)
241*8975f5c5SAndroid Build Coastguard Worker {
242*8975f5c5SAndroid Build Coastguard Worker return mtl::GetMetalAlignmentForGLType(variableType);
243*8975f5c5SAndroid Build Coastguard Worker }
244*8975f5c5SAndroid Build Coastguard Worker
visitVariable(const sh::ShaderVariable & variable,bool isRowMajor)245*8975f5c5SAndroid Build Coastguard Worker void MetalAlignmentVisitor::visitVariable(const sh::ShaderVariable &variable, bool isRowMajor)
246*8975f5c5SAndroid Build Coastguard Worker {
247*8975f5c5SAndroid Build Coastguard Worker size_t baseAlignment = GetMTLBaseAlignment(variable.type, isRowMajor);
248*8975f5c5SAndroid Build Coastguard Worker mCurrentAlignment = std::max(mCurrentAlignment, baseAlignment);
249*8975f5c5SAndroid Build Coastguard Worker }
250*8975f5c5SAndroid Build Coastguard Worker
BlockLayoutEncoderMTL()251*8975f5c5SAndroid Build Coastguard Worker BlockLayoutEncoderMTL::BlockLayoutEncoderMTL() : BlockLayoutEncoder() {}
252*8975f5c5SAndroid Build Coastguard Worker
getBlockLayoutInfo(GLenum type,const std::vector<unsigned int> & arraySizes,bool isRowMajorMatrix,int * arrayStrideOut,int * matrixStrideOut)253*8975f5c5SAndroid Build Coastguard Worker void BlockLayoutEncoderMTL::getBlockLayoutInfo(GLenum type,
254*8975f5c5SAndroid Build Coastguard Worker const std::vector<unsigned int> &arraySizes,
255*8975f5c5SAndroid Build Coastguard Worker bool isRowMajorMatrix,
256*8975f5c5SAndroid Build Coastguard Worker int *arrayStrideOut,
257*8975f5c5SAndroid Build Coastguard Worker int *matrixStrideOut)
258*8975f5c5SAndroid Build Coastguard Worker {
259*8975f5c5SAndroid Build Coastguard Worker size_t baseAlignment = 0;
260*8975f5c5SAndroid Build Coastguard Worker int matrixStride = 0;
261*8975f5c5SAndroid Build Coastguard Worker int arrayStride = 0;
262*8975f5c5SAndroid Build Coastguard Worker
263*8975f5c5SAndroid Build Coastguard Worker if (gl::IsMatrixType(type))
264*8975f5c5SAndroid Build Coastguard Worker {
265*8975f5c5SAndroid Build Coastguard Worker baseAlignment = static_cast<int>(mtl::GetMetalAlignmentForGLType(type));
266*8975f5c5SAndroid Build Coastguard Worker matrixStride = static_cast<int>(mtl::GetMetalAlignmentForGLType(type));
267*8975f5c5SAndroid Build Coastguard Worker if (!arraySizes.empty())
268*8975f5c5SAndroid Build Coastguard Worker {
269*8975f5c5SAndroid Build Coastguard Worker arrayStride = static_cast<int>(mtl::GetMetalSizeForGLType(type));
270*8975f5c5SAndroid Build Coastguard Worker }
271*8975f5c5SAndroid Build Coastguard Worker }
272*8975f5c5SAndroid Build Coastguard Worker else if (!arraySizes.empty())
273*8975f5c5SAndroid Build Coastguard Worker {
274*8975f5c5SAndroid Build Coastguard Worker baseAlignment = static_cast<int>(mtl::GetMetalAlignmentForGLType(type));
275*8975f5c5SAndroid Build Coastguard Worker arrayStride = static_cast<int>(mtl::GetMetalSizeForGLType(type));
276*8975f5c5SAndroid Build Coastguard Worker }
277*8975f5c5SAndroid Build Coastguard Worker else
278*8975f5c5SAndroid Build Coastguard Worker {
279*8975f5c5SAndroid Build Coastguard Worker baseAlignment = mtl::GetMetalAlignmentForGLType(type);
280*8975f5c5SAndroid Build Coastguard Worker }
281*8975f5c5SAndroid Build Coastguard Worker align(baseAlignment);
282*8975f5c5SAndroid Build Coastguard Worker *matrixStrideOut = matrixStride;
283*8975f5c5SAndroid Build Coastguard Worker *arrayStrideOut = arrayStride;
284*8975f5c5SAndroid Build Coastguard Worker }
encodeType(GLenum type,const std::vector<unsigned int> & arraySizes,bool isRowMajorMatrix)285*8975f5c5SAndroid Build Coastguard Worker sh::BlockMemberInfo BlockLayoutEncoderMTL::encodeType(GLenum type,
286*8975f5c5SAndroid Build Coastguard Worker const std::vector<unsigned int> &arraySizes,
287*8975f5c5SAndroid Build Coastguard Worker bool isRowMajorMatrix)
288*8975f5c5SAndroid Build Coastguard Worker {
289*8975f5c5SAndroid Build Coastguard Worker int arrayStride;
290*8975f5c5SAndroid Build Coastguard Worker int matrixStride;
291*8975f5c5SAndroid Build Coastguard Worker
292*8975f5c5SAndroid Build Coastguard Worker getBlockLayoutInfo(type, arraySizes, isRowMajorMatrix, &arrayStride, &matrixStride);
293*8975f5c5SAndroid Build Coastguard Worker const sh::BlockMemberInfo memberInfo(
294*8975f5c5SAndroid Build Coastguard Worker type, static_cast<int>(mCurrentOffset), static_cast<int>(arrayStride),
295*8975f5c5SAndroid Build Coastguard Worker static_cast<int>(matrixStride), gl::ArraySizeProduct(arraySizes), isRowMajorMatrix);
296*8975f5c5SAndroid Build Coastguard Worker assert(memberInfo.offset >= 0);
297*8975f5c5SAndroid Build Coastguard Worker
298*8975f5c5SAndroid Build Coastguard Worker advanceOffset(type, arraySizes, isRowMajorMatrix, arrayStride, matrixStride);
299*8975f5c5SAndroid Build Coastguard Worker
300*8975f5c5SAndroid Build Coastguard Worker return memberInfo;
301*8975f5c5SAndroid Build Coastguard Worker }
302*8975f5c5SAndroid Build Coastguard Worker
encodeArrayOfPreEncodedStructs(size_t size,const std::vector<unsigned int> & arraySizes)303*8975f5c5SAndroid Build Coastguard Worker sh::BlockMemberInfo BlockLayoutEncoderMTL::encodeArrayOfPreEncodedStructs(
304*8975f5c5SAndroid Build Coastguard Worker size_t size,
305*8975f5c5SAndroid Build Coastguard Worker const std::vector<unsigned int> &arraySizes)
306*8975f5c5SAndroid Build Coastguard Worker {
307*8975f5c5SAndroid Build Coastguard Worker const unsigned int innerArraySizeProduct = gl::InnerArraySizeProduct(arraySizes);
308*8975f5c5SAndroid Build Coastguard Worker const unsigned int outermostArraySize = gl::OutermostArraySize(arraySizes);
309*8975f5c5SAndroid Build Coastguard Worker
310*8975f5c5SAndroid Build Coastguard Worker // The size of struct is expected to be already aligned appropriately.
311*8975f5c5SAndroid Build Coastguard Worker const size_t arrayStride = size * innerArraySizeProduct;
312*8975f5c5SAndroid Build Coastguard Worker // Aggregate blockMemberInfo types not needed: only used by the Metal bakcend.
313*8975f5c5SAndroid Build Coastguard Worker const sh::BlockMemberInfo memberInfo(GL_INVALID_ENUM, static_cast<int>(mCurrentOffset),
314*8975f5c5SAndroid Build Coastguard Worker static_cast<int>(arrayStride), -1,
315*8975f5c5SAndroid Build Coastguard Worker gl::ArraySizeProduct(arraySizes), false);
316*8975f5c5SAndroid Build Coastguard Worker
317*8975f5c5SAndroid Build Coastguard Worker angle::base::CheckedNumeric<size_t> checkedOffset(arrayStride);
318*8975f5c5SAndroid Build Coastguard Worker checkedOffset *= outermostArraySize;
319*8975f5c5SAndroid Build Coastguard Worker checkedOffset += mCurrentOffset;
320*8975f5c5SAndroid Build Coastguard Worker mCurrentOffset = checkedOffset.ValueOrDie();
321*8975f5c5SAndroid Build Coastguard Worker
322*8975f5c5SAndroid Build Coastguard Worker return memberInfo;
323*8975f5c5SAndroid Build Coastguard Worker }
324*8975f5c5SAndroid Build Coastguard Worker
getCurrentOffset() const325*8975f5c5SAndroid Build Coastguard Worker size_t BlockLayoutEncoderMTL::getCurrentOffset() const
326*8975f5c5SAndroid Build Coastguard Worker {
327*8975f5c5SAndroid Build Coastguard Worker angle::base::CheckedNumeric<size_t> checkedOffset(mCurrentOffset);
328*8975f5c5SAndroid Build Coastguard Worker return checkedOffset.ValueOrDie();
329*8975f5c5SAndroid Build Coastguard Worker }
330*8975f5c5SAndroid Build Coastguard Worker
enterAggregateType(const sh::ShaderVariable & structVar)331*8975f5c5SAndroid Build Coastguard Worker void BlockLayoutEncoderMTL::enterAggregateType(const sh::ShaderVariable &structVar)
332*8975f5c5SAndroid Build Coastguard Worker {
333*8975f5c5SAndroid Build Coastguard Worker align(getBaseAlignment(structVar));
334*8975f5c5SAndroid Build Coastguard Worker }
335*8975f5c5SAndroid Build Coastguard Worker
exitAggregateType(const sh::ShaderVariable & structVar)336*8975f5c5SAndroid Build Coastguard Worker void BlockLayoutEncoderMTL::exitAggregateType(const sh::ShaderVariable &structVar)
337*8975f5c5SAndroid Build Coastguard Worker {
338*8975f5c5SAndroid Build Coastguard Worker align(getBaseAlignment(structVar));
339*8975f5c5SAndroid Build Coastguard Worker }
340*8975f5c5SAndroid Build Coastguard Worker
getShaderVariableSize(const sh::ShaderVariable & structVar,bool isRowMajor)341*8975f5c5SAndroid Build Coastguard Worker size_t BlockLayoutEncoderMTL::getShaderVariableSize(const sh::ShaderVariable &structVar,
342*8975f5c5SAndroid Build Coastguard Worker bool isRowMajor)
343*8975f5c5SAndroid Build Coastguard Worker {
344*8975f5c5SAndroid Build Coastguard Worker size_t currentOffset = mCurrentOffset;
345*8975f5c5SAndroid Build Coastguard Worker mCurrentOffset = 0;
346*8975f5c5SAndroid Build Coastguard Worker sh::BlockEncoderVisitor visitor("", "", this);
347*8975f5c5SAndroid Build Coastguard Worker enterAggregateType(structVar);
348*8975f5c5SAndroid Build Coastguard Worker TraverseShaderVariables(structVar.fields, isRowMajor, &visitor);
349*8975f5c5SAndroid Build Coastguard Worker exitAggregateType(structVar);
350*8975f5c5SAndroid Build Coastguard Worker size_t structVarSize = getCurrentOffset();
351*8975f5c5SAndroid Build Coastguard Worker mCurrentOffset = currentOffset;
352*8975f5c5SAndroid Build Coastguard Worker return structVarSize;
353*8975f5c5SAndroid Build Coastguard Worker }
354*8975f5c5SAndroid Build Coastguard Worker
advanceOffset(GLenum type,const std::vector<unsigned int> & arraySizes,bool isRowMajorMatrix,int arrayStride,int matrixStride)355*8975f5c5SAndroid Build Coastguard Worker void BlockLayoutEncoderMTL::advanceOffset(GLenum type,
356*8975f5c5SAndroid Build Coastguard Worker const std::vector<unsigned int> &arraySizes,
357*8975f5c5SAndroid Build Coastguard Worker bool isRowMajorMatrix,
358*8975f5c5SAndroid Build Coastguard Worker int arrayStride,
359*8975f5c5SAndroid Build Coastguard Worker int matrixStride)
360*8975f5c5SAndroid Build Coastguard Worker {
361*8975f5c5SAndroid Build Coastguard Worker if (!arraySizes.empty())
362*8975f5c5SAndroid Build Coastguard Worker {
363*8975f5c5SAndroid Build Coastguard Worker angle::base::CheckedNumeric<size_t> checkedOffset(arrayStride);
364*8975f5c5SAndroid Build Coastguard Worker checkedOffset *= gl::ArraySizeProduct(arraySizes);
365*8975f5c5SAndroid Build Coastguard Worker checkedOffset += mCurrentOffset;
366*8975f5c5SAndroid Build Coastguard Worker mCurrentOffset = checkedOffset.ValueOrDie();
367*8975f5c5SAndroid Build Coastguard Worker }
368*8975f5c5SAndroid Build Coastguard Worker else if (gl::IsMatrixType(type))
369*8975f5c5SAndroid Build Coastguard Worker {
370*8975f5c5SAndroid Build Coastguard Worker angle::base::CheckedNumeric<size_t> checkedOffset;
371*8975f5c5SAndroid Build Coastguard Worker checkedOffset = mtl::GetMetalSizeForGLType(type);
372*8975f5c5SAndroid Build Coastguard Worker checkedOffset += mCurrentOffset;
373*8975f5c5SAndroid Build Coastguard Worker mCurrentOffset = checkedOffset.ValueOrDie();
374*8975f5c5SAndroid Build Coastguard Worker }
375*8975f5c5SAndroid Build Coastguard Worker else
376*8975f5c5SAndroid Build Coastguard Worker {
377*8975f5c5SAndroid Build Coastguard Worker angle::base::CheckedNumeric<size_t> checkedOffset(mCurrentOffset);
378*8975f5c5SAndroid Build Coastguard Worker checkedOffset += mtl::GetMetalSizeForGLType(type);
379*8975f5c5SAndroid Build Coastguard Worker mCurrentOffset = checkedOffset.ValueOrDie();
380*8975f5c5SAndroid Build Coastguard Worker }
381*8975f5c5SAndroid Build Coastguard Worker }
382*8975f5c5SAndroid Build Coastguard Worker
getBaseAlignment(const sh::ShaderVariable & shaderVar) const383*8975f5c5SAndroid Build Coastguard Worker size_t BlockLayoutEncoderMTL::getBaseAlignment(const sh::ShaderVariable &shaderVar) const
384*8975f5c5SAndroid Build Coastguard Worker {
385*8975f5c5SAndroid Build Coastguard Worker if (shaderVar.isStruct())
386*8975f5c5SAndroid Build Coastguard Worker {
387*8975f5c5SAndroid Build Coastguard Worker MetalAlignmentVisitor visitor;
388*8975f5c5SAndroid Build Coastguard Worker TraverseShaderVariables(shaderVar.fields, false, &visitor);
389*8975f5c5SAndroid Build Coastguard Worker return visitor.getBaseAlignment();
390*8975f5c5SAndroid Build Coastguard Worker }
391*8975f5c5SAndroid Build Coastguard Worker
392*8975f5c5SAndroid Build Coastguard Worker return GetMTLBaseAlignment(shaderVar.type, shaderVar.isRowMajorLayout);
393*8975f5c5SAndroid Build Coastguard Worker }
394*8975f5c5SAndroid Build Coastguard Worker
getTypeBaseAlignment(GLenum type,bool isRowMajorMatrix) const395*8975f5c5SAndroid Build Coastguard Worker size_t BlockLayoutEncoderMTL::getTypeBaseAlignment(GLenum type, bool isRowMajorMatrix) const
396*8975f5c5SAndroid Build Coastguard Worker {
397*8975f5c5SAndroid Build Coastguard Worker return GetMTLBaseAlignment(type, isRowMajorMatrix);
398*8975f5c5SAndroid Build Coastguard Worker }
399*8975f5c5SAndroid Build Coastguard Worker
400*8975f5c5SAndroid Build Coastguard Worker } // namespace mtl
401*8975f5c5SAndroid Build Coastguard Worker } // namespace rx
402