-
Notifications
You must be signed in to change notification settings - Fork 36
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
[202x] Propose adding vk::SampledTexture* types #343
Open
cassiebeckley
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
cassiebeckley:sampled-texture
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
<!-- {% raw %} --> | ||
|
||
# Vulkan Sampled Textures | ||
|
||
* Proposal: [NNNN](NNNN-vk-sampled-texture.md) | ||
* Author(s): [Cassandra Beckley](https://github.com/cassiebeckley) | ||
* Sponsor: [Steven Perron](https://github.com/s-perron) and TBD | ||
* Status: **Under Consideration** | ||
* Required Version: HLSL 202x | ||
<!-- | ||
* PRs: [#NNNN](https://github.com/microsoft/DirectXShaderCompiler/pull/NNNN) | ||
* Issues: | ||
[#NNNN](https://github.com/microsoft/DirectXShaderCompiler/issues/NNNN) | ||
--> | ||
|
||
## Introduction | ||
|
||
We propose a new set of `vk::SampledTexture*` texture types representing | ||
Vulkan's combined image sampler types (`OpTypeSampledImage`). | ||
|
||
## Motivation | ||
|
||
The [existing annotation](https://github.com/microsoft/DirectXShaderCompiler/wiki/Vulkan-combined-image-sampler-type) | ||
for combined image samplers (`[[vk::combinedImageSampler]]`) was designed with | ||
the explicit goal of avoiding defining new HLSL types and functions. This was | ||
intended to minimize the number of changes to the frontend needed to implement | ||
the feature. However, it is verbose, confusing for users, and requires a | ||
backend pass to pair up textures and samplers which is prone to subtle errors. | ||
We do not intend to implement it in upstream Clang. | ||
|
||
## High-level description | ||
|
||
The `vk::SampledTexture*` types will have the same interface and methods as the | ||
existing `Texture*` types, with the exception that the methods will not take a | ||
sampler state argument. | ||
|
||
Consider this example pixel shader which uses a texture: | ||
|
||
```hlsl | ||
Texture2D tex0 : register(t0); | ||
SamplerState s : register(s0); | ||
|
||
float4 main(float2 uv: TEXCOORD) : SV_Target { | ||
return tex0.Sample(s, uv); | ||
} | ||
``` | ||
|
||
Using the proposed types, this could be rewritten to use a combined image | ||
sampler: | ||
|
||
```hlsl | ||
vk::SampledTexture2D tex0 : register(t0); | ||
|
||
float4 main(float2 uv: TEXCOORD) : SV_Target { | ||
return tex0.Sample(uv); | ||
} | ||
``` | ||
|
||
This is simpler and a more accurate representation of the underlying interface | ||
than using the existing annotation, which looks like: | ||
|
||
```hlsl | ||
[[vk::combinedImageSampler]] | ||
Texture2D tex0 : register(t0); | ||
[[vk::combinedImageSampler]] | ||
SamplerState s : register(s0); | ||
|
||
float4 main(float2 uv: TEXCOORD) : SV_Target { | ||
return tex0.Sample(s, uv); | ||
} | ||
``` | ||
|
||
A benefit of the `[[vk::combinedImageSampler]]` annotation is the ability to | ||
use the same code to represent a combined image sampler in Vulkan and a | ||
separate texture and sampler in DirectX. The new types will only be usable with | ||
Vulkan, but a similar effect can be produced by checking for the existence of | ||
the `__spirv__` macro. | ||
|
||
## Detailed design | ||
|
||
### HLSL Additions | ||
|
||
The following resource types will be added to the `vk` namespace: | ||
|
||
| HLSL Object | Type Parameters | | ||
|---------------------------|-----------------------------------| | ||
| `SampledTexture1D` | _type_ | | ||
| `SampledTexture1DArray` | _type_ | | ||
| `SampledTexture2D` | _type_ | | ||
| `SampledTexture2DArray` | _type_ | | ||
| `SampledTexture2DMS` | _type_, uint _samples_ | | ||
| `SampledTexture2DMSArray` | _type_, uint _samples_ | | ||
| `SampledTexture3D` | _type_ | | ||
| `SampledTextureCUBE` | _type_ | | ||
| `SampledTextureCUBEArray` | _type_ | | ||
|
||
As with the `Texture*` types, the _type_ parameter may be omitted, in which | ||
it will default to `float4`. Values of these types may be assigned to specific | ||
registers or annotated with the Vulkan binding annotations. | ||
|
||
The following builtin methods will be defined for these types: | ||
|
||
* `CalculateLevelOfDetail` | ||
* `CalculateLevelOfDetailUnclamped` | ||
* `Gather` | ||
* `GetDimensions` | ||
* `GetSamplePosition` | ||
* `Load` | ||
* `Sample` | ||
* `SampleBias` | ||
* `SampleCmp` | ||
* `SampleCmpLevelZero` | ||
* `SampleGrad` | ||
* `SampleLevel` | ||
|
||
They will have the same interface as the corresponding methods for the | ||
`Texture*` types, with the exception that they will not take a `SamplerState` | ||
or `SamplerComparisonState` argument. (SPIR-V does not have separate sampler | ||
types for comparison operations, and the combined sampler will be sufficient | ||
for all methods listed). | ||
|
||
### Interchange Format Additions | ||
|
||
This proposal exists to better represent an existing feature of SPIR-V, and no | ||
changes to the specification will be required. Since this is a Vulkan-specific | ||
feature scoped to the `vk` namescape, no changes to the DXIL format or DXIL | ||
lowering will be required. | ||
|
||
The `vk::SampledTexture*` types will be lowered to SPIR-V `OpTypeSampledImage` | ||
type declarations, with an _Image Type_ operand which will be the `OpTypeImage` | ||
produced by lowering the equivalent `Texture*` type. | ||
|
||
The builtin methods will be lowered to the same instructions as the equivalent | ||
methods for `Texture*` types. SPIR-V instructions which operate on images take | ||
these images in one of two formats: either an `OpTypeImage`, for instructions | ||
which do not require a sampler, or an `OpTypeSampledImage`, for instructions | ||
which do. For instructions which require an `OpTypeImage`, we can obtain such | ||
a value by passing the `OpTypeSampledImage` value to the `OpImage` instruction. | ||
|
||
We will likely represent these types in LLVM IR for upstream Clang by the | ||
addition of a new "sampled" attribute for the resource type. | ||
|
||
### Diagnostic Changes | ||
|
||
The `vk::SampledTexture*` types and builtin methods should provide the same | ||
diagnostics as the corresponding `Texture*` types. | ||
|
||
#### Validation Changes | ||
|
||
No additional changes to the SPIR-V validator will be required. | ||
|
||
### Runtime Additions | ||
|
||
No runtime additions or information are required. | ||
|
||
#### Device Capability | ||
|
||
`OpTypeSampledImage` is a core feature of SPIR-V, and part of Vulkan since 1.0. | ||
It should be supported by all existing Vulkan hardware. No changes to either | ||
drivers or hardware will be needed. | ||
|
||
## Testing | ||
|
||
The existing tests for the SPIR-V lowering of `Texture*` types can be expanded | ||
to ensure that `vk::SampledTexture` types exhibit the same behavior, in all | ||
cases. | ||
|
||
## Alternatives considered | ||
|
||
We considered using a `vk::Sampled` template that would take a texture as a | ||
template parameter. For example, `vk::Sampled<Texture2D> >`. We decided against | ||
this as it would require additional validation that the template parameter is a | ||
texture type, and because HLSL has an existing convention for resource types to | ||
have additional behavior by prepending information to the type name – for | ||
example, `Buffer` to `RWBuffer` and `RWStructuredBuffer`. | ||
|
||
We also considered using inline SPIR-V for implementation; however, doing so | ||
would require the ability to assign an HLSL-defined class an HLSL register | ||
and/or Vulkan location binding, and then use that register for a member | ||
variable of the class holding the low-level inline SPIR-V representation. We | ||
decided that such a mechanism would be too general, as new resource types in | ||
HLSL are rare and users should not have the ability to create them themselves. | ||
|
||
<!-- {% endraw %} --> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per our discussion:
Marking the attribute that you want to remove deprecated in HLSL 2021, then remove it in HLSL 202x seems like a good path forward.
For the new types, we can use a
__has_feature
check in the preprocessor and make it available in all language modes.