blob: 9a643baf9878030315b2101efe59021d4d9caf77 [file] [log] [blame]
shannonwoods@chromium.org7e0904d2013-05-30 00:06:45 +00001//
2// Copyright (c) 2013 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
7#include "compiler/Uniform.h"
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +00008#include "common/mathutil.h"
9#include "common/utilities.h"
shannonwoods@chromium.org7e0904d2013-05-30 00:06:45 +000010
11namespace sh
12{
13
shannonwoods@chromium.org38676dc2013-05-30 00:06:52 +000014Uniform::Uniform(GLenum type, GLenum precision, const char *name, unsigned int arraySize, unsigned int registerIndex)
shannonwoods@chromium.org7e0904d2013-05-30 00:06:45 +000015{
16 this->type = type;
17 this->precision = precision;
18 this->name = name;
19 this->arraySize = arraySize;
20 this->registerIndex = registerIndex;
21}
22
shannonwoods@chromium.orgd7784172013-05-30 00:07:03 +000023BlockMemberInfo::BlockMemberInfo(int offset, int arrayStride, int matrixStride, bool isRowMajorMatrix)
24 : offset(offset),
25 arrayStride(arrayStride),
26 matrixStride(matrixStride),
27 isRowMajorMatrix(isRowMajorMatrix)
28{
29}
30
31const BlockMemberInfo BlockMemberInfo::defaultBlockInfo(-1, -1, -1, false);
32
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +000033InterfaceBlock::InterfaceBlock(const char *name, unsigned int arraySize, unsigned int registerIndex)
34 : name(name),
35 arraySize(arraySize),
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +000036 layout(BLOCKLAYOUT_SHARED),
shannonwoods@chromium.org1500f092013-05-30 00:11:20 +000037 registerIndex(registerIndex)
38{
39}
40
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +000041// Use the same layout for packed and shared
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +000042void InterfaceBlock::setBlockLayout(BlockLayoutType newLayout)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +000043{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +000044 layout = newLayout;
45
46 const size_t componentSize = 4;
47 unsigned int currentOffset = 0;
48
49 blockInfo.clear();
50 getBlockLayoutInfo(activeUniforms, &currentOffset);
51
52 dataSize = currentOffset * componentSize;
53}
54
55void InterfaceBlock::getBlockLayoutInfo(const sh::ActiveUniforms &fields, unsigned int *currentOffset)
56{
57 const size_t componentSize = 4;
58
59 // TODO: row major matrices
60 bool isRowMajorMatrix = false;
61
62 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
63 {
64 int arrayStride;
65 int matrixStride;
66
67 const sh::Uniform &uniform = fields[fieldIndex];
68
69 if (getBlockLayoutInfo(uniform, currentOffset, &arrayStride, &matrixStride))
70 {
71 const BlockMemberInfo memberInfo(*currentOffset * componentSize, arrayStride * componentSize, matrixStride * componentSize, isRowMajorMatrix);
72 blockInfo.push_back(memberInfo);
73
74 if (uniform.arraySize > 0)
75 {
76 *currentOffset += arrayStride * uniform.arraySize;
77 }
78 else if (gl::IsMatrixType(uniform.type))
79 {
80 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
81 *currentOffset += matrixStride * componentGroups;
82 }
83 else
84 {
85 *currentOffset += gl::UniformComponentCount(uniform.type);
86 }
87 }
88 }
89}
90
91bool InterfaceBlock::getBlockLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
92{
93 if (!uniform.fields.empty())
94 {
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +000095 if (uniform.arraySize > 0)
96 {
97 for (unsigned int arrayElement = 0; arrayElement < uniform.arraySize; arrayElement++)
98 {
99 getBlockLayoutInfo(uniform.fields, currentOffset);
100 }
101 }
102 else
103 {
104 getBlockLayoutInfo(uniform.fields, currentOffset);
105 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000106 return false;
107 }
108
109 switch (layout)
110 {
111 case BLOCKLAYOUT_SHARED:
112 case BLOCKLAYOUT_PACKED:
113 getD3DLayoutInfo(uniform, currentOffset, arrayStrideOut, matrixStrideOut);
114 return true;
115
116 case BLOCKLAYOUT_STANDARD:
117 getStandardLayoutInfo(uniform, currentOffset, arrayStrideOut, matrixStrideOut);
118 return true;
119
120 default:
121 UNREACHABLE();
122 return false;
123 }
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000124}
125
126// Block layout packed according to the default D3D11 register packing rules
127// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000128void InterfaceBlock::getD3DLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000129{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000130 ASSERT(uniform.fields.empty());
131
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000132 const unsigned int registerSize = 4;
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000133 const size_t componentSize = 4;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000134
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000135 // TODO: row major matrices
136 bool isRowMajorMatrix = false;
137 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
138 ASSERT(gl::UniformComponentSize(gl::UniformComponentType(uniform.type)) == componentSize);
139 int matrixStride = 0;
140 int arrayStride = 0;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000141
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000142 if (gl::IsMatrixType(uniform.type))
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000143 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000144 *currentOffset = rx::roundUp(*currentOffset, 4u);
145 matrixStride = registerSize;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000146
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000147 if (uniform.arraySize > 0)
148 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000149 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
150 arrayStride = matrixStride * componentGroups;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000151 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000152 }
153 else if (uniform.arraySize > 0)
154 {
155 *currentOffset = rx::roundUp(*currentOffset, registerSize);
156 arrayStride = registerSize;
157 }
158 else
159 {
160 int numComponents = gl::UniformComponentCount(uniform.type);
161 if ((numComponents + (*currentOffset % registerSize)) >= registerSize)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000162 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000163 *currentOffset = rx::roundUp(*currentOffset, registerSize);
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000164 }
165 }
166
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000167 *matrixStrideOut = matrixStride;
168 *arrayStrideOut = arrayStride;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000169}
170
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000171// Block layout according to the std140 block layout
172// See "Standard Uniform Block Layout" in Section 2.11.6 of the OpenGL ES 3.0 specification
173void InterfaceBlock::getStandardLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000174{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000175 ASSERT(uniform.fields.empty());
176
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000177 const size_t componentSize = 4;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000178
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000179 // TODO: row major matrices
180 bool isRowMajorMatrix = false;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000181
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000182 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
183 ASSERT(gl::UniformComponentSize(gl::UniformComponentType(uniform.type)) == componentSize);
184
185 int numComponents = gl::UniformComponentCount(uniform.type);
186 size_t baseAlignment = static_cast<size_t>(numComponents == 3 ? 4 : numComponents);
187 int matrixStride = 0;
188 int arrayStride = 0;
189
190 if (gl::IsMatrixType(uniform.type))
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000191 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000192 numComponents = (isRowMajorMatrix ? gl::VariableColumnCount(uniform.type) : gl::VariableRowCount(uniform.type));
193 baseAlignment = rx::roundUp(baseAlignment, 4u);
194 matrixStride = baseAlignment;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000195
196 if (uniform.arraySize > 0)
197 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000198 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
199 arrayStride = matrixStride * componentGroups;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000200 }
201 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000202 else if (uniform.arraySize > 0)
203 {
204 baseAlignment = rx::roundUp(baseAlignment, 4u);
205 arrayStride = baseAlignment;
206 }
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000207
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000208 *currentOffset = rx::roundUp(*currentOffset, baseAlignment);
209
210 *matrixStrideOut = matrixStride;
211 *arrayStrideOut = arrayStride;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000212}
213
shannonwoods@chromium.org7e0904d2013-05-30 00:06:45 +0000214}