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

HLSL: Support depth comparison texture sampling in SM 2/3. #1520

Merged
merged 2 commits into from
Nov 3, 2020

Conversation

rdb
Copy link
Contributor

@rdb rdb commented Oct 31, 2020

In DX9, all samples of a depth texture result in a depth comparison (in fact, disabling this requires use of the special INTZ format, only available on DX10+ hardware). This works with all 4-component texture sample functions (tex2Dlod, tex2Dbias, tex2Dproj).

This PR provides access to this functionality by enabling use of the Dref texture sample instruction and passing in the depth compare value into the appropriate register. It assumes that there is indeed a depth texture bound to the appropriate register (which is the application's responsibility, and not something we can check).

Even though the regular texld instruction does support this as well, HLSL does not (to my knowledge) allow access to this variant since tex2D only accepts 2 coordinates. To work around this, this PR turns an OpImageSampleDrefImplicitLod into a tex2Dproj with a w coordinate of 1.0, which is effectively equivalent.

Thanks for your consideration!

Copy link
Contributor

@HansKristian-Work HansKristian-Work left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see a test here to see what is going on.

@rdb
Copy link
Contributor Author

rdb commented Nov 3, 2020

I added a test for all the samplers supported by SM 3.0.

As you can see it's just a matter of passing the dref into the third coordinate. I verified that the resulting shader works correctly for shadow mapping on both Intel and NVIDIA hardware, and furthermore, this is also what the Cg compiler is producing when using a shadow sampler and targeting SM 3.0 (aside from the fact that it can directly generate a texld instead of texldp, but that has no effect on the result).

Thanks!

spirv_hlsl.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@HansKristian-Work HansKristian-Work left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also missing reference/opt/shaders-hlsl/frag/tex-sampling.sm30.frag to be committed.

@rdb
Copy link
Contributor Author

rdb commented Nov 3, 2020

I just went ahead and just changed the second coordinate to 0.0, because it's clearer. I verified that the resulting shader still works correctly with a 1D depth map.

I also added a return swizzle and added the optimized reference file as requested.

Copy link
Contributor

@HansKristian-Work HansKristian-Work left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks.

@HansKristian-Work HansKristian-Work merged commit 2e1bdeb into KhronosGroup:master Nov 3, 2020
@rdb rdb deleted the dx9-dref-samplers branch November 3, 2020 20:09
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

Successfully merging this pull request may close these issues.

None yet

2 participants