Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Are texture samples specified to be taken at a corner or center? #426

Open
trbabb opened this issue Sep 19, 2024 · 9 comments
Open

Comments

@trbabb
Copy link

trbabb commented Sep 19, 2024

When calling, e.g., textureSampleGrad(tex, sampler, st, dst_dx, dst_dy), is the effective sampled area of the texture a patch with "origin" at st, e.g. st + J * xy, with xy in [0,1]^2 and J the Jacobian to sample? Or is st the center of the sampled patch, with st in [-0.5, 0.5]`? Or something else?

What does the spec say? I couldn't find anything explicit, but I might not be searching for the right keywords.

@almarklein
Copy link
Collaborator

Maybe this helps: https://docs.pygfx.org/stable/_gallery/validation/validate_image1.html (the texture coords of a plane geometry are [0..1]).

When you sample from a texture, the st represents the "center" of the sample. E.g. with linear interpolation you'd get the exact pixel value when st is exactly on top of a pixel, but if it's in between two pixels, you get the mean.

Does this make sense? Or are you asking for a specific detail for the Grad-variant of texture sampling? I'm not 100% sure I understand the question.

@trbabb
Copy link
Author

trbabb commented Sep 24, 2024

A, B, or C?

Untitled_Artwork

It sounds like maybe "B"?

@Vipitis
Copy link

Vipitis commented Sep 24, 2024

It's A but y flipped.

See the version at the bottom of the readme here https://github.com/gfx-rs/wgpu?tab=readme-ov-file#coordinate-systems

@trbabb
Copy link
Author

trbabb commented Sep 24, 2024

@Vipitis I'm worried you might be answering a question I'm not asking, since in this diagram the blue and yellow vectors are user specified and can point in any direction.

Just to restate here, I am not asking about the coordinate system of the entire texture (that's easy enough to find), I'm asking about the coordinate system and origin of a differential patch of texture when taking a single texture sample using textureGrad(). What I expect is that the returned sample value will be an approximate integral over a small finite area of the texture, related to the vectors dst_dx and dst_dy. So a component of that question is whether the sampled point st is in the middle of the differential area to be integrated, or at the corner.

So to double check, is that what you are answering?

@cwfitzgerald
Copy link
Member

cwfitzgerald commented Sep 24, 2024

I think the answer you're looking for is C, though I may be misunderstanding your notation.

The sample "box" is centered at st, and has the size of the dpdx and dpdy vectors.

How this translates to exact samples can get a little mind bendy and in the case of antisotropic filtering is actually implementation defined

@trbabb
Copy link
Author

trbabb commented Sep 25, 2024

Is the center + size behavior guaranteed by the spec, or part of the "implementation defined" behavior?

@almarklein
Copy link
Collaborator

I don't fully understand the question. I guess this is also because I've never used textureSampleGrad and have not yet found what it actually does with the gradients. I presume it uses them to determine the lod and thus the appropriate mip level to sample.

Perhaps you can code an example that will actually show what happens. You can then also try it on different hardware to see if its consistent.

@Vipitis
Copy link

Vipitis commented Sep 25, 2024

ah, now I get the question. I recently saw this video which makes use of this sampling method. So it seems answer C is correct, it's normalized to [-1..1]

@almarklein
Copy link
Collaborator

almarklein commented Sep 26, 2024

I recently saw this video

Wow, that video explains a lot. It's also really well done 🤩 Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants