blob: 7c741056806ef8bec0fce33a5ab653a4435861e5 [file] [log] [blame]
Jamie Madill834e8b72014-04-11 13:33:58 -04001//
2// Copyright (c) 2013-2014 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// blocklayout.cpp:
7// Implementation for block layout classes and methods.
8//
9
Jamie Madill9e0478f2015-01-13 11:13:54 -050010#include "compiler/translator/blocklayout.h"
11
Jamie Madill834e8b72014-04-11 13:33:58 -040012#include "common/mathutil.h"
13#include "common/utilities.h"
14
Jamie Madillf2575982014-06-25 16:04:54 -040015namespace sh
Jamie Madill834e8b72014-04-11 13:33:58 -040016{
17
Jamie Madille04a5b72014-07-18 10:33:12 -040018BlockLayoutEncoder::BlockLayoutEncoder()
Jamie Madilla6f267f2014-08-27 11:44:15 -040019 : mCurrentOffset(0)
Jamie Madill834e8b72014-04-11 13:33:58 -040020{
21}
22
Jamie Madilla6f267f2014-08-27 11:44:15 -040023BlockMemberInfo BlockLayoutEncoder::encodeType(GLenum type, unsigned int arraySize, bool isRowMajorMatrix)
Jamie Madill834e8b72014-04-11 13:33:58 -040024{
25 int arrayStride;
26 int matrixStride;
27
28 getBlockLayoutInfo(type, arraySize, isRowMajorMatrix, &arrayStride, &matrixStride);
29
30 const BlockMemberInfo memberInfo(mCurrentOffset * BytesPerComponent, arrayStride * BytesPerComponent, matrixStride * BytesPerComponent, isRowMajorMatrix);
31
Jamie Madill834e8b72014-04-11 13:33:58 -040032 advanceOffset(type, arraySize, isRowMajorMatrix, arrayStride, matrixStride);
Jamie Madilla6f267f2014-08-27 11:44:15 -040033
34 return memberInfo;
Jamie Madill834e8b72014-04-11 13:33:58 -040035}
36
Jamie Madill2857f482015-02-09 15:35:29 -050037// static
38size_t BlockLayoutEncoder::getBlockRegister(const BlockMemberInfo &info)
39{
40 return (info.offset / BytesPerComponent) / ComponentsPerRegister;
41}
42
43// static
44size_t BlockLayoutEncoder::getBlockRegisterElement(const BlockMemberInfo &info)
45{
46 return (info.offset / BytesPerComponent) % ComponentsPerRegister;
47}
48
Jamie Madill834e8b72014-04-11 13:33:58 -040049void BlockLayoutEncoder::nextRegister()
50{
51 mCurrentOffset = rx::roundUp<size_t>(mCurrentOffset, ComponentsPerRegister);
52}
53
Jamie Madille04a5b72014-07-18 10:33:12 -040054Std140BlockEncoder::Std140BlockEncoder()
Jamie Madill834e8b72014-04-11 13:33:58 -040055{
56}
57
58void Std140BlockEncoder::enterAggregateType()
59{
60 nextRegister();
61}
62
63void Std140BlockEncoder::exitAggregateType()
64{
65 nextRegister();
66}
67
68void Std140BlockEncoder::getBlockLayoutInfo(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int *arrayStrideOut, int *matrixStrideOut)
69{
70 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
Jamie Madillf2575982014-06-25 16:04:54 -040071 ASSERT(gl::VariableComponentSize(gl::VariableComponentType(type)) == BytesPerComponent);
Jamie Madill834e8b72014-04-11 13:33:58 -040072
73 size_t baseAlignment = 0;
74 int matrixStride = 0;
75 int arrayStride = 0;
76
77 if (gl::IsMatrixType(type))
78 {
79 baseAlignment = ComponentsPerRegister;
80 matrixStride = ComponentsPerRegister;
81
82 if (arraySize > 0)
83 {
84 const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix);
85 arrayStride = ComponentsPerRegister * numRegisters;
86 }
87 }
88 else if (arraySize > 0)
89 {
90 baseAlignment = ComponentsPerRegister;
91 arrayStride = ComponentsPerRegister;
92 }
93 else
94 {
Jamie Madillf2575982014-06-25 16:04:54 -040095 const int numComponents = gl::VariableComponentCount(type);
Jamie Madill834e8b72014-04-11 13:33:58 -040096 baseAlignment = (numComponents == 3 ? 4u : static_cast<size_t>(numComponents));
97 }
98
99 mCurrentOffset = rx::roundUp(mCurrentOffset, baseAlignment);
100
101 *matrixStrideOut = matrixStride;
102 *arrayStrideOut = arrayStride;
103}
104
105void Std140BlockEncoder::advanceOffset(GLenum type, unsigned int arraySize, bool isRowMajorMatrix, int arrayStride, int matrixStride)
106{
107 if (arraySize > 0)
108 {
109 mCurrentOffset += arrayStride * arraySize;
110 }
111 else if (gl::IsMatrixType(type))
112 {
113 ASSERT(matrixStride == ComponentsPerRegister);
114 const int numRegisters = gl::MatrixRegisterCount(type, isRowMajorMatrix);
115 mCurrentOffset += ComponentsPerRegister * numRegisters;
116 }
117 else
118 {
Jamie Madillf2575982014-06-25 16:04:54 -0400119 mCurrentOffset += gl::VariableComponentCount(type);
Jamie Madill834e8b72014-04-11 13:33:58 -0400120 }
121}
122
Jamie Madill834e8b72014-04-11 13:33:58 -0400123}