Просмотр исходного кода

Handle the isamplerXXX/usamplerXXX sampler types

Popov72 5 лет назад
Родитель
Сommit
caa859ae4c

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

@@ -23,6 +23,20 @@ export interface WebGPUTextureSamplerBindingDescription {
     textures: Array<WebGPUBindingInfo>;
 }
 
+/** @hidden
+ *  If the binding is a UBO, isSampler=isTexture=false
+*/
+export interface WebGPUBindingDescription {
+    name: string;
+
+    isSampler: boolean;
+    isComparisonSampler?: boolean;
+
+    isTexture: boolean;
+    componentType?: GPUTextureComponentType;
+    textureDimension?: GPUTextureViewDimension;
+}
+
 /**
  * @hidden
  */
@@ -39,7 +53,7 @@ export class WebGPUShaderProcessingContext implements ShaderProcessingContext {
     public leftOverUniforms: { name: string, type: string, length: number }[];
 
     public orderedAttributes: string[];
-    public orderedUBOsAndSamplers: { name: string, isSampler: boolean, isComparisonSampler?: boolean, isTexture: boolean, textureNeedsDepthComparison?: boolean, textureDimension?: GPUTextureViewDimension }[][];
+    public orderedUBOsAndSamplers: WebGPUBindingDescription[][];
 
     private _attributeNextLocation: number;
     private _varyingNextLocation: number;

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

@@ -23,8 +23,6 @@ const _knownSamplers: { [key: string]: WebGPUTextureSamplerBindingDescription }
 
 // TODO WEBGPU. sampler3D
 const _samplerFunctionByWebGLSamplerType: { [key: string]: string } = {
-    "textureCube": "samplerCube",
-    "texture2D": "sampler2D",
     "sampler2D": "sampler2D",
     "sampler2DShadow": "sampler2DShadow",
     "sampler2DArrayShadow": "sampler2DArrayShadow",
@@ -32,8 +30,6 @@ const _samplerFunctionByWebGLSamplerType: { [key: string]: string } = {
 };
 
 const _textureTypeByWebGLSamplerType: { [key: string]: string } = {
-    "textureCube": "textureCube",
-    "texture2D": "texture2D",
     "sampler2D": "texture2D",
     "sampler2DShadow": "texture2D",
     "sampler2DArrayShadow": "texture2DArray",
@@ -142,10 +138,10 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
 
         const match = uniformRegex.exec(uniform);
         if (match != null) {
-            const uniformType = match[1];
+            let uniformType = match[1];
             let name = match[2];
 
-            if (uniformType.indexOf("texture") === 0 || uniformType.indexOf("sampler") === 0) {
+            if (uniformType.indexOf("sampler") === 0 || uniformType.indexOf("sampler") === 1) {
                 let samplerInfo = _knownSamplers[name];
                 let arraySize = 0; // 0 means the sampler/texture is not declared as an array
                 if (!samplerInfo) {
@@ -165,6 +161,12 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                     }
                 }
 
+                const componentType = uniformType.charAt(0) === 'u' ? 'u' : uniformType.charAt(0) === 'i' ? 'i' : '';
+
+                if (componentType) {
+                    uniformType = uniformType.substr(1);
+                }
+
                 const isTextureArray = arraySize > 0;
                 const samplerSetIndex = samplerInfo.sampler.setIndex;
                 const samplerBindingIndex = samplerInfo.sampler.bindingIndex;
@@ -177,12 +179,12 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                 // Manage textures and samplers.
                 if (!isTextureArray) {
                     arraySize = 1;
-                    uniform = `layout(set = ${samplerSetIndex}, binding = ${samplerBindingIndex}) uniform ${samplerType} ${name}Sampler;
+                    uniform = `layout(set = ${samplerSetIndex}, binding = ${samplerBindingIndex}) uniform ${componentType}${samplerType} ${name}Sampler;
                         layout(set = ${samplerInfo.textures[0].setIndex}, binding = ${samplerInfo.textures[0].bindingIndex}) uniform ${textureType} ${name}Texture;
-                        #define ${name} ${samplerFunction}(${name}Texture, ${name}Sampler)`;
+                        #define ${name} ${componentType}${samplerFunction}(${name}Texture, ${name}Sampler)`;
                 } else {
                     let layouts = [];
-                    layouts.push(`layout(set = ${samplerSetIndex}, binding = ${samplerBindingIndex}) uniform ${samplerType} ${name}Sampler;`);
+                    layouts.push(`layout(set = ${samplerSetIndex}, binding = ${samplerBindingIndex}) uniform ${componentType}${samplerType} ${name}Sampler;`);
                     uniform = `\r\n`;
                     for (let i = 0; i < arraySize; ++i) {
                         const textureSetIndex = samplerInfo.textures[i].setIndex;
@@ -190,7 +192,7 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
 
                         layouts.push(`layout(set = ${textureSetIndex}, binding = ${textureBindingIndex}) uniform ${textureType} ${name}Texture${i};`);
 
-                        uniform += `${i > 0 ? '\r\n' : ''}#define ${name}${i} ${samplerFunction}(${name}Texture${i}, ${name}Sampler)`;
+                        uniform += `${i > 0 ? '\r\n' : ''}#define ${name}${i} ${componentType}${samplerFunction}(${name}Texture${i}, ${name}Sampler)`;
                     }
                     uniform = layouts.join('\r\n') + uniform;
                     this._textureArrayProcessing.push(name);
@@ -218,7 +220,9 @@ export class WebGPUShaderProcessor implements IShaderProcessor {
                     webgpuProcessingContext.orderedUBOsAndSamplers[textureSetIndex][textureBindingIndex] = {
                         isSampler: false,
                         isTexture: true,
-                        textureNeedsDepthComparison: isComparisonSampler,
+                        componentType: isComparisonSampler ? WebGPUConstants.TextureComponentType.DepthComparison :
+                                     componentType === 'u' ? WebGPUConstants.TextureComponentType.Uint :
+                                     componentType === 'i' ? WebGPUConstants.TextureComponentType.Sint : WebGPUConstants.TextureComponentType.Float,
                         textureDimension,
                         name: isTextureArray ? name + i.toString() : name,
                     };

+ 2 - 2
src/Engines/webgpuEngine.ts

@@ -3230,8 +3230,8 @@ export class WebGPUEngine extends Engine {
                         visibility: WebGPUConstants.ShaderStage.Vertex | WebGPUConstants.ShaderStage.Fragment,
                         type: WebGPUConstants.BindingType.SampledTexture,
                         viewDimension: bindingDefinition.textureDimension,
-                        //textureComponentType: bindingDefinition.textureNeedsDepthComparison ? WebGPUConstants.TextureComponentType.DepthComparison : undefined,
-                        // TODO WEBGPU. Handle texture component type properly.
+                        //textureComponentType: bindingDefinition.componentType,
+                        // TODO WEBGPU.
                         // hasDynamicOffset?: boolean;
                         // storageTextureFormat?: GPUTextureFormat;
                         // minBufferBindingSize?: number;