OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scattering.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2015-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 #include "scattering.hpp"
22 #include <utility>
23 
25 {
26  prog = common_compile_compute_shader_from_file("scattering.comp");
27 }
28 
30 {
31  if (tex != 0)
32  {
33  GL_CHECK(glDeleteTextures(1, &tex));
34  }
35 
36  if (prog != 0)
37  {
38  GL_CHECK(glDeleteProgram(prog));
39  }
40 }
41 
42 void Scattering::generate(unsigned size, vec3 sun_dir)
43 {
44  if (tex != 0)
45  {
46  GL_CHECK(glDeleteTextures(1, &tex));
47  }
48 
49  bool mipmap_fp16 = common_has_extension("GL_EXT_color_buffer_half_float");
50 
51  GL_CHECK(glGenTextures(1, &tex));
52  GL_CHECK(glBindTexture(GL_TEXTURE_CUBE_MAP, tex));
53  GL_CHECK(glTexStorage2D(GL_TEXTURE_CUBE_MAP, mipmap_fp16 ? int(log2(float(size))) + 1 : 1, GL_RGBA16F, size, size));
54  GL_CHECK(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
55  GL_CHECK(glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, mipmap_fp16 ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR));
56 
57  GL_CHECK(glUseProgram(prog));
58  GL_CHECK(glUniform3fv(0, 1, value_ptr(sun_dir)));
59 
60  int steps = 100;
61  GL_CHECK(glUniform1i(1, steps));
62 
63  float start = 6500000.0f;
64  float end = 7000000.0f;
65  GL_CHECK(glUniform1f(2, start));
66  GL_CHECK(glUniform1f(3, (end - start) / steps));
67 
68  GL_CHECK(glBindImageTexture(0, tex, 0, GL_TRUE, 0, GL_WRITE_ONLY, GL_RGBA16F));
69  GL_CHECK(glDispatchCompute(size / 8, size / 8, 6));
70  GL_CHECK(glMemoryBarrier(GL_ALL_BARRIER_BITS));
71 
72  if (mipmap_fp16)
73  {
74  GL_CHECK(glBindTexture(GL_TEXTURE_CUBE_MAP, tex));
75  GL_CHECK(glGenerateMipmap(GL_TEXTURE_CUBE_MAP));
76  }
77 }
78 
GLuint GLuint end
Definition: gl2ext.h:323
Definition: matrix.h:51
GLuint common_compile_compute_shader_from_file(const char *cs_source)
Definition: common.cpp:317
const T::data_type * value_ptr(const T &vec)
Definition: vector_math.h:35
void generate(unsigned size, vec3 sun_dir)
Definition: scattering.cpp:42
GLuint start
Definition: gl2ext.h:323
GLuint tex
Definition: scattering.hpp:39
bool common_has_extension(const char *ext)
Definition: common.hpp:57
#define GL_CHECK(x)
Definition: AstcTextures.h:59
GLenum GLuint GLintptr GLsizeiptr size
Definition: gl2ext.h:629
GLuint prog
Definition: scattering.hpp:40