diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index b390d81..52d25af 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -12,7 +12,6 @@ struct LightingParameter float3x3 tangentToWorld; float3 viewDir_WS; float3 position_WS; - float3 normal_WS; } @@ -92,7 +91,6 @@ struct FragmentParameter result.tangentToWorld = getTangentToWorld(); result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS); result.position_WS = position_WS; - result.normal_WS = normalize(normal_WS); return result; } @@ -127,9 +125,7 @@ struct VertexAttributes { float3 position_MS; #ifndef POS_ONLY - float3 normal_MS; - float3 tangent_MS; - float3 biTangent_MS; + float4 qTangent; float3 vertexColor; float2 texCoords[MAX_TEXCOORDS]; #endif @@ -142,9 +138,10 @@ struct VertexAttributes FragmentParameter result; result.position_CS = clipPos; #ifndef POS_ONLY - 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); + 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); result.vertexColor = vertexColor; result.position_WS = worldPos.xyz; result.texCoords0 = float4(texCoords[0], texCoords[1]); diff --git a/res/shaders/lib/StaticMeshVertexData.slang b/res/shaders/lib/StaticMeshVertexData.slang index 32d667e..dc5d23f 100644 --- a/res/shaders/lib/StaticMeshVertexData.slang +++ b/res/shaders/lib/StaticMeshVertexData.slang @@ -14,9 +14,9 @@ struct StaticMeshVertexData VertexAttributes attributes; attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]); #ifndef POS_ONLY - 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]); + 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]); 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 +26,9 @@ struct StaticMeshVertexData return attributes; } StructuredBuffer positions; - StructuredBuffer normals; - StructuredBuffer tangents; - StructuredBuffer biTangents; + StructuredBuffer qTangent; + //StructuredBuffer tangents; + //StructuredBuffer biTangents; StructuredBuffer color; StructuredBuffer texCoords[MAX_TEXCOORDS]; }; diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 1869a50..5bf822b 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -522,8 +522,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArraymNumVertices); } Array normals(mesh->mNumVertices); - Array tangents(mesh->mNumVertices); - Array biTangents(mesh->mNumVertices); + //Array tangents(mesh->mNumVertices); + //Array biTangents(mesh->mNumVertices); Array colors(mesh->mNumVertices); for (int32 i = 0; i < mesh->mNumVertices; ++i) { @@ -542,9 +542,9 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArraymTangents[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] = normal; - tangents[i] = tangent; - biTangents[i] = biTangent; + normals[i] = 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); @@ -558,8 +558,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArrayloadTexCoords(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 indices(mesh->mNumFaces * 3); diff --git a/src/Engine/Graphics/Mesh.h b/src/Engine/Graphics/Mesh.h index 111d4e6..0c85bef 100644 --- a/src/Engine/Graphics/Mesh.h +++ b/src/Engine/Graphics/Mesh.h @@ -17,7 +17,7 @@ class Mesh { uint64 vertexCount; uint64 byteSize; PMaterialInstanceAsset referencedMaterial; - Gfx::OBottomLevelAS blas; + Gfx::OBottomLevelAS blas = nullptr; void save(ArchiveBuffer& buffer) const; void load(ArchiveBuffer& buffer); diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index 5b07c96..f8dfa51 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -36,16 +36,16 @@ void StaticMeshVertexData::loadNormals(uint64 offset, const Array& d dirty = true; } -void StaticMeshVertexData::loadTangents(uint64 offset, const Array& 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& 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& 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& 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& data) { assert(offset + data.size() <= head); @@ -68,18 +68,18 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB } Array pos(numVertices); Array nor(numVertices); - Array tan(numVertices); - Array bit(numVertices); + //Array tan(numVertices); + //Array bit(numVertices); Array 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 pos; Array nor; - Array tan; - Array bit; + //Array tan; + //Array bit; Array 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(4, 0, colors); +// descriptorSet->updateBuffer(2, 0, tangents); +// descriptorSet->updateBuffer(3, 0, biTangents); + descriptorSet->updateBuffer(2, 0, colors); for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { - descriptorSet->updateBuffer(5, i, texCoords[i]); + descriptorSet->updateBuffer(3, i, texCoords[i]); } descriptorSet->writeChanges(); } diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index dafebc8..c2d3722 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -10,9 +10,9 @@ namespace Seele { class StaticMeshVertexData : public VertexData { public: using PositionType = Vector; - using NormalType = Vector; - using TangentType = Vector; - using BiTangentType = Vector; + using NormalType = Vector4; + //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& data); void loadTexCoords(uint64 offset, uint64 index, const Array& data); void loadNormals(uint64 offset, const Array& data); - void loadTangents(uint64 offset, const Array& data); - void loadBitangents(uint64 offset, const Array& data); + //void loadTangents(uint64 offset, const Array& data); + //void loadBitangents(uint64 offset, const Array& data); void loadColors(uint64 offset, const Array& 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 posData; Array texData[MAX_TEXCOORDS]; Array norData; - Array tanData; - Array bitData; +// Array tanData; +// Array bitData; Array colData; Gfx::ODescriptorLayout descriptorLayout; Gfx::PDescriptorSet descriptorSet; diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index b2cad79..c88934e 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -78,7 +78,6 @@ class VertexData { const Array& getRayTracingData() const { return rayTracingScene; } const MeshData& getMeshData(MeshId id) const { return meshData[id]; } void registerBottomLevelAccelerationStructure(Gfx::PBottomLevelAS blas) { - assert(blas != nullptr); dataToBuild.add(blas); } uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; }