OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
hiz_cull_no_lod.cs
Go to the documentation of this file.
1 #version 310 es
2 
3 /* Copyright (c) 2014-2017, ARM Limited and Contributors
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge,
8  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
11  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 // Exactly same algorithm as hiz_cull.cs, but with only one LOD.
24 // See hiz_cull.cs for more information.
25 
26 precision highp float;
27 precision highp int;
28 precision highp sampler2DShadow;
29 
30 layout(local_size_x = 64) in;
31 
32 layout(binding = 0, std140) uniform UBO
33 {
34  mat4 uVP;
35  mat4 uView;
36  vec4 uProj[4];
37  vec4 uFrustum[6];
38  vec2 zNearFar;
39 };
40 
41 layout(location = 0) uniform uint uNumBoundingBoxes;
42 layout(binding = 0) uniform sampler2DShadow uDepth;
43 
44 layout(binding = 0, offset = 0) uniform atomic_uint instanceCountLOD0;
45 
46 struct SphereInstance
47 {
48  vec4 position;
49  vec4 velocity;
50 };
51 
52 layout(std430, binding = 0) buffer PerInstanceInput
53 {
54  readonly SphereInstance data[];
56 
57 layout(std430, binding = 1) buffer PerInstanceOutputLOD0
58 {
59  writeonly vec4 data[];
61 
62 layout(std430, binding = 2) buffer PerInstanceOutputLOD1
63 {
64  writeonly vec4 data[];
66 
67 layout(std430, binding = 3) buffer PerInstanceOutputLOD2
68 {
69  writeonly vec4 data[];
71 
72 layout(std430, binding = 4) buffer PerInstanceOutputLOD3
73 {
74  writeonly vec4 data[];
76 
78 {
79  uint count = atomicCounterIncrement(instanceCountLOD0);
80  output_instance_lod0.data[count] = input_instance.data[gl_GlobalInvocationID.x].position;
81 }
82 
83 bool frustum_test(vec3 center, float radius)
84 {
85  for (int f = 0; f < 6; f++)
86  {
87  float plane_distance = dot(uFrustum[f], vec4(center, 1.0));
88  // Bounding sphere not inside frustum. Can safely cull.
89  if (plane_distance < -radius)
90  return false;
91  }
92  return true;
93 }
94 
95 void main()
96 {
97  uint ident = gl_GlobalInvocationID.x;
98  if (ident >= uNumBoundingBoxes)
99  return;
100 
101  vec4 instance_data = input_instance.data[ident].position;
102  vec3 center = instance_data.xyz;
103  float radius = instance_data.w;
104 
105  if (!frustum_test(center, radius))
106  return;
107 
108  vec3 view_center = (uView * vec4(center, 1.0)).xyz;
109  float nearest_z = view_center.z + radius;
110 
111  // Sphere clips against near plane, just assume visibility.
112  if (nearest_z >= -zNearFar.x)
113  {
114  append_instance();
115  return;
116  }
117 
118  float az_plane_horiz_length = length(view_center.xz);
119  float az_plane_vert_length = length(view_center.yz);
120  vec2 az_plane_horiz_norm = view_center.xz / az_plane_horiz_length;
121  vec2 az_plane_vert_norm = view_center.yz / az_plane_vert_length;
122 
123  vec2 t = sqrt(vec2(az_plane_horiz_length, az_plane_vert_length) * vec2(az_plane_horiz_length, az_plane_vert_length) - radius * radius);
124  vec4 w = vec4(t, radius, radius) / vec2(az_plane_horiz_length, az_plane_vert_length).xyxy;
125 
126  vec4 horiz_cos_sin = az_plane_horiz_norm.xyyx * t.x * vec4(w.xx, -w.z, w.z);
127  vec4 vert_cos_sin = az_plane_vert_norm.xyyx * t.y * vec4(w.yy, -w.w, w.w);
128 
129  vec2 horiz0 = horiz_cos_sin.xy + horiz_cos_sin.zw;
130  vec2 horiz1 = horiz_cos_sin.xy - horiz_cos_sin.zw;
131  vec2 vert0 = vert_cos_sin.xy + vert_cos_sin.zw;
132  vec2 vert1 = vert_cos_sin.xy - vert_cos_sin.zw;
133 
134  vec4 projected = -0.5 * vec4(uProj[0][0], uProj[0][0], uProj[1][1], uProj[1][1]) *
135  vec4(horiz0.x, horiz1.x, vert0.x, vert1.x) /
136  vec4(horiz0.y, horiz1.y, vert0.y, vert1.y) + 0.5;
137 
138  vec2 min_xy = projected.yw;
139  vec2 max_xy = projected.xz;
140 
141  vec2 zw = mat2(uProj[2].zw, uProj[3].zw) * vec2(nearest_z, 1.0);
142  nearest_z = 0.5 * zw.x / zw.y + 0.5;
143 
144  vec2 diff_pix = (max_xy - min_xy) * vec2(textureSize(uDepth, 0));
145  float max_diff = max(max(diff_pix.x, diff_pix.y), 1.0);
146  float lod = ceil(log2(max_diff));
147  vec2 mid_pix = 0.5 * (max_xy + min_xy);
148 
149  if (textureLod(uDepth, vec3(mid_pix, nearest_z), lod) > 0.0)
150  append_instance();
151 }
152 
precision highp sampler2DShadow
void main()
Definition: matrix.h:51
Definition: matrix.h:28
output_instance_lod1
precision highp int
GLenum GLuint GLintptr offset
Definition: gl2ext.h:629
output_instance_lod3
bool frustum_test(vec3 center, float radius)
output_instance_lod2
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
Definition: gl2ext.h:179
Definition: matrix.h:75
GLenum GLenum GLsizei count
Definition: gl2ext.h:133
void append_instance()
layout(local_size_x=64) in
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
GLint location
Definition: gl2ext.h:180
GLfloat GLfloat f
Definition: gl2ext.h:2707
float y
Definition: matrix.h:31
float w
Definition: matrix.h:80
float max(float x, float y)
Definition: noise.cpp:29
void uniform(string name, const mat4 &v)
Definition: glutil.cpp:97
float velocity
Definition: Native.cpp:157
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
Definition: matrix.h:104
GLenum GLuint buffer
Definition: gl2ext.h:628
float z
Definition: matrix.h:79
output_instance_lod0
precision highp float
float x
Definition: matrix.h:30
input_instance