compressing normal space to qtangents

This commit is contained in:
Dynamitos
2024-12-31 12:10:26 +01:00
parent d3a2617d7f
commit 382e2d30f5
7 changed files with 80 additions and 84 deletions
+5 -8
View File
@@ -12,7 +12,6 @@ struct LightingParameter
float3x3 tangentToWorld; float3x3 tangentToWorld;
float3 viewDir_WS; float3 viewDir_WS;
float3 position_WS; float3 position_WS;
float3 normal_WS;
} }
@@ -92,7 +91,6 @@ struct FragmentParameter
result.tangentToWorld = getTangentToWorld(); result.tangentToWorld = getTangentToWorld();
result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS); result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS);
result.position_WS = position_WS; result.position_WS = position_WS;
result.normal_WS = normalize(normal_WS);
return result; return result;
} }
@@ -127,9 +125,7 @@ struct VertexAttributes
{ {
float3 position_MS; float3 position_MS;
#ifndef POS_ONLY #ifndef POS_ONLY
float3 normal_MS; float4 qTangent;
float3 tangent_MS;
float3 biTangent_MS;
float3 vertexColor; float3 vertexColor;
float2 texCoords[MAX_TEXCOORDS]; float2 texCoords[MAX_TEXCOORDS];
#endif #endif
@@ -142,9 +138,10 @@ struct VertexAttributes
FragmentParameter result; FragmentParameter result;
result.position_CS = clipPos; result.position_CS = clipPos;
#ifndef POS_ONLY #ifndef POS_ONLY
result.tangent_WS = normalize(mul(transformMatrix, float4(tangent_MS, 0)).xyz); float3x3 tbn = qTangentToMatrix(qTangent);
result.biTangent_WS = normalize(mul(transformMatrix, float4(biTangent_MS, 0)).xyz); result.tangent_WS = normalize(mul(transformMatrix, float4(tbn[0], 0)).xyz);
result.normal_WS = normalize(mul(transformMatrix, float4(normal_MS, 0)).xyz); result.biTangent_WS = normalize(mul(transformMatrix, float4(tbn[1], 0)).xyz);
result.normal_WS = normalize(mul(transformMatrix, float4(tbn[2], 0)).xyz);
result.vertexColor = vertexColor; result.vertexColor = vertexColor;
result.position_WS = worldPos.xyz; result.position_WS = worldPos.xyz;
result.texCoords0 = float4(texCoords[0], texCoords[1]); result.texCoords0 = float4(texCoords[0], texCoords[1]);
+6 -6
View File
@@ -14,9 +14,9 @@ struct StaticMeshVertexData
VertexAttributes attributes; VertexAttributes attributes;
attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]); attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]);
#ifndef POS_ONLY #ifndef POS_ONLY
attributes.normal_MS = float3(normals[index * 3 + 0], normals[index * 3 + 1], normals[index * 3 + 2]); attributes.qTangent = qTangent[index];
attributes.tangent_MS = float3(tangents[index * 3 + 0], tangents[index * 3 + 1], tangents[index * 3 + 2]); //attributes.tangent_MS = float3(tangents[index * 3 + 0], tangents[index * 3 + 1], tangents[index * 3 + 2]);
attributes.biTangent_MS = float3(biTangents[index * 3 + 0], biTangents[index * 3 + 1], biTangents[index * 3 + 2]); //attributes.biTangent_MS = float3(biTangents[index * 3 + 0], biTangents[index * 3 + 1], biTangents[index * 3 + 2]);
for(uint i = 0; i < MAX_TEXCOORDS; ++i) for(uint i = 0; i < MAX_TEXCOORDS; ++i)
{ {
attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1])); attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1]));
@@ -26,9 +26,9 @@ struct StaticMeshVertexData
return attributes; return attributes;
} }
StructuredBuffer<float> positions; StructuredBuffer<float> positions;
StructuredBuffer<float> normals; StructuredBuffer<float4> qTangent;
StructuredBuffer<float> tangents; //StructuredBuffer<float> tangents;
StructuredBuffer<float> biTangents; //StructuredBuffer<float> biTangents;
StructuredBuffer<uint16_t> color; StructuredBuffer<uint16_t> color;
StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS]; StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS];
}; };
+7 -7
View File
@@ -522,8 +522,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
texCoords[i].resize(mesh->mNumVertices); texCoords[i].resize(mesh->mNumVertices);
} }
Array<StaticMeshVertexData::NormalType> normals(mesh->mNumVertices); Array<StaticMeshVertexData::NormalType> normals(mesh->mNumVertices);
Array<StaticMeshVertexData::TangentType> tangents(mesh->mNumVertices); //Array<StaticMeshVertexData::TangentType> tangents(mesh->mNumVertices);
Array<StaticMeshVertexData::BiTangentType> biTangents(mesh->mNumVertices); //Array<StaticMeshVertexData::BiTangentType> biTangents(mesh->mNumVertices);
Array<StaticMeshVertexData::ColorType> colors(mesh->mNumVertices); Array<StaticMeshVertexData::ColorType> colors(mesh->mNumVertices);
for (int32 i = 0; i < mesh->mNumVertices; ++i) { for (int32 i = 0; i < mesh->mNumVertices; ++i) {
@@ -542,9 +542,9 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
tangent = Vector(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z); tangent = Vector(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z);
biTangent = Vector(mesh->mBitangents[i].x, mesh->mBitangents[i].y, mesh->mBitangents[i].z); biTangent = Vector(mesh->mBitangents[i].x, mesh->mBitangents[i].y, mesh->mBitangents[i].z);
} }
normals[i] = normal; normals[i] = encodeQTangent(Matrix3(tangent, biTangent, normal));
tangents[i] = tangent; //tangents[i] = tangent;
biTangents[i] = biTangent; //biTangents[i] = biTangent;
if (mesh->HasVertexColors(0)) { if (mesh->HasVertexColors(0)) {
colors[i] = StaticMeshVertexData::ColorType(mesh->mColors[0][i].r * 65535, mesh->mColors[0][i].g * 65535, mesh->mColors[0][i].b * 65535); colors[i] = StaticMeshVertexData::ColorType(mesh->mColors[0][i].r * 65535, mesh->mColors[0][i].g * 65535, mesh->mColors[0][i].b * 65535);
@@ -558,8 +558,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
vertexData->loadTexCoords(offset, i, texCoords[i]); vertexData->loadTexCoords(offset, i, texCoords[i]);
} }
vertexData->loadNormals(offset, normals); vertexData->loadNormals(offset, normals);
vertexData->loadTangents(offset, tangents); // vertexData->loadTangents(offset, tangents);
vertexData->loadBitangents(offset, biTangents); // vertexData->loadBitangents(offset, biTangents);
vertexData->loadColors(offset, colors); vertexData->loadColors(offset, colors);
Array<uint32> indices(mesh->mNumFaces * 3); Array<uint32> indices(mesh->mNumFaces * 3);
+1 -1
View File
@@ -17,7 +17,7 @@ class Mesh {
uint64 vertexCount; uint64 vertexCount;
uint64 byteSize; uint64 byteSize;
PMaterialInstanceAsset referencedMaterial; PMaterialInstanceAsset referencedMaterial;
Gfx::OBottomLevelAS blas; Gfx::OBottomLevelAS blas = nullptr;
void save(ArchiveBuffer& buffer) const; void save(ArchiveBuffer& buffer) const;
void load(ArchiveBuffer& buffer); void load(ArchiveBuffer& buffer);
+54 -54
View File
@@ -36,16 +36,16 @@ void StaticMeshVertexData::loadNormals(uint64 offset, const Array<NormalType>& d
dirty = true; dirty = true;
} }
void StaticMeshVertexData::loadTangents(uint64 offset, const Array<TangentType>& data) { //void StaticMeshVertexData::loadTangents(uint64 offset, const Array<TangentType>& data) {
assert(offset + data.size() <= head); // assert(offset + data.size() <= head);
std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType)); // std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType));
dirty = true; // dirty = true;
} //}
void StaticMeshVertexData::loadBitangents(uint64 offset, const Array<BiTangentType>& data) { //void StaticMeshVertexData::loadBitangents(uint64 offset, const Array<BiTangentType>& data) {
assert(offset + data.size() <= head); // assert(offset + data.size() <= head);
std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType)); // std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType));
dirty = true; // dirty = true;
} //}
void StaticMeshVertexData::loadColors(uint64 offset, const Array<ColorType>& data) { void StaticMeshVertexData::loadColors(uint64 offset, const Array<ColorType>& data) {
assert(offset + data.size() <= head); assert(offset + data.size() <= head);
@@ -68,18 +68,18 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB
} }
Array<PositionType> pos(numVertices); Array<PositionType> pos(numVertices);
Array<NormalType> nor(numVertices); Array<NormalType> nor(numVertices);
Array<TangentType> tan(numVertices); //Array<TangentType> tan(numVertices);
Array<BiTangentType> bit(numVertices); //Array<BiTangentType> bit(numVertices);
Array<ColorType> col(numVertices); Array<ColorType> col(numVertices);
std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(PositionType)); std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(PositionType));
std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(NormalType)); std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(NormalType));
std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType)); //std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType));
std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType)); //std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType));
std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(ColorType)); std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(ColorType));
Serialization::save(buffer, pos); Serialization::save(buffer, pos);
Serialization::save(buffer, nor); Serialization::save(buffer, nor);
Serialization::save(buffer, tan); //Serialization::save(buffer, tan);
Serialization::save(buffer, bit); //Serialization::save(buffer, bit);
Serialization::save(buffer, col); Serialization::save(buffer, col);
} }
@@ -98,23 +98,23 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
} }
Array<PositionType> pos; Array<PositionType> pos;
Array<NormalType> nor; Array<NormalType> nor;
Array<TangentType> tan; //Array<TangentType> tan;
Array<BiTangentType> bit; //Array<BiTangentType> bit;
Array<ColorType> col; Array<ColorType> col;
Serialization::load(buffer, pos); Serialization::load(buffer, pos);
Serialization::load(buffer, nor); Serialization::load(buffer, nor);
Serialization::load(buffer, tan); //Serialization::load(buffer, tan);
Serialization::load(buffer, bit); //Serialization::load(buffer, bit);
Serialization::load(buffer, col); Serialization::load(buffer, col);
loadPositions(offset, pos); loadPositions(offset, pos);
loadNormals(offset, nor); loadNormals(offset, nor);
loadTangents(offset, tan); //loadTangents(offset, tan);
loadBitangents(offset, bit); //loadBitangents(offset, bit);
loadColors(offset, col); loadColors(offset, col);
result += pos.size() * sizeof(PositionType); result += pos.size() * sizeof(PositionType);
result += nor.size() * sizeof(NormalType); result += nor.size() * sizeof(NormalType);
result += tan.size() * sizeof(TangentType); //result += tan.size() * sizeof(TangentType);
result += bit.size() * sizeof(BiTangentType); //result += bit.size() * sizeof(BiTangentType);
result += col.size() * sizeof(ColorType); result += col.size() * sizeof(ColorType);
return result; return result;
} }
@@ -134,17 +134,17 @@ void StaticMeshVertexData::init(Gfx::PGraphics _graphics) {
.binding = 2, .binding = 2,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
}); });
//descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
// .binding = 3,
// .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
//});
//descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
// .binding = 4,
// .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
//});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = 3, .binding = 3,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = 4,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
});
descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{
.binding = 5,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = MAX_TEXCOORDS, .descriptorCount = MAX_TEXCOORDS,
}); });
descriptorLayout->create(); descriptorLayout->create();
@@ -176,8 +176,8 @@ Gfx::PDescriptorSet StaticMeshVertexData::getVertexDataSet() { return descriptor
void StaticMeshVertexData::resizeBuffers() { void StaticMeshVertexData::resizeBuffers() {
posData.resize(verticesAllocated); posData.resize(verticesAllocated);
norData.resize(verticesAllocated); norData.resize(verticesAllocated);
tanData.resize(verticesAllocated); // tanData.resize(verticesAllocated);
bitData.resize(verticesAllocated); // bitData.resize(verticesAllocated);
colData.resize(verticesAllocated); colData.resize(verticesAllocated);
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
texData[i].resize(verticesAllocated); texData[i].resize(verticesAllocated);
@@ -202,22 +202,22 @@ void StaticMeshVertexData::updateBuffers() {
}, },
.name = "Normals", .name = "Normals",
}); });
tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{ //tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = // .sourceData =
{ // {
.size = verticesAllocated * sizeof(TangentType), // .size = verticesAllocated * sizeof(TangentType),
.data = (uint8*)tanData.data(), // .data = (uint8*)tanData.data(),
}, // },
.name = "Tangents", // .name = "Tangents",
}); //});
biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{ //biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = // .sourceData =
{ // {
.size = verticesAllocated * sizeof(BiTangentType), // .size = verticesAllocated * sizeof(BiTangentType),
.data = (uint8*)bitData.data(), // .data = (uint8*)bitData.data(),
}, // },
.name = "BiTangents", // .name = "BiTangents",
}); //});
colors = graphics->createShaderBuffer(ShaderBufferCreateInfo{ colors = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = .sourceData =
{ {
@@ -240,11 +240,11 @@ void StaticMeshVertexData::updateBuffers() {
descriptorSet = descriptorLayout->allocateDescriptorSet(); descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer(0, 0, positions); descriptorSet->updateBuffer(0, 0, positions);
descriptorSet->updateBuffer(1, 0, normals); descriptorSet->updateBuffer(1, 0, normals);
descriptorSet->updateBuffer(2, 0, tangents); // descriptorSet->updateBuffer(2, 0, tangents);
descriptorSet->updateBuffer(3, 0, biTangents); // descriptorSet->updateBuffer(3, 0, biTangents);
descriptorSet->updateBuffer(4, 0, colors); descriptorSet->updateBuffer(2, 0, colors);
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
descriptorSet->updateBuffer(5, i, texCoords[i]); descriptorSet->updateBuffer(3, i, texCoords[i]);
} }
descriptorSet->writeChanges(); descriptorSet->writeChanges();
} }
+7 -7
View File
@@ -10,9 +10,9 @@ namespace Seele {
class StaticMeshVertexData : public VertexData { class StaticMeshVertexData : public VertexData {
public: public:
using PositionType = Vector; using PositionType = Vector;
using NormalType = Vector; using NormalType = Vector4;
using TangentType = Vector; //using TangentType = Vector;
using BiTangentType = Vector; //using BiTangentType = Vector;
using TexCoordType = U16Vector2; using TexCoordType = U16Vector2;
using ColorType = U16Vector; using ColorType = U16Vector;
@@ -22,8 +22,8 @@ class StaticMeshVertexData : public VertexData {
void loadPositions(uint64 offset, const Array<PositionType>& data); void loadPositions(uint64 offset, const Array<PositionType>& data);
void loadTexCoords(uint64 offset, uint64 index, const Array<TexCoordType>& data); void loadTexCoords(uint64 offset, uint64 index, const Array<TexCoordType>& data);
void loadNormals(uint64 offset, const Array<NormalType>& data); void loadNormals(uint64 offset, const Array<NormalType>& data);
void loadTangents(uint64 offset, const Array<TangentType>& data); //void loadTangents(uint64 offset, const Array<TangentType>& data);
void loadBitangents(uint64 offset, const Array<BiTangentType>& data); //void loadBitangents(uint64 offset, const Array<BiTangentType>& data);
void loadColors(uint64 offset, const Array<ColorType>& data); void loadColors(uint64 offset, const Array<ColorType>& data);
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override; virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer) override; virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer) override;
@@ -49,8 +49,8 @@ class StaticMeshVertexData : public VertexData {
Array<PositionType> posData; Array<PositionType> posData;
Array<TexCoordType> texData[MAX_TEXCOORDS]; Array<TexCoordType> texData[MAX_TEXCOORDS];
Array<NormalType> norData; Array<NormalType> norData;
Array<TangentType> tanData; // Array<TangentType> tanData;
Array<BiTangentType> bitData; // Array<BiTangentType> bitData;
Array<ColorType> colData; Array<ColorType> colData;
Gfx::ODescriptorLayout descriptorLayout; Gfx::ODescriptorLayout descriptorLayout;
Gfx::PDescriptorSet descriptorSet; Gfx::PDescriptorSet descriptorSet;
-1
View File
@@ -78,7 +78,6 @@ class VertexData {
const Array<Gfx::PBottomLevelAS>& getRayTracingData() const { return rayTracingScene; } const Array<Gfx::PBottomLevelAS>& getRayTracingData() const { return rayTracingScene; }
const MeshData& getMeshData(MeshId id) const { return meshData[id]; } const MeshData& getMeshData(MeshId id) const { return meshData[id]; }
void registerBottomLevelAccelerationStructure(Gfx::PBottomLevelAS blas) { void registerBottomLevelAccelerationStructure(Gfx::PBottomLevelAS blas) {
assert(blas != nullptr);
dataToBuild.add(blas); dataToBuild.add(blas);
} }
uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; } uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; }