Falcor icon indicating copy to clipboard operation
Falcor copied to clipboard

Store info per Mesh

Open cjsb opened this issue 5 years ago • 14 comments

Hi,

I need to store some information per Mesh. I put the information on a Texture which I can access but I need the index of the geometry to access the Texture.

Is there any way to do that?

Thank you

cjsb avatar Apr 19 '19 17:04 cjsb

Raytracing or raster?

nbentyNV avatar Apr 19 '19 17:04 nbentyNV

Raytracing. I tried the InstanceID() but I get the same id for any object.

cjsb avatar Apr 19 '19 17:04 cjsb

InstanceID() won't help, since we group geometries together into a single instance and there's no way to get the geometryID in DXR.

You can do something like:

        RtProgramVars::SharedPtr pVars;
        auto& hitVars = pVars->getHitVars(rayIndex);
        for (auto& hit : hitVars)
        {
            // The hit-vars are per mesh, assuming you have a `PerMeshCB` in the hit-program you can do:
            hit["PerMeshCB"]["MeshIndex"] = meshIndex;
        }

The code basically leverages the local-root-signature to expose data to a specific geometry

nbentyNV avatar Apr 19 '19 17:04 nbentyNV

Nice!!! But where does the "meshIndex" come from?

cjsb avatar Apr 19 '19 18:04 cjsb

From you :) This index should correlate to the order in which you created the texture. There's also RtScene::getInstanceID() which given the current mesh you are processing will return the hit-vars index.

nbentyNV avatar Apr 19 '19 18:04 nbentyNV

Sorry, I am not sure if I got correctly.

In that case the "meshIndex" is an array of indices or single value?

cjsb avatar Apr 19 '19 18:04 cjsb

single value per mesh

nbentyNV avatar Apr 19 '19 18:04 nbentyNV

ok, but how do I know which one is being hit?

cjsb avatar Apr 19 '19 18:04 cjsb

DXR tells you that. Do you know how local-root-signature works?

nbentyNV avatar Apr 19 '19 18:04 nbentyNV

To make it clear, I am testing in a simple ray tracing algorithm which gets some information on the hit and show on screen.

for (auto pVars : mpRays->getHitVars(0)) {
		pVars["gWsPos"] = wsPos;
		pVars["gWsNorm"] = wsNorm;
		pVars["gMatDif"] = matDif;
		pVars["gMatSpec"] = matSpec;
		pVars["gMatExtra"] = matExtra;
		pVars["gMatEmissive"] = matEmit;
}

I am understanding that I am going to add the hit["PerMeshCB"]["MeshIndex"] = meshIndex; like this:

for (auto pVars : mpRays->getHitVars(0))  
	{
		pVars["gWsPos"] = wsPos;
		pVars["gWsNorm"] = wsNorm;
		pVars["gMatDif"] = matDif;
		pVars["gMatSpec"] = matSpec;
		pVars["gMatExtra"] = matExtra;
		pVars["gMatEmissive"] = matEmit;
		pVars["MeshIndex"] = meshIndex;
	}

And as happens with the other information, dxr should give me the meshIndex of the hit.

But how do I include the meshIndex on the object?

cjsb avatar Apr 19 '19 18:04 cjsb

Where do you get wsPos and friends from?

I assume that somewhere in your code you loop over all the meshes (you mentioned that you initialize a texture with a per-mesh value). If there is indeed a loop, the index there is your meshIndex.

The new meshIndex is a user defined value. You can make it a float2 and store the per-mesh coordinate into the texture. How you map mesh to coordinate is up to you, just apply the same logic to meshTextCrd (or whatever you want to call it)

nbentyNV avatar Apr 19 '19 19:04 nbentyNV

As each Mesh that I need the index have a different material id, I did a map between the gMaterial.id and the information that I want.

It worked. =)

cjsb avatar Apr 19 '19 19:04 cjsb

About your question: I get the wsPos using the getTexture method and setting it to a constant value before starting the ray tracing.

By the way, I am storing new information on a shared texture, can I just create a array or do I have to use texture? Similar to this:

shared cbuffer PerFrameCB
{
    float4x4 invView;
    float4x4 invModel;
    float2 viewportDims;
    float tanHalfFovY;
};

And include an array on this cbuffer.

cjsb avatar Apr 19 '19 20:04 cjsb

my solution is to create a new Scene class , it has the material like properties and GUI editor for each render object

625673575 avatar Sep 25 '19 10:09 625673575