blob: 20ecda28b22a0f53418b5538916c43e657d64ce0 [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.
28#define MAX_EXTRA_CORNER_VERTEX_NUMBER (2 * EXTRA_CORNER_VERTEX_PER_PI)
29
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
39#define MAX_EXTRA_EDGE_VERTEX_NUMBER (2 * EXTRA_EDGE_VERTEX_PER_PI)
40
41#define EDGE_RADIANS_DIVISOR (M_PI / EXTRA_EDGE_VERTEX_PER_PI)
42
43/**
44 * Other constants:
45 */
ztenghuiecf091e2015-02-17 13:26:10 -080046// For the edge of the penumbra, the opacity is 0. After transform (1 - alpha),
47// it is 1.
48#define TRANSFORMED_OUTER_OPACITY (1.0f)
ztenghuid5e8ade2014-08-13 15:48:02 -070049
50// Once the alpha difference is greater than this threshold, we will allocate extra
51// edge vertices.
52// If this is set to negative value, then all the edge will be tessellated.
53#define ALPHA_THRESHOLD (0.1f / 255.0f)
54
ztenghui55bfb4e2013-12-03 10:38:55 -080055#include "AmbientShadow.h"
Chris Craik9db58c02015-08-19 15:19:18 -070056
ztenghui63d41ab2014-02-14 13:13:41 -080057#include "ShadowTessellator.h"
ztenghui55bfb4e2013-12-03 10:38:55 -080058#include "Vertex.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040059#include "VertexBuffer.h"
Chris Craik9db58c02015-08-19 15:19:18 -070060
61#include <algorithm>
62#include <utils/Log.h>
ztenghui55bfb4e2013-12-03 10:38:55 -080063
64namespace android {
65namespace uirenderer {
66
67/**
ztenghuid5e8ade2014-08-13 15:48:02 -070068 * Local utility functions.
69 */
70inline Vector2 getNormalFromVertices(const Vector3* vertices, int current, int next) {
71 // Convert from Vector3 to Vector2 first.
72 Vector2 currentVertex = { vertices[current].x, vertices[current].y };
73 Vector2 nextVertex = { vertices[next].x, vertices[next].y };
74
75 return ShadowTessellator::calculateNormal(currentVertex, nextVertex);
76}
77
78// The input z value will be converted to be non-negative inside.
79// The output must be ranged from 0 to 1.
80inline float getAlphaFromFactoredZ(float factoredZ) {
Chris Craik9db58c02015-08-19 15:19:18 -070081 return 1.0 / (1 + std::max(factoredZ, 0.0f));
ztenghuid5e8ade2014-08-13 15:48:02 -070082}
83
ztenghuiecf091e2015-02-17 13:26:10 -080084// The shader is using gaussian function e^-(1-x)*(1-x)*4, therefore, we transform
85// the alpha value to (1 - alpha)
ztenghuid5e8ade2014-08-13 15:48:02 -070086inline float getTransformedAlphaFromAlpha(float alpha) {
ztenghuiecf091e2015-02-17 13:26:10 -080087 return 1.0f - alpha;
ztenghuid5e8ade2014-08-13 15:48:02 -070088}
89
ztenghuiecf091e2015-02-17 13:26:10 -080090// The output is ranged from 0 to 1.
ztenghuid5e8ade2014-08-13 15:48:02 -070091inline float getTransformedAlphaFromFactoredZ(float factoredZ) {
92 return getTransformedAlphaFromAlpha(getAlphaFromFactoredZ(factoredZ));
93}
94
ztenghuid5e8ade2014-08-13 15:48:02 -070095inline int getEdgeExtraAndUpdateSpike(Vector2* currentSpike,
96 const Vector3& secondVertex, const Vector3& centroid) {
97 Vector2 secondSpike = {secondVertex.x - centroid.x, secondVertex.y - centroid.y};
98 secondSpike.normalize();
99
ztenghui512e6432014-09-10 13:08:20 -0700100 int result = ShadowTessellator::getExtraVertexNumber(secondSpike, *currentSpike,
101 EDGE_RADIANS_DIVISOR);
ztenghuid5e8ade2014-08-13 15:48:02 -0700102 *currentSpike = secondSpike;
103 return result;
104}
105
106// Given the caster's vertex count, compute all the buffers size depending on
107// whether or not the caster is opaque.
108inline void computeBufferSize(int* totalVertexCount, int* totalIndexCount,
109 int* totalUmbraCount, int casterVertexCount, bool isCasterOpaque) {
110 // Compute the size of the vertex buffer.
111 int outerVertexCount = casterVertexCount * 2 + MAX_EXTRA_CORNER_VERTEX_NUMBER +
112 MAX_EXTRA_EDGE_VERTEX_NUMBER;
113 int innerVertexCount = casterVertexCount + MAX_EXTRA_EDGE_VERTEX_NUMBER;
114 *totalVertexCount = outerVertexCount + innerVertexCount;
115
116 // Compute the size of the index buffer.
117 *totalIndexCount = 2 * outerVertexCount + 2;
118
119 // Compute the size of the umber buffer.
120 // For translucent object, keep track of the umbra(inner) vertex in order to draw
121 // inside. We only need to store the index information.
122 *totalUmbraCount = 0;
123 if (!isCasterOpaque) {
124 // Add the centroid if occluder is translucent.
Andreas Gampeedaecc12014-11-10 20:54:07 -0800125 (*totalVertexCount)++;
ztenghuid5e8ade2014-08-13 15:48:02 -0700126 *totalIndexCount += 2 * innerVertexCount + 1;
127 *totalUmbraCount = innerVertexCount;
128 }
129}
130
131inline bool needsExtraForEdge(float firstAlpha, float secondAlpha) {
Bernhard Rosenkränzerc44958c72014-11-17 21:48:46 +0100132 return fabsf(firstAlpha - secondAlpha) > ALPHA_THRESHOLD;
ztenghuid5e8ade2014-08-13 15:48:02 -0700133}
134
135/**
ztenghui55bfb4e2013-12-03 10:38:55 -0800136 * Calculate the shadows as a triangle strips while alpha value as the
137 * shadow values.
138 *
ztenghui50ecf842014-03-11 16:52:30 -0700139 * @param isCasterOpaque Whether the caster is opaque.
ztenghui55bfb4e2013-12-03 10:38:55 -0800140 * @param vertices The shadow caster's polygon, which is represented in a Vector3
141 * array.
142 * @param vertexCount The length of caster's polygon in terms of number of
143 * vertices.
ztenghui63d41ab2014-02-14 13:13:41 -0800144 * @param centroid3d The centroid of the shadow caster.
ztenghui55bfb4e2013-12-03 10:38:55 -0800145 * @param heightFactor The factor showing the higher the object, the lighter the
146 * shadow.
147 * @param geomFactor The factor scaling the geometry expansion along the normal.
148 *
149 * @param shadowVertexBuffer Return an floating point array of (x, y, a)
150 * triangle strips mode.
ztenghuid5e8ade2014-08-13 15:48:02 -0700151 *
152 * An simple illustration:
153 * For now let's mark the outer vertex as Pi, the inner as Vi, the centroid as C.
154 *
155 * First project the occluder to the Z=0 surface.
156 * Then we got all the inner vertices. And we compute the normal for each edge.
157 * According to the normal, we generate outer vertices. E.g: We generate P1 / P4
158 * as extra corner vertices to make the corner looks round and smoother.
159 *
160 * Due to the fact that the alpha is not linear interpolated along the inner
161 * edge, when the alpha is different, we may add extra vertices such as P2.1, P2.2,
162 * V0.1, V0.2 to avoid the visual artifacts.
163 *
164 * (P3)
165 * (P2) (P2.1) (P2.2) | ' (P4)
166 * (P1)' | | | | '
167 * ' | | | | '
168 * (P0) ------------------------------------------------(P5)
169 * | (V0) (V0.1) (V0.2) |(V1)
170 * | |
171 * | |
172 * | (C) |
173 * | |
174 * | |
175 * | |
176 * | |
177 * (V3)-----------------------------------(V2)
ztenghui55bfb4e2013-12-03 10:38:55 -0800178 */
Chris Craik05f3d6e2014-06-02 16:27:04 -0700179void AmbientShadow::createAmbientShadow(bool isCasterOpaque,
ztenghuid5e8ade2014-08-13 15:48:02 -0700180 const Vector3* casterVertices, int casterVertexCount, const Vector3& centroid3d,
ztenghui50ecf842014-03-11 16:52:30 -0700181 float heightFactor, float geomFactor, VertexBuffer& shadowVertexBuffer) {
Chris Craik117bdbc2015-02-05 10:12:38 -0800182 shadowVertexBuffer.setMeshFeatureFlags(VertexBuffer::kAlpha | VertexBuffer::kIndices);
ztenghui55bfb4e2013-12-03 10:38:55 -0800183
ztenghuid5e8ade2014-08-13 15:48:02 -0700184 // In order to computer the outer vertices in one loop, we need pre-compute
185 // the normal by the vertex (n - 1) to vertex 0, and the spike and alpha value
186 // for vertex 0.
187 Vector2 previousNormal = getNormalFromVertices(casterVertices,
188 casterVertexCount - 1 , 0);
189 Vector2 currentSpike = {casterVertices[0].x - centroid3d.x,
190 casterVertices[0].y - centroid3d.y};
191 currentSpike.normalize();
192 float currentAlpha = getAlphaFromFactoredZ(casterVertices[0].z * heightFactor);
ztenghui55bfb4e2013-12-03 10:38:55 -0800193
ztenghuid5e8ade2014-08-13 15:48:02 -0700194 // Preparing all the output data.
195 int totalVertexCount, totalIndexCount, totalUmbraCount;
196 computeBufferSize(&totalVertexCount, &totalIndexCount, &totalUmbraCount,
197 casterVertexCount, isCasterOpaque);
ztenghui50ecf842014-03-11 16:52:30 -0700198 AlphaVertex* shadowVertices =
ztenghuid5e8ade2014-08-13 15:48:02 -0700199 shadowVertexBuffer.alloc<AlphaVertex>(totalVertexCount);
200 int vertexBufferIndex = 0;
201 uint16_t* indexBuffer = shadowVertexBuffer.allocIndices<uint16_t>(totalIndexCount);
202 int indexBufferIndex = 0;
203 uint16_t umbraVertices[totalUmbraCount];
204 int umbraIndex = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800205
ztenghuid5e8ade2014-08-13 15:48:02 -0700206 for (int i = 0; i < casterVertexCount; i++) {
207 // Corner: first figure out the extra vertices we need for the corner.
208 const Vector3& innerVertex = casterVertices[i];
209 Vector2 currentNormal = getNormalFromVertices(casterVertices, i,
210 (i + 1) % casterVertexCount);
ztenghui55bfb4e2013-12-03 10:38:55 -0800211
ztenghui512e6432014-09-10 13:08:20 -0700212 int extraVerticesNumber = ShadowTessellator::getExtraVertexNumber(currentNormal,
213 previousNormal, CORNER_RADIANS_DIVISOR);
ztenghui55bfb4e2013-12-03 10:38:55 -0800214
ztenghuid5e8ade2014-08-13 15:48:02 -0700215 float expansionDist = innerVertex.z * heightFactor * geomFactor;
216 const int cornerSlicesNumber = extraVerticesNumber + 1; // Minimal as 1.
ztenghui55bfb4e2013-12-03 10:38:55 -0800217#if DEBUG_SHADOW
ztenghuid5e8ade2014-08-13 15:48:02 -0700218 ALOGD("cornerSlicesNumber is %d", cornerSlicesNumber);
ztenghui63d41ab2014-02-14 13:13:41 -0800219#endif
ztenghui55bfb4e2013-12-03 10:38:55 -0800220
ztenghuid5e8ade2014-08-13 15:48:02 -0700221 // Corner: fill the corner Vertex Buffer(VB) and Index Buffer(IB).
222 // We fill the inner vertex first, such that we can fill the index buffer
223 // inside the loop.
224 int currentInnerVertexIndex = vertexBufferIndex;
225 if (!isCasterOpaque) {
226 umbraVertices[umbraIndex++] = vertexBufferIndex;
ztenghui7940dc52014-04-22 11:21:49 -0700227 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700228 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], casterVertices[i].x,
229 casterVertices[i].y,
230 getTransformedAlphaFromAlpha(currentAlpha));
ztenghui7940dc52014-04-22 11:21:49 -0700231
ztenghuid5e8ade2014-08-13 15:48:02 -0700232 const Vector3& innerStart = casterVertices[i];
ztenghui7940dc52014-04-22 11:21:49 -0700233
ztenghuid5e8ade2014-08-13 15:48:02 -0700234 // outerStart is the first outer vertex for this inner vertex.
235 // outerLast is the last outer vertex for this inner vertex.
236 Vector2 outerStart = {0, 0};
237 Vector2 outerLast = {0, 0};
238 // This will create vertices from [0, cornerSlicesNumber] inclusively,
239 // which means minimally 2 vertices even without the extra ones.
240 for (int j = 0; j <= cornerSlicesNumber; j++) {
241 Vector2 averageNormal =
242 previousNormal * (cornerSlicesNumber - j) + currentNormal * j;
243 averageNormal /= cornerSlicesNumber;
244 averageNormal.normalize();
245 Vector2 outerVertex;
246 outerVertex.x = innerVertex.x + averageNormal.x * expansionDist;
247 outerVertex.y = innerVertex.y + averageNormal.y * expansionDist;
ztenghui7940dc52014-04-22 11:21:49 -0700248
ztenghuid5e8ade2014-08-13 15:48:02 -0700249 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
250 indexBuffer[indexBufferIndex++] = currentInnerVertexIndex;
251 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], outerVertex.x,
ztenghuiecf091e2015-02-17 13:26:10 -0800252 outerVertex.y, TRANSFORMED_OUTER_OPACITY);
ztenghui7940dc52014-04-22 11:21:49 -0700253
ztenghuid5e8ade2014-08-13 15:48:02 -0700254 if (j == 0) {
255 outerStart = outerVertex;
256 } else if (j == cornerSlicesNumber) {
257 outerLast = outerVertex;
ztenghui7940dc52014-04-22 11:21:49 -0700258 }
259 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700260 previousNormal = currentNormal;
ztenghui55bfb4e2013-12-03 10:38:55 -0800261
ztenghuid5e8ade2014-08-13 15:48:02 -0700262 // Edge: first figure out the extra vertices needed for the edge.
263 const Vector3& innerNext = casterVertices[(i + 1) % casterVertexCount];
264 float nextAlpha = getAlphaFromFactoredZ(innerNext.z * heightFactor);
265 if (needsExtraForEdge(currentAlpha, nextAlpha)) {
266 // TODO: See if we can / should cache this outer vertex across the loop.
267 Vector2 outerNext;
268 float expansionDist = innerNext.z * heightFactor * geomFactor;
269 outerNext.x = innerNext.x + currentNormal.x * expansionDist;
270 outerNext.y = innerNext.y + currentNormal.y * expansionDist;
271
272 // Compute the angle and see how many extra points we need.
273 int extraVerticesNumber = getEdgeExtraAndUpdateSpike(&currentSpike,
274 innerNext, centroid3d);
ztenghui7940dc52014-04-22 11:21:49 -0700275#if DEBUG_SHADOW
ztenghuid5e8ade2014-08-13 15:48:02 -0700276 ALOGD("extraVerticesNumber %d for edge %d", extraVerticesNumber, i);
ztenghui7940dc52014-04-22 11:21:49 -0700277#endif
ztenghuid5e8ade2014-08-13 15:48:02 -0700278 // Edge: fill the edge's VB and IB.
279 // This will create vertices pair from [1, extraVerticesNumber - 1].
280 // If there is no extra vertices created here, the edge will be drawn
281 // as just 2 triangles.
282 for (int k = 1; k < extraVerticesNumber; k++) {
283 int startWeight = extraVerticesNumber - k;
284 Vector2 currentOuter =
285 (outerLast * startWeight + outerNext * k) / extraVerticesNumber;
286 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
287 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], currentOuter.x,
ztenghuiecf091e2015-02-17 13:26:10 -0800288 currentOuter.y, TRANSFORMED_OUTER_OPACITY);
ztenghui55bfb4e2013-12-03 10:38:55 -0800289
ztenghuid5e8ade2014-08-13 15:48:02 -0700290 if (!isCasterOpaque) {
291 umbraVertices[umbraIndex++] = vertexBufferIndex;
ztenghui55bfb4e2013-12-03 10:38:55 -0800292 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700293 Vector3 currentInner =
294 (innerStart * startWeight + innerNext * k) / extraVerticesNumber;
295 indexBuffer[indexBufferIndex++] = vertexBufferIndex;
296 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], currentInner.x,
297 currentInner.y,
298 getTransformedAlphaFromFactoredZ(currentInner.z * heightFactor));
ztenghui55bfb4e2013-12-03 10:38:55 -0800299 }
300 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700301 currentAlpha = nextAlpha;
ztenghui55bfb4e2013-12-03 10:38:55 -0800302 }
ztenghui55bfb4e2013-12-03 10:38:55 -0800303
ztenghuid5e8ade2014-08-13 15:48:02 -0700304 indexBuffer[indexBufferIndex++] = 1;
305 indexBuffer[indexBufferIndex++] = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800306
ztenghuid5e8ade2014-08-13 15:48:02 -0700307 if (!isCasterOpaque) {
308 // Add the centroid as the last one in the vertex buffer.
309 float centroidOpacity =
310 getTransformedAlphaFromFactoredZ(centroid3d.z * heightFactor);
311 int centroidIndex = vertexBufferIndex;
312 AlphaVertex::set(&shadowVertices[vertexBufferIndex++], centroid3d.x,
313 centroid3d.y, centroidOpacity);
314
315 for (int i = 0; i < umbraIndex; i++) {
316 // Note that umbraVertices[0] is always 0.
317 // So the start and the end of the umbra are using the "0".
318 // And penumbra ended with 0, so a degenerated triangle is formed b/t
319 // the umbra and penumbra.
320 indexBuffer[indexBufferIndex++] = umbraVertices[i];
321 indexBuffer[indexBufferIndex++] = centroidIndex;
322 }
323 indexBuffer[indexBufferIndex++] = 0;
ztenghui55bfb4e2013-12-03 10:38:55 -0800324 }
ztenghuid5e8ade2014-08-13 15:48:02 -0700325
326 // At the end, update the real index and vertex buffer size.
327 shadowVertexBuffer.updateVertexCount(vertexBufferIndex);
328 shadowVertexBuffer.updateIndexCount(indexBufferIndex);
Chris Craik4ac36f82014-12-09 16:54:03 -0800329 shadowVertexBuffer.computeBounds<AlphaVertex>();
ztenghuid5e8ade2014-08-13 15:48:02 -0700330
ztenghuid2dcd6f2014-10-29 16:04:29 -0700331 ShadowTessellator::checkOverflow(vertexBufferIndex, totalVertexCount, "Ambient Vertex Buffer");
332 ShadowTessellator::checkOverflow(indexBufferIndex, totalIndexCount, "Ambient Index Buffer");
333 ShadowTessellator::checkOverflow(umbraIndex, totalUmbraCount, "Ambient Umbra Buffer");
ztenghuid5e8ade2014-08-13 15:48:02 -0700334
335#if DEBUG_SHADOW
336 for (int i = 0; i < vertexBufferIndex; i++) {
337 ALOGD("vertexBuffer i %d, (%f, %f %f)", i, shadowVertices[i].x, shadowVertices[i].y,
338 shadowVertices[i].alpha);
339 }
340 for (int i = 0; i < indexBufferIndex; i++) {
341 ALOGD("indexBuffer i %d, indexBuffer[i] %d", i, indexBuffer[i]);
342 }
343#endif
ztenghui55bfb4e2013-12-03 10:38:55 -0800344}
345
346}; // namespace uirenderer
347}; // namespace android