blob: 917e08f56476b0d86d027b21d720f2c439bd5194 [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 {
95 getBlockLayoutInfo(uniform.fields, currentOffset);
96 return false;
97 }
98
99 switch (layout)
100 {
101 case BLOCKLAYOUT_SHARED:
102 case BLOCKLAYOUT_PACKED:
103 getD3DLayoutInfo(uniform, currentOffset, arrayStrideOut, matrixStrideOut);
104 return true;
105
106 case BLOCKLAYOUT_STANDARD:
107 getStandardLayoutInfo(uniform, currentOffset, arrayStrideOut, matrixStrideOut);
108 return true;
109
110 default:
111 UNREACHABLE();
112 return false;
113 }
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000114}
115
116// Block layout packed according to the default D3D11 register packing rules
117// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000118void InterfaceBlock::getD3DLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000119{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000120 ASSERT(uniform.fields.empty());
121
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000122 const unsigned int registerSize = 4;
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000123 const size_t componentSize = 4;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000124
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000125 // TODO: row major matrices
126 bool isRowMajorMatrix = false;
127 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
128 ASSERT(gl::UniformComponentSize(gl::UniformComponentType(uniform.type)) == componentSize);
129 int matrixStride = 0;
130 int arrayStride = 0;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000131
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000132 if (gl::IsMatrixType(uniform.type))
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000133 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000134 *currentOffset = rx::roundUp(*currentOffset, 4u);
135 matrixStride = registerSize;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000136
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000137 if (uniform.arraySize > 0)
138 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000139 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
140 arrayStride = matrixStride * componentGroups;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000141 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000142 }
143 else if (uniform.arraySize > 0)
144 {
145 *currentOffset = rx::roundUp(*currentOffset, registerSize);
146 arrayStride = registerSize;
147 }
148 else
149 {
150 int numComponents = gl::UniformComponentCount(uniform.type);
151 if ((numComponents + (*currentOffset % registerSize)) >= registerSize)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000152 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000153 *currentOffset = rx::roundUp(*currentOffset, registerSize);
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000154 }
155 }
156
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000157 *matrixStrideOut = matrixStride;
158 *arrayStrideOut = arrayStride;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000159}
160
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000161// Block layout according to the std140 block layout
162// See "Standard Uniform Block Layout" in Section 2.11.6 of the OpenGL ES 3.0 specification
163void InterfaceBlock::getStandardLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000164{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000165 ASSERT(uniform.fields.empty());
166
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000167 const size_t componentSize = 4;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000168
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000169 // TODO: row major matrices
170 bool isRowMajorMatrix = false;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000171
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000172 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
173 ASSERT(gl::UniformComponentSize(gl::UniformComponentType(uniform.type)) == componentSize);
174
175 int numComponents = gl::UniformComponentCount(uniform.type);
176 size_t baseAlignment = static_cast<size_t>(numComponents == 3 ? 4 : numComponents);
177 int matrixStride = 0;
178 int arrayStride = 0;
179
180 if (gl::IsMatrixType(uniform.type))
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000181 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000182 numComponents = (isRowMajorMatrix ? gl::VariableColumnCount(uniform.type) : gl::VariableRowCount(uniform.type));
183 baseAlignment = rx::roundUp(baseAlignment, 4u);
184 matrixStride = baseAlignment;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000185
186 if (uniform.arraySize > 0)
187 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000188 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
189 arrayStride = matrixStride * componentGroups;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000190 }
191 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000192 else if (uniform.arraySize > 0)
193 {
194 baseAlignment = rx::roundUp(baseAlignment, 4u);
195 arrayStride = baseAlignment;
196 }
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000197
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000198 *currentOffset = rx::roundUp(*currentOffset, baseAlignment);
199
200 *matrixStrideOut = matrixStride;
201 *arrayStrideOut = arrayStride;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000202}
203
shannonwoods@chromium.org7e0904d2013-05-30 00:06:45 +0000204}