Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve terrain rendering #78

Merged
merged 19 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion editor/src/Views/FilePickerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ uint64_t FilePickerDialog::StartDialog(const Descriptor& descriptor)
selectedFilePath = std::filesystem::path();

this->descriptor = descriptor;
currentTitleHash = kokko::Hash64(descriptor.popupTitle, std::strlen(descriptor.popupTitle), 0);
currentTitleHash = kokko::HashValue64(descriptor.popupTitle, std::strlen(descriptor.popupTitle), 0);

ImGui::OpenPopup(descriptor.popupTitle);

Expand Down
1 change: 0 additions & 1 deletion engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ set (KOKKO_SOURCES
src/Graphics/TerrainSerializer.hpp
src/Graphics/TerrainSystem.cpp
src/Graphics/TerrainSystem.hpp
src/Graphics/TerrainTileId.hpp
src/Graphics/TerrainQuadTree.cpp
src/Graphics/TerrainQuadTree.hpp
src/Graphics/TransformSerializer.hpp
Expand Down
12 changes: 8 additions & 4 deletions engine/res/shaders/deferred_geometry/terrain.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ uniform sampler2D height_map;

vec3 calc_position(vec2 offset)
{
float height_sample = texture(height_map, position + offset).r;
vec2 xy_pos = (position + offset + uniforms.tile_offset) * uniforms.terrain_size * uniforms.tile_scale;
const float texel_size = 1.0 / (uniforms.terrain_side_verts + 2);
const vec2 origin = vec2(1.5) * texel_size;
const float border_scale_factor = (uniforms.terrain_side_verts - 1) / (uniforms.terrain_side_verts + 2);
vec2 pos_tile_space = position + offset;
vec2 tex_coord = origin + pos_tile_space * border_scale_factor;
float height_sample = texture(height_map, tex_coord).r;
vec2 xy_pos = (pos_tile_space + uniforms.tile_offset) * uniforms.terrain_size * uniforms.tile_scale;
return vec3(xy_pos.x, uniforms.height_origin + height_sample * uniforms.height_range, xy_pos.y);
}

void main()
{
float offset_amount = 1.0 / uniforms.terrain_resolution;
float w_offset = uniforms.terrain_size / uniforms.terrain_resolution * 2.0;
float offset_amount = 1.0 / (uniforms.terrain_side_verts - 1);

vec3 p_0 = calc_position(vec2(0.0, 0.0));
vec3 p_right = calc_position(vec2(offset_amount, 0.0));
Expand Down
2 changes: 1 addition & 1 deletion engine/res/shaders/deferred_geometry/terrain.glsl.meta
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"hash":13725136470591927459,"uid":"62205cec36c6413a9e0603081a64a160"}
{"hash":16143719169263984309,"uid":"62205cec36c6413a9e0603081a64a160"}
2 changes: 1 addition & 1 deletion engine/res/shaders/deferred_geometry/terrain_uniform.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ layout(std140, binding = BLOCK_BINDING_OBJECT) uniform TerrainBlock
vec2 tile_offset;
float tile_scale;
float terrain_size;
float terrain_resolution;
float terrain_side_verts;
float height_origin;
float height_range;
float metalness;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"hash":11585041967935328408,"uid":"a789c0f8fe97e8496859bb5c841bb5db"}
{"hash":15716276276840922323,"uid":"a789c0f8fe97e8496859bb5c841bb5db"}
36 changes: 18 additions & 18 deletions engine/src/Core/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,52 @@

namespace kokko
{
uint32_t Hash32(const void* data, size_t length, uint32_t seed)
uint32_t HashValue32(const void* data, size_t length, uint32_t seed)
{
return XXH32(data, length, seed);
}

uint32_t Hash32(uint8_t data, uint32_t seed)
uint32_t HashValue32(uint8_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(int8_t data, uint32_t seed)
uint32_t HashValue32(int8_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(uint16_t data, uint32_t seed)
uint32_t HashValue32(uint16_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(int16_t data, uint32_t seed)
uint32_t HashValue32(int16_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(uint32_t data, uint32_t seed)
uint32_t HashValue32(uint32_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(int32_t data, uint32_t seed)
uint32_t HashValue32(int32_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(uint64_t data, uint32_t seed)
uint32_t HashValue32(uint64_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint32_t Hash32(int64_t data, uint32_t seed)
uint32_t HashValue32(int64_t data, uint32_t seed)
{
return Hash32(&data, sizeof(data), seed);
return HashValue32(&data, sizeof(data), seed);
}

uint64_t Hash64(const void* data, size_t length, uint64_t seed)
uint64_t HashValue64(const void* data, size_t length, uint64_t seed)
{
return XXH64(data, length, seed);
}
Expand Down
34 changes: 15 additions & 19 deletions engine/src/Core/Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@

namespace kokko
{
uint64_t Hash64(const void* data, size_t length, uint64_t seed);

uint32_t Hash32(const void* data, size_t length, uint32_t seed);
uint64_t HashValue64(const void* data, size_t length, uint64_t seed);

uint32_t Hash32(uint8_t data, uint32_t seed);
uint32_t Hash32(int8_t data, uint32_t seed);
uint32_t Hash32(uint16_t data, uint32_t seed);
uint32_t Hash32(int16_t data, uint32_t seed);
uint32_t Hash32(uint32_t data, uint32_t seed);
uint32_t Hash32(int32_t data, uint32_t seed);
uint32_t Hash32(uint64_t data, uint32_t seed);
uint32_t Hash32(int64_t data, uint32_t seed);
uint32_t HashValue32(const void* data, size_t length, uint32_t seed);

// TODO: Figure out a way to declare the overloads in the types' own header file
// Currently GCC would fail compilation because it doesn't consider those declarations
// to be overloads of the original hash functions.
uint32_t HashValue32(uint8_t data, uint32_t seed);
uint32_t HashValue32(int8_t data, uint32_t seed);
uint32_t HashValue32(uint16_t data, uint32_t seed);
uint32_t HashValue32(int16_t data, uint32_t seed);
uint32_t HashValue32(uint32_t data, uint32_t seed);
uint32_t HashValue32(int32_t data, uint32_t seed);
uint32_t HashValue32(uint64_t data, uint32_t seed);
uint32_t HashValue32(int64_t data, uint32_t seed);

struct Uid;
uint32_t Hash32(const Uid& data, uint32_t seed);

class String;
uint32_t Hash32(const String& data, uint32_t seed);
template<typename T>
struct Hash32 {
uint32_t operator()(const T& val, uint32_t seed) const { return HashValue32(val, seed); }
};

constexpr uint32_t HashString(const char* string, size_t length)
{
Expand All @@ -44,7 +40,7 @@ constexpr uint32_t HashString(const char* string, size_t length)
return hash;
}

}
} // namespace kokko

constexpr uint32_t operator ""_hash(const char* string, size_t size)
{
Expand Down
22 changes: 11 additions & 11 deletions engine/src/Core/HashMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class HashMap
{
++current;

if (current == end || current->first)
if (current == end || !(current->first == KeyType{}))
{
break;
}
Expand Down Expand Up @@ -102,9 +102,9 @@ class HashMap

for (; existing != end; ++existing)
{
if (existing->first != KeyType{}) // Pair has value
if (!(existing->first == KeyType{})) // Pair has value
{
for (size_t i = GetIndex(kokko::Hash32(existing->first, 0u));; i = GetIndex(i + 1))
for (size_t i = GetIndex(kokko::Hash32<KeyType>()(existing->first, 0u));; i = GetIndex(i + 1))
{
if (newData[i].first == KeyType{}) // Insert here
{
Expand Down Expand Up @@ -178,7 +178,7 @@ class HashMap
KeyValuePair* itr = data;
KeyValuePair* end = data + allocated;
for (; itr != end; ++itr)
if (itr->first != KeyType{})
if (!(itr->first == KeyType{}))
itr->second.~ValueType();

if (zeroUsed)
Expand Down Expand Up @@ -240,7 +240,7 @@ class HashMap
KeyValuePair* end = data + allocated;
for (; itr != end; ++itr)
{
if (itr->first != KeyType{})
if (!(itr->first == KeyType{}))
{
itr->second.~ValueType();
itr->first = KeyType{};
Expand Down Expand Up @@ -304,11 +304,11 @@ class HashMap

KeyValuePair* Lookup(const KeyType& key)
{
if (key != KeyType{})
if (!(key == KeyType{}))
{
if (data != nullptr)
{
for (size_t i = GetIndex(kokko::Hash32(key, 0u));; i = GetIndex(i + 1))
for (size_t i = GetIndex(kokko::Hash32<KeyType>()(key, 0u));; i = GetIndex(i + 1))
{
KeyValuePair* pair = data + i;

Expand All @@ -332,7 +332,7 @@ class HashMap
{
if (data != nullptr)
{
for (size_t i = GetIndex(kokko::Hash32(key, 0u));; i = GetIndex(i + 1))
for (size_t i = GetIndex(kokko::Hash32<KeyType>()(key, 0u));; i = GetIndex(i + 1))
{
KeyValuePair* pair = data + i;

Expand All @@ -355,10 +355,10 @@ class HashMap
if (data == nullptr)
ReserveInternal(16);

if (key != KeyType{})
if (!(key == KeyType{}))
{
for (;;)
for (size_t i = GetIndex(kokko::Hash32(key, 0u));; i = GetIndex(i + 1))
for (size_t i = GetIndex(kokko::Hash32<KeyType>()(key, 0u));; i = GetIndex(i + 1))
{
KeyValuePair* pair = data + i;

Expand Down Expand Up @@ -422,7 +422,7 @@ class HashMap
return;
}

KeyValuePair* ideal = data + GetIndex(kokko::Hash32(neighbor->first, 0u));
KeyValuePair* ideal = data + GetIndex(kokko::Hash32<KeyType>()(neighbor->first, 0u));
if (GetOffset(ideal, pair) < GetOffset(ideal, neighbor))
{
// Swap with neighbor, then make neighbor the new cell to remove.
Expand Down
2 changes: 1 addition & 1 deletion engine/src/Core/SortedArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SortedArray
{
size_t m = (l + r + 1) / 2;

if (data[m] > value)
if (value < data[m])
r = m - 1;
else
l = m;
Expand Down
4 changes: 2 additions & 2 deletions engine/src/Core/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,9 @@ bool operator!=(ConstStringView lhs, const String& rhs)
return operator==(lhs, rhs) == false;
}

uint32_t Hash32(const String& value, uint32_t seed)
uint32_t HashValue32(const String& value, uint32_t seed)
{
return Hash32(value.GetData(), value.GetLength(), seed);
return HashValue32(value.GetData(), value.GetLength(), seed);
}

}
2 changes: 2 additions & 0 deletions engine/src/Core/String.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,6 @@ bool operator!=(const char* lhs, const String& rhs);
bool operator!=(const String& lhs, ConstStringView rhs);
bool operator!=(ConstStringView lhs, const String& rhs);

uint32_t HashValue32(const String& data, uint32_t seed);

}
4 changes: 2 additions & 2 deletions engine/src/Core/StringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ TEST_CASE("StringView can find last substring")
template struct StringView<char>;
template struct StringView<const char>;

uint32_t Hash32(const ConstStringView& value, uint32_t seed)
uint32_t HashValue32(const ConstStringView& value, uint32_t seed)
{
return Hash32(value.str, value.len, seed);
return HashValue32(value.str, value.len, seed);
}

}
2 changes: 1 addition & 1 deletion engine/src/Core/StringView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ struct StringView
using MutableStringView = StringView<char>;
using ConstStringView = StringView<const char>;

uint32_t Hash32(const ConstStringView& value, uint32_t seed);
uint32_t HashValue32(const ConstStringView& value, uint32_t seed);

}
4 changes: 2 additions & 2 deletions engine/src/Core/Uid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void Uid::WriteTo(ArrayView<char> out) const
IntegerToHexadecimal(raw[1], out.GetSubView(charsPerRaw, charsPerRaw * 2));
}

uint32_t Hash32(const Uid& uid, uint32_t seed)
uint32_t HashValue32(const Uid& uid, uint32_t seed)
{
return Hash32(&uid, sizeof(Uid), seed);
return HashValue32(&uid, sizeof(Uid), seed);
}

TEST_CASE("Uid.Serialization")
Expand Down
2 changes: 2 additions & 0 deletions engine/src/Core/Uid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ struct Uid
void WriteTo(ArrayView<char> out) const;
};

uint32_t HashValue32(const Uid& data, uint32_t seed);

}
Loading