Browse Source

Add support for shadow/comparison samplers

Popov72 4 years ago
parent
commit
430f68ced9

+ 1 - 1
src/Engines/WebGPU/webgpuPipelineContext.ts

@@ -52,7 +52,7 @@ export class WebGPUPipelineContext implements IPipelineContext {
     public availableSamplers: { [key: string]: { setIndex: number, bindingIndex: number} };
 
     public orderedAttributes: string[];
-    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, textureDimension?: GPUTextureViewDimension }[][];
+    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, isComparisonSampler?: boolean, textureDimension?: GPUTextureViewDimension }[][];
 
     public leftOverUniforms: { name: string, type: string, length: number }[];
     public leftOverUniformsByName: { [name: string]: string };

+ 1 - 1
src/Engines/WebGPU/webgpuShaderProcessingContext.ts

@@ -21,7 +21,7 @@ export class WebGPUShaderProcessingContext implements ShaderProcessingContext {
     public leftOverUniforms: { name: string, type: string, length: number }[];
 
     public orderedAttributes: string[];
-    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, textureDimension?: GPUTextureViewDimension }[][];
+    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, isComparisonSampler?: boolean, textureDimension?: GPUTextureViewDimension }[][];
 
     constructor() {
         this.attributeNextLocation = 0;

+ 15 - 1
src/Engines/WebGPU/webgpuShaderProcessors.ts

@@ -25,6 +25,7 @@ const _samplerFunctionByWebGLSamplerType: { [key: string]: string } = {
     "textureCube": "samplerCube",
     "texture2D": "sampler2D",
     "sampler2D": "sampler2D",
+    "sampler2DShadow": "sampler2DShadow",
     "samplerCube": "samplerCube"
 };
 
@@ -32,6 +33,7 @@ const _textureTypeByWebGLSamplerType: { [key: string]: string } = {
     "textureCube": "textureCube",
     "texture2D": "texture2D",
     "sampler2D": "texture2D",
+    "sampler2DShadow": "texture2D",
     "samplerCube": "textureCube"
 };
 
@@ -40,6 +42,16 @@ const _gpuTextureViewDimensionByWebGPUTextureType: { [key: string]: GPUTextureVi
     "texture2D": WebGPUConstants.TextureViewDimension.E2d,
 };
 
+// if the webgl sampler type is not listed in this array, "sampler" is taken by default
+const _samplerTypeByWebGLSamplerType: { [key: string]: string } = {
+    "sampler2DShadow": "samplerShadow",
+};
+
+const _comparisonSamplerByWebGPUSamplerType: { [key: string]: boolean } = {
+    "samplerShadow": true,
+    "sampler": false,
+};
+
 /** @hidden */
 export class WebGPUShaderProcessor implements IShaderProcessor {
 
@@ -104,12 +116,13 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 const textureBindingIndex = samplerInfo.bindingIndex;
                 const samplerBindingIndex = samplerInfo.bindingIndex + 1;
                 const samplerFunction = _samplerFunctionByWebGLSamplerType[uniformType];
+                const samplerType = _samplerTypeByWebGLSamplerType[uniformType] ?? "sampler";
                 const textureType = _textureTypeByWebGLSamplerType[uniformType];
                 const textureDimension = _gpuTextureViewDimensionByWebGPUTextureType[textureType];
 
                 // Manage textures and samplers.
                 uniform = `layout(set = ${setIndex}, binding = ${textureBindingIndex}) uniform ${textureType} ${name}Texture;
-                    layout(set = ${setIndex}, binding = ${samplerBindingIndex}) uniform sampler ${name}Sampler;
+                    layout(set = ${setIndex}, binding = ${samplerBindingIndex}) uniform ${samplerType} ${name}Sampler;
                     #define ${name} ${samplerFunction}(${name}Texture, ${name}Sampler)`;
 
                 webgpuProcessingContext.availableSamplers[name] = samplerInfo;
@@ -118,6 +131,7 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 }
                 webgpuProcessingContext.orderedUBOsAndSamplers[setIndex][textureBindingIndex] = {
                     isSampler: true,
+                    isComparisonSampler: _comparisonSamplerByWebGPUSamplerType[samplerType] ?? false,
                     textureDimension,
                     name,
                 };

+ 2 - 1
src/Engines/webgpuEngine.ts

@@ -1316,6 +1316,7 @@ export class WebGPUEngine extends Engine {
         return {
             ...this._getSamplerFilterDescriptor(internalTexture),
             ...this._getSamplerWrappingDescriptor(internalTexture),
+            compare: internalTexture._comparisonFunction ? this._getCompareFunction(internalTexture._comparisonFunction) : undefined,
         };
     }
 
@@ -2659,7 +2660,7 @@ export class WebGPUEngine extends Engine {
                         // TODO WEBGPU. No Magic + 1 (coming from current 1 texture 1 sampler startegy).
                         binding: j + 1,
                         visibility: WebGPUConstants.ShaderStage.Vertex | WebGPUConstants.ShaderStage.Fragment,
-                        type: WebGPUConstants.BindingType.Sampler
+                        type: bindingDefinition.isComparisonSampler ? WebGPUConstants.BindingType.ComparisonSampler : WebGPUConstants.BindingType.Sampler
                     });
                 }
                 else {