Sign in to follow this  
ferhua

Why does texture-sampling only work at (0,0,0)?

Recommended Posts

I want to save the color of every point of one object in the RWTexture3D (UAV Resource) and transfer them to another object in its shader.

I made a test in the two shaders. Both in the fragment shader.

In first shader, I gave

RWTexture3D<float4> gUAVColor;
gUAVColor[uint3(0,0,0)] = float4(1.0f,0.0f,0.0f,1.0f);

In second shader:

Texture3D<float4> gVoxelList;
float4 output = gVoxelList.SampleLevel(Filter, uint3(0,0,0),0);

The result is correct, I got red as the result.

But when I change the code, the texture cant be sampled correctly.

In first shader, I gave

RWTexture3D<float4> gUAVColor;
gUAVColor[uint3(1,0,0)] = float4(1.0f,0.0f,0.0f,1.0f);

In second shader:

Texture3D<float4> gVoxelList;
float4 output = gVoxelList.SampleLevel(Filter, uint3(1,0,0),0);

I only changed the pos which save the red color from uint(0,0,0) to uint(1,0,0), but what I got changed to black, which means it's uncorrect.

If I use gVoxelList[uint(1,0,0)].xyz ,it works.

Does anyone have the idea where may be the problem?

Besides, what's the difference between gVoxelList[pos] and gTexture.SampleLevel(Filter, uint3(1,0,0),0);  Both the two function backs the color: float4, right?

Share this post


Link to post
Share on other sites

I have tried the load function,

float4 output = gVoxelList.Load( uint3(1,0,0,0));

 It also can work well.

So the load function and Operator[ ] function both can work, why only sample can't ?

Share this post


Link to post
Share on other sites

This is because the bracket operator require pixel location in [(0,0,0)..(width,height,depth)] while a sampler will require a normalized texture coordinate, first pixel is then (0.5/width,0.5/height,05/depth) and last is (1-0.5/width,1-0.5/height,1-0.5/depth)

DX11 doesn't have half-pixel offset anymore, thankfully, so first pixel should be (0, 0, 0) and last pixel should be(1, 1, 1).

Share this post


Link to post
Share on other sites

This one has fixed, but there's still problem... please read the next post.

Thank you guys,I have a new problem in sampling when I am doing voxelization.

I only return the following color in pixel shader.

The pictures are the visualization of the voxelization. The first one looks no problem. I get it use

//pos is from(0,256)
//get every uint point in x:(0,256) y:(0,256) z:(0,256)
    uint VoxelDim = 256;
    uint sliceNum = VoxelDim*VoxelDim;
//index:256^3
    uint z = vin.index / (sliceNum);
    uint temp = vin.index % (sliceNum);
    uint y = temp / (uint)VoxelDim;
    uint x = temp % (uint)VoxelDim;
    uint3 pos = uint3(x, y, z);

    float3 color=gVoxelList[pos].xyz

Next, I changed operator[ ] to sampling function,

float3 color=gVoxelList.SampleLevel(SVOFilter, pos/256.0f ,1).xyz;

So it comes the second picture, you can see it has very strange "triangle like" pixels...

I have no idea where the problem will be... Is it the accuracy problem in voxelization?

I have checked the pos, every point (every point I need to get from the 3D texture) in the visualization is in the right order to correspond the point in voxelization (every point I save to RW3Dtexture).

visualie.PNG

sample.PNG

Edited by ferhua

Share this post


Link to post
Share on other sites

OK I am sorry, in one fx  it works , but in the other one which need to transform form world coordinate to voxel coordinate it doesn't...

The following code transform pixels from world pos to voxel pos.

float3 world_to_svo(float3 posW,float voxel_size,float3 offset)
{
    float3 pos=posW;
    pos=((pos+offset)/voxel_size);
    return pos;
}

So in the voxelization.fx, I do following to save the voxel pos.

svoPos=world_to_svo(posW.xyz,gVoxelSize,gVoxelOffset);
gUAVColor[svoPos] = float4(litColor,1.0f);

And in the other fx, I want to get the color which I saved in the texture:

float3 pos_svo=world_to_svo(posW,gVoxelSize,gVoxelOffset);
color=gVoxelList.SampleLevel(SVOFilter, pos_svo/gDim+0.5f/256.0f ,0);
color=gVoxelList[pos_svo];

Both the two methods can't work. And I got the wrong picture like I posted before...

It may be a little offset in the sampling texture coordinate, because when I set the gDim to 64, it works. But when I set it to 128,256 it is wrong...

I use the same method so I don't know how to fix this bug.

Anyone have some idea? Thank you very much!

The first picture is set to 128 res and the second is 256, you can see the comparison, the more res it increase, the more imprecise.

128.PNG

256.PNG

Edited by ferhua

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this