normal compression makes shading look terrible

This commit is contained in:
Dynamitos
2025-01-29 16:29:58 +01:00
parent e5ac354527
commit 09660ab642
6 changed files with 84 additions and 80 deletions
+8 -5
View File
@@ -125,7 +125,10 @@ struct VertexAttributes
{
float3 position_MS;
#ifndef POS_ONLY
float4 qTangent;
//float4 qTangent;
float3 normal_MS;
float3 tangent_MS;
float3 biTangent_MS;
float3 vertexColor;
float2 texCoords[MAX_TEXCOORDS];
#endif
@@ -138,10 +141,10 @@ struct VertexAttributes
FragmentParameter result;
result.position_CS = clipPos;
#ifndef POS_ONLY
float3x3 tbn = qTangentToMatrix(qTangent);
result.tangent_WS = normalize(mul(transformMatrix, float4(tbn[0], 0)).xyz);
result.biTangent_WS = normalize(mul(transformMatrix, float4(tbn[1], 0)).xyz);
result.normal_WS = normalize(mul(transformMatrix, float4(tbn[2], 0)).xyz);
//float3x3 tbn = qTangentToMatrix(qTangent);
result.tangent_WS = normalize(mul(transformMatrix, float4(tangent_MS, 0)).xyz);
result.biTangent_WS = normalize(mul(transformMatrix, float4(biTangent_MS, 0)).xyz);
result.normal_WS = normalize(mul(transformMatrix, float4(normal_MS, 0)).xyz);
result.vertexColor = vertexColor;
result.position_WS = worldPos.xyz;
result.texCoords0 = float4(texCoords[0], texCoords[1]);
+7 -6
View File
@@ -14,9 +14,10 @@ struct StaticMeshVertexData
VertexAttributes attributes;
attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]);
#ifndef POS_ONLY
attributes.qTangent = qTangent[index];
//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.qTangent = qTangent[index];
attributes.normal_MS = float3(normals[index * 3 + 0], normals[index * 3 + 1], normals[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]);
for(uint i = 0; i < MAX_TEXCOORDS; ++i)
{
attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1]));
@@ -26,9 +27,9 @@ struct StaticMeshVertexData
return attributes;
}
StructuredBuffer<float> positions;
StructuredBuffer<float4> qTangent;
//StructuredBuffer<float> tangents;
//StructuredBuffer<float> biTangents;
StructuredBuffer<float> normals;
StructuredBuffer<float> tangents;
StructuredBuffer<float> biTangents;
StructuredBuffer<uint16_t> color;
StructuredBuffer<uint16_t> texCoords[MAX_TEXCOORDS];
};
+1 -1
View File
@@ -71,7 +71,7 @@ import MATERIAL_FILE_NAME;
*/
const float eps = 1e-5;
static const float eps = 1e-5;
[shader("closesthit")]
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
{
+7 -7
View File
@@ -535,8 +535,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
texCoords[i].resize(mesh->mNumVertices);
}
Array<StaticMeshVertexData::NormalType> normals(mesh->mNumVertices);
//Array<StaticMeshVertexData::TangentType> tangents(mesh->mNumVertices);
//Array<StaticMeshVertexData::BiTangentType> biTangents(mesh->mNumVertices);
Array<StaticMeshVertexData::TangentType> tangents(mesh->mNumVertices);
Array<StaticMeshVertexData::BiTangentType> biTangents(mesh->mNumVertices);
Array<StaticMeshVertexData::ColorType> colors(mesh->mNumVertices);
for (int32 i = 0; i < mesh->mNumVertices; ++i) {
@@ -555,9 +555,9 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
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);
}
normals[i] = encodeQTangent(Matrix3(tangent, biTangent, normal));
//tangents[i] = tangent;
//biTangents[i] = biTangent;
normals[i] = normal; // encodeQTangent(Matrix3(tangent, biTangent, normal));
tangents[i] = tangent;
biTangents[i] = biTangent;
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);
@@ -571,8 +571,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
vertexData->loadTexCoords(offset, i, texCoords[i]);
}
vertexData->loadNormals(offset, normals);
// vertexData->loadTangents(offset, tangents);
// vertexData->loadBitangents(offset, biTangents);
vertexData->loadTangents(offset, tangents);
vertexData->loadBitangents(offset, biTangents);
vertexData->loadColors(offset, colors);
Array<uint32> indices(mesh->mNumFaces * 3);
+54 -54
View File
@@ -36,16 +36,16 @@ void StaticMeshVertexData::loadNormals(uint64 offset, const Array<NormalType>& d
dirty = true;
}
//void StaticMeshVertexData::loadTangents(uint64 offset, const Array<TangentType>& data) {
// assert(offset + data.size() <= head);
// std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType));
// dirty = true;
//}
//void StaticMeshVertexData::loadBitangents(uint64 offset, const Array<BiTangentType>& data) {
// assert(offset + data.size() <= head);
// std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType));
// dirty = true;
//}
void StaticMeshVertexData::loadTangents(uint64 offset, const Array<TangentType>& data) {
assert(offset + data.size() <= head);
std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType));
dirty = true;
}
void StaticMeshVertexData::loadBitangents(uint64 offset, const Array<BiTangentType>& data) {
assert(offset + data.size() <= head);
std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType));
dirty = true;
}
void StaticMeshVertexData::loadColors(uint64 offset, const Array<ColorType>& data) {
assert(offset + data.size() <= head);
@@ -68,18 +68,18 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB
}
Array<PositionType> pos(numVertices);
Array<NormalType> nor(numVertices);
//Array<TangentType> tan(numVertices);
//Array<BiTangentType> bit(numVertices);
Array<TangentType> tan(numVertices);
Array<BiTangentType> bit(numVertices);
Array<ColorType> col(numVertices);
std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(PositionType));
std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(NormalType));
//std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType));
//std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType));
std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType));
std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType));
std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(ColorType));
Serialization::save(buffer, pos);
Serialization::save(buffer, nor);
//Serialization::save(buffer, tan);
//Serialization::save(buffer, bit);
Serialization::save(buffer, tan);
Serialization::save(buffer, bit);
Serialization::save(buffer, col);
}
@@ -98,23 +98,23 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) {
}
Array<PositionType> pos;
Array<NormalType> nor;
//Array<TangentType> tan;
//Array<BiTangentType> bit;
Array<TangentType> tan;
Array<BiTangentType> bit;
Array<ColorType> col;
Serialization::load(buffer, pos);
Serialization::load(buffer, nor);
//Serialization::load(buffer, tan);
//Serialization::load(buffer, bit);
Serialization::load(buffer, tan);
Serialization::load(buffer, bit);
Serialization::load(buffer, col);
loadPositions(offset, pos);
loadNormals(offset, nor);
//loadTangents(offset, tan);
//loadBitangents(offset, bit);
loadTangents(offset, tan);
loadBitangents(offset, bit);
loadColors(offset, col);
result += pos.size() * sizeof(PositionType);
result += nor.size() * sizeof(NormalType);
//result += tan.size() * sizeof(TangentType);
//result += bit.size() * sizeof(BiTangentType);
result += tan.size() * sizeof(TangentType);
result += bit.size() * sizeof(BiTangentType);
result += col.size() * sizeof(ColorType);
return result;
}
@@ -134,17 +134,17 @@ void StaticMeshVertexData::init(Gfx::PGraphics _graphics) {
.binding = 2,
.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{
.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{
.binding = 5,
.descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER,
.descriptorCount = MAX_TEXCOORDS,
});
descriptorLayout->create();
@@ -176,8 +176,8 @@ Gfx::PDescriptorSet StaticMeshVertexData::getVertexDataSet() { return descriptor
void StaticMeshVertexData::resizeBuffers() {
posData.resize(verticesAllocated);
norData.resize(verticesAllocated);
// tanData.resize(verticesAllocated);
// bitData.resize(verticesAllocated);
tanData.resize(verticesAllocated);
bitData.resize(verticesAllocated);
colData.resize(verticesAllocated);
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
texData[i].resize(verticesAllocated);
@@ -202,22 +202,22 @@ void StaticMeshVertexData::updateBuffers() {
},
.name = "Normals",
});
//tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
// .sourceData =
// {
// .size = verticesAllocated * sizeof(TangentType),
// .data = (uint8*)tanData.data(),
// },
// .name = "Tangents",
//});
//biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
// .sourceData =
// {
// .size = verticesAllocated * sizeof(BiTangentType),
// .data = (uint8*)bitData.data(),
// },
// .name = "BiTangents",
//});
tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = verticesAllocated * sizeof(TangentType),
.data = (uint8*)tanData.data(),
},
.name = "Tangents",
});
biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
.size = verticesAllocated * sizeof(BiTangentType),
.data = (uint8*)bitData.data(),
},
.name = "BiTangents",
});
colors = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData =
{
@@ -240,11 +240,11 @@ void StaticMeshVertexData::updateBuffers() {
descriptorSet = descriptorLayout->allocateDescriptorSet();
descriptorSet->updateBuffer(0, 0, positions);
descriptorSet->updateBuffer(1, 0, normals);
// descriptorSet->updateBuffer(2, 0, tangents);
// descriptorSet->updateBuffer(3, 0, biTangents);
descriptorSet->updateBuffer(2, 0, colors);
descriptorSet->updateBuffer(2, 0, tangents);
descriptorSet->updateBuffer(3, 0, biTangents);
descriptorSet->updateBuffer(4, 0, colors);
for (size_t i = 0; i < MAX_TEXCOORDS; ++i) {
descriptorSet->updateBuffer(3, i, texCoords[i]);
descriptorSet->updateBuffer(5, i, texCoords[i]);
}
descriptorSet->writeChanges();
}
+7 -7
View File
@@ -10,9 +10,9 @@ namespace Seele {
class StaticMeshVertexData : public VertexData {
public:
using PositionType = Vector;
using NormalType = Vector4;
//using TangentType = Vector;
//using BiTangentType = Vector;
using NormalType = Vector;
using TangentType = Vector;
using BiTangentType = Vector;
using TexCoordType = U16Vector2;
using ColorType = U16Vector;
@@ -22,8 +22,8 @@ class StaticMeshVertexData : public VertexData {
void loadPositions(uint64 offset, const Array<PositionType>& data);
void loadTexCoords(uint64 offset, uint64 index, const Array<TexCoordType>& data);
void loadNormals(uint64 offset, const Array<NormalType>& data);
//void loadTangents(uint64 offset, const Array<TangentType>& data);
//void loadBitangents(uint64 offset, const Array<BiTangentType>& data);
void loadTangents(uint64 offset, const Array<TangentType>& data);
void loadBitangents(uint64 offset, const Array<BiTangentType>& data);
void loadColors(uint64 offset, const Array<ColorType>& data);
virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override;
virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer) override;
@@ -49,8 +49,8 @@ class StaticMeshVertexData : public VertexData {
Array<PositionType> posData;
Array<TexCoordType> texData[MAX_TEXCOORDS];
Array<NormalType> norData;
// Array<TangentType> tanData;
// Array<BiTangentType> bitData;
Array<TangentType> tanData;
Array<BiTangentType> bitData;
Array<ColorType> colData;
Gfx::ODescriptorLayout descriptorLayout;
Gfx::PDescriptorSet descriptorSet;