blob: aa96698c1e53fe0bd638a8309a0830fc974275aa [file] [log] [blame]
ztenghui55bfb4e2013-12-03 10:38:55 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
ztenghuid5e8ade2014-08-13 15:48:02 -070017/**
18 * Extra vertices for the corner for smoother corner.
19 * Only for outer vertices.
20 * Note that we use such extra memory to avoid an extra loop.
21 */
22// For half circle, we could add EXTRA_VERTEX_PER_PI vertices.
23// Set to 1 if we don't want to have any.
24#define EXTRA_CORNER_VERTEX_PER_PI 12
25
26// For the whole polygon, the sum of all the deltas b/t normals is 2 * M_PI,
27// therefore, the maximum number of extra vertices will be twice bigger.
John Reck1bcacfd2017-11-03 10:12:19 -070028#define MAX_EXTRA_CORNER_VERTEX_NUMBER (2 * EXTRA_CORNER_VERTEX_PER_PI)
ztenghuid5e8ade2014-08-13 15:48:02 -070029
30// For each RADIANS_DIVISOR, we would allocate one more vertex b/t the normals.
31#define CORNER_RADIANS_DIVISOR (M_PI / EXTRA_CORNER_VERTEX_PER_PI)
32
33/**
34 * Extra vertices for the Edge for interpolation artifacts.
35 * Same value for both inner and outer vertices.
36 */
37#define EXTRA_EDGE_VERTEX_PER_PI 50
38
John Reck1bcacfd2017-11-03 10:12:19 -070039#define MAX_EXTRA_EDGE_VERTEX_NUMBER (2 * EXTRA_EDGE_VERTEX_PER_PI)
ztenghuid5e8ade2014-08-13 15:48:02 -070040
John Reck1bcacfd2017-11-03 10:12:19 -070041#define EDGE_RADIANS_DIVISOR (M_PI / EXTRA_EDGE_VERTEX_PER_PI)
ztenghuid5e8ade2014-08-13 15:48:02 -070042
43/**
44 * Other constants:
45 */
Chris Craik138c21f2016-04-28 16:59:42 -070046#define OUTER_ALPHA (0.0f)
ztenghuid5e8ade2014-08-13 15:48:02 -070047
48// Once the alpha difference is greater than this threshold, we will allocate extra
49// edge vertices.
50// If this is set to negative value, then all the edge will be tessellated.
51#define ALPHA_THRESHOLD (0.1f / 255.0f)
52
ztenghui55bfb4e2013-12-03 10:38:55 -080053#include "AmbientShadow.h"
Chris Craik9db58c02015-08-19 15:19:18 -070054
ztenghui63d41ab2014-02-14 13:13:41 -080055#include "ShadowTessellator.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080056#include "Vertex.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040057#include "VertexBuffer.h"
Chris Craik9db58c02015-08-19 15:19:18 -070058
Chris Craik9db58c02015-08-19 15:19:18 -070059#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070060#include <algorithm>
ztenghui55bfb4e2013-12-03 10:38:55 -080061
62namespace android {
63namespace uirenderer {
64
65/**
ztenghuid5e8ade2014-08-13 15:48:02 -070066 * Local utility functions.
67 */
68inline Vector2 getNormalFromVertices(const Vector3* vertices, int current, int next) {
69 // Convert from Vector3 to Vector2 first.
John Reck1bcacfd2017-11-03 10:12:19 -070070 Vector2 currentVertex = {vertices[current].x, vertices[current].y};
71 Vector2 nextVertex = {vertices[next].x, vertices[next].y};
ztenghuid5e8ade2014-08-13 15:48:02 -070072
73 return ShadowTessellator::calculateNormal(currentVertex, nextVertex);
74}
75
76// The input z value will be converted to be non-negative inside.
77// The output must be ranged from 0 to 1.
78inline float getAlphaFromFactoredZ(float factoredZ) {
Chris Craik9db58c02015-08-19 15:19:18 -070079 return 1.0 / (1 + std::max(factoredZ, 0.0f));
ztenghuid5e8ade2014-08-13 15:48:02 -070080}
81
John Reck1bcacfd2017-11-03 10:12:19 -070082inline int getEdgeExtraAndUpdateSpike(Vector2* currentSpike, const Vector3& secondVertex,
83 const Vector3& centroid) {
84 Vector2 secondSpike = {secondVertex.x - centroid.x, secondVertex.y - centroid.y};
ztenghuid5e8ade2014-08-13 15:48:02 -070085 secondSpike.normalize();
86
ztenghui512e6432014-09-10 13:08:20 -070087 int result = ShadowTessellator::getExtraVertexNumber(secondSpike, *currentSpike,
John Reck1bcacfd2017-11-03 10:12:19 -070088 EDGE_RADIANS_DIVISOR);
ztenghuid5e8ade2014-08-13 15:48:02 -070089 *currentSpike = secondSpike;
90 return result;
91}
92
93// Given the caster's vertex count, compute all the buffers size depending on
94// whether or not the caster is opaque.
John Reck1bcacfd2017-11-03 10:12:19 -070095inline void computeBufferSize(int* totalVertexCount, int* totalIndexCount, int* totalUmbraCount,
96 int casterVertexCount, bool isCasterOpaque) {
ztenghuid5e8ade2014-08-13 15:48:02 -070097 // Compute the size of the vertex buffer.
John Reck1bcacfd2017-11-03 10:12:19 -070098 int outerVertexCount =
99 casterVertexCount * 2 + MAX_EXTRA_CORNER_VERTEX_NUMBER + MAX_EXTRA_EDGE_VERTEX_NUMBER;
ztenghuid5e8ade2014-08-13 15:48:02 -0700100 int innerVertexCount = casterVertexCount + MAX_EXTRA_EDGE_VERTEX_NUMBER;
101 *totalVertexCount = outerVertexCount + innerVertexCount;
102
103 // Compute the size of the index buffer.
104 *totalIndexCount = 2 * outerVertexCount + 2;
105
106 // Compute the size of the umber buffer.
107 // For translucent object, keep track of the umbra(inner) vertex in order to draw
108 // inside. We only need to store the index information.
109 *totalUmbraCount = 0;
110 if (!isCasterOpaque) {
111 // Add the centroid if occluder is translucent.
Andreas Gampeedaecc12014-11-10 20:54:07 -0800112 (*totalVertexCount)++;
ztenghuid5e8ade2014-08-13 15:48:02 -0700113 *totalIndexCount += 2 * innerVertexCount + 1;
114 *totalUmbraCount = innerVertexCount;
115 }
116}
117
118inline bool needsExtraForEdge(float firstAlpha, float secondAlpha) {
Bernhard Rosenkränzerc44958c72014-11-17 21:48:46 +0100119 return fabsf(firstAlpha - secondAlpha) > ALPHA_THRESHOLD;
ztenghuid5e8ade2014-08-13 15:48:02 -0700120}
121
122/**
ztenghui55bfb4e2013-12-03 10:38:55 -0800123 * Calculate the shadows as a triangle strips while alpha value as the
124 * shadow values.
125 *
ztenghui50ecf842014-03-11 16:52:30 -0700126 * @param isCasterOpaque Whether the caster is opaque.
ztenghui55bfb4e2013-12-03 10:38:55 -0800127 * @param vertices The shadow caster's polygon, which is represented in a Vector3
128 * array.
129 * @param vertexCount The length of caster's polygon in terms of number of
130 * vertices.
ztenghui63d41ab2014-02-14 13:13:41 -0800131 * @param centroid3d The centroid of the shadow caster.
ztenghui55bfb4e2013-12-03 10:38:55 -0800132 * @param heightFactor The factor showing the higher the object, the lighter the
133 * shadow.
134 * @param geomFactor The factor scaling the geometry expansion along the normal.
135 *
136 * @param shadowVertexBuffer Return an floating point array of (x, y, a)
137 * triangle strips mode.
ztenghuid5e8ade2014-08-13 15:48:02 -0700138 *
139 * An simple illustration:
140 * For now let's mark the outer vertex as Pi, the inner as Vi, the centroid as C.
141 *
142 * First project the occluder to the Z=0 surface.
143 * Then we got all the inner vertices. And we compute the normal for each edge.
144 * According to the normal, we generate outer vertices. E.g: We generate P1 / P4
145 * as extra corner vertices to make the corner looks round and smoother.
146 *
147 * Due to the fact that the alpha is not linear interpolated along the inner
148 * edge, when the alpha is different, we may add extra vertices such as P2.1, P2.2,
149 * V0.1, V0.2 to avoid the visual artifacts.
150 *
151 * (P3)
152 * (P2) (P2.1) (P2.2) | ' (P4)
153 * (P1)' | | | | '
154 * ' | | | | '
155 * (P0) ------------------------------------------------(P5)
156 * | (V0) (V0.1) (V0.2) |(V1)
157 * | |
158 * | |
159 * | (C) |
160 * | |
161 * | |
162 * | |
163 * | |
164 * (V3)-----------------------------------(V2)
ztenghui55bfb4e2013-12-03 10:38:55 -0800165 */
John Reck1bcacfd2017-11-03 10:12:19 -0700166void AmbientShadow::createAmbientShadow(bool isCasterOpaque, const Vector3* casterVertices,
167 int casterVertexCount, const Vector3& centroid3d,
168 float heightFactor, float geomFactor,
169 VertexBuffer& shadowVertexBuffer) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800170 shadowVertexBuffer.setMeshFeatureFlags(VertexBuffer::kAlpha | VertexBuffer::kIndices);
ztenghui55bfb4e2013-12-03 10:38:55 -0800171
ztenghuid5e8ade2014-08-13 15:48:02 -0700172 // In order to computer the outer vertices in one loop, we need pre-compute
173 // the normal by the vertex (n - 1) to vertex 0, and the spike and alpha value
174 // for vertex 0.
John Reck1bcacfd2017-11-03 10:12:19 -0700175 Vector2 previousNormal = getNormalFromVertices(casterVertices, casterVertexCount - 1, 0);
176 Vector2 currentSpike = {casterVertices[0].x - centroid3d.x, casterVertices[0].y - centroid3d.y};
ztenghuid5e8ade2014-08-13 15:48:02 -0700177 currentSpike.normalize();
178 float currentAlpha = getAlphaFromFactoredZ(casterVertices[0].z * heightFactor);
ztenghui55bfb4e2013-12-03 10:38:55 -0800179
ztenghuid5e8ade2014-08-13 15:48:02 -0700180 // Preparing all the output data.
181 int totalVertexCount, totalIndexCount, totalUmbraCount;
John Reck1bcacfd2017-11-03 10:12:19 -0700182 computeBufferSize(&totalVertexCount, &totalIndexCount, &totalUmbraCount, casterVertexCount,
183 isCasterOpaque);
184 AlphaVertex* shadowVertices = shadowVertexBuffer.alloc<AlphaVertex>(totalVertexCount);
ztenghuid5e8ade2014-08-13 15:48:02 -0700185 int vertexBufferIndex = 0;
186 uint16_t* indexBuffer = shadowVertexBuffer.allocIndices<uint16_t>(totalIndexCount);
187 int indexBufferIndex = 0;
188 uint16_t umbraVertices[totalUmbraCount];
189 int umbraIndex = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800190
John Reck1bcacfd2017-11-03 10:12:19 -0700191 for (int i = 0; i < casterVertexCount; i++) {
ztenghuid5e8ade2014-08-13 15:48:02 -0700192 // Corner: first figure out the extra vertices we need for the corner.
193 const Vector3& innerVertex = casterVertices[i];
John Reck1bcacfd2017-11-03 10:12:19 -0700194 Vector2 currentNormal =
195 getNormalFromVertices(casterVertices, i, (i + 1) % casterVertexCount);
ztenghui55bfb4e2013-12-03 10:38:55 -0800196
John Reck1bcacfd2017-11-03 10:12:19 -0700197 int extraVerticesNumber = ShadowTessellator::getExtraVertexNumber(
198 currentNormal, previousNormal, CORNER_RADIANS_DIVISOR);
ztenghui55bfb4e2013-12-03 10:38:55 -0800199
ztenghuid5e8ade2014-08-13 15:48:02 -0700200 float expansionDist = innerVertex.z * heightFactor * geomFactor;
John Reck1bcacfd2017-11-03 10:12:19 -0700201 const int cornerSlicesNumber = extraVerticesNumber + 1; // Minimal as 1.
ztenghui55bfb4e2013-12-03 10:38:55 -0800202#if DEBUG_SHADOW
ztenghuid5e8ade2014-08-13 15:48:02 -0700203 ALOGD("cornerSlicesNumber is %d", cornerSlicesNumber);
ztenghui63d41ab2014-02-14 13:13:41 -0800204#endif
ztenghui55bfb4e2013-12-03 10:38:55 -0800205
ztenghuid5e8ade2014-08-13 15:48:02 -0700206 // Corner: fill the corner Vertex Buffer(VB) and Index Buffer(IB).
207 // We fill the inner vertex first, such that we can fill the index buffer
208 // inside the loop.
209 int currentInnerVertexIndex = vertexBufferIndex;
210 if (!isCasterOpaque) {
211 umbraVertices[umbraIndex++] = vertexBufferIndex;
ztenghui7940dc52014-04-22 11:21:49 -0700212 }
John Reck1bcacfd2017-11-03 10:12:19 -0700213 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], casterVertices[i].x,
214 casterVertices[i].y, currentAlpha);
ztenghui7940dc52014-04-22 11:21:49 -0700215
ztenghuid5e8ade2014-08-13 15:48:02 -0700216 const Vector3& innerStart = casterVertices[i];
ztenghui7940dc52014-04-22 11:21:49 -0700217
ztenghuid5e8ade2014-08-13 15:48:02 -0700218 // outerStart is the first outer vertex for this inner vertex.
219 // outerLast is the last outer vertex for this inner vertex.
220 Vector2 outerStart = {0, 0};
221 Vector2 outerLast = {0, 0};
222 // This will create vertices from [0, cornerSlicesNumber] inclusively,
223 // which means minimally 2 vertices even without the extra ones.
224 for (int j = 0; j <= cornerSlicesNumber; j++) {
John Reck1bcacfd2017-11-03 10:12:19 -0700225 Vector2 averageNormal = previousNormal * (cornerSlicesNumber - j) + currentNormal * j;
ztenghuid5e8ade2014-08-13 15:48:02 -0700226 averageNormal /= cornerSlicesNumber;
227 averageNormal.normalize();
228 Vector2 outerVertex;
229 outerVertex.x = innerVertex.x + averageNormal.x * expansionDist;
230 outerVertex.y = innerVertex.y + averageNormal.y * expansionDist;
ztenghui7940dc52014-04-22 11:21:49 -0700231
ztenghuid5e8ade2014-08-13 15:48:02 -0700232 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
233 indexBuffer[indexBufferIndex++] = currentInnerVertexIndex;
John Reck1bcacfd2017-11-03 10:12:19 -0700234 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], outerVertex.x, outerVertex.y,
235 OUTER_ALPHA);
ztenghui7940dc52014-04-22 11:21:49 -0700236
ztenghuid5e8ade2014-08-13 15:48:02 -0700237 if (j == 0) {
238 outerStart = outerVertex;
239 } else if (j == cornerSlicesNumber) {
240 outerLast = outerVertex;
ztenghui7940dc52014-04-22 11:21:49 -0700241 }
242 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700243 previousNormal = currentNormal;
ztenghui55bfb4e2013-12-03 10:38:55 -0800244
ztenghuid5e8ade2014-08-13 15:48:02 -0700245 // Edge: first figure out the extra vertices needed for the edge.
246 const Vector3& innerNext = casterVertices[(i + 1) % casterVertexCount];
247 float nextAlpha = getAlphaFromFactoredZ(innerNext.z * heightFactor);
248 if (needsExtraForEdge(currentAlpha, nextAlpha)) {
249 // TODO: See if we can / should cache this outer vertex across the loop.
250 Vector2 outerNext;
251 float expansionDist = innerNext.z * heightFactor * geomFactor;
252 outerNext.x = innerNext.x + currentNormal.x * expansionDist;
253 outerNext.y = innerNext.y + currentNormal.y * expansionDist;
254
255 // Compute the angle and see how many extra points we need.
John Reck1bcacfd2017-11-03 10:12:19 -0700256 int extraVerticesNumber =
257 getEdgeExtraAndUpdateSpike(&currentSpike, innerNext, centroid3d);
ztenghui7940dc52014-04-22 11:21:49 -0700258#if DEBUG_SHADOW
ztenghuid5e8ade2014-08-13 15:48:02 -0700259 ALOGD("extraVerticesNumber %d for edge %d", extraVerticesNumber, i);
ztenghui7940dc52014-04-22 11:21:49 -0700260#endif
ztenghuid5e8ade2014-08-13 15:48:02 -0700261 // Edge: fill the edge's VB and IB.
262 // This will create vertices pair from [1, extraVerticesNumber - 1].
263 // If there is no extra vertices created here, the edge will be drawn
264 // as just 2 triangles.
265 for (int k = 1; k < extraVerticesNumber; k++) {
266 int startWeight = extraVerticesNumber - k;
267 Vector2 currentOuter =
John Reck1bcacfd2017-11-03 10:12:19 -0700268 (outerLast * startWeight + outerNext * k) / extraVerticesNumber;
ztenghuid5e8ade2014-08-13 15:48:02 -0700269 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
270 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], currentOuter.x,
John Reck1bcacfd2017-11-03 10:12:19 -0700271 currentOuter.y, OUTER_ALPHA);
ztenghui55bfb4e2013-12-03 10:38:55 -0800272
ztenghuid5e8ade2014-08-13 15:48:02 -0700273 if (!isCasterOpaque) {
274 umbraVertices[umbraIndex++] = vertexBufferIndex;
ztenghui55bfb4e2013-12-03 10:38:55 -0800275 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700276 Vector3 currentInner =
John Reck1bcacfd2017-11-03 10:12:19 -0700277 (innerStart * startWeight + innerNext * k) / extraVerticesNumber;
ztenghuid5e8ade2014-08-13 15:48:02 -0700278 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
279 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], currentInner.x,
John Reck1bcacfd2017-11-03 10:12:19 -0700280 currentInner.y,
281 getAlphaFromFactoredZ(currentInner.z * heightFactor));
ztenghui55bfb4e2013-12-03 10:38:55 -0800282 }
283 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700284 currentAlpha = nextAlpha;
ztenghui55bfb4e2013-12-03 10:38:55 -0800285 }
ztenghui55bfb4e2013-12-03 10:38:55 -0800286
ztenghuid5e8ade2014-08-13 15:48:02 -0700287 indexBuffer[indexBufferIndex++] = 1;
288 indexBuffer[indexBufferIndex++] = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800289
ztenghuid5e8ade2014-08-13 15:48:02 -0700290 if (!isCasterOpaque) {
291 // Add the centroid as the last one in the vertex buffer.
John Reck1bcacfd2017-11-03 10:12:19 -0700292 float centroidOpacity = getAlphaFromFactoredZ(centroid3d.z * heightFactor);
ztenghuid5e8ade2014-08-13 15:48:02 -0700293 int centroidIndex = vertexBufferIndex;
John Reck1bcacfd2017-11-03 10:12:19 -0700294 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid3d.x, centroid3d.y,
295 centroidOpacity);
ztenghuid5e8ade2014-08-13 15:48:02 -0700296
297 for (int i = 0; i < umbraIndex; i++) {
298 // Note that umbraVertices[0] is always 0.
299 // So the start and the end of the umbra are using the "0".
300 // And penumbra ended with 0, so a degenerated triangle is formed b/t
301 // the umbra and penumbra.
302 indexBuffer[indexBufferIndex++] = umbraVertices[i];
303 indexBuffer[indexBufferIndex++] = centroidIndex;
304 }
305 indexBuffer[indexBufferIndex++] = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800306 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700307
308 // At the end, update the real index and vertex buffer size.
309 shadowVertexBuffer.updateVertexCount(vertexBufferIndex);
310 shadowVertexBuffer.updateIndexCount(indexBufferIndex);
Chris Craik4ac36f82014-12-09 16:54:03 -0800311 shadowVertexBuffer.computeBounds<AlphaVertex>();
ztenghuid5e8ade2014-08-13 15:48:02 -0700312
ztenghuid2dcd6f2014-10-29 16:04:29 -0700313 ShadowTessellator::checkOverflow(vertexBufferIndex, totalVertexCount, "Ambient Vertex Buffer");
314 ShadowTessellator::checkOverflow(indexBufferIndex, totalIndexCount, "Ambient Index Buffer");
315 ShadowTessellator::checkOverflow(umbraIndex, totalUmbraCount, "Ambient Umbra Buffer");
ztenghuid5e8ade2014-08-13 15:48:02 -0700316
317#if DEBUG_SHADOW
318 for (int i = 0; i < vertexBufferIndex; i++) {
319 ALOGD("vertexBuffer i %d, (%f, %f %f)", i, shadowVertices[i].x, shadowVertices[i].y,
John Reck1bcacfd2017-11-03 10:12:19 -0700320 shadowVertices[i].alpha);
ztenghuid5e8ade2014-08-13 15:48:02 -0700321 }
322 for (int i = 0; i < indexBufferIndex; i++) {
323 ALOGD("indexBuffer i %d, indexBuffer[i] %d", i, indexBuffer[i]);
324 }
325#endif
ztenghui55bfb4e2013-12-03 10:38:55 -0800326}
327
John Reck1bcacfd2017-11-03 10:12:19 -0700328}; // namespace uirenderer
329}; // namespace android