Skip to content

Commit

Permalink
Working on fixing what appears to be incorrect material assignment wh…
Browse files Browse the repository at this point in the history
…en raytracing.
  • Loading branch information
Duttenheim committed Jan 22, 2025
1 parent 48d4fda commit dc920ce
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
1 change: 1 addition & 0 deletions code/render/materials/materialloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct MaterialBuffer
this->hostBufferData = (char*)CoreGraphics::BufferMap(this->hostBuffer);
this->deviceBuffer = CoreGraphics::CreateBuffer(this->deviceBufferCreateInfo);
this->deviceAddress = CoreGraphics::BufferGetDeviceAddress(this->deviceBuffer);
this->dirty = true;
}
}
return ret;
Expand Down
21 changes: 11 additions & 10 deletions code/render/raytracing/raytracingcontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,11 @@ RaytracingContext::SetupModel(const Graphics::GraphicsEntityId id, CoreGraphics:
raytracingContextAllocator.Set<Raytracing_NumStructures>(contextId.id, numObjects);
raytracingContextAllocator.Set<Raytracing_UpdateType>(contextId.id, UpdateType::Dynamic);

IndexT counter = 0;
IndexT instanceCounter = 0;
for (IndexT i = nodes.begin; i < nodes.end; i++)
{
Models::PrimitiveNode* pNode = static_cast<Models::PrimitiveNode*>(Models::ModelContext::NodeInstances.renderable.nodes[i]);
Resources::CreateResourceListener(pNode->GetMeshResource(), [flags, mask, offset = alloc.offset, &counter, &instanceCounter, i, pNode](Resources::ResourceId id)
Resources::CreateResourceListener(pNode->GetMeshResource(), [flags, mask, offset = alloc.offset, primitiveGroup = pNode->GetPrimitiveGroupIndex(), &instanceCounter, i, pNode](Resources::ResourceId id)
{
Threading::CriticalScope _s(&state.blasLock);
CoreGraphics::MeshResourceId meshRes = id;
Expand All @@ -417,7 +416,6 @@ RaytracingContext::SetupModel(const Graphics::GraphicsEntityId id, CoreGraphics:
if (blasIndex == InvalidIndex)
{
// Reset primitive group counter
counter = 0;
const CoreGraphics::VertexLayoutId layout = CoreGraphics::MeshGetVertexLayout(mesh);
auto& comps = CoreGraphics::VertexLayoutGetComponents(layout);

Expand Down Expand Up @@ -450,32 +448,35 @@ RaytracingContext::SetupModel(const Graphics::GraphicsEntityId id, CoreGraphics:
CoreGraphics::BufferIdLock _2(CoreGraphics::GetIndexBuffer());

// Because the smallest machine unit is 4 bytes, the offset must be in integers, not in bytes
CoreGraphics::PrimitiveGroup group = CoreGraphics::MeshGetPrimitiveGroup(mesh, counter);
CoreGraphics::PrimitiveGroup group = CoreGraphics::MeshGetPrimitiveGroup(mesh, primitiveGroup);
constants.Use16BitIndex = CoreGraphics::MeshGetIndexType(mesh) == CoreGraphics::IndexType::Index16 ? 1 : 0;
constants.PositionsPtr = CoreGraphics::BufferGetDeviceAddress(CoreGraphics::GetVertexBuffer()) + CoreGraphics::MeshGetVertexOffset(mesh, 0);
constants.AttrPtr = CoreGraphics::BufferGetDeviceAddress(CoreGraphics::GetVertexBuffer()) + CoreGraphics::MeshGetVertexOffset(mesh, 1);
CoreGraphics::DeviceAddress attributeAddress = CoreGraphics::BufferGetDeviceAddress(CoreGraphics::GetVertexBuffer()) + CoreGraphics::MeshGetVertexOffset(mesh, 1);
memcpy(constants.AttrPtr, &attributeAddress, sizeof(CoreGraphics::DeviceAddress));
constants.IndexPtr = CoreGraphics::BufferGetDeviceAddress(CoreGraphics::GetIndexBuffer()) + CoreGraphics::MeshGetIndexOffset(mesh);
constants.AttributeStride = (uint)CoreGraphics::VertexLayoutGetStreamSize(CoreGraphics::MeshGetVertexLayout(mesh), 1);
constants.VertexLayout = (uint)temp->vertexLayout;
state.objects[offset + instanceCounter] = constants;

uint instanceIndex = offset + instanceCounter;
state.objects[instanceIndex] = constants;

// Setup instance
CoreGraphics::BlasInstanceCreateInfo createIntInfo;
createIntInfo.flags = flags;
createIntInfo.mask = mask;
createIntInfo.shaderOffset = MaterialPropertyMappings[(uint)temp->properties];
createIntInfo.instanceIndex = offset;
createIntInfo.instanceIndex = instanceIndex;
createIntInfo.blas = blas;
createIntInfo.transform = Models::ModelContext::NodeInstances.transformable.nodeTransforms[Models::ModelContext::NodeInstances.renderable.nodeTransformIndex[i]];

// Disable instance if the vertex layout isn't supported
CoreGraphics::BlasIdLock _0(createIntInfo.blas);
state.blasInstances[offset + instanceCounter] = CoreGraphics::CreateBlasInstance(createIntInfo);
state.blasInstanceMeshes[offset + instanceCounter] = mesh;
state.blasInstances[instanceIndex] = CoreGraphics::CreateBlasInstance(createIntInfo);
state.blasInstanceMeshes[instanceIndex] = mesh;

state.numRegisteredInstances++;
state.topLevelNeedsReconstruction = true;
});
counter++;
instanceCounter++;
}
state.topLevelNeedsReconstruction = true;
Expand Down
2 changes: 1 addition & 1 deletion code/render/raytracing/shaders/gltfhit.fx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ClosestHit(
float facing = dot(normal, gl_WorldRayDirectionEXT);
Result.bits |= facing > 0 ? RAY_BACK_FACE_BIT : 0x0;

GLTFMaterial mat = GLTFMaterials + obj.MaterialOffset;
GLTFMaterial mat = GLTFMaterials[obj.MaterialOffset];

/* Tangent space normal transform */
vec4 normals = sample2DLod(mat.normalTexture, NormalSampler, uv, 0) * mat.normalScale;
Expand Down
60 changes: 36 additions & 24 deletions syswork/shaders/vk/lib/raytracing.fxh
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ ptr alignment(32) struct VertexAttributeSkin
uint indices;
};

ptr alignment(4) struct VertexAttributeDummy
{
uint dummy;
};

ptr alignment(4) struct Indexes32
{
uint index;
Expand All @@ -105,8 +100,9 @@ MESH_BINDING rw_buffer Geometry
struct Object
{
VertexPosUv PositionsPtr;
VertexAttributeDummy AttrPtr;
uvec2 AttrPtr;
Indexes16 IndexPtr;
uint AttributeStride;
uint Use16BitIndex;
uint MaterialOffset;
uint VertexLayout;
Expand Down Expand Up @@ -185,7 +181,20 @@ float
UnpackSign(int packedNormal)
{
int sig = (packedNormal >> 24) & 0xFF;
return sig == -128 ? -1.0f : 1.0f;
return sig == 0x7F ? -1.0f : 1.0f;
}


//------------------------------------------------------------------------------
/**
*/
uvec2
OffsetPointer(uvec2 basePtr, uint offset)
{
uint carry;
uint lo = uaddCarry(basePtr.x, offset, carry);
uint hi = basePtr.y + carry;
return uvec2(lo, hi);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -233,31 +242,34 @@ SampleGeometry(in Object obj, uint prim, in vec3 baryCoords, out uvec3 indices,

vec3 n1, n2, n3;
vec3 t1, t2, t3;

switch (obj.VertexLayout)
{
case 1: // Normal
{
VertexAttributeNormals attrs = VertexAttributeNormals(obj.AttrPtr);
n1 = UnpackNormal32(attrs[indices.x].normal_tangent.x);
n2 = UnpackNormal32(attrs[indices.y].normal_tangent.x);
n3 = UnpackNormal32(attrs[indices.z].normal_tangent.x);
t1 = UnpackNormal32(attrs[indices.x].normal_tangent.y);
t2 = UnpackNormal32(attrs[indices.y].normal_tangent.y);
t3 = UnpackNormal32(attrs[indices.z].normal_tangent.y);
sign = UnpackSign(attrs[indices.x].normal_tangent.y);
VertexAttributeNormals attrs1 = VertexAttributeNormals(OffsetPointer(obj.AttrPtr, indices.x * obj.AttributeStride));
VertexAttributeNormals attrs2 = VertexAttributeNormals(OffsetPointer(obj.AttrPtr, indices.y * obj.AttributeStride));
VertexAttributeNormals attrs3 = VertexAttributeNormals(OffsetPointer(obj.AttrPtr, indices.z * obj.AttributeStride));
n1 = UnpackNormal32(attrs1.normal_tangent.x);
n2 = UnpackNormal32(attrs2.normal_tangent.x);
n3 = UnpackNormal32(attrs3.normal_tangent.x);
t1 = UnpackNormal32(attrs1.normal_tangent.y);
t2 = UnpackNormal32(attrs2.normal_tangent.y);
t3 = UnpackNormal32(attrs3.normal_tangent.y);
sign = UnpackSign(attrs1.normal_tangent.y);
break;
}
case 4: // Skin
{
VertexAttributeSkin attrs = VertexAttributeSkin(obj.AttrPtr);
n1 = UnpackNormal32(attrs[indices.x].normal_tangent.x);
n2 = UnpackNormal32(attrs[indices.y].normal_tangent.x);
n3 = UnpackNormal32(attrs[indices.z].normal_tangent.x);
t1 = UnpackNormal32(attrs[indices.x].normal_tangent.y);
t2 = UnpackNormal32(attrs[indices.y].normal_tangent.y);
t3 = UnpackNormal32(attrs[indices.z].normal_tangent.y);
sign = UnpackSign(attrs[indices.x].normal_tangent.y);
VertexAttributeSkin attrs1 = VertexAttributeSkin(OffsetPointer(obj.AttrPtr, indices.x * obj.AttributeStride));
VertexAttributeSkin attrs2 = VertexAttributeSkin(OffsetPointer(obj.AttrPtr, indices.y * obj.AttributeStride));
VertexAttributeSkin attrs3 = VertexAttributeSkin(OffsetPointer(obj.AttrPtr, indices.z * obj.AttributeStride));
n1 = UnpackNormal32(attrs1.normal_tangent.x);
n2 = UnpackNormal32(attrs2.normal_tangent.x);
n3 = UnpackNormal32(attrs3.normal_tangent.x);
t1 = UnpackNormal32(attrs1.normal_tangent.y);
t2 = UnpackNormal32(attrs2.normal_tangent.y);
t3 = UnpackNormal32(attrs3.normal_tangent.y);
sign = UnpackSign(attrs1.normal_tangent.y);
break;
}
}
Expand Down

0 comments on commit dc920ce

Please sign in to comment.