Skip to content

Commit 82bd935

Browse files
committed
m_instanced_zoom and array index untested, but bounds work (AlpineMapsOrg#162)
1 parent 8837365 commit 82bd935

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

gl_engine/TileGeometry.cpp

+40-1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ void TileGeometry::init()
8989
m_array_index_texture = std::make_unique<Texture>(Texture::Target::_2d, Texture::Format::R16UI);
9090
m_array_index_texture->setParams(Texture::Filter::Nearest, Texture::Filter::Nearest);
9191

92+
m_instanced_zoom = std::make_unique<Texture>(Texture::Target::_2d, Texture::Format::R8UI);
93+
m_instanced_zoom->setParams(Texture::Filter::Nearest, Texture::Filter::Nearest);
94+
95+
m_instanced_array_index = std::make_unique<Texture>(Texture::Target::_2d, Texture::Format::R16UI);
96+
m_instanced_array_index->setParams(Texture::Filter::Nearest, Texture::Filter::Nearest);
97+
98+
m_instanced_bounds = std::make_unique<Texture>(Texture::Target::_2d, Texture::Format::RGBA32F);
99+
m_instanced_bounds->setParams(Texture::Filter::Nearest, Texture::Filter::Nearest);
100+
92101
auto example_shader = std::make_shared<ShaderProgram>("tile.vert", "tile.frag");
93102
int bounds = example_shader->attribute_location("bounds");
94103
qDebug() << "attrib location for bounds: " << bounds;
@@ -197,6 +206,32 @@ void TileGeometry::draw(ShaderProgram* shader, const nucleus::camera::Definition
197206
height_texture_layer.emplace_back(tileset.second->height_texture_layer);
198207
}
199208

209+
{
210+
const auto draw_list = tile_list;
211+
nucleus::Raster<uint8_t> zoom_level_raster = { glm::uvec2 { 1024, 1 } };
212+
nucleus::Raster<uint16_t> array_index_raster = { glm::uvec2 { 1024, 1 } };
213+
nucleus::Raster<glm::vec4> bounds_raster = { glm::uvec2 { 1024, 1 } };
214+
for (unsigned i = 0; i < std::min(unsigned(draw_list.size()), 1024u); ++i) {
215+
const auto layer = m_gpu_array_helper.layer(draw_list[i].second->tile_id);
216+
zoom_level_raster.pixel({ i, 0 }) = layer.id.zoom_level;
217+
array_index_raster.pixel({ i, 0 }) = layer.index;
218+
const auto aabb = m_aabb_decorator->aabb(layer.id);
219+
bounds_raster.pixel({ i, 0 }) = { aabb.min.x - camera.position().x, aabb.min.y - camera.position().y, aabb.max.x - camera.position().x, aabb.max.y - camera.position().y };
220+
}
221+
222+
m_instanced_array_index->bind(5);
223+
shader->set_uniform("instanced_geom_array_index_sampler", 5);
224+
m_instanced_array_index->upload(array_index_raster);
225+
226+
m_instanced_zoom->bind(6);
227+
shader->set_uniform("instanced_geom_zoom_sampler", 6);
228+
m_instanced_zoom->upload(zoom_level_raster);
229+
230+
m_instanced_bounds->bind(9);
231+
shader->set_uniform("instanced_geom_bounds_sampler", 9);
232+
m_instanced_bounds->upload(bounds_raster);
233+
}
234+
200235
m_bounds_buffer->bind();
201236
m_bounds_buffer->write(0, bounds.data(), GLsizei(bounds.size() * sizeof(decltype(bounds)::value_type)));
202237

@@ -225,7 +260,11 @@ void TileGeometry::remove_tile(const nucleus::tile::Id& tile_id)
225260
m_gpu_tiles.erase(found_tile);
226261
}
227262

228-
void TileGeometry::set_aabb_decorator(const nucleus::tile::utils::AabbDecoratorPtr& new_aabb_decorator) { m_draw_list_generator.set_aabb_decorator(new_aabb_decorator); }
263+
void TileGeometry::set_aabb_decorator(const nucleus::tile::utils::AabbDecoratorPtr& new_aabb_decorator)
264+
{
265+
m_aabb_decorator = new_aabb_decorator;
266+
m_draw_list_generator.set_aabb_decorator(new_aabb_decorator);
267+
}
229268

230269
void TileGeometry::set_quad_limit(unsigned int new_limit) { m_gpu_array_helper.set_quad_limit(new_limit); }
231270

gl_engine/TileGeometry.h

+4
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public slots:
7777
std::unique_ptr<Texture> m_heightmap_textures;
7878
std::unique_ptr<Texture> m_tile_id_texture;
7979
std::unique_ptr<Texture> m_array_index_texture;
80+
std::unique_ptr<Texture> m_instanced_zoom;
81+
std::unique_ptr<Texture> m_instanced_array_index;
82+
std::unique_ptr<Texture> m_instanced_bounds;
8083
std::unique_ptr<QOpenGLVertexArrayObject> m_vao;
8184
std::pair<std::unique_ptr<QOpenGLBuffer>, size_t> m_index_buffer;
8285
std::unique_ptr<QOpenGLBuffer> m_bounds_buffer;
@@ -86,5 +89,6 @@ public slots:
8689
std::vector<TileInfo> m_gpu_tiles;
8790
nucleus::tile::DrawListGenerator m_draw_list_generator;
8891
nucleus::tile::GpuArrayHelper m_gpu_array_helper;
92+
nucleus::tile::utils::AabbDecoratorPtr m_aabb_decorator;
8993
};
9094
} // namespace gl_engine

gl_engine/shaders/tile.glsl

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919

2020
#include "tile_id.glsl"
2121

22-
layout(location = 0) in highp vec4 bounds;
22+
uniform highp usampler2D instanced_texture_array_index_sampler;
23+
uniform highp usampler2D instanced_texture_zoom_sampler;
24+
uniform highp sampler2D instanced_geom_bounds_sampler;
25+
26+
layout(location = 0) in highp vec4 bounds_attrib;
2327
layout(location = 1) in highp int height_texture_layer;
2428
layout(location = 2) in highp uvec2 packed_tile_id;
2529

@@ -38,6 +42,7 @@ highp float y_to_lat(highp float y) {
3842
highp vec3 camera_world_space_position(out vec2 uv, out float n_quads_per_direction, out float quad_width, out float quad_height, out float altitude_correction_factor) {
3943
highp int n_quads_per_direction_int = n_edge_vertices - 1;
4044
n_quads_per_direction = float(n_quads_per_direction_int);
45+
highp vec4 bounds = texelFetch(instanced_geom_bounds_sampler, ivec2(uint(gl_InstanceID), 0), 0);
4146
quad_width = (bounds.z - bounds.x) / n_quads_per_direction;
4247
quad_height = (bounds.w - bounds.y) / n_quads_per_direction;
4348

@@ -70,6 +75,11 @@ highp vec3 camera_world_space_position(out vec2 uv, out float n_quads_per_direct
7075
altitude_correction_factor = 0.125 / cos(y_to_lat(pos_y)); // https://github.com/AlpineMapsOrg/renderer/issues/5
7176

7277
uv = vec2(float(col) / n_quads_per_direction, float(row) / n_quads_per_direction);
78+
79+
/////////
80+
// decrease_zoom_level_until(tile_id, uv, texelFetch(instanced_texture_zoom_sampler, ivec2(instance_id, 0), 0).x);
81+
// highp float texture_layer_f = float(texelFetch(instanced_texture_array_index_sampler, ivec2(instance_id, 0), 0).x);
82+
////////
7383
float altitude_tex = float(texelFetch(height_tex_sampler, ivec3(col, row, height_texture_layer), 0).r);
7484
float adjusted_altitude = altitude_tex * altitude_correction_factor;
7585

0 commit comments

Comments
 (0)