blob: 111e83d38ed944f4c09ad4a79cadb3c4a7e5c808 [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 {
Jamie Madill52660ff2013-06-07 14:11:09 -040095 const unsigned int elementCount = std::max(1u, uniform.arraySize);
96
97 for (unsigned int elementIndex = 0; elementIndex < elementCount; elementIndex++)
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +000098 {
Jamie Madill52660ff2013-06-07 14:11:09 -040099 // align struct to register size
100 *currentOffset = rx::roundUp(*currentOffset, 4u);
shannonwoods@chromium.org5f8d9b42013-05-30 00:17:57 +0000101 getBlockLayoutInfo(uniform.fields, currentOffset);
102 }
Jamie Madill52660ff2013-06-07 14:11:09 -0400103
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000104 return false;
105 }
106
107 switch (layout)
108 {
109 case BLOCKLAYOUT_SHARED:
110 case BLOCKLAYOUT_PACKED:
111 getD3DLayoutInfo(uniform, currentOffset, arrayStrideOut, matrixStrideOut);
112 return true;
113
114 case BLOCKLAYOUT_STANDARD:
115 getStandardLayoutInfo(uniform, currentOffset, arrayStrideOut, matrixStrideOut);
116 return true;
117
118 default:
119 UNREACHABLE();
120 return false;
121 }
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000122}
123
124// Block layout packed according to the default D3D11 register packing rules
125// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000126void InterfaceBlock::getD3DLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000127{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000128 ASSERT(uniform.fields.empty());
129
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000130 const unsigned int registerSize = 4;
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000131 const size_t componentSize = 4;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000132
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000133 // TODO: row major matrices
134 bool isRowMajorMatrix = false;
135 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
136 ASSERT(gl::UniformComponentSize(gl::UniformComponentType(uniform.type)) == componentSize);
137 int matrixStride = 0;
138 int arrayStride = 0;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000139
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000140 if (gl::IsMatrixType(uniform.type))
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000141 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000142 *currentOffset = rx::roundUp(*currentOffset, 4u);
143 matrixStride = registerSize;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000144
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000145 if (uniform.arraySize > 0)
146 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000147 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
148 arrayStride = matrixStride * componentGroups;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000149 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000150 }
151 else if (uniform.arraySize > 0)
152 {
153 *currentOffset = rx::roundUp(*currentOffset, registerSize);
154 arrayStride = registerSize;
155 }
156 else
157 {
158 int numComponents = gl::UniformComponentCount(uniform.type);
159 if ((numComponents + (*currentOffset % registerSize)) >= registerSize)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000160 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000161 *currentOffset = rx::roundUp(*currentOffset, registerSize);
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000162 }
163 }
164
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000165 *matrixStrideOut = matrixStride;
166 *arrayStrideOut = arrayStride;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000167}
168
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000169// Block layout according to the std140 block layout
170// See "Standard Uniform Block Layout" in Section 2.11.6 of the OpenGL ES 3.0 specification
171void InterfaceBlock::getStandardLayoutInfo(const sh::Uniform &uniform, unsigned int *currentOffset, int *arrayStrideOut, int *matrixStrideOut)
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000172{
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000173 ASSERT(uniform.fields.empty());
174
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000175 const size_t componentSize = 4;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000176
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000177 // TODO: row major matrices
178 bool isRowMajorMatrix = false;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000179
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000180 // We assume we are only dealing with 4 byte components (no doubles or half-words currently)
181 ASSERT(gl::UniformComponentSize(gl::UniformComponentType(uniform.type)) == componentSize);
182
183 int numComponents = gl::UniformComponentCount(uniform.type);
184 size_t baseAlignment = static_cast<size_t>(numComponents == 3 ? 4 : numComponents);
185 int matrixStride = 0;
186 int arrayStride = 0;
187
188 if (gl::IsMatrixType(uniform.type))
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000189 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000190 numComponents = (isRowMajorMatrix ? gl::VariableColumnCount(uniform.type) : gl::VariableRowCount(uniform.type));
191 baseAlignment = rx::roundUp(baseAlignment, 4u);
192 matrixStride = baseAlignment;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000193
194 if (uniform.arraySize > 0)
195 {
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000196 const int componentGroups = (isRowMajorMatrix ? gl::VariableRowCount(uniform.type) : gl::VariableColumnCount(uniform.type));
197 arrayStride = matrixStride * componentGroups;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000198 }
199 }
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000200 else if (uniform.arraySize > 0)
201 {
202 baseAlignment = rx::roundUp(baseAlignment, 4u);
203 arrayStride = baseAlignment;
204 }
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000205
shannonwoods@chromium.org70961b32013-05-30 00:17:48 +0000206 *currentOffset = rx::roundUp(*currentOffset, baseAlignment);
207
208 *matrixStrideOut = matrixStride;
209 *arrayStrideOut = arrayStride;
shannonwoods@chromium.org61aaf242013-05-30 00:12:20 +0000210}
211
shannonwoods@chromium.org7e0904d2013-05-30 00:06:45 +0000212}