[WHLSL] Implement texture types
[WebKit-https.git] / Tools / WebGPUShadingLanguageRI / Sampler.js
1 /*
2  * Copyright (C) 2018 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 "use strict";
26
27 class Sampler {
28     constructor(options)
29     {
30         if (options.rAddressMode == undefined)
31             this._rAddressMode = "clampToEdge";
32         else
33             this._rAddressMode = options.rAddressMode;
34         if (options.sAddressMode == undefined)
35             this._sAddressMode = "clampToEdge";
36         else
37             this._sAddressMode = options.sAddressMode;
38         if (options.tAddressMode == undefined)
39             this._tAddressMode = "clampToEdge";
40         else
41             this._tAddressMode = options.tAddressMode;
42         if (options.minFilter == undefined)
43             this._minFilter = "nearest";
44         else
45             this._minFilter = options.minFilter;
46         if (options.magFilter == undefined)
47             this._magFilter = "nearest";
48         else
49             this._magFilter = options.magFilter;
50         if (options.mipmapFilter == undefined)
51             this._mipmapFilter = "nearest";
52         else
53             this._mipmapFilter = options.mipmapFilter;
54         if (options.lodMinClamp == undefined)
55             this._lodMinClamp = -Number.MAX_VALUE;
56         else
57             this._lodMinClamp = options.lodMinClamp;
58         if (options.lodMaxClamp == undefined)
59             this._lodMaxClamp = Number.MAX_VALUE;
60         else
61             this._lodMaxClamp = options.lodMaxClamp;
62         if (options.maxAnisotropy == undefined)
63             this._maxAnisotropy = 1;
64         else
65             this._maxAnisotropy = options.maxAnisotropy;
66         if (options.compareFunction == undefined)
67             this._compareFunction = "never";
68         else
69             this._compareFunction = options.compareFunction;
70         if (options.borderColor == undefined)
71             this._borderColor = "transparentBlack";
72         else
73             this._borderColor = options.borderColor;
74     }
75     
76     get rAddressMode() { return this._rAddressMode; }
77     get sAddressMode() { return this._sAddressMode; }
78     get tAddressMode() { return this._tAddressMode; }
79     get minFilter() { return this._minFilter; }
80     get magFilter() { return this._magFilter; }
81     get mipmapFilter() { return this._mipmapFilter; }
82     get lodMinClamp() { return this._lodMinClamp; }
83     get lodMaxClamp() { return this._lodMaxClamp; }
84     get maxAnisotropy() { return this._maxAnisotropy; }
85     get compareFunction() { return this._compareFunction; }
86     get borderColor() { return this._borderColor; }
87
88     calculateBorderColor(innerType)
89     {
90         // Vulkan 1.1.83 section 15.3.3
91         function computeValues() {
92             switch (innerType) {
93                 case "uchar":
94                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 0xFF};
95                 case "ushort":
96                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 0xFFFF};
97                 case "uint":
98                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 0xFFFFFFFF};
99                 case "char":
100                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 0x7F};
101                 case "short":
102                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 0x7FFF};
103                 case "int":
104                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 0x7FFFFFFF};
105                 case "half":
106                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 1};
107                 case "float":
108                     return {transparentBlack: 0, opaqueBlack: 0, opaqueWhite: 1};
109                 case "uchar2":
110                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [0xFF, 0xFF]};
111                 case "ushort2":
112                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [0xFFFF, 0xFFFF]};
113                 case "uint2":
114                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [0xFFFFFFFF, 0xFFFFFFFF]};
115                 case "char2":
116                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [0x7F, 0x7F]};
117                 case "short2":
118                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [0x7FFF, 0x7FFF]};
119                 case "int2":
120                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [0x7FFFFFFF, 0x7FFFFFFF]};
121                 case "half2":
122                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [1, 1]};
123                 case "float2":
124                     return {transparentBlack: [0, 0], opaqueBlack: [0, 0], opaqueWhite: [1, 1]};
125                 case "uchar3":
126                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [0xFF, 0xFF, 0xFF]};
127                 case "ushort3":
128                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [0xFFFF, 0xFFFF, 0xFFFF]};
129                 case "uint3":
130                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF]};
131                 case "char3":
132                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [0x7F, 0x7F, 0x7F]};
133                 case "short3":
134                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [0x7FFF, 0x7FFF, 0x7FFF]};
135                 case "int3":
136                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF]};
137                 case "half3":
138                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [1, 1, 1]};
139                 case "float3":
140                     return {transparentBlack: [0, 0, 0], opaqueBlack: [0, 0, 0], opaqueWhite: [1, 1, 1]};
141                 case "uchar4":
142                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 0xFF], opaqueWhite: [0xFF, 0xFF, 0xFF, 0xFF]};
143                 case "ushort4":
144                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 0xFFFF], opaqueWhite: [0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF]};
145                 case "uint4":
146                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 0xFFFFFFFF], opaqueWhite: [0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF]};
147                 case "char4":
148                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 0x7F], opaqueWhite: [0x7F, 0x7F, 0x7F, 0x7F]};
149                 case "short4":
150                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 0x7FFF], opaqueWhite: [0x7FFF, 0x7FFF, 0x7FFF, 0x7FFF]};
151                 case "int4":
152                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 0x7FFFFFFF], opaqueWhite: [0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF]};
153                 case "half4":
154                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 1], opaqueWhite: [1, 1, 1, 1]};
155                 case "float4":
156                     return {transparentBlack: [0, 0, 0, 0], opaqueBlack: [0, 0, 0, 1], opaqueWhite: [1, 1, 1, 1]};
157                 default:
158                     throw new Error("Unknown texture inner type");
159             }
160         }
161         return computeValues()[this.borderColor];
162     }
163 }