Skip to content

Commit e29bbf9

Browse files
author
kevyuu
committed
Remove packSnorm
1 parent 53f81af commit e29bbf9

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/nbl/asset/utils/CGeometryCreator.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,19 @@ namespace nbl::asset
2929

3030
constexpr auto snorm_all_ones = hlsl::vector<int8_t, 4>(snorm_one, snorm_one, snorm_one, snorm_one);
3131

32-
}
32+
template <typename ElementT>
33+
requires(std::is_same_v<ElementT, uint8_t> || std::is_same_v<ElementT, uint16_t>)
34+
constexpr E_FORMAT get_uv_format()
35+
{
36+
if constexpr(std::is_same_v<ElementT, uint8_t>)
37+
{
38+
return EF_R8G8_UNORM;
39+
} else
40+
{
41+
return EF_R16G16_UNORM;
42+
}
43+
}
3344

34-
static uint8_t packSnorm(float val)
35-
{
36-
return round(hlsl::clamp(val, -1.0f, 1.0f) * 127);
3745
}
3846

3947
template <typename ElementT>
@@ -61,13 +69,13 @@ static ICPUPolygonGeometry::SDataView createUvView(size_t vertexCount)
6169
if constexpr(std::is_same_v<ElementT, uint8_t>)
6270
{
6371
retval.composed.encodedDataRange.u8 = aabb;
64-
retval.composed.format = EF_R8G8_UNORM;
72+
retval.composed.format = get_uv_format<ElementT>();
6573
retval.composed.rangeFormat = IGeometryBase::EAABBFormat::U8_NORM;
6674
}
6775
else if constexpr(std::is_same_v<ElementT, uint16_t>)
6876
{
6977
retval.composed.encodedDataRange.u16 = aabb;
70-
retval.composed.format = EF_R16G16_UNORM;
78+
retval.composed.format = get_uv_format<ElementT>();
7179
retval.composed.rangeFormat = IGeometryBase::EAABBFormat::U16_NORM;
7280
}
7381

@@ -458,7 +466,8 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createSphere(float
458466
//tu = ((float*)(tmpMem+(i-polyCountXPitch)*vertexSize))[4];
459467

460468
positions[vertex_i] = pos;
461-
uvs[vertex_i] = { packSnorm(tu), packSnorm(static_cast<float>(ay * numbers::inv_pi<float32_t>())) };
469+
float32_t2 f32_uv = { tu, static_cast<float>(ay * numbers::inv_pi<float32_t>()) };
470+
encodePixels<get_uv_format<uv_element_t>(), float>(uvs + vertex_i, f32_uv.data.data);
462471
memcpy(normals + vertex_i, &quantizedNormal, sizeof(quantizedNormal));
463472

464473
vertex_i++;
@@ -537,6 +546,7 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCylinder(
537546
snorm_normal_t* normals;
538547

539548
using uv_element_t = uint16_t;
549+
constexpr auto UnityUV = std::numeric_limits<uv_element_t>::max();
540550
hlsl::vector<uv_element_t, 2>* uvs;
541551
{
542552
{
@@ -573,11 +583,12 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCylinder(
573583

574584
positions[i] = { p.x, p.y, p.z };
575585
memcpy(normals + i, &n, sizeof(n));
576-
uvs[i] = { packSnorm(f_i * tesselationRec), packSnorm(0.0) };
586+
float32_t2 f32_uv = { f_i * tesselationRec, 0.f };
587+
encodePixels<get_uv_format<uv_element_t>(), float>(uvs + i, f32_uv.data.data);
577588

578589
positions[i + halfIx] = { p.x, p.y, length };
579590
normals[i + halfIx] = normals[i];
580-
uvs[i + halfIx] = { packSnorm(1.0f), packSnorm(0.0f) };
591+
uvs[i + halfIx] = { UnityUV, 0 };
581592
}
582593

583594
CPolygonGeometryManipulator::recomputeContentHashes(retval.get());

0 commit comments

Comments
 (0)