OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
culling.hpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #ifndef CULLING_HPP__
22 #define CULLING_HPP__
23 
24 #include "common.hpp"
25 #include "mesh.hpp"
26 #include <vector>
27 #include <stdint.h>
28 #include <stddef.h>
29 #define SPHERE_LODS 4
30 
31 // Layout is defined by OpenGL ES 3.1.
32 // We don't care about the three last elements in this case.
34 {
38 };
39 
41 {
42  public:
43  virtual ~CullingInterface() {}
44 
45  // Sets up occlusion geometry. This is mostly static and should be done at startup of a scene.
46  virtual void setup_occluder_geometry(const std::vector<vec4> &positions, const std::vector<uint32_t> &indices) = 0;
47 
48  // Sets current view and projection matrices.
49  virtual void set_view_projection(const mat4 &projection, const mat4& view, const vec2 &zNearFar) = 0;
50 
51  // Rasterize occluders to depth map.
52  virtual void rasterize_occluders() = 0;
53 
54  // Test bounding boxes in our scene.
55  virtual void test_bounding_boxes(GLuint counter_buffer, const unsigned *counter_offsets, unsigned num_offsets,
56  const GLuint *culled_instance_buffer, GLuint instance_data_buffer,
57  unsigned num_instances) = 0;
58 
59  // Debugging functionality. Verify that the depth map is being rasterized correctly.
60  virtual GLuint get_depth_texture() const { return 0; }
61 
62  virtual unsigned get_num_lods() const { return SPHERE_LODS; }
63 
64  protected:
65  // Common functionality for various occlusion culling implementations.
66  void compute_frustum_from_view_projection(vec4 *planes, const mat4 &view_projection);
67 };
68 
69 #define DEPTH_SIZE 256
70 #define DEPTH_SIZE_LOG2 8
72 {
73  public:
74  HiZCulling();
75  HiZCulling(const char *program);
76  ~HiZCulling();
77 
78  void setup_occluder_geometry(const std::vector<vec4> &positions, const std::vector<uint32_t> &indices);
79  void set_view_projection(const mat4 &projection, const mat4 &view, const vec2 &zNearFar);
80 
81  void rasterize_occluders();
82  void test_bounding_boxes(GLuint counter_buffer, const unsigned *counter_offsets, unsigned num_offsets,
83  const GLuint *culled_instance_buffer, GLuint instance_data_buffer,
84  unsigned num_instances);
85 
87 
88  private:
92 
94 
95  struct
96  {
100  unsigned elements;
101  } occluder;
102 
105  unsigned lod_levels;
106  std::vector<GLuint> framebuffers;
107 
109  struct Uniforms
110  {
116  };
118 
119  void init();
120 };
121 
122 // Variant of HiZRasterizer which only uses a single LOD.
124 {
125  public:
127  : HiZCulling("hiz_cull_no_lod.cs")
128  {}
129  unsigned get_num_lods() const { return 1; }
130 };
131 
132 #endif
133 
GLuint depth_texture
Definition: culling.hpp:103
unsigned lod_levels
Definition: culling.hpp:105
GLuint uniform_buffer
Definition: culling.hpp:108
virtual ~CullingInterface()
Definition: culling.hpp:43
virtual unsigned get_num_lods() const
Definition: culling.hpp:62
virtual void test_bounding_boxes(GLuint counter_buffer, const unsigned *counter_offsets, unsigned num_offsets, const GLuint *culled_instance_buffer, GLuint instance_data_buffer, unsigned num_instances)=0
unsigned elements
Definition: culling.hpp:100
Definition: matrix.h:28
GLuint get_depth_texture() const
Definition: culling.hpp:86
struct HiZCulling::@25 occluder
GLuint instanceCount
Definition: culling.hpp:36
Matrix projection
Definition: matrix.h:75
unsigned get_num_lods() const
Definition: culling.hpp:129
GLuint depth_render_program
Definition: culling.hpp:89
GLuint zero[3]
Definition: culling.hpp:37
GLuint vao
Definition: culling.hpp:99
GLuint depth_mip_program
Definition: culling.hpp:90
GLsizei GLenum const void * indices
Definition: gl2ext.h:322
void setup_occluder_geometry(const std::vector< vec4 > &positions, const std::vector< uint32_t > &indices)
Definition: hizculling.cpp:142
virtual void setup_occluder_geometry(const std::vector< vec4 > &positions, const std::vector< uint32_t > &indices)=0
void set_view_projection(const mat4 &projection, const mat4 &view, const vec2 &zNearFar)
Definition: hizculling.cpp:202
virtual GLuint get_depth_texture() const
Definition: culling.hpp:60
GLuint count
Definition: culling.hpp:35
#define SPHERE_LODS
Definition: culling.hpp:29
GLuint vertex
Definition: culling.hpp:97
void rasterize_occluders()
Definition: hizculling.cpp:163
virtual void set_view_projection(const mat4 &projection, const mat4 &view, const vec2 &zNearFar)=0
GLuint index
Definition: culling.hpp:98
GLuint culling_program
Definition: culling.hpp:91
GLuint shadow_sampler
Definition: culling.hpp:104
Definition: matrix.h:104
std::vector< GLuint > framebuffers
Definition: culling.hpp:106
void test_bounding_boxes(GLuint counter_buffer, const unsigned *counter_offsets, unsigned num_offsets, const GLuint *culled_instance_buffer, GLuint instance_data_buffer, unsigned num_instances)
Definition: hizculling.cpp:105
virtual void rasterize_occluders()=0
void init()
Definition: hizculling.cpp:40
void compute_frustum_from_view_projection(vec4 *planes, const mat4 &view_projection)
Definition: culling.cpp:23
GLDrawable quad
Definition: culling.hpp:93
GLuint program
Definition: gl2ext.h:1475
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
Uniforms uniforms
Definition: culling.hpp:117