@@ -106,6 +106,37 @@ index buffer for each bind group. This buffer is accessed in the shader to get t
106
106
sampler index within the heap. See the wgpu_hal dx12 backend documentation for more
107
107
information.
108
108
109
+ # External textures
110
+
111
+ Support for [`crate::ImageClass::External`] textures is implemented by lowering
112
+ each external texture global variable to 3 `Texture2D<float4>`s, and a `cbuffer`
113
+ of type `NagaExternalTextureParams`. This provides up to 3 planes of texture
114
+ data (for example single planar RGBA, or separate Y, Cb, and Cr planes), and the
115
+ parameters buffer containing information describing how to handle these
116
+ correctly. The bind target to use for each of these globals is specified via
117
+ [`Options::external_texture_binding_map`].
118
+
119
+ External textures are supported by WGSL's `textureDimensions()`,
120
+ `textureLoad()`, and `textureSampleBaseClampToEdge()` built-in functions. These
121
+ are implemented using helper functions. See the following functions for how
122
+ these are generated:
123
+ * [`Writer::write_wrapped_image_query_function`]
124
+ * [`Writer::write_wrapped_image_load_function`]
125
+ * [`Writer::write_wrapped_image_sample_function`]
126
+
127
+ Ideally the set of global variables could be wrapped in a single struct that
128
+ could conveniently be passed around. But, alas, HLSL does not allow structs to
129
+ have `Texture2D` members. Fortunately, however, external textures can only be
130
+ used as arguments to either built-in or user-defined functions. We therefore
131
+ expand any external texture function argument to four consecutive arguments (3
132
+ textures and the params struct) when declaring user-defined functions, and
133
+ ensure our built-in function implementations take the same arguments. Then,
134
+ whenever we need to emit an external texture in [`Writer::write_expr`], which
135
+ fortunately can only ever be for a global variable or function argument, we
136
+ simply emit the variable name of each of the three textures and the parameters
137
+ struct in a comma-separated list. This won't win any awards for elegance, but
138
+ it works for our purposes.
139
+
109
140
[hlsl]: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl
110
141
[ilov]: https://gpuweb.github.io/gpuweb/wgsl/#internal-value-layout
111
142
[16bb]: https://github.com/microsoft/DirectXShaderCompiler/wiki/Buffer-Packing#constant-buffer-packing
0 commit comments